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