1 | /* |
1 | /* |
2 | * static char *rcsid_porting_c = |
2 | * static char *rcsid_porting_c = |
3 | * "$Id: porting.C,v 1.1 2006/08/13 17:16:00 elmex Exp $"; |
3 | * "$Id: porting.C,v 1.2 2006/08/29 08:01:35 root Exp $"; |
4 | */ |
4 | */ |
5 | |
5 | |
6 | /* |
6 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
7 | CrossFire, A Multiplayer game for X-windows |
8 | |
8 | |
… | |
… | |
96 | #ifndef MAXPATHLEN |
96 | #ifndef MAXPATHLEN |
97 | #define MAXPATHLEN 4096 |
97 | #define MAXPATHLEN 4096 |
98 | #endif |
98 | #endif |
99 | |
99 | |
100 | if (!(name = (char *) malloc(MAXPATHLEN))) |
100 | if (!(name = (char *) malloc(MAXPATHLEN))) |
101 | return(NULL); |
101 | return(NULL); |
102 | |
102 | |
103 | if (!pfx) |
103 | if (!pfx) |
104 | pfx = "cftmp."; |
104 | pfx = "cftmp."; |
105 | |
105 | |
106 | /* This is a pretty simple method - put the pid as a hex digit and |
106 | /* 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 |
107 | * 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 |
108 | * already exists - if so, we'll just keep looking - eventually we should |
109 | * find one that is free. |
109 | * find one that is free. |
110 | */ |
110 | */ |
111 | if (dir!=NULL) { |
111 | if (dir!=NULL) { |
112 | do { |
112 | do { |
113 | #ifdef HAVE_SNPRINTF |
113 | #ifdef HAVE_SNPRINTF |
114 | (void)snprintf(name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp); |
114 | (void)snprintf(name, MAXPATHLEN, "%s/%s%hx.%d", dir, pfx, pid, curtmp); |
115 | #else |
115 | #else |
116 | (void)sprintf(name,"%s/%s%hx%d", dir, pfx, pid, curtmp); |
116 | (void)sprintf(name,"%s/%s%hx%d", dir, pfx, pid, curtmp); |
117 | #endif |
117 | #endif |
118 | curtmp++; |
118 | curtmp++; |
119 | } while (access(name, F_OK)!=-1); |
119 | } while (access(name, F_OK)!=-1); |
120 | return(name); |
120 | return(name); |
121 | } |
121 | } |
122 | return(NULL); |
122 | return(NULL); |
123 | } |
123 | } |
124 | |
124 | |
125 | |
125 | |
… | |
… | |
131 | char buf[MAX_BUF]; |
131 | char buf[MAX_BUF]; |
132 | struct stat statbuf; |
132 | struct stat statbuf; |
133 | int status; |
133 | int status; |
134 | |
134 | |
135 | if ((dirp=opendir(path))!=NULL) { |
135 | if ((dirp=opendir(path))!=NULL) { |
136 | struct dirent *de; |
136 | struct dirent *de; |
137 | |
137 | |
138 | for (de=readdir(dirp); de; de = readdir(dirp)) { |
138 | for (de=readdir(dirp); de; de = readdir(dirp)) { |
139 | /* Don't remove '.' or '..' In theory we should do a better |
139 | /* Don't remove '.' or '..' In theory we should do a better |
140 | * check for .., but the directories we are removing are fairly |
140 | * check for .., but the directories we are removing are fairly |
141 | * limited and should not have dot files in them. |
141 | * limited and should not have dot files in them. |
142 | */ |
142 | */ |
143 | if (de->d_name[0] == '.') continue; |
143 | if (de->d_name[0] == '.') continue; |
144 | |
144 | |
145 | /* Linux actually has a type field in the dirent structure, |
145 | /* Linux actually has a type field in the dirent structure, |
146 | * but that is not portable - stat should be portable |
146 | * but that is not portable - stat should be portable |
147 | */ |
147 | */ |
148 | status=stat(de->d_name, &statbuf); |
148 | status=stat(de->d_name, &statbuf); |
149 | if ((status!=-1) && (S_ISDIR(statbuf.st_mode))) { |
149 | if ((status!=-1) && (S_ISDIR(statbuf.st_mode))) { |
150 | sprintf(buf,"%s/%s", path, de->d_name); |
150 | sprintf(buf,"%s/%s", path, de->d_name); |
151 | remove_directory(buf); |
151 | remove_directory(buf); |
152 | continue; |
152 | continue; |
153 | } |
153 | } |
154 | sprintf(buf,"%s/%s", path, de->d_name); |
154 | sprintf(buf,"%s/%s", path, de->d_name); |
155 | if (unlink(buf)) { |
155 | if (unlink(buf)) { |
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 | LOG(llevError,"Unable to remove directory %s\n", path); |
162 | LOG(llevError,"Unable to remove directory %s\n", path); |
163 | } |
163 | } |
164 | } |
164 | } |
165 | |
165 | |
166 | #if defined(sgi) |
166 | #if defined(sgi) |
167 | |
167 | |
… | |
… | |
171 | |
171 | |
172 | #define popen fixed_popen |
172 | #define popen fixed_popen |
173 | |
173 | |
174 | FILE *popen_local(const char *command, const char *type) |
174 | FILE *popen_local(const char *command, const char *type) |
175 | { |
175 | { |
176 | int fd[2]; |
176 | int fd[2]; |
177 | int pd; |
177 | int pd; |
178 | FILE *ret; |
178 | FILE *ret; |
179 | if (!strcmp(type,"r")) |
179 | if (!strcmp(type,"r")) |
180 | { |
180 | { |
181 | pd=STDOUT_FILENO; |
181 | pd=STDOUT_FILENO; |
182 | } |
182 | } |
183 | else if (!strcmp(type,"w")) |
183 | else if (!strcmp(type,"w")) |
184 | { |
184 | { |
185 | pd=STDIN_FILENO; |
185 | pd=STDIN_FILENO; |
186 | } |
186 | } |
187 | else |
187 | else |
188 | { |
188 | { |
189 | return NULL; |
189 | return NULL; |
190 | } |
190 | } |
191 | if (pipe(fd)!=-1) |
191 | if (pipe(fd)!=-1) |
192 | { |
192 | { |
193 | switch (fork()) |
193 | switch (fork()) |
194 | { |
194 | { |
195 | case -1: |
195 | case -1: |
196 | close(fd[0]); |
196 | close(fd[0]); |
197 | close(fd[1]); |
197 | close(fd[1]); |
198 | break; |
198 | break; |
199 | case 0: |
199 | case 0: |
200 | close(fd[0]); |
200 | close(fd[0]); |
201 | if ((fd[1]==pd)||(dup2(fd[1],pd)==pd)) |
201 | if ((fd[1]==pd)||(dup2(fd[1],pd)==pd)) |
202 | { |
202 | { |
203 | if (fd[1]!=pd) |
203 | if (fd[1]!=pd) |
204 | { |
204 | { |
205 | close(fd[1]); |
205 | close(fd[1]); |
206 | } |
206 | } |
207 | execl("/bin/sh","sh","-c",command,NULL); |
207 | execl("/bin/sh","sh","-c",command,NULL); |
208 | close(pd); |
208 | close(pd); |
209 | } |
209 | } |
210 | exit(1); |
210 | exit(1); |
211 | break; |
211 | break; |
212 | default: |
212 | default: |
213 | close(fd[1]); |
213 | close(fd[1]); |
214 | if (ret=fdopen(fd[0],type)) |
214 | if (ret=fdopen(fd[0],type)) |
215 | { |
215 | { |
216 | return ret; |
216 | return ret; |
217 | } |
217 | } |
218 | close(fd[0]); |
218 | close(fd[0]); |
219 | break; |
219 | break; |
220 | } |
220 | } |
221 | } |
221 | } |
222 | return NULL; |
222 | return NULL; |
223 | } |
223 | } |
224 | |
224 | |
225 | #endif /* defined(sgi) */ |
225 | #endif /* defined(sgi) */ |
226 | |
226 | |
227 | |
227 | |
… | |
… | |
279 | return (c1 - c2); |
279 | return (c1 - c2); |
280 | s1++; |
280 | s1++; |
281 | s2++; |
281 | s2++; |
282 | } |
282 | } |
283 | if (*s1=='\0' && *s2=='\0') |
283 | if (*s1=='\0' && *s2=='\0') |
284 | return 0; |
284 | return 0; |
285 | return (int) (*s1 - *s2); |
285 | return (int) (*s1 - *s2); |
286 | } |
286 | } |
287 | #endif |
287 | #endif |
288 | |
288 | |
289 | char *strcasestr_local(const char *s, const char *find) |
289 | char *strcasestr_local(const char *s, const char *find) |
290 | { |
290 | { |
291 | char c, sc; |
291 | char c, sc; |
292 | size_t len; |
292 | size_t len; |
293 | |
293 | |
294 | if ((c = *find++) != 0) { |
294 | if ((c = *find++) != 0) { |
295 | c = tolower(c); |
295 | c = tolower(c); |
296 | len = strlen(find); |
296 | len = strlen(find); |
297 | do { |
297 | do { |
298 | do { |
298 | do { |
299 | if ((sc = *s++) == 0) |
299 | if ((sc = *s++) == 0) |
300 | return NULL; |
300 | return NULL; |
… | |
… | |
345 | * series n^2 = 1 + 3 + ... + (2n-1) |
345 | * series n^2 = 1 + 3 + ... + (2n-1) |
346 | */ |
346 | */ |
347 | int |
347 | int |
348 | isqrt(int n) |
348 | isqrt(int n) |
349 | { |
349 | { |
350 | int result, sum, prev; |
350 | int result, sum, prev; |
351 | result = 0; |
351 | result = 0; |
352 | prev = sum = 1; |
352 | prev = sum = 1; |
353 | while (sum <= n) { |
353 | while (sum <= n) { |
354 | prev += 2; |
354 | prev += 2; |
355 | sum += prev; |
355 | sum += prev; |
356 | ++result; |
356 | ++result; |
357 | } |
357 | } |
358 | return result; |
358 | return result; |
359 | } |
359 | } |
360 | |
360 | |
361 | |
361 | |
362 | /* |
362 | /* |
363 | * returns a char-pointer to a static array, in which a representation |
363 | * returns a char-pointer to a static array, in which a representation |
364 | * of the decimal number given will be stored. |
364 | * of the decimal number given will be stored. |
365 | */ |
365 | */ |
366 | |
366 | |
367 | char *ltostr10(signed long n) { |
367 | char *ltostr10(signed long n) { |
368 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
368 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
369 | character for the trailing nul character */ |
369 | character for the trailing nul character */ |
370 | snprintf(buf, sizeof(buf), "%ld", n); |
370 | snprintf(buf, sizeof(buf), "%ld", n); |
371 | return buf; |
371 | return buf; |
372 | } |
372 | } |
373 | char *doubletostr10(double v){ |
373 | char *doubletostr10(double v){ |
374 | static char tbuf[200]; |
374 | static char tbuf[200]; |
… | |
… | |
552 | { |
552 | { |
553 | char buf[MAX_BUF], *cp = buf; |
553 | char buf[MAX_BUF], *cp = buf; |
554 | struct stat statbuf; |
554 | struct stat statbuf; |
555 | |
555 | |
556 | if (!filename || !*filename) |
556 | if (!filename || !*filename) |
557 | return; |
557 | return; |
558 | strcpy (buf, filename); |
558 | strcpy (buf, filename); |
559 | |
559 | |
560 | while ((cp = strchr (cp + 1, (int) '/'))) { |
560 | while ((cp = strchr (cp + 1, (int) '/'))) { |
561 | *cp = '\0'; |
561 | *cp = '\0'; |
562 | if (stat(buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) { |
562 | if (stat(buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) { |
563 | if (mkdir (buf, SAVE_DIR_MODE)) { |
563 | if (mkdir (buf, SAVE_DIR_MODE)) { |
564 | LOG(llevError, "Cannot mkdir %s: %s\n", buf, strerror_local(errno)); |
564 | LOG(llevError, "Cannot mkdir %s: %s\n", buf, strerror_local(errno)); |
565 | return; |
565 | return; |
|
|
566 | } |
|
|
567 | } |
|
|
568 | *cp = '/'; |
566 | } |
569 | } |
567 | } |
|
|
568 | *cp = '/'; |
|
|
569 | } |
|
|
570 | } |
570 | } |
571 | |
571 | |