|
|
1 | |
1 | /* |
2 | /* |
2 | * static char *rcsid_porting_c = |
3 | * static char *rcsid_porting_c = |
3 | * "$Id: porting.C,v 1.2 2006/08/29 08:01:35 root Exp $"; |
4 | * "$Id: porting.C,v 1.5 2006/09/10 16:00:23 root Exp $"; |
4 | */ |
5 | */ |
5 | |
6 | |
6 | /* |
7 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
8 | CrossFire, A Multiplayer game for X-windows |
8 | |
9 | |
… | |
… | |
32 | * nasty system dependent stuff is contained here, with the program |
33 | * nasty system dependent stuff is contained here, with the program |
33 | * calling these functions. |
34 | * calling these functions. |
34 | */ |
35 | */ |
35 | |
36 | |
36 | |
37 | |
37 | #ifdef WIN32 /* ---win32 exclude/include headers */ |
38 | #ifdef WIN32 /* ---win32 exclude/include headers */ |
38 | #include "process.h" |
39 | # include "process.h" |
39 | #define pid_t int /* we include it non global, because there is a redefinition in python.h */ |
40 | # define pid_t int /* we include it non global, because there is a redefinition in python.h */ |
40 | #else |
41 | #else |
41 | #include <ctype.h> |
42 | # include <ctype.h> |
42 | #include <sys/stat.h> |
43 | # include <sys/stat.h> |
43 | #include <sys/wait.h> |
44 | # include <sys/wait.h> |
44 | |
45 | |
45 | #include <sys/param.h> |
46 | # include <sys/param.h> |
46 | #include <stdio.h> |
47 | # include <stdio.h> |
47 | |
48 | |
48 | /* Need to pull in the HAVE_... values somehow */ |
49 | /* Need to pull in the HAVE_... values somehow */ |
|
|
50 | |
49 | /* win32 reminder: always put this in a ifndef win32 block */ |
51 | /* win32 reminder: always put this in a ifndef win32 block */ |
50 | #include <autoconf.h> |
52 | # include <autoconf.h> |
51 | #endif |
53 | #endif |
52 | |
54 | |
53 | |
55 | |
54 | #ifdef HAVE_STDLIB_H |
56 | #ifdef HAVE_STDLIB_H |
55 | #include <stdlib.h> |
57 | # include <stdlib.h> |
56 | #endif |
58 | #endif |
57 | |
59 | |
58 | #ifdef HAVE_UNISTD_H |
60 | #ifdef HAVE_UNISTD_H |
59 | #include <unistd.h> |
61 | # include <unistd.h> |
60 | #endif |
62 | #endif |
61 | |
63 | |
62 | #include <stdarg.h> |
64 | #include <stdarg.h> |
|
|
65 | |
63 | /* Has to be after above includes so we don't redefine some values */ |
66 | /* Has to be after above includes so we don't redefine some values */ |
64 | #include "global.h" |
67 | #include "global.h" |
65 | |
68 | |
66 | static unsigned int curtmp = 0; |
69 | static unsigned int curtmp = 0; |
67 | |
|
|
68 | /* This is a list of the suffix, uncompress and compress functions. Thus, |
|
|
69 | * if you have some other compress program you want to use, the only thing |
|
|
70 | * that needs to be done is to extended this. |
|
|
71 | * The first entry must be NULL - this is what is used for non |
|
|
72 | * compressed files. |
|
|
73 | */ |
|
|
74 | EXTERN char *uncomp[NROF_COMPRESS_METHODS][3] = { |
|
|
75 | {NULL, NULL, NULL}, |
|
|
76 | {".Z", UNCOMPRESS, COMPRESS}, |
|
|
77 | {".gz", GUNZIP, GZIP}, |
|
|
78 | {".bz2", BUNZIP, BZIP} |
|
|
79 | }; |
|
|
80 | |
70 | |
81 | /***************************************************************************** |
71 | /***************************************************************************** |
82 | * File related functions |
72 | * File related functions |
83 | ****************************************************************************/ |
73 | ****************************************************************************/ |
84 | |
74 | |
85 | /* |
75 | /* |
86 | * A replacement for the tempnam() function since it's not defined |
76 | * A replacement for the tempnam() function since it's not defined |
87 | * at some unix variants. |
77 | * at some unix variants. |
88 | */ |
78 | */ |
89 | |
79 | |
|
|
80 | char * |
90 | char *tempnam_local(const char *dir, const char *pfx) |
81 | tempnam_local (const char *dir, const char *pfx) |
91 | { |
82 | { |
92 | char *name; |
83 | char *name; |
93 | pid_t pid=getpid(); |
84 | pid_t pid = getpid (); |
94 | |
85 | |
95 | /* HURD does not have a hard limit, but we do */ |
86 | /* HURD does not have a hard limit, but we do */ |
96 | #ifndef MAXPATHLEN |
87 | #ifndef MAXPATHLEN |
97 | #define MAXPATHLEN 4096 |
88 | # define MAXPATHLEN 4096 |
98 | #endif |
89 | #endif |
99 | |
90 | |
100 | if (!(name = (char *) malloc(MAXPATHLEN))) |
91 | if (!(name = (char *) malloc (MAXPATHLEN))) |
101 | return(NULL); |
92 | return (NULL); |
102 | |
93 | |
103 | if (!pfx) |
94 | if (!pfx) |
104 | pfx = "cftmp."; |
95 | pfx = "cftmp."; |
105 | |
96 | |
106 | /* This is a pretty simple method - put the pid as a hex digit and |
97 | /* This is a pretty simple method - put the pid as a hex digit and |
107 | * just keep incrementing the last digit. Check to see if the file |
98 | * just keep incrementing the last digit. Check to see if the file |
108 | * already exists - if so, we'll just keep looking - eventually we should |
99 | * already exists - if so, we'll just keep looking - eventually we should |
109 | * find one that is free. |
100 | * find one that is free. |
110 | */ |
101 | */ |
111 | if (dir!=NULL) { |
102 | if (dir != NULL) |
|
|
103 | { |
|
|
104 | do |
112 | do { |
105 | { |
113 | #ifdef HAVE_SNPRINTF |
106 | #ifdef HAVE_SNPRINTF |
114 | (void)snprintf(name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp); |
107 | (void) snprintf (name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp); |
115 | #else |
108 | #else |
116 | (void)sprintf(name,"%s/%s%hx%d", dir, pfx, pid, curtmp); |
109 | (void) sprintf (name, "%s/%s%hx%d", dir, pfx, pid, curtmp); |
117 | #endif |
110 | #endif |
118 | curtmp++; |
111 | curtmp++; |
|
|
112 | } |
119 | } while (access(name, F_OK)!=-1); |
113 | while (access (name, F_OK) != -1); |
120 | return(name); |
114 | return (name); |
121 | } |
115 | } |
122 | return(NULL); |
116 | return (NULL); |
123 | } |
117 | } |
124 | |
118 | |
125 | |
119 | |
126 | |
120 | |
127 | /* This function removes everything in the directory. */ |
121 | /* This function removes everything in the directory. */ |
|
|
122 | void |
128 | void remove_directory(const char *path) |
123 | remove_directory (const char *path) |
129 | { |
124 | { |
130 | DIR *dirp; |
125 | DIR *dirp; |
131 | char buf[MAX_BUF]; |
126 | char buf[MAX_BUF]; |
132 | struct stat statbuf; |
127 | struct stat statbuf; |
133 | int status; |
128 | int status; |
134 | |
129 | |
135 | if ((dirp=opendir(path))!=NULL) { |
130 | if ((dirp = opendir (path)) != NULL) |
|
|
131 | { |
136 | struct dirent *de; |
132 | struct dirent *de; |
137 | |
133 | |
138 | for (de=readdir(dirp); de; de = readdir(dirp)) { |
134 | for (de = readdir (dirp); de; de = readdir (dirp)) |
|
|
135 | { |
139 | /* Don't remove '.' or '..' In theory we should do a better |
136 | /* Don't remove '.' or '..' In theory we should do a better |
140 | * check for .., but the directories we are removing are fairly |
137 | * check for .., but the directories we are removing are fairly |
141 | * limited and should not have dot files in them. |
138 | * limited and should not have dot files in them. |
142 | */ |
139 | */ |
143 | if (de->d_name[0] == '.') continue; |
140 | if (de->d_name[0] == '.') |
|
|
141 | continue; |
144 | |
142 | |
145 | /* Linux actually has a type field in the dirent structure, |
143 | /* Linux actually has a type field in the dirent structure, |
146 | * but that is not portable - stat should be portable |
144 | * but that is not portable - stat should be portable |
147 | */ |
145 | */ |
148 | status=stat(de->d_name, &statbuf); |
146 | status = stat (de->d_name, &statbuf); |
149 | if ((status!=-1) && (S_ISDIR(statbuf.st_mode))) { |
147 | if ((status != -1) && (S_ISDIR (statbuf.st_mode))) |
|
|
148 | { |
150 | sprintf(buf,"%s/%s", path, de->d_name); |
149 | sprintf (buf, "%s/%s", path, de->d_name); |
151 | remove_directory(buf); |
150 | remove_directory (buf); |
152 | continue; |
151 | continue; |
153 | } |
152 | } |
154 | sprintf(buf,"%s/%s", path, de->d_name); |
153 | sprintf (buf, "%s/%s", path, de->d_name); |
155 | if (unlink(buf)) { |
154 | if (unlink (buf)) |
|
|
155 | { |
156 | LOG(llevError,"Unable to remove directory %s\n", path); |
156 | LOG (llevError, "Unable to remove directory %s\n", path); |
157 | } |
157 | } |
158 | } |
158 | } |
159 | closedir(dirp); |
159 | closedir (dirp); |
160 | } |
160 | } |
161 | if (rmdir(path)) { |
161 | if (rmdir (path)) |
|
|
162 | { |
162 | LOG(llevError,"Unable to remove directory %s\n", path); |
163 | LOG (llevError, "Unable to remove directory %s\n", path); |
163 | } |
164 | } |
164 | } |
165 | } |
165 | |
166 | |
166 | #if defined(sgi) |
167 | #if defined(sgi) |
167 | |
168 | |
168 | #include <stdio.h> |
169 | # include <stdio.h> |
169 | #include <stdlib.h> |
170 | # include <stdlib.h> |
170 | #include <string.h> |
171 | # include <string.h> |
171 | |
172 | |
172 | #define popen fixed_popen |
173 | # define popen fixed_popen |
173 | |
174 | |
|
|
175 | FILE * |
174 | FILE *popen_local(const char *command, const char *type) |
176 | popen_local (const char *command, const char *type) |
175 | { |
177 | { |
176 | int fd[2]; |
178 | int fd[2]; |
177 | int pd; |
179 | int pd; |
178 | FILE *ret; |
180 | FILE *ret; |
|
|
181 | |
179 | if (!strcmp(type,"r")) |
182 | if (!strcmp (type, "r")) |
|
|
183 | { |
|
|
184 | pd = STDOUT_FILENO; |
|
|
185 | } |
|
|
186 | else if (!strcmp (type, "w")) |
|
|
187 | { |
|
|
188 | pd = STDIN_FILENO; |
|
|
189 | } |
|
|
190 | else |
|
|
191 | { |
|
|
192 | return NULL; |
|
|
193 | } |
|
|
194 | if (pipe (fd) != -1) |
|
|
195 | { |
|
|
196 | switch (fork ()) |
180 | { |
197 | { |
181 | pd=STDOUT_FILENO; |
198 | case -1: |
|
|
199 | close (fd[0]); |
|
|
200 | close (fd[1]); |
|
|
201 | break; |
|
|
202 | case 0: |
|
|
203 | close (fd[0]); |
|
|
204 | if ((fd[1] == pd) || (dup2 (fd[1], pd) == pd)) |
|
|
205 | { |
|
|
206 | if (fd[1] != pd) |
|
|
207 | { |
|
|
208 | close (fd[1]); |
|
|
209 | } |
|
|
210 | execl ("/bin/sh", "sh", "-c", command, NULL); |
|
|
211 | close (pd); |
|
|
212 | } |
|
|
213 | exit (1); |
|
|
214 | break; |
|
|
215 | default: |
|
|
216 | close (fd[1]); |
|
|
217 | if (ret = fdopen (fd[0], type)) |
|
|
218 | { |
|
|
219 | return ret; |
|
|
220 | } |
|
|
221 | close (fd[0]); |
|
|
222 | break; |
182 | } |
223 | } |
183 | else if (!strcmp(type,"w")) |
224 | } |
184 | { |
|
|
185 | pd=STDIN_FILENO; |
|
|
186 | } |
|
|
187 | else |
|
|
188 | { |
|
|
189 | return NULL; |
|
|
190 | } |
|
|
191 | if (pipe(fd)!=-1) |
|
|
192 | { |
|
|
193 | switch (fork()) |
|
|
194 | { |
|
|
195 | case -1: |
|
|
196 | close(fd[0]); |
|
|
197 | close(fd[1]); |
|
|
198 | break; |
|
|
199 | case 0: |
|
|
200 | close(fd[0]); |
|
|
201 | if ((fd[1]==pd)||(dup2(fd[1],pd)==pd)) |
|
|
202 | { |
|
|
203 | if (fd[1]!=pd) |
|
|
204 | { |
|
|
205 | close(fd[1]); |
|
|
206 | } |
|
|
207 | execl("/bin/sh","sh","-c",command,NULL); |
|
|
208 | close(pd); |
|
|
209 | } |
|
|
210 | exit(1); |
|
|
211 | break; |
|
|
212 | default: |
|
|
213 | close(fd[1]); |
|
|
214 | if (ret=fdopen(fd[0],type)) |
|
|
215 | { |
|
|
216 | return ret; |
|
|
217 | } |
|
|
218 | close(fd[0]); |
|
|
219 | break; |
|
|
220 | } |
|
|
221 | } |
|
|
222 | return NULL; |
225 | return NULL; |
223 | } |
226 | } |
224 | |
227 | |
225 | #endif /* defined(sgi) */ |
228 | #endif /* defined(sgi) */ |
226 | |
229 | |
227 | |
230 | |
… | |
… | |
233 | |
236 | |
234 | /* |
237 | /* |
235 | * A replacement of strdup(), since it's not defined at some |
238 | * A replacement of strdup(), since it's not defined at some |
236 | * unix variants. |
239 | * unix variants. |
237 | */ |
240 | */ |
|
|
241 | char * |
238 | char *strdup_local(const char *str) { |
242 | strdup_local (const char *str) |
|
|
243 | { |
239 | char *c=(char *)malloc(sizeof(char)*(strlen(str)+1)); |
244 | char *c = (char *) malloc (sizeof (char) * (strlen (str) + 1)); |
|
|
245 | |
240 | strcpy(c,str); |
246 | strcpy (c, str); |
241 | return c; |
247 | return c; |
242 | } |
248 | } |
243 | |
249 | |
244 | |
250 | |
245 | #define DIGIT(x) (isdigit(x) ? (x) - '0' : \ |
251 | #define DIGIT(x) (isdigit(x) ? (x) - '0' : \ |
246 | islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') |
252 | islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') |
247 | #define MBASE ('z' - 'a' + 1 + 10) |
253 | #define MBASE ('z' - 'a' + 1 + 10) |
248 | |
254 | |
249 | /* This seems to be lacking on some system */ |
255 | /* This seems to be lacking on some system */ |
250 | #if !defined(HAVE_STRNCASECMP) |
256 | #if !defined(HAVE_STRNCASECMP) |
|
|
257 | int |
251 | int strncasecmp(const char *s1, const char *s2, int n) |
258 | strncasecmp (const char *s1, const char *s2, int n) |
252 | { |
259 | { |
253 | register int c1, c2; |
260 | register int c1, c2; |
254 | |
261 | |
255 | while (*s1 && *s2 && n) { |
262 | while (*s1 && *s2 && n) |
|
|
263 | { |
256 | c1 = tolower(*s1); |
264 | c1 = tolower (*s1); |
257 | c2 = tolower(*s2); |
265 | c2 = tolower (*s2); |
258 | if (c1 != c2) |
266 | if (c1 != c2) |
259 | return (c1 - c2); |
267 | return (c1 - c2); |
260 | s1++; |
268 | s1++; |
261 | s2++; |
269 | s2++; |
262 | n--; |
270 | n--; |
263 | } |
271 | } |
264 | if (!n) |
272 | if (!n) |
265 | return(0); |
273 | return (0); |
266 | return (int) (*s1 - *s2); |
274 | return (int) (*s1 - *s2); |
267 | } |
275 | } |
268 | #endif |
276 | #endif |
269 | |
277 | |
270 | #if !defined(HAVE_STRCASECMP) |
278 | #if !defined(HAVE_STRCASECMP) |
|
|
279 | int |
271 | int strcasecmp(const char *s1, const char*s2) |
280 | strcasecmp (const char *s1, const char *s2) |
272 | { |
281 | { |
273 | register int c1, c2; |
282 | register int c1, c2; |
274 | |
283 | |
275 | while (*s1 && *s2) { |
284 | while (*s1 && *s2) |
|
|
285 | { |
276 | c1 = tolower(*s1); |
286 | c1 = tolower (*s1); |
277 | c2 = tolower(*s2); |
287 | c2 = tolower (*s2); |
278 | if (c1 != c2) |
288 | if (c1 != c2) |
279 | return (c1 - c2); |
289 | return (c1 - c2); |
280 | s1++; |
290 | s1++; |
281 | s2++; |
291 | s2++; |
282 | } |
292 | } |
283 | if (*s1=='\0' && *s2=='\0') |
293 | if (*s1 == '\0' && *s2 == '\0') |
284 | return 0; |
294 | return 0; |
285 | return (int) (*s1 - *s2); |
295 | return (int) (*s1 - *s2); |
286 | } |
296 | } |
287 | #endif |
297 | #endif |
288 | |
298 | |
|
|
299 | char * |
289 | char *strcasestr_local(const char *s, const char *find) |
300 | strcasestr_local (const char *s, const char *find) |
290 | { |
301 | { |
291 | char c, sc; |
302 | char c, sc; |
292 | size_t len; |
303 | size_t len; |
293 | |
304 | |
294 | if ((c = *find++) != 0) { |
305 | if ((c = *find++) != 0) |
|
|
306 | { |
295 | c = tolower(c); |
307 | c = tolower (c); |
296 | len = strlen(find); |
308 | len = strlen (find); |
|
|
309 | do |
297 | do { |
310 | { |
|
|
311 | do |
298 | do { |
312 | { |
299 | if ((sc = *s++) == 0) |
313 | if ((sc = *s++) == 0) |
300 | return NULL; |
314 | return NULL; |
|
|
315 | } |
301 | } while (tolower(sc) != c); |
316 | while (tolower (sc) != c); |
|
|
317 | } |
302 | } while (strncasecmp(s, find, len) != 0); |
318 | while (strncasecmp (s, find, len) != 0); |
303 | s--; |
319 | s--; |
304 | } |
320 | } |
305 | return (char *)s; |
321 | return (char *) s; |
306 | } |
322 | } |
307 | |
323 | |
308 | #if !defined(HAVE_SNPRINTF) |
324 | #if !defined(HAVE_SNPRINTF) |
309 | |
325 | |
|
|
326 | int |
310 | int snprintf(char *dest, int max, const char *format, ...) |
327 | snprintf (char *dest, int max, const char *format, ...) |
311 | { |
328 | { |
312 | va_list var; |
329 | va_list var; |
313 | int ret; |
330 | int ret; |
314 | |
331 | |
315 | va_start(var, format); |
332 | va_start (var, format); |
316 | ret = vsprintf(dest, format, var); |
333 | ret = vsprintf (dest, format, var); |
317 | va_end(var); |
334 | va_end (var); |
318 | if (ret > max) abort(); |
335 | if (ret > max) |
|
|
336 | abort (); |
319 | |
337 | |
320 | return ret; |
338 | return ret; |
321 | } |
339 | } |
322 | #endif |
340 | #endif |
323 | |
341 | |
324 | |
|
|
325 | /* This takes an err number and returns a string with a description of |
|
|
326 | * the error. |
|
|
327 | */ |
|
|
328 | char *strerror_local(int errnum) |
|
|
329 | { |
|
|
330 | #if defined(HAVE_STRERROR) |
|
|
331 | return(strerror(errnum)); |
|
|
332 | #else |
|
|
333 | return("strerror_local not implemented"); |
|
|
334 | #endif |
|
|
335 | } |
|
|
336 | |
342 | |
337 | /* |
343 | /* |
338 | * Based on (n+1)^2 = n^2 + 2n + 1 |
344 | * Based on (n+1)^2 = n^2 + 2n + 1 |
339 | * given that 1^2 = 1, then |
345 | * given that 1^2 = 1, then |
340 | * 2^2 = 1 + (2 + 1) = 1 + 3 = 4 |
346 | * 2^2 = 1 + (2 + 1) = 1 + 3 = 4 |
… | |
… | |
343 | * ... |
349 | * ... |
344 | * In other words, a square number can be express as the sum of the |
350 | * In other words, a square number can be express as the sum of the |
345 | * series n^2 = 1 + 3 + ... + (2n-1) |
351 | * series n^2 = 1 + 3 + ... + (2n-1) |
346 | */ |
352 | */ |
347 | int |
353 | int |
348 | isqrt(int n) |
354 | isqrt (int n) |
349 | { |
355 | { |
350 | int result, sum, prev; |
356 | int result, sum, prev; |
|
|
357 | |
351 | result = 0; |
358 | result = 0; |
352 | prev = sum = 1; |
359 | prev = sum = 1; |
353 | while (sum <= n) { |
360 | while (sum <= n) |
|
|
361 | { |
354 | prev += 2; |
362 | prev += 2; |
355 | sum += prev; |
363 | sum += prev; |
356 | ++result; |
364 | ++result; |
357 | } |
365 | } |
358 | return result; |
366 | return result; |
359 | } |
367 | } |
360 | |
368 | |
361 | |
369 | |
362 | /* |
370 | /* |
363 | * returns a char-pointer to a static array, in which a representation |
371 | * returns a char-pointer to a static array, in which a representation |
364 | * of the decimal number given will be stored. |
372 | * of the decimal number given will be stored. |
365 | */ |
373 | */ |
366 | |
374 | |
|
|
375 | char * |
367 | char *ltostr10(signed long n) { |
376 | ltostr10 (signed long n) |
|
|
377 | { |
368 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
378 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
369 | character for the trailing nul character */ |
379 | character for the trailing nul character */ |
370 | snprintf(buf, sizeof(buf), "%ld", n); |
380 | snprintf (buf, sizeof (buf), "%ld", n); |
371 | return buf; |
381 | return buf; |
372 | } |
382 | } |
|
|
383 | |
|
|
384 | char * |
373 | char *doubletostr10(double v){ |
385 | doubletostr10 (double v) |
|
|
386 | { |
374 | static char tbuf[200]; |
387 | static char tbuf[200]; |
|
|
388 | |
375 | sprintf(tbuf,"%f",v); |
389 | sprintf (tbuf, "%f", v); |
376 | return tbuf; |
390 | return tbuf; |
377 | } |
|
|
378 | |
|
|
379 | /* |
|
|
380 | * A fast routine which appends the name and decimal number specified |
|
|
381 | * to the given buffer. |
|
|
382 | * Could be faster, though, if the strcat()s at the end could be changed |
|
|
383 | * into alternate strcat which returned a pointer to the _end_, not the |
|
|
384 | * start! |
|
|
385 | * |
|
|
386 | * Hey good news, it IS faster now, according to changes in get_ob_diff |
|
|
387 | * Completly redone prototype and made define in loader.l. See changes there. |
|
|
388 | * Didn't touch those for speed reason (don't use them anymore) . |
|
|
389 | * Tchize |
|
|
390 | */ |
|
|
391 | |
|
|
392 | void save_long(char *buf, char *name, long n) { |
|
|
393 | char buf2[MAX_BUF]; |
|
|
394 | strcpy(buf2,name); |
|
|
395 | strcat(buf2," "); |
|
|
396 | strcat(buf2,ltostr10(n)); |
|
|
397 | strcat(buf2,"\n"); |
|
|
398 | strcat(buf,buf2); |
|
|
399 | } |
|
|
400 | |
|
|
401 | |
|
|
402 | |
|
|
403 | void save_long_long(char *buf, char *name, sint64 n) { |
|
|
404 | char buf2[MAX_BUF]; |
|
|
405 | |
|
|
406 | #ifndef WIN32 |
|
|
407 | sprintf(buf2,"%s %lld\n", name, n); |
|
|
408 | #else |
|
|
409 | sprintf(buf2,"%s %I64d\n", name, n); |
|
|
410 | #endif |
|
|
411 | strcat(buf,buf2); |
|
|
412 | } |
|
|
413 | |
|
|
414 | /** |
|
|
415 | * Open and possibly uncompress a file. |
|
|
416 | * |
|
|
417 | * @param ext the extension if the file is compressed. |
|
|
418 | * |
|
|
419 | * @param uncompressor the command to uncompress the file if the file is |
|
|
420 | * compressed. |
|
|
421 | * |
|
|
422 | * @param name the base file name without compression extension |
|
|
423 | * |
|
|
424 | * @param flag only used for compressed files: if set, uncompress and open the |
|
|
425 | * file; if unset, uncompress the file via pipe |
|
|
426 | * |
|
|
427 | * @param *compressed set to zero if the file was uncompressed |
|
|
428 | */ |
|
|
429 | static FILE *open_and_uncompress_file(const char *ext, const char *uncompressor, const char *name, int flag, int *compressed) { |
|
|
430 | struct stat st; |
|
|
431 | char buf[MAX_BUF]; |
|
|
432 | char buf2[MAX_BUF]; |
|
|
433 | int ret; |
|
|
434 | |
|
|
435 | if (ext == NULL) { |
|
|
436 | ext = ""; |
|
|
437 | } |
|
|
438 | |
|
|
439 | if (strlen(name)+strlen(ext) >= sizeof(buf)) { |
|
|
440 | errno = ENAMETOOLONG; /* File name too long */ |
|
|
441 | return NULL; |
|
|
442 | } |
|
|
443 | sprintf(buf, "%s%s", name, ext); |
|
|
444 | |
|
|
445 | if (stat(buf, &st) != 0) { |
|
|
446 | return NULL; |
|
|
447 | } |
|
|
448 | |
|
|
449 | if (!S_ISREG(st.st_mode)) { |
|
|
450 | errno = EISDIR; /* Not a regular file */ |
|
|
451 | return NULL; |
|
|
452 | } |
|
|
453 | |
|
|
454 | if (uncompressor == NULL) { |
|
|
455 | /* open without uncompression */ |
|
|
456 | |
|
|
457 | return fopen(buf, "r"); |
|
|
458 | } |
|
|
459 | |
|
|
460 | /* The file name buf (and its substring name) is passed as an argument to a |
|
|
461 | * shell command, therefore check for characters that could confuse the |
|
|
462 | * shell. |
|
|
463 | */ |
|
|
464 | if (strpbrk(buf, "'\\\r\n") != NULL) { |
|
|
465 | errno = ENOENT; /* Pretend the file does not exist */ |
|
|
466 | return NULL; |
|
|
467 | } |
|
|
468 | |
|
|
469 | if (!flag) { |
|
|
470 | /* uncompress via pipe */ |
|
|
471 | |
|
|
472 | if (strlen(uncompressor)+4+strlen(buf)+1 >= sizeof(buf2)) { |
|
|
473 | errno = ENAMETOOLONG; /* File name too long */ |
|
|
474 | return NULL; |
|
|
475 | } |
|
|
476 | sprintf(buf2, "%s < '%s'", uncompressor, buf); |
|
|
477 | |
|
|
478 | return popen(buf2, "r"); |
|
|
479 | } |
|
|
480 | |
|
|
481 | /* remove compression from file, then open file */ |
|
|
482 | |
|
|
483 | if (stat(name, &st) == 0 && !S_ISREG(st.st_mode)) { |
|
|
484 | errno = EISDIR; |
|
|
485 | return NULL; |
|
|
486 | } |
|
|
487 | |
|
|
488 | if (strlen(uncompressor)+4+strlen(buf)+5+strlen(name)+1 >= sizeof(buf2)) { |
|
|
489 | errno = ENAMETOOLONG; /* File name too long */ |
|
|
490 | return NULL; |
|
|
491 | } |
|
|
492 | sprintf(buf2, "%s < '%s' > '%s'", uncompressor, buf, name); |
|
|
493 | |
|
|
494 | ret = system(buf2); |
|
|
495 | if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) { |
|
|
496 | LOG(llevError, "system(%s) returned %d\n", buf2, ret); |
|
|
497 | errno = ENOENT; |
|
|
498 | return NULL; |
|
|
499 | } |
|
|
500 | |
|
|
501 | unlink(buf); /* Delete the original */ |
|
|
502 | *compressed = 0; /* Change to "uncompressed file" */ |
|
|
503 | chmod(name, st.st_mode); /* Copy access mode from compressed file */ |
|
|
504 | |
|
|
505 | return fopen(name, "r"); |
|
|
506 | } |
391 | } |
507 | |
392 | |
508 | /** |
393 | /** |
509 | * open_and_uncompress() first searches for the original filename. If it exist, |
394 | * open_and_uncompress() first searches for the original filename. If it exist, |
510 | * then it opens it and returns the file-pointer. |
395 | * then it opens it and returns the file-pointer. |
511 | * |
396 | */ |
512 | * If not, it does two things depending on the flag. If the flag is set, it |
397 | FILE * |
513 | * tries to create the original file by appending a compression suffix to name |
|
|
514 | * and uncompressing it. If the flag is not set, it creates a pipe that is used |
|
|
515 | * for reading the file (NOTE - you can not use fseek on pipes). |
|
|
516 | * |
|
|
517 | * The compressed pointer is set to nonzero if the file is compressed (and |
|
|
518 | * thus, fp is actually a pipe.) It returns 0 if it is a normal file. |
|
|
519 | */ |
|
|
520 | FILE *open_and_uncompress(const char *name, int flag, int *compressed) { |
398 | open_and_uncompress (const char *name, int flag, int *compressed) |
|
|
399 | { |
521 | size_t i; |
400 | size_t i; |
522 | FILE *fp; |
401 | FILE *fp; |
523 | |
402 | |
524 | for (i = 0; i < NROF_COMPRESS_METHODS; i++) { |
|
|
525 | *compressed = i; |
403 | *compressed = 0; |
526 | fp = open_and_uncompress_file(uncomp[i][0], uncomp[i][1], name, flag, compressed); |
404 | return fopen (name, "r"); |
527 | if (fp != NULL) { |
|
|
528 | return fp; |
|
|
529 | } |
|
|
530 | } |
|
|
531 | |
|
|
532 | errno = ENOENT; |
|
|
533 | return NULL; |
|
|
534 | } |
405 | } |
535 | |
406 | |
536 | /* |
407 | /* |
537 | * See open_and_uncompress(). |
408 | * See open_and_uncompress(). |
538 | */ |
409 | */ |
539 | |
410 | |
|
|
411 | void |
540 | void close_and_delete(FILE *fp, int compressed) { |
412 | close_and_delete (FILE * fp, int compressed) |
541 | if (compressed) |
413 | { |
542 | pclose(fp); |
|
|
543 | else |
|
|
544 | fclose(fp); |
414 | fclose (fp); |
545 | } |
415 | } |
546 | |
416 | |
547 | /* |
417 | /* |
548 | * If any directories in the given path doesn't exist, they are created. |
418 | * If any directories in the given path doesn't exist, they are created. |
549 | */ |
419 | */ |
550 | |
420 | |
|
|
421 | void |
551 | void make_path_to_file (char *filename) |
422 | make_path_to_file (char *filename) |
552 | { |
423 | { |
553 | char buf[MAX_BUF], *cp = buf; |
424 | char buf[MAX_BUF], *cp = buf; |
554 | struct stat statbuf; |
425 | struct stat statbuf; |
555 | |
426 | |
556 | if (!filename || !*filename) |
427 | if (!filename || !*filename) |
557 | return; |
428 | return; |
558 | strcpy (buf, filename); |
429 | strcpy (buf, filename); |
559 | |
430 | |
560 | while ((cp = strchr (cp + 1, (int) '/'))) { |
431 | while ((cp = strchr (cp + 1, (int) '/'))) |
|
|
432 | { |
561 | *cp = '\0'; |
433 | *cp = '\0'; |
562 | if (stat(buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) { |
434 | if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) |
|
|
435 | { |
563 | if (mkdir (buf, SAVE_DIR_MODE)) { |
436 | if (mkdir (buf, SAVE_DIR_MODE)) |
|
|
437 | { |
564 | LOG(llevError, "Cannot mkdir %s: %s\n", buf, strerror_local(errno)); |
438 | LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno)); |
565 | return; |
439 | return; |
566 | } |
440 | } |
567 | } |
441 | } |
568 | *cp = '/'; |
442 | *cp = '/'; |
569 | } |
443 | } |
570 | } |
444 | } |
571 | |
|
|