17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
3*1da57d55SToomas Soome  *
47c478bd9Sstevel@tonic-gate  * All rights reserved.
5*1da57d55SToomas Soome  *
67c478bd9Sstevel@tonic-gate  * Permission is hereby granted, free of charge, to any person obtaining a
77c478bd9Sstevel@tonic-gate  * copy of this software and associated documentation files (the
87c478bd9Sstevel@tonic-gate  * "Software"), to deal in the Software without restriction, including
97c478bd9Sstevel@tonic-gate  * without limitation the rights to use, copy, modify, merge, publish,
107c478bd9Sstevel@tonic-gate  * distribute, and/or sell copies of the Software, and to permit persons
117c478bd9Sstevel@tonic-gate  * to whom the Software is furnished to do so, provided that the above
127c478bd9Sstevel@tonic-gate  * copyright notice(s) and this permission notice appear in all copies of
137c478bd9Sstevel@tonic-gate  * the Software and that both the above copyright notice(s) and this
147c478bd9Sstevel@tonic-gate  * permission notice appear in supporting documentation.
15*1da57d55SToomas Soome  *
167c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
177c478bd9Sstevel@tonic-gate  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
187c478bd9Sstevel@tonic-gate  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
197c478bd9Sstevel@tonic-gate  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
207c478bd9Sstevel@tonic-gate  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
217c478bd9Sstevel@tonic-gate  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
227c478bd9Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
237c478bd9Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
247c478bd9Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25*1da57d55SToomas Soome  *
267c478bd9Sstevel@tonic-gate  * Except as contained in this notice, the name of a copyright holder
277c478bd9Sstevel@tonic-gate  * shall not be used in advertising or otherwise to promote the sale, use
287c478bd9Sstevel@tonic-gate  * or other dealings in this Software without prior written authorization
297c478bd9Sstevel@tonic-gate  * of the copyright holder.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * If file-system access is to be excluded, this module has no function,
347c478bd9Sstevel@tonic-gate  * so all of its code should be excluded.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate #ifndef WITHOUT_FILE_SYSTEM
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <ctype.h>
437c478bd9Sstevel@tonic-gate #include <limits.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/stat.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include "pathutil.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*.......................................................................
527c478bd9Sstevel@tonic-gate  * Create a new PathName object.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * Output:
557c478bd9Sstevel@tonic-gate  *  return  PathName *  The new object, or NULL on error.
567c478bd9Sstevel@tonic-gate  */
_new_PathName(void)577c478bd9Sstevel@tonic-gate PathName *_new_PathName(void)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate   PathName *path;  /* The object to be returned */
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Allocate the container.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate   path = (PathName *) malloc(sizeof(PathName));
647c478bd9Sstevel@tonic-gate   if(!path) {
657c478bd9Sstevel@tonic-gate     errno = ENOMEM;
667c478bd9Sstevel@tonic-gate     return NULL;
677c478bd9Sstevel@tonic-gate   };
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * Before attempting any operation that might fail, initialize the
707c478bd9Sstevel@tonic-gate  * container at least up to the point at which it can safely be passed
717c478bd9Sstevel@tonic-gate  * to _del_PathName().
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate   path->name = NULL;
747c478bd9Sstevel@tonic-gate   path->dim = 0;
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Figure out the maximum length of an expanded pathname.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate   path->dim = _pu_pathname_dim();
797c478bd9Sstevel@tonic-gate   if(path->dim == 0)
807c478bd9Sstevel@tonic-gate     return _del_PathName(path);
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * Allocate the pathname buffer.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate   path->name = (char *)malloc(path->dim * sizeof(char));
857c478bd9Sstevel@tonic-gate   if(!path->name) {
867c478bd9Sstevel@tonic-gate     errno = ENOMEM;
877c478bd9Sstevel@tonic-gate     return _del_PathName(path);
887c478bd9Sstevel@tonic-gate   };
897c478bd9Sstevel@tonic-gate   return path;
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*.......................................................................
937c478bd9Sstevel@tonic-gate  * Delete a PathName object.
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  * Input:
967c478bd9Sstevel@tonic-gate  *  path   PathName *  The object to be deleted.
977c478bd9Sstevel@tonic-gate  * Output:
987c478bd9Sstevel@tonic-gate  *  return PathName *  The deleted object (always NULL).
997c478bd9Sstevel@tonic-gate  */
_del_PathName(PathName * path)1007c478bd9Sstevel@tonic-gate PathName *_del_PathName(PathName *path)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate   if(path) {
1037c478bd9Sstevel@tonic-gate     if(path->name)
1047c478bd9Sstevel@tonic-gate       free(path->name);
1057c478bd9Sstevel@tonic-gate     free(path);
1067c478bd9Sstevel@tonic-gate   };
1077c478bd9Sstevel@tonic-gate   return NULL;
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*.......................................................................
1117c478bd9Sstevel@tonic-gate  * Return the pathname to a zero-length string.
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * Input:
1147c478bd9Sstevel@tonic-gate  *  path     PathName *  The pathname container.
1157c478bd9Sstevel@tonic-gate  * Output:
1167c478bd9Sstevel@tonic-gate  *  return       char *  The cleared pathname buffer, or NULL on error.
1177c478bd9Sstevel@tonic-gate  */
_pn_clear_path(PathName * path)1187c478bd9Sstevel@tonic-gate char *_pn_clear_path(PathName *path)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * Check the arguments.
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate   if(!path) {
1247c478bd9Sstevel@tonic-gate     errno = EINVAL;
1257c478bd9Sstevel@tonic-gate     return NULL;
1267c478bd9Sstevel@tonic-gate   };
1277c478bd9Sstevel@tonic-gate   path->name[0] = '\0';
1287c478bd9Sstevel@tonic-gate   return path->name;
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*.......................................................................
1327c478bd9Sstevel@tonic-gate  * Append a string to a pathname, increasing the size of the pathname
1337c478bd9Sstevel@tonic-gate  * buffer if needed.
1347c478bd9Sstevel@tonic-gate  *
1357c478bd9Sstevel@tonic-gate  * Input:
1367c478bd9Sstevel@tonic-gate  *  path        PathName *  The pathname container.
1377c478bd9Sstevel@tonic-gate  *  string    const char *  The string to be appended to the pathname.
1387c478bd9Sstevel@tonic-gate  *                          Note that regardless of the slen argument,
1397c478bd9Sstevel@tonic-gate  *                          this should be a '\0' terminated string.
1407c478bd9Sstevel@tonic-gate  *  slen             int    The maximum number of characters to append
1417c478bd9Sstevel@tonic-gate  *                          from string[], or -1 to append the whole
1427c478bd9Sstevel@tonic-gate  *                          string.
1437c478bd9Sstevel@tonic-gate  *  remove_escapes   int    If true, remove the backslashes that escape
1447c478bd9Sstevel@tonic-gate  *                          spaces, tabs, backslashes etc..
1457c478bd9Sstevel@tonic-gate  * Output:
1467c478bd9Sstevel@tonic-gate  *  return          char *  The pathname string path->name[], which may
1477c478bd9Sstevel@tonic-gate  *                          have been reallocated, or NULL if there was
1487c478bd9Sstevel@tonic-gate  *                          insufficient memory to extend the pathname.
1497c478bd9Sstevel@tonic-gate  */
_pn_append_to_path(PathName * path,const char * string,int slen,int remove_escapes)1507c478bd9Sstevel@tonic-gate char *_pn_append_to_path(PathName *path, const char *string, int slen,
1517c478bd9Sstevel@tonic-gate 			int remove_escapes)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate   int pathlen;     /* The length of the pathname */
1547c478bd9Sstevel@tonic-gate   int i;
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Check the arguments.
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate   if(!path || !string) {
1597c478bd9Sstevel@tonic-gate     errno = EINVAL;
1607c478bd9Sstevel@tonic-gate     return NULL;
1617c478bd9Sstevel@tonic-gate   };
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Get the current length of the pathname.
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate   pathlen = strlen(path->name);
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  * How many characters should be appended?
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate   if(slen < 0 || slen > strlen(string))
1707c478bd9Sstevel@tonic-gate     slen = strlen(string);
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * Resize the pathname if needed.
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate   if(!_pn_resize_path(path, pathlen + slen))
1757c478bd9Sstevel@tonic-gate     return NULL;
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * Append the string to the output pathname, removing any escape
1787c478bd9Sstevel@tonic-gate  * characters found therein.
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate   if(remove_escapes) {
1817c478bd9Sstevel@tonic-gate     int is_escape = 0;
1827c478bd9Sstevel@tonic-gate     for(i=0; i<slen; i++) {
1837c478bd9Sstevel@tonic-gate       is_escape = !is_escape && string[i] == '\\';
1847c478bd9Sstevel@tonic-gate       if(!is_escape)
1857c478bd9Sstevel@tonic-gate 	path->name[pathlen++] = string[i];
1867c478bd9Sstevel@tonic-gate     };
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * Terminate the string.
1897c478bd9Sstevel@tonic-gate  */
1907c478bd9Sstevel@tonic-gate     path->name[pathlen] = '\0';
1917c478bd9Sstevel@tonic-gate   } else {
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate  * Append the string directly to the pathname.
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate     memcpy(path->name + pathlen, string, slen);
1967c478bd9Sstevel@tonic-gate     path->name[pathlen + slen] = '\0';
1977c478bd9Sstevel@tonic-gate   };
1987c478bd9Sstevel@tonic-gate   return path->name;
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*.......................................................................
2027c478bd9Sstevel@tonic-gate  * Prepend a string to a pathname, increasing the size of the pathname
2037c478bd9Sstevel@tonic-gate  * buffer if needed.
2047c478bd9Sstevel@tonic-gate  *
2057c478bd9Sstevel@tonic-gate  * Input:
2067c478bd9Sstevel@tonic-gate  *  path        PathName *  The pathname container.
2077c478bd9Sstevel@tonic-gate  *  string    const char *  The string to be prepended to the pathname.
2087c478bd9Sstevel@tonic-gate  *                          Note that regardless of the slen argument,
2097c478bd9Sstevel@tonic-gate  *                          this should be a '\0' terminated string.
2107c478bd9Sstevel@tonic-gate  *  slen             int    The maximum number of characters to prepend
2117c478bd9Sstevel@tonic-gate  *                          from string[], or -1 to append the whole
2127c478bd9Sstevel@tonic-gate  *                          string.
2137c478bd9Sstevel@tonic-gate  *  remove_escapes   int    If true, remove the backslashes that escape
2147c478bd9Sstevel@tonic-gate  *                          spaces, tabs, backslashes etc..
2157c478bd9Sstevel@tonic-gate  * Output:
2167c478bd9Sstevel@tonic-gate  *  return          char *  The pathname string path->name[], which may
2177c478bd9Sstevel@tonic-gate  *                          have been reallocated, or NULL if there was
2187c478bd9Sstevel@tonic-gate  *                          insufficient memory to extend the pathname.
2197c478bd9Sstevel@tonic-gate  */
_pn_prepend_to_path(PathName * path,const char * string,int slen,int remove_escapes)2207c478bd9Sstevel@tonic-gate char *_pn_prepend_to_path(PathName *path, const char *string, int slen,
2217c478bd9Sstevel@tonic-gate 			  int remove_escapes)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate   int pathlen;     /* The length of the pathname */
2247c478bd9Sstevel@tonic-gate   int shift;       /* The number of characters to shift the suffix by */
2257c478bd9Sstevel@tonic-gate   int i,j;
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * Check the arguments.
2287c478bd9Sstevel@tonic-gate  */
2297c478bd9Sstevel@tonic-gate   if(!path || !string) {
2307c478bd9Sstevel@tonic-gate     errno = EINVAL;
2317c478bd9Sstevel@tonic-gate     return NULL;
2327c478bd9Sstevel@tonic-gate   };
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * Get the current length of the pathname.
2357c478bd9Sstevel@tonic-gate  */
2367c478bd9Sstevel@tonic-gate   pathlen = strlen(path->name);
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate  * How many characters should be appended?
2397c478bd9Sstevel@tonic-gate  */
2407c478bd9Sstevel@tonic-gate   if(slen < 0 || slen > strlen(string))
2417c478bd9Sstevel@tonic-gate     slen = strlen(string);
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * Work out how far we need to shift the original path string to make
2447c478bd9Sstevel@tonic-gate  * way for the new prefix. When removing escape characters, we need
2457c478bd9Sstevel@tonic-gate  * final length of the new prefix, after unescaped backslashes have
2467c478bd9Sstevel@tonic-gate  * been removed.
2477c478bd9Sstevel@tonic-gate  */
2487c478bd9Sstevel@tonic-gate   if(remove_escapes) {
2497c478bd9Sstevel@tonic-gate     int is_escape = 0;
2507c478bd9Sstevel@tonic-gate     for(shift=0,i=0; i<slen; i++) {
2517c478bd9Sstevel@tonic-gate       is_escape = !is_escape && string[i] == '\\';
2527c478bd9Sstevel@tonic-gate       if(!is_escape)
2537c478bd9Sstevel@tonic-gate 	shift++;
2547c478bd9Sstevel@tonic-gate     };
2557c478bd9Sstevel@tonic-gate   } else {
2567c478bd9Sstevel@tonic-gate     shift = slen;
2577c478bd9Sstevel@tonic-gate   };
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * Resize the pathname if needed.
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate   if(!_pn_resize_path(path, pathlen + shift))
2627c478bd9Sstevel@tonic-gate     return NULL;
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * Make room for the prefix at the beginning of the string.
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate   memmove(path->name + shift, path->name, pathlen+1);
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate  * Copy the new prefix into the vacated space at the beginning of the
2697c478bd9Sstevel@tonic-gate  * output pathname, removing any escape characters if needed.
2707c478bd9Sstevel@tonic-gate  */
2717c478bd9Sstevel@tonic-gate   if(remove_escapes) {
2727c478bd9Sstevel@tonic-gate     int is_escape = 0;
2737c478bd9Sstevel@tonic-gate     for(i=j=0; i<slen; i++) {
2747c478bd9Sstevel@tonic-gate       is_escape = !is_escape && string[i] == '\\';
2757c478bd9Sstevel@tonic-gate       if(!is_escape)
2767c478bd9Sstevel@tonic-gate 	path->name[j++] = string[i];
2777c478bd9Sstevel@tonic-gate     };
2787c478bd9Sstevel@tonic-gate   } else {
2797c478bd9Sstevel@tonic-gate     memcpy(path->name, string, slen);
2807c478bd9Sstevel@tonic-gate   };
2817c478bd9Sstevel@tonic-gate   return path->name;
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*.......................................................................
2857c478bd9Sstevel@tonic-gate  * If needed reallocate a given pathname buffer to allow a string of
2867c478bd9Sstevel@tonic-gate  * a given length to be stored in it.
2877c478bd9Sstevel@tonic-gate  *
2887c478bd9Sstevel@tonic-gate  * Input:
2897c478bd9Sstevel@tonic-gate  *  path     PathName *  The pathname container object.
2907c478bd9Sstevel@tonic-gate  *  length     size_t    The required length of the pathname buffer,
2917c478bd9Sstevel@tonic-gate  *                       not including the terminating '\0'.
2927c478bd9Sstevel@tonic-gate  * Output:
2937c478bd9Sstevel@tonic-gate  *  return       char *  The pathname buffer, or NULL if there was
2947c478bd9Sstevel@tonic-gate  *                       insufficient memory.
2957c478bd9Sstevel@tonic-gate  */
_pn_resize_path(PathName * path,size_t length)2967c478bd9Sstevel@tonic-gate char *_pn_resize_path(PathName *path, size_t length)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate /*
2997c478bd9Sstevel@tonic-gate  * Check the arguments.
3007c478bd9Sstevel@tonic-gate  */
3017c478bd9Sstevel@tonic-gate   if(!path) {
3027c478bd9Sstevel@tonic-gate     errno = EINVAL;
3037c478bd9Sstevel@tonic-gate     return NULL;
3047c478bd9Sstevel@tonic-gate   };
3057c478bd9Sstevel@tonic-gate /*
3067c478bd9Sstevel@tonic-gate  * If the pathname buffer isn't large enough to accomodate a string
3077c478bd9Sstevel@tonic-gate  * of the specified length, attempt to reallocate it with the new
3087c478bd9Sstevel@tonic-gate  * size, plus space for a terminating '\0'. Also add a bit of
3097c478bd9Sstevel@tonic-gate  * head room to prevent too many reallocations if the initial length
3107c478bd9Sstevel@tonic-gate  * turned out to be very optimistic.
3117c478bd9Sstevel@tonic-gate  */
3127c478bd9Sstevel@tonic-gate   if(length + 1 > path->dim) {
3137c478bd9Sstevel@tonic-gate     size_t dim =  length + 1 + PN_PATHNAME_INC;
3147c478bd9Sstevel@tonic-gate     char *name = (char *) realloc(path->name, dim);
3157c478bd9Sstevel@tonic-gate     if(!name)
3167c478bd9Sstevel@tonic-gate       return NULL;
3177c478bd9Sstevel@tonic-gate     path->name = name;
3187c478bd9Sstevel@tonic-gate     path->dim = dim;
3197c478bd9Sstevel@tonic-gate   };
3207c478bd9Sstevel@tonic-gate   return path->name;
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate /*.......................................................................
3247c478bd9Sstevel@tonic-gate  * Estimate the largest amount of space needed to store a pathname.
3257c478bd9Sstevel@tonic-gate  *
3267c478bd9Sstevel@tonic-gate  * Output:
3277c478bd9Sstevel@tonic-gate  *  return size_t   The number of bytes needed, including space for the
3287c478bd9Sstevel@tonic-gate  *                  terminating '\0'.
3297c478bd9Sstevel@tonic-gate  */
_pu_pathname_dim(void)3307c478bd9Sstevel@tonic-gate size_t _pu_pathname_dim(void)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate   int maxlen;   /* The return value excluding space for the '\0' */
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate  * If the POSIX PATH_MAX macro is defined in limits.h, use it.
3357c478bd9Sstevel@tonic-gate  */
3367c478bd9Sstevel@tonic-gate #ifdef PATH_MAX
3377c478bd9Sstevel@tonic-gate   maxlen = PATH_MAX;
3387c478bd9Sstevel@tonic-gate /*
3397c478bd9Sstevel@tonic-gate  * If we have pathconf, use it.
3407c478bd9Sstevel@tonic-gate  */
3417c478bd9Sstevel@tonic-gate #elif defined(_PC_PATH_MAX)
3427c478bd9Sstevel@tonic-gate   errno = 0;
3437c478bd9Sstevel@tonic-gate   maxlen = pathconf(FS_ROOT_DIR, _PC_PATH_MAX);
3447c478bd9Sstevel@tonic-gate   if(maxlen <= 0 || errno)
3457c478bd9Sstevel@tonic-gate     maxlen = MAX_PATHLEN_FALLBACK;
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate  * None of the above approaches worked, so substitute our fallback
3487c478bd9Sstevel@tonic-gate  * guess.
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate #else
3517c478bd9Sstevel@tonic-gate     maxlen = MAX_PATHLEN_FALLBACK;
3527c478bd9Sstevel@tonic-gate #endif
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate  * Return the amount of space needed to accomodate a pathname plus
3557c478bd9Sstevel@tonic-gate  * a terminating '\0'.
3567c478bd9Sstevel@tonic-gate  */
3577c478bd9Sstevel@tonic-gate   return maxlen + 1;
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate /*.......................................................................
3617c478bd9Sstevel@tonic-gate  * Return non-zero if the specified path name refers to a directory.
3627c478bd9Sstevel@tonic-gate  *
3637c478bd9Sstevel@tonic-gate  * Input:
3647c478bd9Sstevel@tonic-gate  *  pathname  const char *  The path to test.
3657c478bd9Sstevel@tonic-gate  * Output:
3667c478bd9Sstevel@tonic-gate  *  return           int    0 - Not a directory.
3677c478bd9Sstevel@tonic-gate  *                          1 - pathname[] refers to a directory.
3687c478bd9Sstevel@tonic-gate  */
_pu_path_is_dir(const char * pathname)3697c478bd9Sstevel@tonic-gate int _pu_path_is_dir(const char *pathname)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate   struct stat statbuf;    /* The file-statistics return buffer */
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate  * Look up the file attributes.
3747c478bd9Sstevel@tonic-gate  */
3757c478bd9Sstevel@tonic-gate   if(stat(pathname, &statbuf) < 0)
3767c478bd9Sstevel@tonic-gate     return 0;
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * Is the file a directory?
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate   return S_ISDIR(statbuf.st_mode) != 0;
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /*.......................................................................
3847c478bd9Sstevel@tonic-gate  * Return non-zero if the specified path name refers to a regular file.
3857c478bd9Sstevel@tonic-gate  *
3867c478bd9Sstevel@tonic-gate  * Input:
3877c478bd9Sstevel@tonic-gate  *  pathname  const char *  The path to test.
3887c478bd9Sstevel@tonic-gate  * Output:
3897c478bd9Sstevel@tonic-gate  *  return           int    0 - Not a regular file.
3907c478bd9Sstevel@tonic-gate  *                          1 - pathname[] refers to a regular file.
3917c478bd9Sstevel@tonic-gate  */
_pu_path_is_file(const char * pathname)3927c478bd9Sstevel@tonic-gate int _pu_path_is_file(const char *pathname)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate   struct stat statbuf;    /* The file-statistics return buffer */
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate  * Look up the file attributes.
3977c478bd9Sstevel@tonic-gate  */
3987c478bd9Sstevel@tonic-gate   if(stat(pathname, &statbuf) < 0)
3997c478bd9Sstevel@tonic-gate     return 0;
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate  * Is the file a regular file?
4027c478bd9Sstevel@tonic-gate  */
4037c478bd9Sstevel@tonic-gate   return S_ISREG(statbuf.st_mode) != 0;
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate /*.......................................................................
4077c478bd9Sstevel@tonic-gate  * Return non-zero if the specified path name refers to an executable.
4087c478bd9Sstevel@tonic-gate  *
4097c478bd9Sstevel@tonic-gate  * Input:
4107c478bd9Sstevel@tonic-gate  *  pathname  const char *  The path to test.
4117c478bd9Sstevel@tonic-gate  * Output:
4127c478bd9Sstevel@tonic-gate  *  return           int    0 - Not an executable file.
4137c478bd9Sstevel@tonic-gate  *                          1 - pathname[] refers to an executable file.
4147c478bd9Sstevel@tonic-gate  */
_pu_path_is_exe(const char * pathname)4157c478bd9Sstevel@tonic-gate int _pu_path_is_exe(const char *pathname)
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate   struct stat statbuf;    /* The file-statistics return buffer */
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate  * Look up the file attributes.
4207c478bd9Sstevel@tonic-gate  */
4217c478bd9Sstevel@tonic-gate   if(stat(pathname, &statbuf) < 0)
4227c478bd9Sstevel@tonic-gate     return 0;
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate  * Is the file a regular file which is executable by the current user.
4257c478bd9Sstevel@tonic-gate  */
4267c478bd9Sstevel@tonic-gate   return S_ISREG(statbuf.st_mode) != 0 &&
4277c478bd9Sstevel@tonic-gate     (statbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
4287c478bd9Sstevel@tonic-gate     access(pathname, X_OK) == 0;
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate /*.......................................................................
4327c478bd9Sstevel@tonic-gate  * Search backwards for the potential start of a filename. This
4337c478bd9Sstevel@tonic-gate  * looks backwards from the specified index in a given string,
4347c478bd9Sstevel@tonic-gate  * stopping at the first unescaped space or the start of the line.
4357c478bd9Sstevel@tonic-gate  *
4367c478bd9Sstevel@tonic-gate  * Input:
4377c478bd9Sstevel@tonic-gate  *  string  const char *  The string to search backwards in.
4387c478bd9Sstevel@tonic-gate  *  back_from      int    The index of the first character in string[]
4397c478bd9Sstevel@tonic-gate  *                        that follows the pathname.
4407c478bd9Sstevel@tonic-gate  * Output:
4417c478bd9Sstevel@tonic-gate  *  return        char *  The pointer to the first character of
4427c478bd9Sstevel@tonic-gate  *                        the potential pathname, or NULL on error.
4437c478bd9Sstevel@tonic-gate  */
_pu_start_of_path(const char * string,int back_from)4447c478bd9Sstevel@tonic-gate char *_pu_start_of_path(const char *string, int back_from)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate   int i, j;
4477c478bd9Sstevel@tonic-gate /*
4487c478bd9Sstevel@tonic-gate  * Check the arguments.
4497c478bd9Sstevel@tonic-gate  */
4507c478bd9Sstevel@tonic-gate   if(!string || back_from < 0) {
4517c478bd9Sstevel@tonic-gate     errno = EINVAL;
4527c478bd9Sstevel@tonic-gate     return NULL;
4537c478bd9Sstevel@tonic-gate   };
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate  * Search backwards from the specified index.
4567c478bd9Sstevel@tonic-gate  */
4577c478bd9Sstevel@tonic-gate   for(i=back_from-1; i>=0; i--) {
4587c478bd9Sstevel@tonic-gate     int c = string[i];
4597c478bd9Sstevel@tonic-gate /*
4607c478bd9Sstevel@tonic-gate  * Stop on unescaped spaces.
4617c478bd9Sstevel@tonic-gate  */
4627c478bd9Sstevel@tonic-gate     if(isspace((int)(unsigned char)c)) {
4637c478bd9Sstevel@tonic-gate /*
4647c478bd9Sstevel@tonic-gate  * The space can't be escaped if we are at the start of the line.
4657c478bd9Sstevel@tonic-gate  */
4667c478bd9Sstevel@tonic-gate       if(i==0)
4677c478bd9Sstevel@tonic-gate         break;
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate  * Find the extent of the escape characters which precedes the space.
4707c478bd9Sstevel@tonic-gate  */
4717c478bd9Sstevel@tonic-gate       for(j=i-1; j>=0 && string[j]=='\\'; j--)
4727c478bd9Sstevel@tonic-gate 	;
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate  * If there isn't an odd number of escape characters before the space,
4757c478bd9Sstevel@tonic-gate  * then the space isn't escaped.
4767c478bd9Sstevel@tonic-gate  */
4777c478bd9Sstevel@tonic-gate       if((i - 1 - j) % 2 == 0)
4787c478bd9Sstevel@tonic-gate 	break;
4797c478bd9Sstevel@tonic-gate     };
4807c478bd9Sstevel@tonic-gate   };
4817c478bd9Sstevel@tonic-gate   return (char *)string + i + 1;
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate /*.......................................................................
4857c478bd9Sstevel@tonic-gate  * Find the length of a potential filename starting from a given
4867c478bd9Sstevel@tonic-gate  * point. This looks forwards from the specified index in a given string,
4877c478bd9Sstevel@tonic-gate  * stopping at the first unescaped space or the end of the line.
4887c478bd9Sstevel@tonic-gate  *
4897c478bd9Sstevel@tonic-gate  * Input:
4907c478bd9Sstevel@tonic-gate  *  string   const char *  The string to search backwards in.
4917c478bd9Sstevel@tonic-gate  *  start_from      int    The index of the first character of the pathname
4927c478bd9Sstevel@tonic-gate  *                         in string[].
4937c478bd9Sstevel@tonic-gate  * Output:
4947c478bd9Sstevel@tonic-gate  *  return         char *  The pointer to the character that follows
4957c478bd9Sstevel@tonic-gate  *                         the potential pathname, or NULL on error.
4967c478bd9Sstevel@tonic-gate  */
_pu_end_of_path(const char * string,int start_from)4977c478bd9Sstevel@tonic-gate char *_pu_end_of_path(const char *string, int start_from)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate   int c;             /* The character being examined */
5007c478bd9Sstevel@tonic-gate   int escaped = 0;   /* True when the next character is escaped */
5017c478bd9Sstevel@tonic-gate   int i;
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate  * Check the arguments.
5047c478bd9Sstevel@tonic-gate  */
5057c478bd9Sstevel@tonic-gate   if(!string || start_from < 0) {
5067c478bd9Sstevel@tonic-gate     errno = EINVAL;
5077c478bd9Sstevel@tonic-gate     return NULL;
5087c478bd9Sstevel@tonic-gate   };
5097c478bd9Sstevel@tonic-gate /*
5107c478bd9Sstevel@tonic-gate  * Search forwards from the specified index.
5117c478bd9Sstevel@tonic-gate  */
5127c478bd9Sstevel@tonic-gate   for(i=start_from; (c=string[i]) != '\0'; i++) {
5137c478bd9Sstevel@tonic-gate     if(escaped) {
5147c478bd9Sstevel@tonic-gate       escaped = 0;
5157c478bd9Sstevel@tonic-gate     } else if(isspace(c)) {
5167c478bd9Sstevel@tonic-gate       break;
5177c478bd9Sstevel@tonic-gate     } else if(c == '\\') {
5187c478bd9Sstevel@tonic-gate       escaped = 1;
5197c478bd9Sstevel@tonic-gate     };
5207c478bd9Sstevel@tonic-gate   };
5217c478bd9Sstevel@tonic-gate   return (char *)string + i;
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate /*.......................................................................
5257c478bd9Sstevel@tonic-gate  * Return non-zero if the specified path name refers to an existing file.
5267c478bd9Sstevel@tonic-gate  *
5277c478bd9Sstevel@tonic-gate  * Input:
5287c478bd9Sstevel@tonic-gate  *  pathname   const char *  The path to test.
5297c478bd9Sstevel@tonic-gate  * Output:
5307c478bd9Sstevel@tonic-gate  *  return            int    0 - The file doesn't exist.
5317c478bd9Sstevel@tonic-gate  *                           1 - The file does exist.
5327c478bd9Sstevel@tonic-gate  */
_pu_file_exists(const char * pathname)5337c478bd9Sstevel@tonic-gate int _pu_file_exists(const char *pathname)
5347c478bd9Sstevel@tonic-gate {
5357c478bd9Sstevel@tonic-gate   struct stat statbuf;
5367c478bd9Sstevel@tonic-gate   return stat(pathname, &statbuf) == 0;
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate #endif  /* ifndef WITHOUT_FILE_SYSTEM */
540