xref: /onnv-gate/usr/src/lib/libtecla/common/pathutil.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate #ifndef pathutil_h
2*0Sstevel@tonic-gate #define pathutil_h
3*0Sstevel@tonic-gate 
4*0Sstevel@tonic-gate /*
5*0Sstevel@tonic-gate  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
6*0Sstevel@tonic-gate  *
7*0Sstevel@tonic-gate  * All rights reserved.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * Permission is hereby granted, free of charge, to any person obtaining a
10*0Sstevel@tonic-gate  * copy of this software and associated documentation files (the
11*0Sstevel@tonic-gate  * "Software"), to deal in the Software without restriction, including
12*0Sstevel@tonic-gate  * without limitation the rights to use, copy, modify, merge, publish,
13*0Sstevel@tonic-gate  * distribute, and/or sell copies of the Software, and to permit persons
14*0Sstevel@tonic-gate  * to whom the Software is furnished to do so, provided that the above
15*0Sstevel@tonic-gate  * copyright notice(s) and this permission notice appear in all copies of
16*0Sstevel@tonic-gate  * the Software and that both the above copyright notice(s) and this
17*0Sstevel@tonic-gate  * permission notice appear in supporting documentation.
18*0Sstevel@tonic-gate  *
19*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20*0Sstevel@tonic-gate  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21*0Sstevel@tonic-gate  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
22*0Sstevel@tonic-gate  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23*0Sstevel@tonic-gate  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
24*0Sstevel@tonic-gate  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
25*0Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
26*0Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
27*0Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28*0Sstevel@tonic-gate  *
29*0Sstevel@tonic-gate  * Except as contained in this notice, the name of a copyright holder
30*0Sstevel@tonic-gate  * shall not be used in advertising or otherwise to promote the sale, use
31*0Sstevel@tonic-gate  * or other dealings in this Software without prior written authorization
32*0Sstevel@tonic-gate  * of the copyright holder.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * The following object encapsulates a buffer designed to be used to
39*0Sstevel@tonic-gate  * store pathnames. The pathname member of the object is initially
40*0Sstevel@tonic-gate  * allocated with the size that _pu_pathname_dim() returns, and then
41*0Sstevel@tonic-gate  * if this turns out to be pessimistic, the pathname can be reallocated
42*0Sstevel@tonic-gate  * via calls to pb_append_to_path() and/or pb_resize_path().
43*0Sstevel@tonic-gate  */
44*0Sstevel@tonic-gate typedef struct {
45*0Sstevel@tonic-gate   char *name;         /* The path buffer */
46*0Sstevel@tonic-gate   size_t dim;         /* The current allocated size of buffer[] */
47*0Sstevel@tonic-gate } PathName;
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate PathName *_new_PathName(void);
50*0Sstevel@tonic-gate PathName *_del_PathName(PathName *path);
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate char *_pn_clear_path(PathName *path);
53*0Sstevel@tonic-gate char *_pn_append_to_path(PathName *path, const char *string, int slen,
54*0Sstevel@tonic-gate 			int remove_escapes);
55*0Sstevel@tonic-gate char *_pn_prepend_to_path(PathName *path, const char *string, int slen,
56*0Sstevel@tonic-gate 			  int remove_escapes);
57*0Sstevel@tonic-gate char *_pn_resize_path(PathName *path, size_t length);
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate  * Search backwards for the potential start of a filename. This
61*0Sstevel@tonic-gate  * looks backwards from the specified index in a given string,
62*0Sstevel@tonic-gate  * stopping at the first unescaped space or the start of the line.
63*0Sstevel@tonic-gate  */
64*0Sstevel@tonic-gate char *_pu_start_of_path(const char *string, int back_from);
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate /*
67*0Sstevel@tonic-gate  * Find the end of a potential filename, starting from a given index
68*0Sstevel@tonic-gate  * in the string. This looks forwards from the specified index in a
69*0Sstevel@tonic-gate  * given string, stopping at the first unescaped space or the end
70*0Sstevel@tonic-gate  * of the line.
71*0Sstevel@tonic-gate  */
72*0Sstevel@tonic-gate char *_pu_end_of_path(const char *string, int start_from);
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /*
76*0Sstevel@tonic-gate  * Return an estimate of the the length of the longest pathname
77*0Sstevel@tonic-gate  * on the local system.
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate size_t _pu_pathname_dim(void);
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate /*
82*0Sstevel@tonic-gate  * Return non-zero if the specified path name refers to a directory.
83*0Sstevel@tonic-gate  */
84*0Sstevel@tonic-gate int _pu_path_is_dir(const char *pathname);
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /*
87*0Sstevel@tonic-gate  * Return non-zero if the specified path name refers to a regular file.
88*0Sstevel@tonic-gate  */
89*0Sstevel@tonic-gate int _pu_path_is_file(const char *pathname);
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate  * Return non-zero if the specified path name refers to an executable.
93*0Sstevel@tonic-gate  */
94*0Sstevel@tonic-gate int _pu_path_is_exe(const char *pathname);
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate /*
97*0Sstevel@tonic-gate  * Return non-zero if a file exists with the specified pathname.
98*0Sstevel@tonic-gate  */
99*0Sstevel@tonic-gate int _pu_file_exists(const char *pathname);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate /*
102*0Sstevel@tonic-gate  * If neither the POSIX PATH_MAX macro nor the pathconf() function
103*0Sstevel@tonic-gate  * can be used to find out the maximum pathlength on the target
104*0Sstevel@tonic-gate  * system, the following fallback maximum length is used.
105*0Sstevel@tonic-gate  */
106*0Sstevel@tonic-gate #define MAX_PATHLEN_FALLBACK 1024
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate /*
109*0Sstevel@tonic-gate  * If the pathname buffer turns out to be too small, it will be extended
110*0Sstevel@tonic-gate  * in chunks of the following amount (plus whatever is needed at the time).
111*0Sstevel@tonic-gate  */
112*0Sstevel@tonic-gate #define PN_PATHNAME_INC 100
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate /*
115*0Sstevel@tonic-gate  * Define the special character-sequences of the filesystem.
116*0Sstevel@tonic-gate  */
117*0Sstevel@tonic-gate #define FS_ROOT_DIR "/"     /* The root directory */
118*0Sstevel@tonic-gate #define FS_ROOT_DIR_LEN (sizeof(FS_ROOT_DIR) - 1)
119*0Sstevel@tonic-gate #define FS_PWD "."          /* The current working directory */
120*0Sstevel@tonic-gate #define FS_PWD_LEN (sizeof(FS_PWD_LEN) - 1)
121*0Sstevel@tonic-gate #define FS_DIR_SEP "/"      /* The directory separator string */
122*0Sstevel@tonic-gate #define FS_DIR_SEP_LEN (sizeof(FS_DIR_SEP) - 1)
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate #endif
125