* Update configure script according to previous change to configure.ac
[alpine.git] / pith / osdep / bldpath.c
blob5a976472b79c6fd2c292741d2a861f3852b4d67b
1 /*
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 * ========================================================================
15 #include <system.h>
17 #if STDC_HEADERS
18 #include <ctype.h>
19 #endif
21 #include "bldpath.h"
24 * Useful definitions
26 #ifdef _WINDOWS
27 #define ROOTED(S) (*(S) == '\\' || (isalpha((unsigned char) (S)[0]) && (S)[1] == ':'))
28 #define HOMERELATIVE(S) (FALSE)
29 #else /* UNIX */
30 #define ROOTED(S) (*(S) == '/')
31 #define HOMERELATIVE(S) (*(S) == '~')
32 #endif
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
50 still exist.
51 ----*/
52 void
53 build_path(char *pathbuf, char *first_part, char *second_part, size_t len)
55 if(!(pathbuf && len > 0))
56 return;
58 pathbuf[0] = '\0';
60 if(!first_part || is_rooted_path(second_part)){
61 if(second_part)
62 strncpy(pathbuf, second_part, len-1);
64 pathbuf[len-1] = '\0';
66 else{
67 #ifdef _WINDOWS
68 int i;
69 char *orig_pathbuf = pathbuf;
71 for(i = 0; i < len-2 && first_part[i]; i++)
72 *pathbuf++ = first_part[i];
74 if(second_part){
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++;
86 *pathbuf = '\0';
88 #else /* UNIX */
90 size_t fpl = 0;
92 strncpy(pathbuf, first_part, len-2);
93 pathbuf[len-2] = '\0';
94 if(second_part){
95 if(*pathbuf && pathbuf[(fpl=strlen(pathbuf))-1] != '/'){
96 pathbuf[fpl++] = '/';
97 pathbuf[fpl] = '\0';
100 strncat(pathbuf, second_part, len-1-strlen(pathbuf));
103 pathbuf[len-1] = '\0';
105 #endif
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
117 ----*/
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
127 * reference starts
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
138 * reference starts
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
149 * reference starts
151 char *
152 homedir_in_path(char *path)
154 #ifdef _WINDOWS
155 return(NULL);
156 #else /* UNIX */
157 char *p;
159 return((p = strchr(path, '~')) ? p : NULL);
160 #endif
167 char *
168 filename_parent_ref(char *s)
170 #ifdef _WINDOWS
171 return(strstr(s, "\\..\\"));
172 #else /* UNIX */
173 return(strstr(s, "/../"));
174 #endif
179 filename_is_restricted(char *s)
181 #ifdef _WINDOWS
182 return(filename_parent_ref(s) != NULL);
183 #else /* UNIX */
184 return(strchr("./~", s[0]) != NULL || filename_parent_ref(s) != NULL);
185 #endif