File: parse.c

package info (click to toggle)
mtools 3.8-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,116 kB
  • ctags: 1,306
  • sloc: ansic: 11,489; sh: 2,052; makefile: 223; sed: 8
file content (189 lines) | stat: -rw-r--r-- 3,490 bytes parent folder | download
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
#include "sysincludes.h"
#include "msdos.h"
#include "vfat.h"

/*
 * Get name component of filename.
 *
 * Formerly translated name to upper case; now preserves case.
 * This is because long DOS names are case sensitive, but also because we
 * want to preserve the user-specified case for the copied Unix files
 */

char *get_name(const char *filename, char *ans, char *mcwd)
{
	char *s;
	const char *temp;

	temp = filename;
					/* skip drive letter */
	if (temp[0] && temp[1] == ':')
		temp = &temp[2];
					/* find the last separator */
	if ((s = strrchr(temp, '/')))
		temp = s + 1;
	if ((s = strrchr(temp, '\\')))
		temp = s + 1;

	strncpy(ans, temp, VBUFSIZE-1);
	ans[VBUFSIZE-1]='\0';

	return ans;
}

static inline void strip_last_dir(char *begin, char **targetp)
{
	char *tmp;

	/* IN condition: begin stops with a slash */

	/* remove slash */
	(*targetp) --;
	**targetp = '\0';

	tmp = strrchr(begin,'/');
	if(tmp) {
		tmp++;
	} else {
		begin[0]='/';
		tmp=begin+1;
	}
	*tmp = '\0';
	*targetp = tmp;
}

static inline int handle_dot(char *begin, char **sourcep, char **targetp)
{
	char *source = *sourcep;

	switch(source[1]) {
		case '/':
			(*sourcep) += 2;
			return 1;
		case '.':
			switch(source[2]) {
				case '\0':
					strip_last_dir(begin, targetp);
					(*sourcep) += 2;
					return 1;
				case '/':
					strip_last_dir(begin, targetp);
					(*sourcep) += 3;
					return 1;
				default:
					return 0;
			}
		case '\0':
			(*sourcep) ++;
			return 1;
		default:
			return 0;
	}
}


static inline void canonize_path(char *begin)
{
	char tmp, *source, *target;

	/* transform all backslashes into slashes */
	for(source = begin; *source; source++)
		if(*source == '\\')
			*source ='/';

	/* prune off double slashes */
	source = begin;
	target = begin;
	while(*source) {
		tmp = *target++ = *source++;
		if(tmp == '/') {
			while(1) {				
				switch(source[0]) {
					case '/':
						source++;
						continue;
					case '.':
						if(handle_dot(begin, &source,
							      &target))
							continue;
						break;
				}
				break;
			}
			if(!*source)
				target--;
		}
	}

	*target='\0';
	if(!*begin) {
		begin[0] = '/';
		begin[1] = '\0';
	}
}

/*
 * Get the path component of the filename.
 * Doesn't alter leading separator, always strips trailing separator (unless
 * it is the path itself).
 *
 * Formerly translated name to upper case; now preserves case.
 * This is because long DOS names are case sensitive, but also because we
 * want to preserve the user-specified case for the copied Unix files
 */

char *get_path(const char *filename, char *ans, char *mcwd, int mode)
{
	char *s;
	const char *end;
	const char *begin;
	char drive;

	begin = filename;

	/* skip drive letter */
	drive = '\0';
	if (begin[0] && begin[1] == ':') {
		drive = toupper(begin[0]);
		begin += 2;
	}

	ans[0] = '/';
	ans[1] = '\0';
	if ((*begin != '/') && (!drive || drive == *mcwd))
		strcpy(ans+1, mcwd + 2);

	strcat(ans, "/");

	/* cut last separator (if needed) */
	if(mode) 
		end = begin + strlen(begin);
	else {
		if ((s = strrchr(begin, '/')))
			end = s;
		else
			end = begin;
	}

	if(strlen(ans)+end-begin+1 > MAX_PATH){
		fprintf(stderr,"Path too long\n");
		exit(1);
	}

	strncat(ans, begin, end - begin);
	ans[strlen(ans)+end-begin] = '\0';
	canonize_path(ans);
	return ans;
}

/*
 * get the drive letter designation
 */

char get_drive(const char *filename, char def)
{
	if (*filename && *(filename + 1) == ':')
		return(toupper(*filename));
	else
		return def;
}