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