ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/porting.C
Revision: 1.20
Committed: Tue May 6 16:55:25 2008 UTC (16 years ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_6, rel-2_7, rel-2_71, rel-2_54, rel-2_55, rel-2_56, rel-2_61
Changes since 1.19: +1 -1 lines
Log Message:
update copyright

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.19 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 pippijn 1.13 *
4 root 1.20 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.17 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992,2007 Frank Tore Johansen
7 pippijn 1.13 *
8 root 1.19 * Deliantra is free software: you can redistribute it and/or modify
9 root 1.18 * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation, either version 3 of the License, or
11     * (at your option) any later version.
12 pippijn 1.13 *
13 root 1.18 * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     * GNU General Public License for more details.
17 pippijn 1.13 *
18 root 1.18 * You should have received a copy of the GNU General Public License
19     * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 root 1.17 *
21 root 1.19 * The authors can be reached via e-mail to <support@deliantra.net>
22 pippijn 1.13 */
23 elmex 1.1
24     /* This file contains various functions that are not really unique for
25     * crossfire, but rather provides what should be standard functions
26     * for systems that do not have them. In this way, most of the
27     * nasty system dependent stuff is contained here, with the program
28     * calling these functions.
29     */
30    
31    
32 root 1.10 /* Need to pull in the HAVE_... values somehow */
33 elmex 1.1
34 root 1.10 #include <autoconf.h>
35 elmex 1.1
36 root 1.10 #include <cstdio>
37     #include <cstdlib>
38     #include <cstdarg>
39 root 1.5
40 root 1.10 #include <cctype>
41 elmex 1.1
42 root 1.10 #include <sys/stat.h>
43     #include <sys/wait.h>
44 elmex 1.1
45 root 1.10 #include <sys/param.h>
46 elmex 1.1
47 root 1.10 #include <unistd.h>
48 root 1.5
49 elmex 1.1 /* Has to be after above includes so we don't redefine some values */
50     #include "global.h"
51    
52     /* This function removes everything in the directory. */
53 root 1.4 void
54     remove_directory (const char *path)
55 elmex 1.1 {
56 root 1.4 DIR *dirp;
57     char buf[MAX_BUF];
58     struct stat statbuf;
59     int status;
60    
61     if ((dirp = opendir (path)) != NULL)
62     {
63     struct dirent *de;
64    
65     for (de = readdir (dirp); de; de = readdir (dirp))
66 root 1.5 {
67     /* Don't remove '.' or '..' In theory we should do a better
68     * check for .., but the directories we are removing are fairly
69     * limited and should not have dot files in them.
70     */
71     if (de->d_name[0] == '.')
72     continue;
73    
74     /* Linux actually has a type field in the dirent structure,
75     * but that is not portable - stat should be portable
76     */
77     status = stat (de->d_name, &statbuf);
78     if ((status != -1) && (S_ISDIR (statbuf.st_mode)))
79     {
80     sprintf (buf, "%s/%s", path, de->d_name);
81     remove_directory (buf);
82     continue;
83     }
84     sprintf (buf, "%s/%s", path, de->d_name);
85     if (unlink (buf))
86     {
87     LOG (llevError, "Unable to remove directory %s\n", path);
88     }
89     }
90 root 1.4 closedir (dirp);
91     }
92     if (rmdir (path))
93     {
94     LOG (llevError, "Unable to remove directory %s\n", path);
95 elmex 1.1 }
96     }
97    
98     #define DIGIT(x) (isdigit(x) ? (x) - '0' : \
99     islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
100     #define MBASE ('z' - 'a' + 1 + 10)
101    
102 root 1.4 char *
103     strcasestr_local (const char *s, const char *find)
104 elmex 1.1 {
105 root 1.4 char c, sc;
106     size_t len;
107 elmex 1.1
108 root 1.4 if ((c = *find++) != 0)
109     {
110     c = tolower (c);
111     len = strlen (find);
112     do
113 root 1.5 {
114     do
115     {
116     if ((sc = *s++) == 0)
117     return NULL;
118     }
119     while (tolower (sc) != c);
120     }
121 root 1.4 while (strncasecmp (s, find, len) != 0);
122     s--;
123     }
124     return (char *) s;
125 elmex 1.1 }
126    
127     /*
128     * returns a char-pointer to a static array, in which a representation
129     * of the decimal number given will be stored.
130     */
131 root 1.4 char *
132     ltostr10 (signed long n)
133     {
134 root 1.5 static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1
135     character for the trailing nul character */
136 root 1.4 snprintf (buf, sizeof (buf), "%ld", n);
137 elmex 1.1 return buf;
138     }
139 root 1.4
140     char *
141     doubletostr10 (double v)
142     {
143 elmex 1.1 static char tbuf[200];
144 root 1.5
145 root 1.4 sprintf (tbuf, "%f", v);
146 elmex 1.1 return tbuf;
147     }
148    
149     /**
150     * open_and_uncompress() first searches for the original filename. If it exist,
151     * then it opens it and returns the file-pointer.
152     */
153 root 1.4 FILE *
154     open_and_uncompress (const char *name, int flag, int *compressed)
155     {
156     *compressed = 0;
157     return fopen (name, "r");
158 elmex 1.1 }
159    
160     /*
161     * See open_and_uncompress().
162     */
163    
164 root 1.4 void
165 root 1.5 close_and_delete (FILE * fp, int compressed)
166 root 1.4 {
167     fclose (fp);
168 elmex 1.1 }
169    
170     /*
171     * If any directories in the given path doesn't exist, they are created.
172     */
173    
174 root 1.4 void
175     make_path_to_file (char *filename)
176 elmex 1.1 {
177 root 1.4 char buf[MAX_BUF], *cp = buf;
178     struct stat statbuf;
179 elmex 1.1
180 root 1.4 if (!filename || !*filename)
181     return;
182 root 1.14
183     assign (buf, filename);
184 root 1.4
185     while ((cp = strchr (cp + 1, (int) '/')))
186     {
187     *cp = '\0';
188     if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode))
189 root 1.5 {
190     if (mkdir (buf, SAVE_DIR_MODE))
191     {
192     LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno));
193     return;
194     }
195     }
196 root 1.4 *cp = '/';
197 elmex 1.1 }
198     }