summaryrefslogtreecommitdiff
path: root/pith/osdep/bldpath.c
blob: cb6a49bd6c04d6e643acde6071ea59b8fee7b150 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: bldpath.c 934 2008-02-23 00:44:29Z hubert@u.washington.edu $";
#endif

/*
 * ========================================================================
 * Copyright 2013-2018 Eduardo Chappa
 * Copyright 2006-2008 University of Washington
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 */

#include <system.h>

#if	STDC_HEADERS
#include <ctype.h>
#endif

#include "bldpath.h"

/*
 * Useful definitions
 */
#ifdef	_WINDOWS
#define	ROOTED(S)	(*(S) == '\\' || (isalpha((unsigned char) (S)[0]) && (S)[1] == ':'))
#define	HOMERELATIVE(S)	(FALSE)
#else	/* UNIX */
#define	ROOTED(S)	(*(S) == '/')
#define	HOMERELATIVE(S)	(*(S) == '~')
#endif




/*----------------------------------------------------------------------
      Paste together two pieces of a file name path

  Args: pathbuf      -- Put the result here
        first_part   -- of path name
        second_part  -- of path name
	len          -- Length of pathbuf
 
 Result: New path is in pathbuf.  Note that
	 we don't have to check for /'s at end of first_part and beginning
	 of second_part since multiple slashes are ok.

BUGS:  This is a first stab at dealing with fs naming dependencies, and others 
still exist.
  ----*/
void
build_path(char *pathbuf, char *first_part, char *second_part, size_t len)
{
    if(!(pathbuf && len > 0))
      return;

    pathbuf[0] = '\0';

    if(!first_part || is_rooted_path(second_part)){
	if(second_part)
	  strncpy(pathbuf, second_part, len-1);

	pathbuf[len-1] = '\0';
    }
    else{
#ifdef	_WINDOWS
	int i;
	char *orig_pathbuf = pathbuf;

	for(i = 0; i < len-2 && first_part[i]; i++)
	  *pathbuf++ = first_part[i];

	if(second_part){
	    if(i && first_part[i-1] == '\\'){	/* first part ended with \  */
		if(*second_part == '\\')		/* and second starts with \ */
		  second_part++;			/* else just append second  */
	    }
	    else if(*second_part != '\\')		/* no slash at all, so      */
	      *pathbuf++ = '\\';			/* insert one...	    */

	    while(pathbuf-orig_pathbuf < len-1 && *second_part)
	      *pathbuf++ = *second_part++;
	}

	*pathbuf = '\0';

#else  /* UNIX */

	size_t fpl = 0;

	strncpy(pathbuf, first_part, len-2);
	pathbuf[len-2] = '\0';
	if(second_part){
	    if(*pathbuf && pathbuf[(fpl=strlen(pathbuf))-1] != '/'){
		pathbuf[fpl++] = '/';
		pathbuf[fpl] = '\0';
	    }

	    strncat(pathbuf, second_part, len-1-strlen(pathbuf));
	}

	pathbuf[len-1] = '\0';

#endif
    }
}


/*----------------------------------------------------------------------
  Test to see if the given file path is absolute

  Args: file -- file path to test

 Result: TRUE if absolute, FALSE otw

  ----*/
int
is_absolute_path(char *path)
{
    return(path && (ROOTED(path) || HOMERELATIVE(path)));
}


/*
 * homedir_in_path - return pointer to point in path string where home dir
 *                   reference starts
 */
int
is_rooted_path(char *path)
{
    return(path && ROOTED(path));
}


/*
 * homedir_in_path - return pointer to point in path string where home dir
 *                   reference starts
 */
int
is_homedir_path(char *path)
{
    return(path && HOMERELATIVE(path));
}


/*
 * homedir_in_path - return pointer to point in path string where home dir
 *                   reference starts
 */
char *
homedir_in_path(char *path)
{
#ifdef	_WINDOWS
    return(NULL);
#else	/* UNIX */
    char *p; 

    return((p = strchr(path, '~')) ? p : NULL);
#endif
}


/*
 *
 */
char *
filename_parent_ref(char *s)
{
#ifdef	_WINDOWS
    return(strstr(s, "\\..\\"));
#else	/* UNIX */
    return(strstr(s, "/../"));
#endif
}


int
filename_is_restricted(char *s)
{
#ifdef	_WINDOWS
    return(filename_parent_ref(s) != NULL);
#else	/* UNIX */
    return(strchr("./~", s[0]) != NULL || filename_parent_ref(s) != NULL);
#endif
}