"Fossies" - the Fresh Open Source Software Archive 
Member "unipkg-0.6.5/common.c" (16 Dec 2005, 6997 Bytes) of package /linux/privat/old/unipkg-0.6.5.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /******************************************************************************\
2 * *
3 * UNIPKG (c) iSteve <isteve@bofh.cz>, 2005 *
4 * *
5 * Universal PacKaGer. *
6 * Licensed under GNU/GPL - if you don't like the software, fix it! *
7 * *
8 \******************************************************************************/
9
10 #define _GNU_SOURCE
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <fnmatch.h>
17 #include <errno.h>
18 #include <dlfcn.h>
19 #include <libgen.h>
20 #include <dirent.h>
21
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <sys/types.h>
25
26 #include "common.h"
27
28 // CISDIGIT()
29 //
30 // Performs number comparsion ignoring locales
31 //
32 // Parameters:
33 // character
34 //
35 // Return value:
36 // 1 if is digit
37 // 0 if is not digit
38 static int cisdigit(char ch) {
39 return ((ch >= '0') && (ch <= '9'));
40 }
41
42 // FREEPINFO()
43 //
44 // Frees all members of pinfo structure, including filenames.
45 //
46 // Parameters:
47 // pointer to the pkginfo structure
48 //
49 // Return value:
50 // none
51 void freepinfo(pkginfo *pinfo) {
52 unsigned long int i;
53 free(pinfo->name);
54 free(pinfo->version);
55 free(pinfo->description);
56
57 free(pinfo->preinst.data);
58 for (i = 0; i < A_COUNT; i++) {
59 free(pinfo->preinst.parameters[i]);
60 }
61
62 free(pinfo->postinst.data);
63 for (i = 0; i < A_COUNT; i++) {
64 free(pinfo->postinst.parameters[i]);
65 }
66
67 free(pinfo->prerm.data);
68 for (i = 0; i < A_COUNT; i++) {
69 free(pinfo->prerm.parameters[i]);
70 }
71
72 free(pinfo->postrm.data);
73 for (i = 0; i < A_COUNT; i++) {
74 free(pinfo->postrm.parameters[i]);
75 }
76
77 for (i=0; i<pinfo->filecount; i++) {
78
79 free(pinfo->files[i].name);
80 }
81 free(pinfo->files);
82 }
83
84 // CLEARPINFO()
85 //
86 // Sets the values of pkginfo to NULL.
87 //
88 // Parameters:
89 // pointer to the pkginfo structure
90 //
91 // Return value:
92 // none
93 void clearpinfo(pkginfo *pinfo) {
94 int i;
95
96 pinfo->name = NULL;
97 pinfo->version = NULL;
98 pinfo->description = NULL;
99
100 pinfo->preinst.len = 0;
101 pinfo->preinst.data = NULL;
102 for (i = 0; i < A_COUNT; i++) {
103 pinfo->preinst.parameters[i] = NULL;
104 }
105
106 pinfo->postinst.len = 0;
107 pinfo->postinst.data = NULL;
108 for (i = 0; i < A_COUNT; i++) {
109 pinfo->postinst.parameters[i] = NULL;
110 }
111
112 pinfo->prerm.len = 0;
113 pinfo->prerm.data = NULL;
114 for (i = 0; i < A_COUNT; i++) {
115 pinfo->prerm.parameters[i] = NULL;
116 }
117
118 pinfo->postrm.len = 0;
119 pinfo->postrm.data = NULL;
120 for (i = 0; i < A_COUNT; i++) {
121 pinfo->postrm.parameters[i] = NULL;
122 }
123
124 pinfo->files=NULL;
125 pinfo->filecount = 0;
126
127 pinfo->pkgsize = 0;
128 }
129
130 // ABSOLUTIZESTR()
131 //
132 // Turns the string into absolute path, not relevant to current filesystem.
133 //
134 // Parameters:
135 // string, path which should be absolutized
136 // string prefixing the path in first parameter
137 //
138 // Return value:
139 // pointer to malloced string which contains the absolutized path
140 char *absolutizestr(char *toabs, char *dirprefix) {
141 char *path, *prefix, *ptr;
142
143 prefix = malloc(strlen(dirprefix) + 1);
144 prefix = strcpy(prefix, dirprefix);
145
146 if (prefix[strlen(prefix)-1] == '/') { prefix[strlen(prefix)-1] = 0x0; }
147 ptr = toabs;
148 if ((*ptr == '.') && (*(ptr+1) == '/')) { ptr+=2; } // It starts with './', which is to be skipped
149 while (*ptr == '/') { ptr++; } // Skip heading slashes*/
150
151 path = malloc(strlen(ptr) + strlen(prefix) + 2);
152
153 strcpy(path, prefix);
154 strcat(path, "/");
155 strcat(path, ptr);
156
157 free(prefix);
158 return path;
159 }
160
161 // VERSIONCMP()
162 //
163 // Compares two versions of software.
164 //
165 // Parameters:
166 // string, first version
167 // string, second version
168 //
169 // Return value:
170 // -1 if val version < ref version
171 // 0 if val version == ref version
172 // 1 if val version >= ref version
173 int versioncmp(const char *val, const char *ref) {
174 int rv1, rv2;
175
176 if (!val) val="";
177 if (!ref) ref="";
178
179 while (*val || *ref) {
180 if (!*val) { while (*ref == '0') ref++; }
181 if (!*ref) { while (*val == '0') val++; }
182
183 if (cisdigit(*val)) {
184 rv1 = *val - 48;
185 } else {
186 rv1 = *val;
187 }
188
189 if (cisdigit(*ref)) {
190 rv2 = *ref - 48;
191 } else {
192 rv2 = *ref;
193 }
194
195 if (rv1 != rv2) {
196 return (rv1 > rv2) ? 1 : -1;
197 }
198
199 val++;
200 ref++;
201 }
202 return 0;
203 }
204
205 // NEEDLELIZE()
206 //
207 // Turns a string into a glob needle. If it starts with '^', '*' is not
208 // prepended. If it ends with '$', '*' is not appended.
209 //
210 // Parameters:
211 // string, first version
212 // string, second version
213 //
214 // Return value:
215 // -1 if val version < ref version
216 // 0 if val version == ref version
217 // 1 if val version >= ref version
218 char *needlelize(char *input) {
219 size_t input_len;
220 char *reval;
221
222 input_len = strlen(input);
223
224 reval = malloc(input_len + ((input[0] == '^') ? 0 : 1) + ((input[input_len - 1] == '$') ? 0 : 1) + 1);
225
226 reval[0] = '\0';
227 if (input[0] != '^') {
228 strcat(reval, "*");
229 } else {
230 input++;
231 input_len--;
232 }
233
234 if (input[input_len - 1] != '$') {
235 strcat(reval, input);
236 strcat(reval, "*");
237 } else {
238 input_len--;
239 strncat(reval, input, input_len);
240 }
241
242 return reval;
243 }
244
245 // ADDFILE()
246 //
247 // Adds a file to the pkginfo structure.
248 //
249 // Parameters:
250 // pointer to pkginfo structure
251 // filename to be added
252 // flag of the file
253 //
254 // Return value:
255 // 1 if is digit
256 // 0 if is not digit
257 void addfile(pkginfo *pinfo, char *filename, char flag) {
258 char *tmp;
259 unsigned long len;
260
261 if (flag == F_DIRECTORY) {
262 len = strlen(filename);
263
264 // So dir always trails with '/'
265 if (filename[len-1] != '/') {
266 tmp = malloc(len + 2);
267 memcpy(tmp, filename, len);
268 tmp[len] = '/';
269 tmp[len+1] = '\0';
270 free(filename);
271 filename = tmp;
272 }
273 }
274 pinfo->filecount++;
275 pinfo->files = realloc(pinfo->files, sizeof(fileinfo) * pinfo->filecount);
276 pinfo->files[pinfo->filecount-1].name = filename;
277 pinfo->files[pinfo->filecount-1].flag = flag;
278 }
279
280 // FISDIR()
281 //
282 // Checks if a filename suggests the file is a dir.
283 //
284 // Parameters:
285 // pointer to pkginfo structure
286 // filename to be added
287 // flag of the file
288 //
289 // Return value:
290 // 1 if dir
291 // 0 if not
292 int fisdir(char *filename) {
293 return (filename[strlen(filename)-1] == '/');
294 }
295
296 // PSTRNSTR()
297 //
298 // Private strstr with limit to length.
299 //
300 // Parameters:
301 // haystack
302 // needle
303 // max. size of haystack
304 // max. size of the needle -- if 0, use strlen
305 //
306 // Return value:
307 // NULL if no match
308 // otherwise ptr to match
309 char *pstrnstr(char *haystack, char *needle, off_t len, off_t needlen) {
310 off_t i, counter;
311
312 if (needlen == 0) {
313 needlen = strlen(needle);
314 }
315 counter = 0;
316 for (i = 0; i < len; i++) {
317 if (haystack[i] == needle[counter]) {
318 counter++;
319 if (counter == needlen) {
320 return &haystack[i - counter + 1];
321 }
322 } else {
323 counter = 0;
324 if (haystack[i] == '\0') {
325 break;
326 }
327 }
328 }
329
330 return NULL;
331 }