1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: bldpath.c 934 2008-02-23 00:44:29Z hubert@u.washington.edu $";
6 * ========================================================================
7 * Copyright 2013-2018 Eduardo Chappa
8 * Copyright 2006-2008 University of Washington
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
31 #define ROOTED(S) (*(S) == '\\' || (isalpha((unsigned char) (S)[0]) && (S)[1] == ':'))
32 #define HOMERELATIVE(S) (FALSE)
34 #define ROOTED(S) (*(S) == '/')
35 #define HOMERELATIVE(S) (*(S) == '~')
41 /*----------------------------------------------------------------------
42 Paste together two pieces of a file name path
44 Args: pathbuf -- Put the result here
45 first_part -- of path name
46 second_part -- of path name
47 len -- Length of pathbuf
49 Result: New path is in pathbuf. Note that
50 we don't have to check for /'s at end of first_part and beginning
51 of second_part since multiple slashes are ok.
53 BUGS: This is a first stab at dealing with fs naming dependencies, and others
57 build_path(char *pathbuf
, char *first_part
, char *second_part
, size_t len
)
59 if(!(pathbuf
&& len
> 0))
64 if(!first_part
|| is_rooted_path(second_part
)){
66 strncpy(pathbuf
, second_part
, len
-1);
68 pathbuf
[len
-1] = '\0';
73 char *orig_pathbuf
= pathbuf
;
75 for(i
= 0; i
< len
-2 && first_part
[i
]; i
++)
76 *pathbuf
++ = first_part
[i
];
79 if(i
&& first_part
[i
-1] == '\\'){ /* first part ended with \ */
80 if(*second_part
== '\\') /* and second starts with \ */
81 second_part
++; /* else just append second */
83 else if(*second_part
!= '\\') /* no slash at all, so */
84 *pathbuf
++ = '\\'; /* insert one... */
86 while(pathbuf
-orig_pathbuf
< len
-1 && *second_part
)
87 *pathbuf
++ = *second_part
++;
96 strncpy(pathbuf
, first_part
, len
-2);
97 pathbuf
[len
-2] = '\0';
99 if(*pathbuf
&& pathbuf
[(fpl
=strlen(pathbuf
))-1] != '/'){
100 pathbuf
[fpl
++] = '/';
104 strncat(pathbuf
, second_part
, len
-1-strlen(pathbuf
));
107 pathbuf
[len
-1] = '\0';
114 /*----------------------------------------------------------------------
115 Test to see if the given file path is absolute
117 Args: file -- file path to test
119 Result: TRUE if absolute, FALSE otw
123 is_absolute_path(char *path
)
125 return(path
&& (ROOTED(path
) || HOMERELATIVE(path
)));
130 * homedir_in_path - return pointer to point in path string where home dir
134 is_rooted_path(char *path
)
136 return(path
&& ROOTED(path
));
141 * homedir_in_path - return pointer to point in path string where home dir
145 is_homedir_path(char *path
)
147 return(path
&& HOMERELATIVE(path
));
152 * homedir_in_path - return pointer to point in path string where home dir
156 homedir_in_path(char *path
)
163 return((p
= strchr(path
, '~')) ? p
: NULL
);
172 filename_parent_ref(char *s
)
175 return(strstr(s
, "\\..\\"));
177 return(strstr(s
, "/../"));
183 filename_is_restricted(char *s
)
186 return(filename_parent_ref(s
) != NULL
);
188 return(strchr("./~", s
[0]) != NULL
|| filename_parent_ref(s
) != NULL
);