xref: /onnv-gate/usr/src/lib/libtecla/common/pathutil.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * All rights reserved.
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  * Permission is hereby granted, free of charge, to any person obtaining a
7*0Sstevel@tonic-gate  * copy of this software and associated documentation files (the
8*0Sstevel@tonic-gate  * "Software"), to deal in the Software without restriction, including
9*0Sstevel@tonic-gate  * without limitation the rights to use, copy, modify, merge, publish,
10*0Sstevel@tonic-gate  * distribute, and/or sell copies of the Software, and to permit persons
11*0Sstevel@tonic-gate  * to whom the Software is furnished to do so, provided that the above
12*0Sstevel@tonic-gate  * copyright notice(s) and this permission notice appear in all copies of
13*0Sstevel@tonic-gate  * the Software and that both the above copyright notice(s) and this
14*0Sstevel@tonic-gate  * permission notice appear in supporting documentation.
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17*0Sstevel@tonic-gate  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*0Sstevel@tonic-gate  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
19*0Sstevel@tonic-gate  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20*0Sstevel@tonic-gate  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
21*0Sstevel@tonic-gate  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
22*0Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
23*0Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
24*0Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  * Except as contained in this notice, the name of a copyright holder
27*0Sstevel@tonic-gate  * shall not be used in advertising or otherwise to promote the sale, use
28*0Sstevel@tonic-gate  * or other dealings in this Software without prior written authorization
29*0Sstevel@tonic-gate  * of the copyright holder.
30*0Sstevel@tonic-gate  */
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate /*
35*0Sstevel@tonic-gate  * If file-system access is to be excluded, this module has no function,
36*0Sstevel@tonic-gate  * so all of its code should be excluded.
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate #ifndef WITHOUT_FILE_SYSTEM
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <stdio.h>
41*0Sstevel@tonic-gate #include <stdlib.h>
42*0Sstevel@tonic-gate #include <errno.h>
43*0Sstevel@tonic-gate #include <string.h>
44*0Sstevel@tonic-gate #include <ctype.h>
45*0Sstevel@tonic-gate #include <limits.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #include <unistd.h>
48*0Sstevel@tonic-gate #include <sys/types.h>
49*0Sstevel@tonic-gate #include <sys/stat.h>
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #include "pathutil.h"
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate /*.......................................................................
54*0Sstevel@tonic-gate  * Create a new PathName object.
55*0Sstevel@tonic-gate  *
56*0Sstevel@tonic-gate  * Output:
57*0Sstevel@tonic-gate  *  return  PathName *  The new object, or NULL on error.
58*0Sstevel@tonic-gate  */
_new_PathName(void)59*0Sstevel@tonic-gate PathName *_new_PathName(void)
60*0Sstevel@tonic-gate {
61*0Sstevel@tonic-gate   PathName *path;  /* The object to be returned */
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate  * Allocate the container.
64*0Sstevel@tonic-gate  */
65*0Sstevel@tonic-gate   path = (PathName *) malloc(sizeof(PathName));
66*0Sstevel@tonic-gate   if(!path) {
67*0Sstevel@tonic-gate     errno = ENOMEM;
68*0Sstevel@tonic-gate     return NULL;
69*0Sstevel@tonic-gate   };
70*0Sstevel@tonic-gate /*
71*0Sstevel@tonic-gate  * Before attempting any operation that might fail, initialize the
72*0Sstevel@tonic-gate  * container at least up to the point at which it can safely be passed
73*0Sstevel@tonic-gate  * to _del_PathName().
74*0Sstevel@tonic-gate  */
75*0Sstevel@tonic-gate   path->name = NULL;
76*0Sstevel@tonic-gate   path->dim = 0;
77*0Sstevel@tonic-gate /*
78*0Sstevel@tonic-gate  * Figure out the maximum length of an expanded pathname.
79*0Sstevel@tonic-gate  */
80*0Sstevel@tonic-gate   path->dim = _pu_pathname_dim();
81*0Sstevel@tonic-gate   if(path->dim == 0)
82*0Sstevel@tonic-gate     return _del_PathName(path);
83*0Sstevel@tonic-gate /*
84*0Sstevel@tonic-gate  * Allocate the pathname buffer.
85*0Sstevel@tonic-gate  */
86*0Sstevel@tonic-gate   path->name = (char *)malloc(path->dim * sizeof(char));
87*0Sstevel@tonic-gate   if(!path->name) {
88*0Sstevel@tonic-gate     errno = ENOMEM;
89*0Sstevel@tonic-gate     return _del_PathName(path);
90*0Sstevel@tonic-gate   };
91*0Sstevel@tonic-gate   return path;
92*0Sstevel@tonic-gate }
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate /*.......................................................................
95*0Sstevel@tonic-gate  * Delete a PathName object.
96*0Sstevel@tonic-gate  *
97*0Sstevel@tonic-gate  * Input:
98*0Sstevel@tonic-gate  *  path   PathName *  The object to be deleted.
99*0Sstevel@tonic-gate  * Output:
100*0Sstevel@tonic-gate  *  return PathName *  The deleted object (always NULL).
101*0Sstevel@tonic-gate  */
_del_PathName(PathName * path)102*0Sstevel@tonic-gate PathName *_del_PathName(PathName *path)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate   if(path) {
105*0Sstevel@tonic-gate     if(path->name)
106*0Sstevel@tonic-gate       free(path->name);
107*0Sstevel@tonic-gate     free(path);
108*0Sstevel@tonic-gate   };
109*0Sstevel@tonic-gate   return NULL;
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate /*.......................................................................
113*0Sstevel@tonic-gate  * Return the pathname to a zero-length string.
114*0Sstevel@tonic-gate  *
115*0Sstevel@tonic-gate  * Input:
116*0Sstevel@tonic-gate  *  path     PathName *  The pathname container.
117*0Sstevel@tonic-gate  * Output:
118*0Sstevel@tonic-gate  *  return       char *  The cleared pathname buffer, or NULL on error.
119*0Sstevel@tonic-gate  */
_pn_clear_path(PathName * path)120*0Sstevel@tonic-gate char *_pn_clear_path(PathName *path)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate /*
123*0Sstevel@tonic-gate  * Check the arguments.
124*0Sstevel@tonic-gate  */
125*0Sstevel@tonic-gate   if(!path) {
126*0Sstevel@tonic-gate     errno = EINVAL;
127*0Sstevel@tonic-gate     return NULL;
128*0Sstevel@tonic-gate   };
129*0Sstevel@tonic-gate   path->name[0] = '\0';
130*0Sstevel@tonic-gate   return path->name;
131*0Sstevel@tonic-gate }
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate /*.......................................................................
134*0Sstevel@tonic-gate  * Append a string to a pathname, increasing the size of the pathname
135*0Sstevel@tonic-gate  * buffer if needed.
136*0Sstevel@tonic-gate  *
137*0Sstevel@tonic-gate  * Input:
138*0Sstevel@tonic-gate  *  path        PathName *  The pathname container.
139*0Sstevel@tonic-gate  *  string    const char *  The string to be appended to the pathname.
140*0Sstevel@tonic-gate  *                          Note that regardless of the slen argument,
141*0Sstevel@tonic-gate  *                          this should be a '\0' terminated string.
142*0Sstevel@tonic-gate  *  slen             int    The maximum number of characters to append
143*0Sstevel@tonic-gate  *                          from string[], or -1 to append the whole
144*0Sstevel@tonic-gate  *                          string.
145*0Sstevel@tonic-gate  *  remove_escapes   int    If true, remove the backslashes that escape
146*0Sstevel@tonic-gate  *                          spaces, tabs, backslashes etc..
147*0Sstevel@tonic-gate  * Output:
148*0Sstevel@tonic-gate  *  return          char *  The pathname string path->name[], which may
149*0Sstevel@tonic-gate  *                          have been reallocated, or NULL if there was
150*0Sstevel@tonic-gate  *                          insufficient memory to extend the pathname.
151*0Sstevel@tonic-gate  */
_pn_append_to_path(PathName * path,const char * string,int slen,int remove_escapes)152*0Sstevel@tonic-gate char *_pn_append_to_path(PathName *path, const char *string, int slen,
153*0Sstevel@tonic-gate 			int remove_escapes)
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate   int pathlen;     /* The length of the pathname */
156*0Sstevel@tonic-gate   int i;
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate  * Check the arguments.
159*0Sstevel@tonic-gate  */
160*0Sstevel@tonic-gate   if(!path || !string) {
161*0Sstevel@tonic-gate     errno = EINVAL;
162*0Sstevel@tonic-gate     return NULL;
163*0Sstevel@tonic-gate   };
164*0Sstevel@tonic-gate /*
165*0Sstevel@tonic-gate  * Get the current length of the pathname.
166*0Sstevel@tonic-gate  */
167*0Sstevel@tonic-gate   pathlen = strlen(path->name);
168*0Sstevel@tonic-gate /*
169*0Sstevel@tonic-gate  * How many characters should be appended?
170*0Sstevel@tonic-gate  */
171*0Sstevel@tonic-gate   if(slen < 0 || slen > strlen(string))
172*0Sstevel@tonic-gate     slen = strlen(string);
173*0Sstevel@tonic-gate /*
174*0Sstevel@tonic-gate  * Resize the pathname if needed.
175*0Sstevel@tonic-gate  */
176*0Sstevel@tonic-gate   if(!_pn_resize_path(path, pathlen + slen))
177*0Sstevel@tonic-gate     return NULL;
178*0Sstevel@tonic-gate /*
179*0Sstevel@tonic-gate  * Append the string to the output pathname, removing any escape
180*0Sstevel@tonic-gate  * characters found therein.
181*0Sstevel@tonic-gate  */
182*0Sstevel@tonic-gate   if(remove_escapes) {
183*0Sstevel@tonic-gate     int is_escape = 0;
184*0Sstevel@tonic-gate     for(i=0; i<slen; i++) {
185*0Sstevel@tonic-gate       is_escape = !is_escape && string[i] == '\\';
186*0Sstevel@tonic-gate       if(!is_escape)
187*0Sstevel@tonic-gate 	path->name[pathlen++] = string[i];
188*0Sstevel@tonic-gate     };
189*0Sstevel@tonic-gate /*
190*0Sstevel@tonic-gate  * Terminate the string.
191*0Sstevel@tonic-gate  */
192*0Sstevel@tonic-gate     path->name[pathlen] = '\0';
193*0Sstevel@tonic-gate   } else {
194*0Sstevel@tonic-gate /*
195*0Sstevel@tonic-gate  * Append the string directly to the pathname.
196*0Sstevel@tonic-gate  */
197*0Sstevel@tonic-gate     memcpy(path->name + pathlen, string, slen);
198*0Sstevel@tonic-gate     path->name[pathlen + slen] = '\0';
199*0Sstevel@tonic-gate   };
200*0Sstevel@tonic-gate   return path->name;
201*0Sstevel@tonic-gate }
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate /*.......................................................................
204*0Sstevel@tonic-gate  * Prepend a string to a pathname, increasing the size of the pathname
205*0Sstevel@tonic-gate  * buffer if needed.
206*0Sstevel@tonic-gate  *
207*0Sstevel@tonic-gate  * Input:
208*0Sstevel@tonic-gate  *  path        PathName *  The pathname container.
209*0Sstevel@tonic-gate  *  string    const char *  The string to be prepended to the pathname.
210*0Sstevel@tonic-gate  *                          Note that regardless of the slen argument,
211*0Sstevel@tonic-gate  *                          this should be a '\0' terminated string.
212*0Sstevel@tonic-gate  *  slen             int    The maximum number of characters to prepend
213*0Sstevel@tonic-gate  *                          from string[], or -1 to append the whole
214*0Sstevel@tonic-gate  *                          string.
215*0Sstevel@tonic-gate  *  remove_escapes   int    If true, remove the backslashes that escape
216*0Sstevel@tonic-gate  *                          spaces, tabs, backslashes etc..
217*0Sstevel@tonic-gate  * Output:
218*0Sstevel@tonic-gate  *  return          char *  The pathname string path->name[], which may
219*0Sstevel@tonic-gate  *                          have been reallocated, or NULL if there was
220*0Sstevel@tonic-gate  *                          insufficient memory to extend the pathname.
221*0Sstevel@tonic-gate  */
_pn_prepend_to_path(PathName * path,const char * string,int slen,int remove_escapes)222*0Sstevel@tonic-gate char *_pn_prepend_to_path(PathName *path, const char *string, int slen,
223*0Sstevel@tonic-gate 			  int remove_escapes)
224*0Sstevel@tonic-gate {
225*0Sstevel@tonic-gate   int pathlen;     /* The length of the pathname */
226*0Sstevel@tonic-gate   int shift;       /* The number of characters to shift the suffix by */
227*0Sstevel@tonic-gate   int i,j;
228*0Sstevel@tonic-gate /*
229*0Sstevel@tonic-gate  * Check the arguments.
230*0Sstevel@tonic-gate  */
231*0Sstevel@tonic-gate   if(!path || !string) {
232*0Sstevel@tonic-gate     errno = EINVAL;
233*0Sstevel@tonic-gate     return NULL;
234*0Sstevel@tonic-gate   };
235*0Sstevel@tonic-gate /*
236*0Sstevel@tonic-gate  * Get the current length of the pathname.
237*0Sstevel@tonic-gate  */
238*0Sstevel@tonic-gate   pathlen = strlen(path->name);
239*0Sstevel@tonic-gate /*
240*0Sstevel@tonic-gate  * How many characters should be appended?
241*0Sstevel@tonic-gate  */
242*0Sstevel@tonic-gate   if(slen < 0 || slen > strlen(string))
243*0Sstevel@tonic-gate     slen = strlen(string);
244*0Sstevel@tonic-gate /*
245*0Sstevel@tonic-gate  * Work out how far we need to shift the original path string to make
246*0Sstevel@tonic-gate  * way for the new prefix. When removing escape characters, we need
247*0Sstevel@tonic-gate  * final length of the new prefix, after unescaped backslashes have
248*0Sstevel@tonic-gate  * been removed.
249*0Sstevel@tonic-gate  */
250*0Sstevel@tonic-gate   if(remove_escapes) {
251*0Sstevel@tonic-gate     int is_escape = 0;
252*0Sstevel@tonic-gate     for(shift=0,i=0; i<slen; i++) {
253*0Sstevel@tonic-gate       is_escape = !is_escape && string[i] == '\\';
254*0Sstevel@tonic-gate       if(!is_escape)
255*0Sstevel@tonic-gate 	shift++;
256*0Sstevel@tonic-gate     };
257*0Sstevel@tonic-gate   } else {
258*0Sstevel@tonic-gate     shift = slen;
259*0Sstevel@tonic-gate   };
260*0Sstevel@tonic-gate /*
261*0Sstevel@tonic-gate  * Resize the pathname if needed.
262*0Sstevel@tonic-gate  */
263*0Sstevel@tonic-gate   if(!_pn_resize_path(path, pathlen + shift))
264*0Sstevel@tonic-gate     return NULL;
265*0Sstevel@tonic-gate /*
266*0Sstevel@tonic-gate  * Make room for the prefix at the beginning of the string.
267*0Sstevel@tonic-gate  */
268*0Sstevel@tonic-gate   memmove(path->name + shift, path->name, pathlen+1);
269*0Sstevel@tonic-gate /*
270*0Sstevel@tonic-gate  * Copy the new prefix into the vacated space at the beginning of the
271*0Sstevel@tonic-gate  * output pathname, removing any escape characters if needed.
272*0Sstevel@tonic-gate  */
273*0Sstevel@tonic-gate   if(remove_escapes) {
274*0Sstevel@tonic-gate     int is_escape = 0;
275*0Sstevel@tonic-gate     for(i=j=0; i<slen; i++) {
276*0Sstevel@tonic-gate       is_escape = !is_escape && string[i] == '\\';
277*0Sstevel@tonic-gate       if(!is_escape)
278*0Sstevel@tonic-gate 	path->name[j++] = string[i];
279*0Sstevel@tonic-gate     };
280*0Sstevel@tonic-gate   } else {
281*0Sstevel@tonic-gate     memcpy(path->name, string, slen);
282*0Sstevel@tonic-gate   };
283*0Sstevel@tonic-gate   return path->name;
284*0Sstevel@tonic-gate }
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate /*.......................................................................
287*0Sstevel@tonic-gate  * If needed reallocate a given pathname buffer to allow a string of
288*0Sstevel@tonic-gate  * a given length to be stored in it.
289*0Sstevel@tonic-gate  *
290*0Sstevel@tonic-gate  * Input:
291*0Sstevel@tonic-gate  *  path     PathName *  The pathname container object.
292*0Sstevel@tonic-gate  *  length     size_t    The required length of the pathname buffer,
293*0Sstevel@tonic-gate  *                       not including the terminating '\0'.
294*0Sstevel@tonic-gate  * Output:
295*0Sstevel@tonic-gate  *  return       char *  The pathname buffer, or NULL if there was
296*0Sstevel@tonic-gate  *                       insufficient memory.
297*0Sstevel@tonic-gate  */
_pn_resize_path(PathName * path,size_t length)298*0Sstevel@tonic-gate char *_pn_resize_path(PathName *path, size_t length)
299*0Sstevel@tonic-gate {
300*0Sstevel@tonic-gate /*
301*0Sstevel@tonic-gate  * Check the arguments.
302*0Sstevel@tonic-gate  */
303*0Sstevel@tonic-gate   if(!path) {
304*0Sstevel@tonic-gate     errno = EINVAL;
305*0Sstevel@tonic-gate     return NULL;
306*0Sstevel@tonic-gate   };
307*0Sstevel@tonic-gate /*
308*0Sstevel@tonic-gate  * If the pathname buffer isn't large enough to accomodate a string
309*0Sstevel@tonic-gate  * of the specified length, attempt to reallocate it with the new
310*0Sstevel@tonic-gate  * size, plus space for a terminating '\0'. Also add a bit of
311*0Sstevel@tonic-gate  * head room to prevent too many reallocations if the initial length
312*0Sstevel@tonic-gate  * turned out to be very optimistic.
313*0Sstevel@tonic-gate  */
314*0Sstevel@tonic-gate   if(length + 1 > path->dim) {
315*0Sstevel@tonic-gate     size_t dim =  length + 1 + PN_PATHNAME_INC;
316*0Sstevel@tonic-gate     char *name = (char *) realloc(path->name, dim);
317*0Sstevel@tonic-gate     if(!name)
318*0Sstevel@tonic-gate       return NULL;
319*0Sstevel@tonic-gate     path->name = name;
320*0Sstevel@tonic-gate     path->dim = dim;
321*0Sstevel@tonic-gate   };
322*0Sstevel@tonic-gate   return path->name;
323*0Sstevel@tonic-gate }
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate /*.......................................................................
326*0Sstevel@tonic-gate  * Estimate the largest amount of space needed to store a pathname.
327*0Sstevel@tonic-gate  *
328*0Sstevel@tonic-gate  * Output:
329*0Sstevel@tonic-gate  *  return size_t   The number of bytes needed, including space for the
330*0Sstevel@tonic-gate  *                  terminating '\0'.
331*0Sstevel@tonic-gate  */
_pu_pathname_dim(void)332*0Sstevel@tonic-gate size_t _pu_pathname_dim(void)
333*0Sstevel@tonic-gate {
334*0Sstevel@tonic-gate   int maxlen;   /* The return value excluding space for the '\0' */
335*0Sstevel@tonic-gate /*
336*0Sstevel@tonic-gate  * If the POSIX PATH_MAX macro is defined in limits.h, use it.
337*0Sstevel@tonic-gate  */
338*0Sstevel@tonic-gate #ifdef PATH_MAX
339*0Sstevel@tonic-gate   maxlen = PATH_MAX;
340*0Sstevel@tonic-gate /*
341*0Sstevel@tonic-gate  * If we have pathconf, use it.
342*0Sstevel@tonic-gate  */
343*0Sstevel@tonic-gate #elif defined(_PC_PATH_MAX)
344*0Sstevel@tonic-gate   errno = 0;
345*0Sstevel@tonic-gate   maxlen = pathconf(FS_ROOT_DIR, _PC_PATH_MAX);
346*0Sstevel@tonic-gate   if(maxlen <= 0 || errno)
347*0Sstevel@tonic-gate     maxlen = MAX_PATHLEN_FALLBACK;
348*0Sstevel@tonic-gate /*
349*0Sstevel@tonic-gate  * None of the above approaches worked, so substitute our fallback
350*0Sstevel@tonic-gate  * guess.
351*0Sstevel@tonic-gate  */
352*0Sstevel@tonic-gate #else
353*0Sstevel@tonic-gate     maxlen = MAX_PATHLEN_FALLBACK;
354*0Sstevel@tonic-gate #endif
355*0Sstevel@tonic-gate /*
356*0Sstevel@tonic-gate  * Return the amount of space needed to accomodate a pathname plus
357*0Sstevel@tonic-gate  * a terminating '\0'.
358*0Sstevel@tonic-gate  */
359*0Sstevel@tonic-gate   return maxlen + 1;
360*0Sstevel@tonic-gate }
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate /*.......................................................................
363*0Sstevel@tonic-gate  * Return non-zero if the specified path name refers to a directory.
364*0Sstevel@tonic-gate  *
365*0Sstevel@tonic-gate  * Input:
366*0Sstevel@tonic-gate  *  pathname  const char *  The path to test.
367*0Sstevel@tonic-gate  * Output:
368*0Sstevel@tonic-gate  *  return           int    0 - Not a directory.
369*0Sstevel@tonic-gate  *                          1 - pathname[] refers to a directory.
370*0Sstevel@tonic-gate  */
_pu_path_is_dir(const char * pathname)371*0Sstevel@tonic-gate int _pu_path_is_dir(const char *pathname)
372*0Sstevel@tonic-gate {
373*0Sstevel@tonic-gate   struct stat statbuf;    /* The file-statistics return buffer */
374*0Sstevel@tonic-gate /*
375*0Sstevel@tonic-gate  * Look up the file attributes.
376*0Sstevel@tonic-gate  */
377*0Sstevel@tonic-gate   if(stat(pathname, &statbuf) < 0)
378*0Sstevel@tonic-gate     return 0;
379*0Sstevel@tonic-gate /*
380*0Sstevel@tonic-gate  * Is the file a directory?
381*0Sstevel@tonic-gate  */
382*0Sstevel@tonic-gate   return S_ISDIR(statbuf.st_mode) != 0;
383*0Sstevel@tonic-gate }
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate /*.......................................................................
386*0Sstevel@tonic-gate  * Return non-zero if the specified path name refers to a regular file.
387*0Sstevel@tonic-gate  *
388*0Sstevel@tonic-gate  * Input:
389*0Sstevel@tonic-gate  *  pathname  const char *  The path to test.
390*0Sstevel@tonic-gate  * Output:
391*0Sstevel@tonic-gate  *  return           int    0 - Not a regular file.
392*0Sstevel@tonic-gate  *                          1 - pathname[] refers to a regular file.
393*0Sstevel@tonic-gate  */
_pu_path_is_file(const char * pathname)394*0Sstevel@tonic-gate int _pu_path_is_file(const char *pathname)
395*0Sstevel@tonic-gate {
396*0Sstevel@tonic-gate   struct stat statbuf;    /* The file-statistics return buffer */
397*0Sstevel@tonic-gate /*
398*0Sstevel@tonic-gate  * Look up the file attributes.
399*0Sstevel@tonic-gate  */
400*0Sstevel@tonic-gate   if(stat(pathname, &statbuf) < 0)
401*0Sstevel@tonic-gate     return 0;
402*0Sstevel@tonic-gate /*
403*0Sstevel@tonic-gate  * Is the file a regular file?
404*0Sstevel@tonic-gate  */
405*0Sstevel@tonic-gate   return S_ISREG(statbuf.st_mode) != 0;
406*0Sstevel@tonic-gate }
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate /*.......................................................................
409*0Sstevel@tonic-gate  * Return non-zero if the specified path name refers to an executable.
410*0Sstevel@tonic-gate  *
411*0Sstevel@tonic-gate  * Input:
412*0Sstevel@tonic-gate  *  pathname  const char *  The path to test.
413*0Sstevel@tonic-gate  * Output:
414*0Sstevel@tonic-gate  *  return           int    0 - Not an executable file.
415*0Sstevel@tonic-gate  *                          1 - pathname[] refers to an executable file.
416*0Sstevel@tonic-gate  */
_pu_path_is_exe(const char * pathname)417*0Sstevel@tonic-gate int _pu_path_is_exe(const char *pathname)
418*0Sstevel@tonic-gate {
419*0Sstevel@tonic-gate   struct stat statbuf;    /* The file-statistics return buffer */
420*0Sstevel@tonic-gate /*
421*0Sstevel@tonic-gate  * Look up the file attributes.
422*0Sstevel@tonic-gate  */
423*0Sstevel@tonic-gate   if(stat(pathname, &statbuf) < 0)
424*0Sstevel@tonic-gate     return 0;
425*0Sstevel@tonic-gate /*
426*0Sstevel@tonic-gate  * Is the file a regular file which is executable by the current user.
427*0Sstevel@tonic-gate  */
428*0Sstevel@tonic-gate   return S_ISREG(statbuf.st_mode) != 0 &&
429*0Sstevel@tonic-gate     (statbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
430*0Sstevel@tonic-gate     access(pathname, X_OK) == 0;
431*0Sstevel@tonic-gate }
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate /*.......................................................................
434*0Sstevel@tonic-gate  * Search backwards for the potential start of a filename. This
435*0Sstevel@tonic-gate  * looks backwards from the specified index in a given string,
436*0Sstevel@tonic-gate  * stopping at the first unescaped space or the start of the line.
437*0Sstevel@tonic-gate  *
438*0Sstevel@tonic-gate  * Input:
439*0Sstevel@tonic-gate  *  string  const char *  The string to search backwards in.
440*0Sstevel@tonic-gate  *  back_from      int    The index of the first character in string[]
441*0Sstevel@tonic-gate  *                        that follows the pathname.
442*0Sstevel@tonic-gate  * Output:
443*0Sstevel@tonic-gate  *  return        char *  The pointer to the first character of
444*0Sstevel@tonic-gate  *                        the potential pathname, or NULL on error.
445*0Sstevel@tonic-gate  */
_pu_start_of_path(const char * string,int back_from)446*0Sstevel@tonic-gate char *_pu_start_of_path(const char *string, int back_from)
447*0Sstevel@tonic-gate {
448*0Sstevel@tonic-gate   int i, j;
449*0Sstevel@tonic-gate /*
450*0Sstevel@tonic-gate  * Check the arguments.
451*0Sstevel@tonic-gate  */
452*0Sstevel@tonic-gate   if(!string || back_from < 0) {
453*0Sstevel@tonic-gate     errno = EINVAL;
454*0Sstevel@tonic-gate     return NULL;
455*0Sstevel@tonic-gate   };
456*0Sstevel@tonic-gate /*
457*0Sstevel@tonic-gate  * Search backwards from the specified index.
458*0Sstevel@tonic-gate  */
459*0Sstevel@tonic-gate   for(i=back_from-1; i>=0; i--) {
460*0Sstevel@tonic-gate     int c = string[i];
461*0Sstevel@tonic-gate /*
462*0Sstevel@tonic-gate  * Stop on unescaped spaces.
463*0Sstevel@tonic-gate  */
464*0Sstevel@tonic-gate     if(isspace((int)(unsigned char)c)) {
465*0Sstevel@tonic-gate /*
466*0Sstevel@tonic-gate  * The space can't be escaped if we are at the start of the line.
467*0Sstevel@tonic-gate  */
468*0Sstevel@tonic-gate       if(i==0)
469*0Sstevel@tonic-gate         break;
470*0Sstevel@tonic-gate /*
471*0Sstevel@tonic-gate  * Find the extent of the escape characters which precedes the space.
472*0Sstevel@tonic-gate  */
473*0Sstevel@tonic-gate       for(j=i-1; j>=0 && string[j]=='\\'; j--)
474*0Sstevel@tonic-gate 	;
475*0Sstevel@tonic-gate /*
476*0Sstevel@tonic-gate  * If there isn't an odd number of escape characters before the space,
477*0Sstevel@tonic-gate  * then the space isn't escaped.
478*0Sstevel@tonic-gate  */
479*0Sstevel@tonic-gate       if((i - 1 - j) % 2 == 0)
480*0Sstevel@tonic-gate 	break;
481*0Sstevel@tonic-gate     };
482*0Sstevel@tonic-gate   };
483*0Sstevel@tonic-gate   return (char *)string + i + 1;
484*0Sstevel@tonic-gate }
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate /*.......................................................................
487*0Sstevel@tonic-gate  * Find the length of a potential filename starting from a given
488*0Sstevel@tonic-gate  * point. This looks forwards from the specified index in a given string,
489*0Sstevel@tonic-gate  * stopping at the first unescaped space or the end of the line.
490*0Sstevel@tonic-gate  *
491*0Sstevel@tonic-gate  * Input:
492*0Sstevel@tonic-gate  *  string   const char *  The string to search backwards in.
493*0Sstevel@tonic-gate  *  start_from      int    The index of the first character of the pathname
494*0Sstevel@tonic-gate  *                         in string[].
495*0Sstevel@tonic-gate  * Output:
496*0Sstevel@tonic-gate  *  return         char *  The pointer to the character that follows
497*0Sstevel@tonic-gate  *                         the potential pathname, or NULL on error.
498*0Sstevel@tonic-gate  */
_pu_end_of_path(const char * string,int start_from)499*0Sstevel@tonic-gate char *_pu_end_of_path(const char *string, int start_from)
500*0Sstevel@tonic-gate {
501*0Sstevel@tonic-gate   int c;             /* The character being examined */
502*0Sstevel@tonic-gate   int escaped = 0;   /* True when the next character is escaped */
503*0Sstevel@tonic-gate   int i;
504*0Sstevel@tonic-gate /*
505*0Sstevel@tonic-gate  * Check the arguments.
506*0Sstevel@tonic-gate  */
507*0Sstevel@tonic-gate   if(!string || start_from < 0) {
508*0Sstevel@tonic-gate     errno = EINVAL;
509*0Sstevel@tonic-gate     return NULL;
510*0Sstevel@tonic-gate   };
511*0Sstevel@tonic-gate /*
512*0Sstevel@tonic-gate  * Search forwards from the specified index.
513*0Sstevel@tonic-gate  */
514*0Sstevel@tonic-gate   for(i=start_from; (c=string[i]) != '\0'; i++) {
515*0Sstevel@tonic-gate     if(escaped) {
516*0Sstevel@tonic-gate       escaped = 0;
517*0Sstevel@tonic-gate     } else if(isspace(c)) {
518*0Sstevel@tonic-gate       break;
519*0Sstevel@tonic-gate     } else if(c == '\\') {
520*0Sstevel@tonic-gate       escaped = 1;
521*0Sstevel@tonic-gate     };
522*0Sstevel@tonic-gate   };
523*0Sstevel@tonic-gate   return (char *)string + i;
524*0Sstevel@tonic-gate }
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate /*.......................................................................
527*0Sstevel@tonic-gate  * Return non-zero if the specified path name refers to an existing file.
528*0Sstevel@tonic-gate  *
529*0Sstevel@tonic-gate  * Input:
530*0Sstevel@tonic-gate  *  pathname   const char *  The path to test.
531*0Sstevel@tonic-gate  * Output:
532*0Sstevel@tonic-gate  *  return            int    0 - The file doesn't exist.
533*0Sstevel@tonic-gate  *                           1 - The file does exist.
534*0Sstevel@tonic-gate  */
_pu_file_exists(const char * pathname)535*0Sstevel@tonic-gate int _pu_file_exists(const char *pathname)
536*0Sstevel@tonic-gate {
537*0Sstevel@tonic-gate   struct stat statbuf;
538*0Sstevel@tonic-gate   return stat(pathname, &statbuf) == 0;
539*0Sstevel@tonic-gate }
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate #endif  /* ifndef WITHOUT_FILE_SYSTEM */
542