File: file.cc

package info (click to toggle)
ht 2.0.20-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,324 kB
  • sloc: cpp: 97,563; ansic: 17,183; sh: 3,811; lex: 226; makefile: 213; yacc: 127
file content (419 lines) | stat: -rwxr-xr-x 9,280 bytes parent folder | download | duplicates (2)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* 
 *	HT Editor
 *	file.cc
 *
 *	Copyright (C) 1999-2004 Stefan Weyergraf (stefan@weyergraf.de)
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License version 2 as
 *	published by the Free Software Foundation.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "file.h"

#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/stat.h>

#include "strtools.h"

/*
 *	COMMON SYS
 */
#ifndef S_IFMT
#define S_IFMT 0xf000
#endif

#ifndef S_ISREG
#	ifndef S_IFREG
#		define S_ISREG(m) (0)
#	else
#		define S_ISREG(m) (((m) & S_IFMT)==S_IFREG)
#	endif
#endif

#ifndef S_ISBLK
#	ifndef S_IFBLK
#		define S_ISBLK(m) (0)
#	else
#		define S_ISBLK(m) (((m) & S_IFMT)==S_IFBLK)
#	endif
#endif


#ifndef S_ISCHR
#	ifndef S_IFCHR
#		define S_ISCHR(m) (0)
#	else
#		define S_ISCHR(m) (((m) & S_IFMT)==S_IFCHR)
#	endif
#endif

#ifndef S_ISDIR
#	ifndef S_IFDIR
#		define S_ISDIR(m) (0)
#	else
#		define S_ISDIR(m) (((m) & S_IFMT)==S_IFDIR)
#	endif
#endif

#ifndef S_ISFIFO
#	ifndef S_IFFIFO
#		define S_ISFIFO(m) (0)
#	else
#		define S_ISFIFO(m) (((m) & S_IFMT)==S_IFFIFO)
#	endif
#endif

#ifndef S_ISLNK
#	ifndef S_IFLNK
#		define S_ISLNK(m) (0)
#	else
#		define S_ISLNK(m) (((m) & S_IFMT)==S_IFLNK)
#	endif
#endif

#ifndef S_ISSOCK
#	ifndef S_IFSOCK
#		define S_ISSOCK(m) (0)
#	else
#		define S_ISSOCK(m) (((m) & S_IFMT)==S_IFSOCK)
#	endif
#endif

#ifndef S_IRUSR
#define S_IRUSR 0
#endif
#ifndef S_IRGRP
#define S_IRGRP 0
#endif
#ifndef S_IROTH
#define S_IROTH 0
#endif

#ifndef S_IWUSR
#define S_IWUSR 0
#endif
#ifndef S_IWGRP
#define S_IWGRP 0
#endif
#ifndef S_IWOTH
#define S_IWOTH 0
#endif

#ifndef S_IXUSR
#define S_IXUSR 0
#endif
#ifndef S_IXGRP
#define S_IXGRP 0
#endif
#ifndef S_IXOTH
#define S_IXOTH 0
#endif


