2 * ========================================================================
3 * Copyright 2013-2022 Eduardo Chappa
4 * Copyright 2006-2008 University of Washington
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
27 #define ROOTED(S) (*(S) == '\\' || (isalpha((unsigned char) (S)[0]) && (S)[1] == ':'))
28 #define HOMERELATIVE(S) (FALSE)
30 #define ROOTED(S) (*(S) == '/')
31 #define HOMERELATIVE(S) (*(S) == '~')
37 /*----------------------------------------------------------------------
38 Paste together two pieces of a file name path
40 Args: pathbuf -- Put the result here
41 first_part -- of path name
42 second_part -- of path name
43 len -- Length of pathbuf
45 Result: New path is in pathbuf. Note that
46 we don't have to check for /'s at end of first_part and beginning
47 of second_part since multiple slashes are ok.
49 BUGS: This is a first stab at dealing with fs naming dependencies, and others
53 build_path(char *pathbuf
, char *first_part
, char *second_part
, size_t len
)
55 if(!(pathbuf
&& len
> 0))
60 if(!first_part
|| is_rooted_path(second_part
)){
62 strncpy(pathbuf
, second_part
, len
-1);
64 pathbuf
[len
-1] = '\0';
69 char *orig_pathbuf
= pathbuf
;
71 for(i
= 0; i
< len
-2 && first_part
[i
]; i
++)
72 *pathbuf
++ = first_part
[i
];
75 if(i
&& first_part
[i
-1] == '\\'){ /* first part ended with \ */
76 if(*second_part
== '\\') /* and second starts with \ */
77 second_part
++; /* else just append second */
79 else if(*second_part
!= '\\') /* no slash at all, so */
80 *pathbuf
++ = '\\'; /* insert one... */
82 while(pathbuf
-orig_pathbuf
< len
-1 && *second_part
)
83 *pathbuf
++ = *second_part
++;
92 strncpy(pathbuf
, first_part
, len
-2);
93 pathbuf
[len
-2] = '\0';
95 if(*pathbuf
&& pathbuf
[(fpl
=strlen(pathbuf
))-1] != '/'){
100 strncat(pathbuf
, second_part
, len
-1-strlen(pathbuf
));
103 pathbuf
[len
-1] = '\0';
110 /*----------------------------------------------------------------------
111 Test to see if the given file path is absolute
113 Args: file -- file path to test
115 Result: TRUE if absolute, FALSE otw
119 is_absolute_path(char *path
)
121 return(path
&& (ROOTED(path
) || HOMERELATIVE(path
)));
126 * homedir_in_path - return pointer to point in path string where home dir
130 is_rooted_path(char *path
)
132 return(path
&& ROOTED(path
));
137 * homedir_in_path - return pointer to point in path string where home dir
141 is_homedir_path(char *path
)
143 return(path
&& HOMERELATIVE(path
));
148 * homedir_in_path - return pointer to point in path string where home dir
152 homedir_in_path(char *path
)
159 return((p
= strchr(path
, '~')) ? p
: NULL
);
168 filename_parent_ref(char *s
)
171 return(strstr(s
, "\\..\\"));
173 return(strstr(s
, "/../"));
179 filename_is_restricted(char *s
)
182 return(filename_parent_ref(s
) != NULL
);
184 return(strchr("./~", s
[0]) != NULL
|| filename_parent_ref(s
) != NULL
);