NASM 0.98.26
[nasm/avx512.git] / rdoff / rdflib.c
blobc8bab74bd8aabc33a202d105bf972dfbe6c3644d
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 * There may be an optional directory placed on the end of the file.
9 * The format of the directory will be 'RDLDD' followed by a version
10 * number, followed by the length of the directory, and then the
11 * directory, the format of which has not yet been designed.
12 * The module name of the directory must be '.dir'.
14 * All module names beginning with '.' are reserved
15 * for possible future extensions. The linker ignores all such modules,
16 * assuming they have the format of a six byte type & version identifier
17 * followed by long content size, followed by data.
20 #include <stdio.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <unistd.h>
25 /* functions supported:
26 * create a library (no extra operands required)
27 * add a module from a library (requires filename and name to give mod.)
28 * replace a module in a library (requires given name and filename)
29 * delete a module from a library (requires given name)
30 * extract a module from the library (requires given name and filename)
31 * list modules
34 const char *usage =
35 "usage:\n"
36 " rdflib x libname [extra operands]\n\n"
37 " where x is one of:\n"
38 " c - create library\n"
39 " a - add module (operands = filename module-name)\n"
40 " x - extract (module-name filename)\n"
41 " r - replace (module-name filename)\n"
42 " d - delete (module-name)\n"
43 " t - list\n";
45 char **_argv;
47 #define _ENDIANNESS 0 /* 0 for little, 1 for big */
49 static void longtolocal(long * l)
51 #if _ENDIANNESS
52 unsigned char t;
53 unsigned char * p = (unsigned char *) l;
55 t = p[0];
56 p[0] = p[3];
57 p[3] = t;
58 t = p[1];
59 p[1] = p[2];
60 p[2] = p[1];
61 #endif
64 char copybytes(FILE *fp, FILE *fp2, int n)
66 int i, t = 0;
68 for (i = 0 ; i < n; i++ )
70 t = fgetc(fp);
71 if (t == EOF)
73 fprintf(stderr,"rdflib: premature end of file in '%s'\n",
74 _argv[2]);
75 exit(1);
77 if (fp2)
78 if (fputc(t, fp2) == EOF)
80 fprintf(stderr,"rdflib: write error\n");
81 exit(1);
84 return (char) t; /* return last char read */
87 long copylong(FILE *fp, FILE *fp2)
89 long l;
90 int i,t;
91 unsigned char * p = (unsigned char *) &l;
94 for (i = 0 ; i < 4; i++ ) /* skip magic no */
96 t = fgetc(fp);
97 if (t == EOF)
99 fprintf(stderr,"rdflib: premature end of file in '%s'\n",
100 _argv[2]);
101 exit(1);
103 if (fp2)
104 if (fputc(t, fp2) == EOF)
106 fprintf(stderr,"rdflib: write error\n");
107 exit(1);
109 *p++ = t;
111 longtolocal (&l);
112 return l;
115 int main(int argc, char **argv)
117 FILE *fp, *fp2, *fptmp;
118 char *p, buf[256], c;
119 int i;
120 long l;
121 char tmptempl[L_tmpnam], rdbuf[10];
123 _argv = argv;
125 if (argc < 3 || !strncmp(argv[1],"-h",2) || !strncmp(argv[1],"--h",3))
127 printf(usage);
128 exit(1);
131 switch(argv[1][0])
133 case 'c': /* create library */
134 fp = fopen(argv[2],"wb");
135 if (! fp) {
136 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
137 perror("rdflib");
138 exit(1);
140 fclose(fp);
141 break;
143 case 'a': /* add module */
144 if (argc < 5) {
145 fprintf(stderr,"rdflib: required parameter missing\n");
146 exit(1);
148 fp = fopen(argv[2],"ab");
149 if (! fp)
151 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
152 perror("rdflib");
153 exit(1);
156 fp2 = fopen(argv[3],"rb");
157 if (! fp2)
159 fprintf(stderr,"rdflib: could not open '%s'\n",argv[3]);
160 perror("rdflib");
161 exit(1);
164 p = argv[4];
165 do {
166 if ( fputc(*p,fp) == EOF ) {
167 fprintf(stderr,"rdflib: write error\n");
168 exit(1);
170 } while (*p++);
172 while (! feof (fp2) ) {
173 i = fgetc (fp2);
174 if (i == EOF) {
175 break;
178 if ( fputc(i, fp) == EOF ) {
179 fprintf(stderr,"rdflib: write error\n");
180 exit(1);
183 fclose(fp2);
184 fclose(fp);
185 break;
187 case 'x':
188 if (argc < 5) {
189 fprintf(stderr, "rdflib: required paramenter missing\n");
190 exit(1);
192 case 't':
193 fp = fopen(argv[2],"rb");
194 if (! fp)
196 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
197 perror("rdflib");
198 exit(1);
201 fp2 = NULL;
202 while (! feof(fp) ) {
203 /* read name */
204 p = buf;
205 while( ( *(p++) = (char) fgetc(fp) ) )
206 if (feof(fp)) break;
208 if (feof(fp)) break;
210 fp2 = NULL;
211 if (argv[1][0] == 'x') {
212 /* check against desired name */
213 if (! strcmp(buf,argv[3]) )
215 fp2 = fopen(argv[4],"wb");
216 if (! fp2)
218 fprintf(stderr,"rdflib: could not open '%s'\n",argv[4]);
219 perror("rdflib");
220 exit(1);
224 else
225 printf("%-40s ", buf);
227 /* step over the RDOFF file, extracting type information for
228 * the listing, and copying it if fp2 != NULL */
230 if (buf[0] == '.') {
232 if (argv[1][0] == 't')
233 for (i = 0; i < 6; i++)
234 printf("%c", copybytes(fp,fp2,1));
235 else
236 copybytes(fp,fp2,6);
238 l = copylong(fp,fp2);
240 if (argv[1][0] == 't') printf(" %ld bytes content\n", l);
242 copybytes(fp,fp2,l);
244 else if ((c=copybytes(fp,fp2,6)) >= '2') /* version 2 or above */
246 l = copylong(fp,fp2);
248 if (argv[1][0] == 't')
249 printf("RDOFF%c %ld bytes content\n", c, l);
250 copybytes(fp,fp2, l); /* entire object */
252 else
254 if (argv[1][0] == 't')
255 printf("RDOFF1\n");
257 * version 1 object, so we don't have an object content
258 * length field.
260 copybytes(fp,fp2, copylong(fp,fp2)); /* header */
261 copybytes(fp,fp2, copylong(fp,fp2)); /* text */
262 copybytes(fp,fp2, copylong(fp,fp2)); /* data */
265 if (fp2)
266 break;
268 fclose(fp);
269 if (fp2)
270 fclose(fp2);
271 else if (argv[1][0] == 'x')
273 fprintf(stderr,"rdflib: module '%s' not found in '%s'\n",
274 argv[3],argv[2]);
275 exit(1);
277 break;
279 case 'r': /* replace module */
280 argc--;
281 case 'd': /* delete module */
282 if (argc < 4) {
283 fprintf(stderr, "rdflib: required paramenter missing\n");
284 exit(1);
287 fp = fopen(argv[2],"rb");
288 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)
299 fprintf(stderr, "rdflib: could not open '%s'\n", argv[4]);
300 perror("rdflib");
301 exit(1);
305 tmpnam(tmptempl);
306 fptmp = fopen(tmptempl,"wb");
307 if (! fptmp)
309 fprintf(stderr,"rdflib: could not open temporary file\n");
310 perror("rdflib");
311 exit(1);
314 /* copy library into temporary file */
315 fseek(fp, 0, SEEK_END); /* get file length */
316 l = ftell(fp);
317 fseek(fp, 0, SEEK_SET);
318 copybytes(fp, fptmp, l);
319 freopen(tmptempl, "rb", fptmp); /* reopen files */
320 freopen(argv[2], "wb", fp);
322 while (! feof(fptmp) ) {
323 /* read name */
324 p = buf;
325 while( ( *(p++) = (char) fgetc(fptmp) ) )
326 if (feof(fptmp)) break;
328 if (feof(fptmp)) break;
330 /* check against desired name */
331 if (! strcmp(buf, argv[3]) ) {
332 fread(p=rdbuf, 1, sizeof(rdbuf), fptmp);
333 l = *(long*)(p+6);
334 fseek(fptmp, l, SEEK_CUR);
335 break;
336 } else {
337 fwrite(buf, 1, strlen(buf)+1, fp); /* module name */
338 if ((c=copybytes(fptmp, fp, 6)) >= '2') {
339 l = copylong(fptmp, fp); /* version 2 or above */
340 copybytes(fptmp, fp, l); /* entire object */
345 if (argv[1][0] == 'r') {
346 /* copy new module into library */
347 p = argv[3];
348 do {
349 if ( fputc(*p, fp) == EOF ) {
350 fprintf(stderr, "rdflib: write error\n");
351 exit(1);
353 } while (*p++);
355 while (! feof (fp2) ) {
356 i = fgetc (fp2);
357 if (i == EOF) {
358 break;
360 if ( fputc(i, fp) == EOF ) {
361 fprintf(stderr, "rdflib: write error\n");
362 exit(1);
365 fclose(fp2);
368 /* copy rest of library if any */
369 while (! feof (fptmp) ) {
370 i = fgetc (fptmp);
371 if (i == EOF) {
372 break;
375 if ( fputc(i, fp) == EOF ) {
376 fprintf(stderr,"rdflib: write error\n");
377 exit(1);
381 fclose(fp);
382 fclose(fptmp);
383 unlink(tmptempl);
384 break;
386 default:
387 fprintf(stderr,"rdflib: command '%c' not recognized\n",
388 argv[1][0]);
389 exit(1);
391 return 0;