#if defined(WIN32) || defined(__WIN32__)
# include <windows.h>
# include <winreg.h>
#endif
//-----------------------------------------------------------------------------
static char *_str_strip(char *str, int len)
{
	char *pr =  ht_malloc(len + 1);
	pr[len] = 0;
	memcpy(pr, str, len);
	return pr;
}
//-----------------------------------------------------------------------------
/*
 1. char *s            - old string
 2. unsigned int s_len - old string len
 3. char *r            - new string
 4. unsigned int r_len - new string len
 5. unsigned int at    - offset in old string
 6. unsigned int len   - len of substring for delete ( at - start)
*/
static char *_substr_replace(char *s, unsigned int s_len, char *r, unsigned int r_len,\
                                            unsigned int at, unsigned int len)
{
	if (!s_len || !r_len) return NULL;
	if (at > s_len) at = 0;
	unsigned int nlen = s_len + r_len - len;
	char *out = ht_malloc(nlen+1);
	out[nlen] = 0; 
	memcpy(out, s, at);
	memcpy(out+at, r, r_len);
	while (s_len-- > (at + len)) {
		out[--nlen] = s[s_len];
	}
	return out;
}
//-----------------------------------------------------------------------------
static char *_env_replace(char *str, unsigned int len)
{
	bool e = false;
	unsigned int cc = 0; // count
	unsigned int sp = 0; // start pos
	char *p = NULL;
	char *prt = NULL;
	char *t;
	char *t2;
	while (cc != len) {
		t2 = prt?prt:str;
#if defined(WIN32) || defined(__WIN32__) || defined(DJGPP) || defined(MSDOC)
		if (t2[cc] == '%' || t2[cc] == ' ') {
#else
		if (t2[cc] == '$' || t2[cc] == ' ') {
#endif
			if (e) {
				free(p);
				p = _str_strip(t2 + sp, cc - sp);
				if ((t = getenv(p)) != NULL) {
					prt = _substr_replace(t2, prt?strlen(prt):strlen(str), t, strlen(t),
                                                          sp-1, (cc - sp) + 2);
					if (t2 != str) free(t2);
					len = strlen(prt);
					cc = sp;
				}
				e = false;
			} else {
				if (t2[cc] != ' ') {
					sp = cc + 1;
					e = true;
				}
			}
		}
		cc++;
	}
	if (prt == NULL) {
		prt = ht_malloc(len+1);
		prt[len] = 0;	
		memcpy(prt, str, len);
	}
	return prt;
}
//-----------------------------------------------------------------------------
int sys_basename(char *result, const char *filename)
{
	// FIXME: use is_path_delim
	const char *slash1 = strrchr(filename, '/');
	const char *slash2 = strrchr(filename, '\\');
	const char *slash = (slash1 > slash2) ? slash1 : slash2;
	if (slash) {
		int l = strlen(filename);
		ht_strlcpy(result, slash+1, l-(slash-filename)+1);
		return 0;
	}
	strcpy(result, filename);
	return 0;
}

char *sys_dirname(char *path)
{
	char *r;           // result
	char *ptr = path;
	int c = 0;
	int last = 0;
	do {
		c++;
		if (*ptr == '/' || *ptr == '\\') {
			last = c;
		} else if (*ptr == 0) {
			r = ht_malloc(last + 1);
			strncpy(r, path, last);
			r[last] = 0;
			return r;
		}
	} while (*ptr++);
	return NULL;
}

/* filename and pathname must be canonicalized */
int sys_relname(char *result, const char *filename, const char *cwd)
{
	const char *f = filename, *p = cwd;
	while ((*f == *p) && (*f)) {
		f++;
		p++;
	}
	if (*f == '/') f++;
	const char *last = f, *h = f;
	while (*h) {
		if (*h == '/') {
			*(result++) = '.';
			*(result++) = '.';
			*(result++) = '/';
			last = h+1;
		}
		h++;
	}
	while (f<last) {
		*(result++) = *f;
		f++;
	}
	*result = 0;
	strcat(result, last);
	return 0;
}

char *sys_get_home_dir(void)
{
#if defined(WIN32) || defined(__WIN32__)
	HKEY hKey;
	char path[HT_NAME_MAX];
	/*
	 LONG len = HT_NAME_MAX;
	 if(RegOpenKey(HKEY_CURRENT_USER,
	 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
	                                                      &hKey) == ERROR_SUCCESS){
	  if(RegQueryValue(hKey, "Personal", path, &len) == ERROR_SUCCESS){
	   RegCloseKey(hKey);
	   if(len > HT_NAME_MAX) return NULL;
	   else return _env_replace(path, len);
	  }
	 }
	*/
	// not work on Windows 3.1 ?
	DWORD len = HT_NAME_MAX;
	if (RegOpenKeyEx(HKEY_CURRENT_USER,
		"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
                0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
		
		if (RegQueryValueEx(hKey, "Personal", NULL, NULL, (LPBYTE)path, &len) ==
                                                                ERROR_SUCCESS) {
			RegCloseKey(hKey);
			if (len > HT_NAME_MAX) {
				return NULL;
			} else {
				return _env_replace(path, len);
			}
		}
	}
#elif defined(MSDOS) 
	// -?
#elif defined(DJGPP)
	// unknown...
#else 
	char *path = getenv("HOME");
	if (path) return _env_replace(path, strlen(path));
#endif
	return NULL;
}

int sys_file_mode(int mode)
{
	int m = 0;
	if (S_ISREG(mode)) {
		m |= HT_S_IFREG;
	} else if (S_ISBLK(mode)) {
		m |= HT_S_IFBLK;
	} else if (S_ISCHR(mode)) {
		m |= HT_S_IFCHR;
	} else if (S_ISDIR(mode)) {
		m |= HT_S_IFDIR;
	} else if (S_ISFIFO(mode)) {
		m |= HT_S_IFFIFO;
	} else if (S_ISLNK(mode)) {
		m |= HT_S_IFLNK;
	} else if (S_ISSOCK(mode)) {
		m |= HT_S_IFSOCK;
	}
	if (mode & S_IRUSR) m |= HT_S_IRUSR;
	if (mode & S_IRGRP) m |= HT_S_IRGRP;
	if (mode & S_IROTH) m |= HT_S_IROTH;
	
	if (mode & S_IWUSR) m |= HT_S_IWUSR;
	if (mode & S_IWGRP) m |= HT_S_IWGRP;
	if (mode & S_IWOTH) m |= HT_S_IWOTH;
	
	if (mode & S_IXUSR) m |= HT_S_IXUSR;
	if (mode & S_IXGRP) m |= HT_S_IXGRP;
	if (mode & S_IXOTH) m |= HT_S_IXOTH;
	return m;
}

static char *next_delim(char *s, is_path_delim delim)
{
	while (*s) {
		s++;
		if (delim(*s)) return s;
	}
	return NULL;
}

static int flatten_path(char *path, is_path_delim delim)
{
	if (!path || !*path)
		return 0;
	char *q = next_delim(path, delim);
	int pp = flatten_path(q, delim);
	int ll = q ? (q-path-1) : strlen(path)-1;
	if (ll == 2 && ht_strncmp(path+1, "..", 2) == 0) {
		if (q) memmove(path, q, strlen(q)+1); else *path = 0;
		pp++;
	} else if (ll == 1 && ht_strncmp(path+1, ".", 1) == 0) {
		if (q) memmove(path, q, strlen(q)+1); else *path = 0;
	} else if (pp) {
		if (q) memmove(path, q, strlen(q)+1); else *path = 0;
		pp--;
	}
	return pp;
}

bool sys_path_is_absolute(const char *filename, is_path_delim delim)
{
	return delim(filename[0]) || (isalpha(filename[0]) && (filename[1] == ':'));
}

int sys_common_canonicalize(char *result, const char *filename, const char *cwd, is_path_delim delim)
{
	char *o = result;
	if (!sys_path_is_absolute(filename, delim)) {
		if (cwd) strcpy(o, cwd); else return EINVAL;
		int ol = strlen(o);
		if (ol && !delim(o[ol-1])) {
			o[ol] = '/';
			o[ol+1] = 0;
		}
	} else *o = 0;
	strcat(o, filename);
	int k = flatten_path(o, delim);
	return (k == 0) ? 0 : EINVAL;
}

const char *sys_filename_suffix(const char *fn)
{
	const char *s = NULL;
	while (fn && *fn) {
		if (sys_is_path_delim(*fn)) s = fn+1;
		fn++;
	}
	const char *p = s ? strrchr(s, '.') : NULL;
	return p ? p+1 : NULL;
}

int sys_tmpfile_fd()
{
#if 1
	// FIXME: this might leak something...
	FILE *f = tmpfile();
	return fileno(f);
#else
	// is this better ?
	return open(tmpnam(NULL), O_RDWR | O_EXCL | O_CREAT);
#endif
}