Debian Codegraph @

common/porting.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file porting.c
16 * This file contains various functions that are not really unique for
17 * crossfire, but rather provides what should be standard functions
18 * for systems that do not have them. In this way, most of the
19 * nasty system dependent stuff is contained here, with the program
20 * calling these functions.
21 */
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #ifdef WIN32 /* ---win32 exclude/include headers */
32 #include "process.h"
33 #else
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37 #endif
38
39 /* Has to be after above includes so we don't redefine some values */
40 #include "global.h"
41
42 /*****************************************************************************
43 * File related functions
44 ****************************************************************************/
45
46 #ifndef HAVE_TEMPNAM
47 /**
48 * Portable implementation of tempnam(3).
49 *
50 * Do not use this function for new code, use tempnam_secure() instead.
51 */
52 char *tempnam(const char *dir, const char *pfx) {
53 /** Used to generate temporary unique name. */
54 static unsigned int curtmp;
55
56 char *name;
57 pid_t pid = getpid();
58
59 /* HURD does not have a hard limit, but we do */
60 #ifndef MAXPATHLEN
61 #define MAXPATHLEN 4096
62 #endif
63
64 if (!pfx)
65 pfx = "cftmp.";
66
67 /* This is a pretty simple method - put the pid as a hex digit and
68 * just keep incrementing the last digit. Check to see if the file
69 * already exists - if so, we'll just keep looking - eventually we should
70 * find one that is free.
71 */
72 if (dir != NULL) {
73 if (!(name = (char *)malloc(MAXPATHLEN)))
74 return(NULL);
75 do {
76 snprintf(name, MAXPATHLEN, "%s/%s%x.%u", dir, pfx, (unsigned int)pid, curtmp);
77 curtmp++;
78 } while (access(name, F_OK) != -1);
79 return(name);
80 }
81 return(NULL);
82 }
83 #endif
84
85 /**
86 * A replacement for the tempnam_local() function since that one is not very
87 * secure. This one will open the file in an atomic way on platforms where it is
88 * possible.
89 *
90 * @param dir
91 * Directory where to create the file. Can be NULL, in which case NULL is returned.
92 * @param pfx
93 * Prefix to create unique name. Can be NULL.
94 * @param filename
95 * This should be a pointer to a char*, the function will overwrite the char*
96 * with the name of the resulting file. Must be freed by caller. Value is
97 * unchanged if the function returns NULL.
98 * @return
99 * A pointer to a FILE opened exclusively, or NULL if failure.
100 * It is up to the caller to properly close it.
101 * @note
102 * The file will be opened read-write.
103 *
104 * @todo
105 * Maybe adding some ifdef for non-UNIX? I don't have any such system around
106 * to test with.
107 */
108 FILE *tempnam_secure(const char *dir, const char *pfx, char **filename) {
109 char *tempname = NULL;
110 int fd;
111 int i;
112 FILE *file = NULL;
113 const int maxretry = 10;
114
115 /* Limit number of retries to MAXRETRY */
116 for (i = 0; i < maxretry; i++) {
117 tempname = tempnam(dir, pfx);
118 if (!tempname)
119 return NULL;
120
121 fd = open(tempname, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
122 if (fd != -1)
123 break;
124 if (errno == EEXIST)
125 LOG(llevError, "Created file detected in tempnam_secure. Someone hoping for a race condition?\n");
126 free(tempname);
127 }
128 /* Check that we successfully got an fd. */
129 if (fd == -1)
130 return NULL;
131
132 file = fdopen(fd, "w+");
133 if (!file) {
134 LOG(llevError, "fdopen() failed in tempnam_secure()!\n");
135 free(tempname);
136 return NULL;
137 }
138
139 *filename = tempname;
140 return file;
141 }
142
143 /**
144 * This function removes everything in the directory, and the directory itself.
145 *
146 * Errors are LOG() to error level.
147 *
148 * @param path
149 * directory to remove.
150 *
151 * @note
152 * will fail if any file has a name starting by .
153 */
154 void remove_directory(const char *path) {
155 DIR *dirp;
156 char buf[MAX_BUF];
157 struct stat statbuf;
158 int status;
159
160 if ((dirp = opendir(path)) != NULL) {
161 struct dirent *de;
162
163 for (de = readdir(dirp); de; de = readdir(dirp)) {
164 /* Don't remove '.' or '..' In theory we should do a better
165 * check for .., but the directories we are removing are fairly
166 * limited and should not have dot files in them.
167 */
168 if (de->d_name[0] == '.')
169 continue;
170
171 /* Linux actually has a type field in the dirent structure,
172 * but that is not portable - stat should be portable
173 */
174 status = stat(de->d_name, &statbuf);
175 if ((status != -1) && (S_ISDIR(statbuf.st_mode))) {
176 snprintf(buf, sizeof(buf), "%s/%s", path, de->d_name);
177 remove_directory(buf);
178 continue;
179 }
180 snprintf(buf, sizeof(buf), "%s/%s", path, de->d_name);
181 if (unlink(buf)) {
182 LOG(llevError, "Unable to remove %s\n", path);
183 }
184 }
185 closedir(dirp);
186 }
187 if (rmdir(path)) {
188 LOG(llevError, "Unable to remove directory %s\n", path);
189 }
190 }
191
192 /*****************************************************************************
193 * String related function
194 ****************************************************************************/
195
196 #ifndef HAVE_STRDUP
197 /**
198 * Portable implementation of strdup(3).
199 */
200 char *strdup(const char *str) {
201 char *c = (char *)malloc(strlen(str)+1);
202 if (c != NULL)
203 strcpy(c, str);
204 return c;
205 }
206 #endif
207
208 #ifndef HAVE_STRNCASECMP
209 /**
210 * Case-insensitive comparaison of strings.
211 *
212 * This seems to be lacking on some system.
213 *
214 * @param s1
215 * @param s2
216 * strings to compare.
217 * @param n
218 * maximum number of chars to compare.
219 * @return
220 * @li -1 if s1 is less than s2
221 * @li 0 if s1 equals s2
222 * @li 1 if s1 is greater than s2
223 */
224 int strncasecmp(const char *s1, const char *s2, int n) {
225 register int c1, c2;
226
227 while (*s1 && *s2 && n) {
228 c1 = tolower(*s1);
229 c2 = tolower(*s2);
230 if (c1 != c2)
231 return (c1-c2);
232 s1++;
233 s2++;
234 n--;
235 }
236 if (!n)
237 return (0);
238 return (int)(*s1-*s2);
239 }
240 #endif
241
242 #ifndef HAVE_STRCASECMP
243 /**
244 * Case-insensitive comparaison of strings.
245 *
246 * This seems to be lacking on some system.
247 *
248 * @param s1
249 * @param s2
250 * strings to compare.
251 * @return
252 * @li -1 if s1 is less than s2
253 * @li 0 if s1 equals s2
254 * @li 1 if s1 is greater than s2
255 */
256 int strcasecmp(const char *s1, const char *s2) {
257 register int c1, c2;
258
259 while (*s1 && *s2) {
260 c1 = tolower(*s1);
261 c2 = tolower(*s2);
262 if (c1 != c2)
263 return (c1-c2);
264 s1++;
265 s2++;
266 }
267 if (*s1 == '\0' && *s2 == '\0')
268 return 0;
269 return (int)(*s1-*s2);
270 }
271 #endif
272
273 #ifndef HAVE_STRCASESTR
274 /**
275 * Finds a substring in a string, in a case-insensitive manner.
276 *
277 * @param s
278 * string we're searching into.
279 * @param find
280 * string we're searching for.
281 * @return
282 * pointer to first occurrence of find in s, NULL if not found.
283 */
284 char *strcasestr(const char *s, const char *find) {
285 char c, sc;
286 size_t len;
287
288 if ((c = *find++) != 0) {
289 c = tolower(c);
290 len = strlen(find);
291 do {
292 do {
293 if ((sc = *s++) == 0)
294 return NULL;
295 } while (tolower(sc) != c);
296 } while (strncasecmp(s, find, len) != 0);
297 s--;
298 }
299 return s;
300 }
301 #endif
302
303 /**
304 * Checks if any directories in the given path doesn't exist, and creates if necessary.
305 *
306 * @param filename
307 * file path we'll want to access. Can be NULL.
308 *
309 * @note
310 * will LOG() to debug and error.
311 */
312 void make_path_to_file(const char *filename) {
313 char buf[MAX_BUF], *cp = buf;
314 struct stat statbuf;
315
316 if (!filename || !*filename)
317 return;
318
319 safe_strncpy(buf, filename, sizeof(buf));
320 while ((cp = strchr(cp+1, (int)'/'))) {
321 *cp = '\0';
322 if (stat(buf, &statbuf) || !S_ISDIR(statbuf.st_mode)) {
323 LOG(llevDebug, "Was not dir: %s\n", buf);
324 if (mkdir(buf, SAVE_DIR_MODE)) {
325 LOG(llevError, "Cannot mkdir %s: %s\n", buf, strerror(errno));
326 return;
327 }
328 }
329 *cp = '/';
330 }
331 }
332
333 /**
334 * Simple function we use below to keep adding to the same string
335 * but also make sure we don't overwrite that string.
336 *
337 * @param dest
338 * string to append to.
339 * @param orig
340 * string to append.
341 * @param[out] curlen
342 * current length of dest. Will be updated by this function.
343 * @param maxlen
344 * maximum length of dest buffer.
345 */
346 void safe_strcat(char *dest, const char *orig, size_t *curlen, size_t maxlen) {
347 assert(curlen != NULL);
348 assert(*curlen < maxlen);
349 #ifdef HAVE_STRLCAT
350 *curlen = strlcat(dest, orig, maxlen);
351 #else
352 if (*curlen == (maxlen-1))
353 return;
354 strncpy(dest+*curlen, orig, maxlen-*curlen-1);
355 dest[maxlen-1] = 0;
356 *curlen += strlen(orig);
357 #endif
358 if (*curlen > (maxlen-1))
359 *curlen = maxlen-1;
360 }
361
362 #ifndef HAVE_STRLCPY
363 /**
364 * Portable implementation of strlcpy(3).
365 */
366 size_t strlcpy(char *dst, const char *src, size_t size) {
367 strncpy(dst, src, size - 1);
368 dst[size - 1] = '\0';
369 return strlen(src);
370 }
371 #endif