* Update to version 2.19.5
[alpine.git] / pith / osdep / bldpath.c
blob3a227aade959b93f0e6de88c91cb2f5b154f7ac5
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: bldpath.c 934 2008-02-23 00:44:29Z hubert@u.washington.edu $";
3 #endif
5 /*
6 * ========================================================================
7 * Copyright 2006-2008 University of Washington
8 * Copyright 2013-2014 Eduardo Chappa
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 * ========================================================================
19 #include <system.h>
21 #if STDC_HEADERS
22 #include <ctype.h>
23 #endif
25 #include "bldpath.h"
28 * Useful definitions
30 #ifdef _WINDOWS
31 #define ROOTED(S) (*(S) == '\\' || (isalpha((unsigned char) (S)[0]) && (S)[1] == ':'))
32 #define HOMERELATIVE(S) (FALSE)
33 #else /* UNIX */
34 #define ROOTED(S) (*(S) == '/')
35 #define HOMERELATIVE(S) (*(S) == '~')
36 #endif
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
54 still exist.
55 ----*/
56 void
57 build_path(char *pathbuf, char *first_part, char *second_part, size_t len)
59 if(!(pathbuf && len > 0))
60 return;
62 pathbuf[0] = '\0';
64 if(!first_part || is_rooted_path(second_part)){
65 if(second_part)
66 strncpy(pathbuf, second_part, len-1);
68 pathbuf[len-1] = '\0';
70 else{
71 #ifdef _WINDOWS
72 int i;
73 char *orig_pathbuf = pathbuf;
75 for(i = 0; i < len-2 && first_part[i]; i++)
76 *pathbuf++ = first_part[i];
78 if(second_part){
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++;
90 *pathbuf = '\0';
92 #else /* UNIX */
94 size_t fpl = 0;
96 strncpy(pathbuf, first_part, len-2);
97 pathbuf[len-2] = '\0';
98 if(second_part){
99 if(*pathbuf && pathbuf[(fpl=strlen(pathbuf))-1] != '/'){
100 pathbuf[fpl++] = '/';
101 pathbuf[fpl] = '\0';
104 strncat(pathbuf, second_part, len-1-strlen(pathbuf));
107 pathbuf[len-1] = '\0';
109 #endif
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
121 ----*/
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
131 * reference starts
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
142 * reference starts
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
153 * reference starts
155 char *
156 homedir_in_path(char *path)
158 #ifdef _WINDOWS
159 return(NULL);
160 #else /* UNIX */
161 char *p;
163 return((p = strchr(path, '~')) ? p : NULL);
164 #endif
171 char *
172 filename_parent_ref(char *s)
174 #ifdef _WINDOWS
175 return(strstr(s, "\\..\\"));
176 #else /* UNIX */
177 return(strstr(s, "/../"));
178 #endif
183 filename_is_restricted(char *s)
185 #ifdef _WINDOWS
186 return(filename_parent_ref(s) != NULL);
187 #else /* UNIX */
188 return(strchr("./~", s[0]) != NULL || filename_parent_ref(s) != NULL);
189 #endif