xref: /illumos-gate/usr/src/lib/libtecla/common/pathutil.h (revision afab0816ecb604f0099a09ad8ee398f0d7b77b1c)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * The following object encapsulates a buffer designed to be used to
39  * store pathnames. The pathname member of the object is initially
40  * allocated with the size that _pu_pathname_dim() returns, and then
41  * if this turns out to be pessimistic, the pathname can be reallocated
42  * via calls to pb_append_to_path() and/or pb_resize_path().
43  */
44 typedef struct {
45   char *name;         /* The path buffer */
46   size_t dim;         /* The current allocated size of buffer[] */
47 } PathName;
48 
49 PathName *_new_PathName(void);
50 PathName *_del_PathName(PathName *path);
51 
52 char *_pn_clear_path(PathName *path);
53 char *_pn_append_to_path(PathName *path, const char *string, int slen,
54 			int remove_escapes);
55 char *_pn_prepend_to_path(PathName *path, const char *string, int slen,
56 			  int remove_escapes);
57 char *_pn_resize_path(PathName *path, size_t length);
58 
59 /*
60  * Search backwards for the potential start of a filename. This
61  * looks backwards from the specified index in a given string,
62  * stopping at the first unescaped space or the start of the line.
63  */
64 char *_pu_start_of_path(const char *string, int back_from);
65 
66 /*
67  * Find the end of a potential filename, starting from a given index
68  * in the string. This looks forwards from the specified index in a
69  * given string, stopping at the first unescaped space or the end
70  * of the line.
71  */
72 char *_pu_end_of_path(const char *string, int start_from);
73 
74 
75 /*
76  * Return an estimate of the the length of the longest pathname
77  * on the local system.
78  */
79 size_t _pu_pathname_dim(void);
80 
81 /*
82  * Return non-zero if the specified path name refers to a directory.
83  */
84 int _pu_path_is_dir(const char *pathname);
85 
86 /*
87  * Return non-zero if the specified path name refers to a regular file.
88  */
89 int _pu_path_is_file(const char *pathname);
90 
91 /*
92  * Return non-zero if the specified path name refers to an executable.
93  */
94 int _pu_path_is_exe(const char *pathname);
95 
96 /*
97  * Return non-zero if a file exists with the specified pathname.
98  */
99 int _pu_file_exists(const char *pathname);
100 
101 /*
102  * If neither the POSIX PATH_MAX macro nor the pathconf() function
103  * can be used to find out the maximum pathlength on the target
104  * system, the following fallback maximum length is used.
105  */
106 #define MAX_PATHLEN_FALLBACK 1024
107 
108 /*
109  * If the pathname buffer turns out to be too small, it will be extended
110  * in chunks of the following amount (plus whatever is needed at the time).
111  */
112 #define PN_PATHNAME_INC 100
113 
114 /*
115  * Define the special character-sequences of the filesystem.
116  */
117 #define FS_ROOT_DIR "/"     /* The root directory */
118 #define FS_ROOT_DIR_LEN (sizeof(FS_ROOT_DIR) - 1)
119 #define FS_PWD "."          /* The current working directory */
120 #define FS_PWD_LEN (sizeof(FS_PWD_LEN) - 1)
121 #define FS_DIR_SEP "/"      /* The directory separator string */
122 #define FS_DIR_SEP_LEN (sizeof(FS_DIR_SEP) - 1)
123 
124 #endif
125