… | |
… | |
47 | #include <unistd.h> |
47 | #include <unistd.h> |
48 | |
48 | |
49 | /* Has to be after above includes so we don't redefine some values */ |
49 | /* Has to be after above includes so we don't redefine some values */ |
50 | #include "global.h" |
50 | #include "global.h" |
51 | |
51 | |
52 | /* This function removes everything in the directory. */ |
|
|
53 | void |
|
|
54 | remove_directory (const char *path) |
|
|
55 | { |
|
|
56 | 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 | { |
|
|
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 | closedir (dirp); |
|
|
91 | } |
|
|
92 | if (rmdir (path)) |
|
|
93 | { |
|
|
94 | LOG (llevError, "Unable to remove directory %s\n", path); |
|
|
95 | } |
|
|
96 | } |
|
|
97 | |
|
|
98 | #define DIGIT(x) (isdigit(x) ? (x) - '0' : \ |
52 | #define DIGIT(x) (isdigit(x) ? (x) - '0' : \ |
99 | islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') |
53 | islower (x) ? (x) + 10 - 'a' : (x) + 10 - 'A') |
100 | #define MBASE ('z' - 'a' + 1 + 10) |
54 | #define MBASE ('z' - 'a' + 1 + 10) |
101 | |
55 | |
102 | char * |
56 | char * |
… | |
… | |
122 | s--; |
76 | s--; |
123 | } |
77 | } |
124 | return (char *) s; |
78 | return (char *) s; |
125 | } |
79 | } |
126 | |
80 | |
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 | char * |
|
|
132 | ltostr10 (signed long n) |
|
|
133 | { |
|
|
134 | static char buf[12]; /* maximum size is n=-2 billion, i.e. 11 characters+1 |
|
|
135 | character for the trailing nul character */ |
|
|
136 | snprintf (buf, sizeof (buf), "%ld", n); |
|
|
137 | return buf; |
|
|
138 | } |
|
|
139 | |
|
|
140 | char * |
|
|
141 | doubletostr10 (double v) |
|
|
142 | { |
|
|
143 | static char tbuf[200]; |
|
|
144 | |
|
|
145 | sprintf (tbuf, "%f", v); |
|
|
146 | return tbuf; |
|
|
147 | } |
|
|
148 | |
|
|
149 | /** |
81 | /** |
150 | * open_and_uncompress() first searches for the original filename. If it exist, |
82 | * open_and_uncompress() first searches for the original filename. If it exist, |
151 | * then it opens it and returns the file-pointer. |
83 | * then it opens it and returns the file-pointer. |
152 | */ |
84 | */ |
153 | FILE * |
85 | FILE * |
… | |
… | |
165 | close_and_delete (FILE * fp, int compressed) |
97 | close_and_delete (FILE * fp, int compressed) |
166 | { |
98 | { |
167 | fclose (fp); |
99 | fclose (fp); |
168 | } |
100 | } |
169 | |
101 | |
170 | /* |
|
|
171 | * If any directories in the given path doesn't exist, they are created. |
|
|
172 | */ |
|
|
173 | |
|
|
174 | void |
|
|
175 | make_path_to_file (char *filename) |
|
|
176 | { |
|
|
177 | char buf[MAX_BUF], *cp = buf; |
|
|
178 | struct stat statbuf; |
|
|
179 | |
|
|
180 | if (!filename || !*filename) |
|
|
181 | return; |
|
|
182 | |
|
|
183 | assign (buf, filename); |
|
|
184 | |
|
|
185 | while ((cp = strchr (cp + 1, (int) '/'))) |
|
|
186 | { |
|
|
187 | *cp = '\0'; |
|
|
188 | if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) |
|
|
189 | { |
|
|
190 | if (mkdir (buf, SAVE_DIR_MODE)) |
|
|
191 | { |
|
|
192 | LOG (llevError, "Cannot mkdir %s: %s\n", buf, strerror (errno)); |
|
|
193 | return; |
|
|
194 | } |
|
|
195 | } |
|
|
196 | *cp = '/'; |
|
|
197 | } |
|
|
198 | } |
|
|