|
xorp
|
00001 #ifndef pathutil_h 00002 #define pathutil_h 00003 00004 /* 00005 * Copyright (c) 2000, 2001 by Martin C. Shepherd. 00006 * 00007 * All rights reserved. 00008 * 00009 * Permission is hereby granted, free of charge, to any person obtaining a 00010 * copy of this software and associated documentation files (the 00011 * "Software"), to deal in the Software without restriction, including 00012 * without limitation the rights to use, copy, modify, merge, publish, 00013 * distribute, and/or sell copies of the Software, and to permit persons 00014 * to whom the Software is furnished to do so, provided that the above 00015 * copyright notice(s) and this permission notice appear in all copies of 00016 * the Software and that both the above copyright notice(s) and this 00017 * permission notice appear in supporting documentation. 00018 * 00019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00020 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00021 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 00022 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00023 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 00024 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 00025 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00026 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00027 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00028 * 00029 * Except as contained in this notice, the name of a copyright holder 00030 * shall not be used in advertising or otherwise to promote the sale, use 00031 * or other dealings in this Software without prior written authorization 00032 * of the copyright holder. 00033 */ 00034 00035 /* 00036 * The following object encapsulates a buffer designed to be used to 00037 * store pathnames. The pathname member of the object is initially 00038 * allocated with the size that _pu_pathname_dim() returns, and then 00039 * if this turns out to be pessimistic, the pathname can be reallocated 00040 * via calls to pb_append_to_path() and/or pb_resize_path(). 00041 */ 00042 typedef struct { 00043 char *name; /* The path buffer */ 00044 size_t dim; /* The current allocated size of buffer[] */ 00045 } PathName; 00046 00047 PathName *_new_PathName(void); 00048 PathName *_del_PathName(PathName *path); 00049 00050 char *_pn_clear_path(PathName *path); 00051 char *_pn_append_to_path(PathName *path, const char *string, int slen, 00052 int remove_escapes); 00053 char *_pn_prepend_to_path(PathName *path, const char *string, int slen, 00054 int remove_escapes); 00055 char *_pn_resize_path(PathName *path, size_t length); 00056 00057 /* 00058 * Search backwards for the potential start of a filename. This 00059 * looks backwards from the specified index in a given string, 00060 * stopping at the first unescaped space or the start of the line. 00061 */ 00062 char *_pu_start_of_path(const char *string, int back_from); 00063 00064 /* 00065 * Find the end of a potential filename, starting from a given index 00066 * in the string. This looks forwards from the specified index in a 00067 * given string, stopping at the first unescaped space or the end 00068 * of the line. 00069 */ 00070 char *_pu_end_of_path(const char *string, int start_from); 00071 00072 00073 /* 00074 * Return an estimate of the the length of the longest pathname 00075 * on the local system. 00076 */ 00077 size_t _pu_pathname_dim(void); 00078 00079 /* 00080 * Return non-zero if the specified path name refers to a directory. 00081 */ 00082 int _pu_path_is_dir(const char *pathname); 00083 00084 /* 00085 * Return non-zero if the specified path name refers to a regular file. 00086 */ 00087 int _pu_path_is_file(const char *pathname); 00088 00089 /* 00090 * Return non-zero if the specified path name refers to an executable. 00091 */ 00092 int _pu_path_is_exe(const char *pathname); 00093 00094 /* 00095 * Return non-zero if a file exists with the specified pathname. 00096 */ 00097 int _pu_file_exists(const char *pathname); 00098 00099 /* 00100 * If neither the POSIX PATH_MAX macro nor the pathconf() function 00101 * can be used to find out the maximum pathlength on the target 00102 * system, the following fallback maximum length is used. 00103 */ 00104 #define MAX_PATHLEN_FALLBACK 1024 00105 00106 /* 00107 * If the pathname buffer turns out to be too small, it will be extended 00108 * in chunks of the following amount (plus whatever is needed at the time). 00109 */ 00110 #define PN_PATHNAME_INC 100 00111 00112 /* 00113 * Define the special character-sequences of the filesystem. 00114 */ 00115 #define FS_ROOT_DIR "/" /* The root directory */ 00116 #define FS_ROOT_DIR_LEN (sizeof(FS_ROOT_DIR) - 1) 00117 #define FS_PWD "." /* The current working directory */ 00118 #define FS_PWD_LEN (sizeof(FS_PWD_LEN) - 1) 00119 #define FS_DIR_SEP "/" /* The directory separator string */ 00120 #define FS_DIR_SEP_LEN (sizeof(FS_DIR_SEP) - 1) 00121 00122 #endif