/[pearpc]/src/system/file.cc
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /src/system/file.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Wed Sep 5 17:11:21 2007 UTC (16 years, 7 months ago) by dpavlin
File size: 5827 byte(s)
import upstream CVS
1 /*
2 * HT Editor
3 * file.cc
4 *
5 * Copyright (C) 1999-2002 Stefan Weyergraf
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "file.h"
22
23 #include <cctype>
24 #include <cerrno>
25 #include <climits>
26 #include <cstdarg>
27 #include <cstdio>
28 #include <cstdlib>
29 #include <cstring>
30 #include <sys/stat.h>
31
32 /*
33 * COMMON SYS
34 */
35
36 #ifndef S_IFMT
37 #define S_IFMT 0xf000
38 #endif
39
40 #ifndef S_ISREG
41 # ifndef S_IFREG
42 # define S_ISREG(m) (0)
43 # else
44 # define S_ISREG(m) (((m) & S_IFMT)==S_IFREG)
45 # endif
46 #endif
47
48 #ifndef S_ISBLK
49 # ifndef S_IFBLK
50 # define S_ISBLK(m) (0)
51 # else
52 # define S_ISBLK(m) (((m) & S_IFMT)==S_IFBLK)
53 # endif
54 #endif
55
56
57 #ifndef S_ISCHR
58 # ifndef S_IFCHR
59 # define S_ISCHR(m) (0)
60 # else
61 # define S_ISCHR(m) (((m) & S_IFMT)==S_IFCHR)
62 # endif
63 #endif
64
65 #ifndef S_ISDIR
66 # ifndef S_IFDIR
67 # define S_ISDIR(m) (0)
68 # else
69 # define S_ISDIR(m) (((m) & S_IFMT)==S_IFDIR)
70 # endif
71 #endif
72
73 #ifndef S_ISFIFO
74 # ifndef S_IFFIFO
75 # define S_ISFIFO(m) (0)
76 # else
77 # define S_ISFIFO(m) (((m) & S_IFMT)==S_IFFIFO)
78 # endif
79 #endif
80
81 #ifndef S_ISLNK
82 # ifndef S_IFLNK
83 # define S_ISLNK(m) (0)
84 # else
85 # define S_ISLNK(m) (((m) & S_IFMT)==S_IFLNK)
86 # endif
87 #endif
88
89 #ifndef S_ISSOCK
90 # ifndef S_IFSOCK
91 # define S_ISSOCK(m) (0)
92 # else
93 # define S_ISSOCK(m) (((m) & S_IFMT)==S_IFSOCK)
94 # endif
95 #endif
96
97 #ifndef S_IRUSR
98 #define S_IRUSR 0
99 #endif
100 #ifndef S_IRGRP
101 #define S_IRGRP 0
102 #endif
103 #ifndef S_IROTH
104 #define S_IROTH 0
105 #endif
106
107 #ifndef S_IWUSR
108 #define S_IWUSR 0
109 #endif
110 #ifndef S_IWGRP
111 #define S_IWGRP 0
112 #endif
113 #ifndef S_IWOTH
114 #define S_IWOTH 0
115 #endif
116
117 #ifndef S_IXUSR
118 #define S_IXUSR 0
119 #endif
120 #ifndef S_IXGRP
121 #define S_IXGRP 0
122 #endif
123 #ifndef S_IXOTH
124 #define S_IXOTH 0
125 #endif
126
127 int sys_basename(char *result, const char *filename)
128 {
129 // FIXME: use is_path_delim
130 char *slash1 = strrchr(filename, '/');
131 char *slash2 = strrchr(filename, '\\');
132 char *slash=(slash1>slash2) ? slash1 : slash2;
133 if (slash) {
134 int l=strlen(filename);
135 strncpy(result, slash+1, l-(slash-filename)-1);
136 result[l-(slash-filename)-1]=0;
137 return 0;
138 }
139 strcpy(result, filename);
140 return 0;
141 }
142
143 int sys_dirname(char *result, const char *filename)
144 {
145 // FIXME: use is_path_delim
146 char *slash1 = strrchr(filename, '/');
147 char *slash2 = strrchr(filename, '\\');
148 char *slash = (slash1>slash2) ? slash1 : slash2;
149 if (slash) {
150 strncpy(result, filename, slash-filename);
151 result[slash-filename] = 0;
152 return 0;
153 }
154 strcpy(result, ".");
155 return 0;
156 }
157
158 /* filename and pathname must be canonicalized */
159 int sys_relname(char *result, const char *filename, const char *cwd)
160 {
161 const char *f = filename, *p = cwd;
162 while ((*f == *p) && (*f)) {
163 f++;
164 p++;
165 }
166 if (*f == '/') f++;
167 const char *last = f, *h = f;
168 while (*h) {
169 if (*h == '/') {
170 *(result++) = '.';
171 *(result++) = '.';
172 *(result++) = '/';
173 last = h+1;
174 }
175 h++;
176 }
177 while (f<last) {
178 *(result++) = *f;
179 f++;
180 }
181 *result = 0;
182 strcat(result, last);
183 return 0;
184 }
185
186 int sys_ht_mode(int mode)
187 {
188 int m = 0;
189 if (S_ISREG(mode)) {
190 m |= HT_S_IFREG;
191 } else if (S_ISBLK(mode)) {
192 m |= HT_S_IFBLK;
193 } else if (S_ISCHR(mode)) {
194 m |= HT_S_IFCHR;
195 } else if (S_ISDIR(mode)) {
196 m |= HT_S_IFDIR;
197 } else if (S_ISFIFO(mode)) {
198 m |= HT_S_IFFIFO;
199 } else if (S_ISLNK(mode)) {
200 m |= HT_S_IFLNK;
201 } else if (S_ISSOCK(mode)) {
202 m |= HT_S_IFSOCK;
203 }
204 if (mode & S_IRUSR) m |= HT_S_IRUSR;
205 if (mode & S_IRGRP) m |= HT_S_IRGRP;
206 if (mode & S_IROTH) m |= HT_S_IROTH;
207
208 if (mode & S_IWUSR) m |= HT_S_IWUSR;
209 if (mode & S_IWGRP) m |= HT_S_IWGRP;
210 if (mode & S_IWOTH) m |= HT_S_IWOTH;
211
212 if (mode & S_IXUSR) m |= HT_S_IXUSR;
213 if (mode & S_IXGRP) m |= HT_S_IXGRP;
214 if (mode & S_IXOTH) m |= HT_S_IXOTH;
215 return m;
216 }
217
218 static char *next_delim(char *s, is_path_delim delim)
219 {
220 while (*s) {
221 s++;
222 if (delim(*s)) return s;
223 }
224 return NULL;
225 }
226
227 static int flatten_path(char *path, is_path_delim delim)
228 {
229 if (!path || !*path)
230 return 0;
231 char *q = next_delim(path, delim);
232 int pp = flatten_path(q, delim);
233 int ll = q ? (q-path-1) : strlen(path)-1;
234 if ((ll == 2) && (strncmp(path+1, "..", 2) == 0)) {
235 if (q) memmove(path, q, strlen(q)+1); else *path = 0;
236 pp++;
237 } else if ((ll == 1) && (strncmp(path+1, ".", 1) == 0)) {
238 if (q) memmove(path, q, strlen(q)+1); else *path = 0;
239 } else if (pp) {
240 if (q) memmove(path, q, strlen(q)+1); else *path = 0;
241 pp--;
242 }
243 return pp;
244 }
245
246 bool sys_path_is_absolute(const char *filename, is_path_delim delim)
247 {
248 return delim(filename[0]) || (isalpha(filename[0]) && (filename[1] == ':'));
249 }
250
251 int sys_common_canonicalize(char *result, const char *filename, const char *cwd, is_path_delim delim)
252 {
253 char *o = result;
254 if (!sys_path_is_absolute(filename, delim)) {
255 if (cwd) strcpy(o, cwd); else return EINVAL;
256 int ol = strlen(o);
257 if (ol && !delim(o[ol-1])) {
258 o[ol] = '/';
259 o[ol+1] = 0;
260 }
261 } else *o = 0;
262 strcat(o, filename);
263 int k = flatten_path(o, delim);
264 return (k == 0) ? 0 : EINVAL;
265 }
266
267 char *sys_filename_suffix(const char *fn)
268 {
269 const char *s = NULL;
270 while (fn && *fn) {
271 if (sys_is_path_delim(*fn)) s = fn+1;
272 fn++;
273 }
274 char *p = s ? strrchr(s, '.') : NULL;
275 return p ? p+1 : NULL;
276 }
277
278 int sys_tmpfile_fd()
279 {
280 #if 1
281 // FIXME: this might leak something...
282 FILE *f = tmpfile();
283 return fileno(f);
284 #else
285 // is this better ?
286 return open(tmpnam(NULL), O_RDWR | O_EXCL | O_CREAT);
287 #endif
288 }

  ViewVC Help
Powered by ViewVC 1.1.26