ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/porting.C
Revision: 1.16
Committed: Thu May 17 21:32:08 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_1
Changes since 1.15: +1 -1 lines
Log Message:
- prepare common/ for head_ => head change
- add some copyrights for files where they were missing

File Contents

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