Apply Nindent to all .c and .h files
[nasm/avx512.git] / rdoff / rdflib.c
blobcd5861cf385bb280e2aedee795f204b75d2de19b
1 /* rdflib - manipulate RDOFF library files (.rdl) */
3 /*
4 * an rdoff library is simply a sequence of RDOFF object files, each
5 * preceded by the name of the module, an ASCII string of up to 255
6 * characters, terminated by a zero.
8 * When a library is being created, special signature block is placed
9 * in the beginning of the file. It is a string 'RDLIB' followed by a
10 * version number, then long content size and a long time stamp.
11 * The module name of the signature block is '.sig'.
14 * There may be an optional directory placed on the end of the file.
15 * The format of the directory will be 'RDLDD' followed by a version
16 * number, followed by the length of the directory, and then the
17 * directory, the format of which has not yet been designed.
18 * The module name of the directory must be '.dir'.
20 * All module names beginning with '.' are reserved for possible future
21 * extensions. The linker ignores all such modules, assuming they have
22 * the format of a six byte type & version identifier followed by long
23 * content size, followed by data.
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <time.h>
33 /* functions supported:
34 * create a library (no extra operands required)
35 * add a module from a library (requires filename and name to give mod.)
36 * replace a module in a library (requires given name and filename)
37 * delete a module from a library (requires given name)
38 * extract a module from the library (requires given name and filename)
39 * list modules
42 const char *usage =
43 "usage:\n"
44 " rdflib x libname [extra operands]\n\n"
45 " where x is one of:\n"
46 " c - create library\n"
47 " a - add module (operands = filename module-name)\n"
48 " x - extract (module-name filename)\n"
49 " r - replace (module-name filename)\n"
50 " d - delete (module-name)\n" " t - list\n";
52 /* Library signature */
53 const char *rdl_signature = "RDLIB2", *sig_modname = ".sig";
55 char **_argv;
57 #define _ENDIANNESS 0 /* 0 for little, 1 for big */
59 static void longtolocal(long *l)
61 #if _ENDIANNESS
62 unsigned char t;
63 unsigned char *p = (unsigned char *)l;
65 t = p[0];
66 p[0] = p[3];
67 p[3] = t;
68 t = p[1];
69 p[1] = p[2];
70 p[2] = p[1];
71 #endif
74 char copybytes(FILE * fp, FILE * fp2, int n)
76 int i, t = 0;
78 for (i = 0; i < n; i++) {
79 t = fgetc(fp);
80 if (t == EOF) {
81 fprintf(stderr, "rdflib: premature end of file in '%s'\n",
82 _argv[2]);
83 exit(1);
85 if (fp2)
86 if (fputc(t, fp2) == EOF) {
87 fprintf(stderr, "rdflib: write error\n");
88 exit(1);
91 return (char)t; /* return last char read */
94 long copylong(FILE * fp, FILE * fp2)
96 long l;
97 int i, t;
98 unsigned char *p = (unsigned char *)&l;
100 for (i = 0; i < 4; i++) { /* skip magic no */
101 t = fgetc(fp);
102 if (t == EOF) {
103 fprintf(stderr, "rdflib: premature end of file in '%s'\n",
104 _argv[2]);
105 exit(1);
107 if (fp2)
108 if (fputc(t, fp2) == EOF) {
109 fprintf(stderr, "rdflib: write error\n");
110 exit(1);
112 *p++ = t;
114 longtolocal(&l);
115 return l;
118 int main(int argc, char **argv)
120 FILE *fp, *fp2 = NULL, *fptmp;
121 char *p, buf[256], c;
122 int i;
123 long l;
124 time_t t;
125 char rdbuf[10];
127 _argv = argv;
129 if (argc < 3 || !strncmp(argv[1], "-h", 2)
130 || !strncmp(argv[1], "--h", 3)) {
131 printf(usage);
132 exit(1);
135 switch (argv[1][0]) {
136 case 'c': /* create library */
137 fp = fopen(argv[2], "wb");
138 if (!fp) {
139 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
140 perror("rdflib");
141 exit(1);
143 fwrite(sig_modname, 1, strlen(sig_modname) + 1, fp);
144 fwrite(rdl_signature, 1, strlen(rdl_signature), fp);
145 l = sizeof(t = time(NULL));
146 fwrite(&l, sizeof(l), 1, fp);
147 fwrite(&t, 1, l, fp);
148 fclose(fp);
149 break;
151 case 'a': /* add module */
152 if (argc < 5) {
153 fprintf(stderr, "rdflib: required parameter missing\n");
154 exit(1);
156 fp = fopen(argv[2], "ab");
157 if (!fp) {
158 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
159 perror("rdflib");
160 exit(1);
163 fp2 = fopen(argv[3], "rb");
164 if (!fp2) {
165 fprintf(stderr, "rdflib: could not open '%s'\n", argv[3]);
166 perror("rdflib");
167 exit(1);
170 p = argv[4];
171 do {
172 if (fputc(*p, fp) == EOF) {
173 fprintf(stderr, "rdflib: write error\n");
174 exit(1);
176 } while (*p++);
178 while (!feof(fp2)) {
179 i = fgetc(fp2);
180 if (i == EOF) {
181 break;
184 if (fputc(i, fp) == EOF) {
185 fprintf(stderr, "rdflib: write error\n");
186 exit(1);
189 fclose(fp2);
190 fclose(fp);
191 break;
193 case 'x':
194 if (argc < 5) {
195 fprintf(stderr, "rdflib: required parameter missing\n");
196 exit(1);
198 case 't':
199 fp = fopen(argv[2], "rb");
200 if (!fp) {
201 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
202 perror("rdflib");
203 exit(1);
206 fp2 = NULL;
207 while (!feof(fp)) {
208 /* read name */
209 p = buf;
210 while ((*(p++) = (char)fgetc(fp)))
211 if (feof(fp))
212 break;
214 if (feof(fp))
215 break;
217 fp2 = NULL;
218 if (argv[1][0] == 'x') {
219 /* check against desired name */
220 if (!strcmp(buf, argv[3])) {
221 fp2 = fopen(argv[4], "wb");
222 if (!fp2) {
223 fprintf(stderr, "rdflib: could not open '%s'\n",
224 argv[4]);
225 perror("rdflib");
226 exit(1);
229 } else
230 printf("%-40s ", buf);
232 /* step over the RDOFF file, extracting type information for
233 * the listing, and copying it if fp2 != NULL */
235 if (buf[0] == '.') {
237 if (argv[1][0] == 't')
238 for (i = 0; i < 6; i++)
239 printf("%c", copybytes(fp, fp2, 1));
240 else
241 copybytes(fp, fp2, 6);
243 l = copylong(fp, fp2);
245 if (argv[1][0] == 't')
246 printf(" %ld bytes content\n", l);
248 copybytes(fp, fp2, l);
249 } else if ((c = copybytes(fp, fp2, 6)) >= '2') { /* version 2 or above */
250 l = copylong(fp, fp2);
252 if (argv[1][0] == 't')
253 printf("RDOFF%c %ld bytes content\n", c, l);
254 copybytes(fp, fp2, l); /* entire object */
255 } else {
256 if (argv[1][0] == 't')
257 printf("RDOFF1\n");
259 * version 1 object, so we don't have an object content
260 * length field.
262 copybytes(fp, fp2, copylong(fp, fp2)); /* header */
263 copybytes(fp, fp2, copylong(fp, fp2)); /* text */
264 copybytes(fp, fp2, copylong(fp, fp2)); /* data */
267 if (fp2)
268 break;
270 fclose(fp);
271 if (fp2)
272 fclose(fp2);
273 else if (argv[1][0] == 'x') {
274 fprintf(stderr, "rdflib: module '%s' not found in '%s'\n",
275 argv[3], argv[2]);
276 exit(1);
278 break;
280 case 'r': /* replace module */
281 argc--;
282 case 'd': /* delete module */
283 if (argc < 4) {
284 fprintf(stderr, "rdflib: required parameter missing\n");
285 exit(1);
288 fp = fopen(argv[2], "rb");
289 if (!fp) {
290 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
291 perror("rdflib");
292 exit(1);
295 if (argv[1][0] == 'r') {
296 fp2 = fopen(argv[4], "rb");
297 if (!fp2) {
298 fprintf(stderr, "rdflib: could not open '%s'\n", argv[4]);
299 perror("rdflib");
300 exit(1);
304 fptmp = tmpfile();
305 if (!fptmp) {
306 fprintf(stderr, "rdflib: could not open temporary file\n");
307 perror("rdflib");
308 exit(1);
311 /* copy library into temporary file */
312 fseek(fp, 0, SEEK_END); /* get file length */
313 l = ftell(fp);
314 fseek(fp, 0, SEEK_SET);
315 copybytes(fp, fptmp, l);
316 rewind(fptmp);
317 freopen(argv[2], "wb", fp);
319 while (!feof(fptmp)) {
320 /* read name */
321 p = buf;
322 while ((*(p++) = (char)fgetc(fptmp)))
323 if (feof(fptmp))
324 break;
326 if (feof(fptmp))
327 break;
329 /* check against desired name */
330 if (!strcmp(buf, argv[3])) {
331 fread(p = rdbuf, 1, sizeof(rdbuf), fptmp);
332 l = *(long *)(p + 6);
333 fseek(fptmp, l, SEEK_CUR);
334 break;
335 } else {
336 fwrite(buf, 1, strlen(buf) + 1, fp); /* module name */
337 if ((c = copybytes(fptmp, fp, 6)) >= '2') {
338 l = copylong(fptmp, fp); /* version 2 or above */
339 copybytes(fptmp, fp, l); /* entire object */
344 if (argv[1][0] == 'r') {
345 /* copy new module into library */
346 p = argv[3];
347 do {
348 if (fputc(*p, fp) == EOF) {
349 fprintf(stderr, "rdflib: write error\n");
350 exit(1);
352 } while (*p++);
354 while (!feof(fp2)) {
355 i = fgetc(fp2);
356 if (i == EOF) {
357 break;
359 if (fputc(i, fp) == EOF) {
360 fprintf(stderr, "rdflib: write error\n");
361 exit(1);
364 fclose(fp2);
367 /* copy rest of library if any */
368 while (!feof(fptmp)) {
369 i = fgetc(fptmp);
370 if (i == EOF) {
371 break;
374 if (fputc(i, fp) == EOF) {
375 fprintf(stderr, "rdflib: write error\n");
376 exit(1);
380 fclose(fp);
381 fclose(fptmp);
382 break;
384 default:
385 fprintf(stderr, "rdflib: command '%c' not recognized\n",
386 argv[1][0]);
387 exit(1);
389 return 0;