RAA fix from theowl
[nasm.git] / rdoff / rdflib.c
blobedf4a22835f832cf479176e34021af04e023b219
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 <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <unistd.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"
51 " t - list\n";
53 /* Library signature */
54 const char *rdl_signature = "RDLIB2", *sig_modname = ".sig";
56 char **_argv;
58 #define _ENDIANNESS 0 /* 0 for little, 1 for big */
60 static void longtolocal(long * l)
62 #if _ENDIANNESS
63 unsigned char t;
64 unsigned char * p = (unsigned char *) l;
66 t = p[0];
67 p[0] = p[3];
68 p[3] = t;
69 t = p[1];
70 p[1] = p[2];
71 p[2] = p[1];
72 #endif
75 char copybytes(FILE *fp, FILE *fp2, int n)
77 int i, t = 0;
79 for (i = 0 ; i < n; i++ )
81 t = fgetc(fp);
82 if (t == EOF)
84 fprintf(stderr,"rdflib: premature end of file in '%s'\n",
85 _argv[2]);
86 exit(1);
88 if (fp2)
89 if (fputc(t, fp2) == EOF)
91 fprintf(stderr,"rdflib: write error\n");
92 exit(1);
95 return (char) t; /* return last char read */
98 long copylong(FILE *fp, FILE *fp2)
100 long l;
101 int i,t;
102 unsigned char * p = (unsigned char *) &l;
105 for (i = 0 ; i < 4; i++ ) /* skip magic no */
107 t = fgetc(fp);
108 if (t == EOF)
110 fprintf(stderr,"rdflib: premature end of file in '%s'\n",
111 _argv[2]);
112 exit(1);
114 if (fp2)
115 if (fputc(t, fp2) == EOF)
117 fprintf(stderr,"rdflib: write error\n");
118 exit(1);
120 *p++ = t;
122 longtolocal (&l);
123 return l;
126 int main(int argc, char **argv)
128 FILE *fp, *fp2 = NULL, *fptmp;
129 char *p, buf[256], c;
130 int i;
131 long l;
132 time_t t;
133 char rdbuf[10];
135 _argv = argv;
137 if (argc < 3 || !strncmp(argv[1],"-h",2) || !strncmp(argv[1],"--h",3))
139 printf(usage);
140 exit(1);
143 switch(argv[1][0])
145 case 'c': /* create library */
146 fp = fopen(argv[2],"wb");
147 if (! fp) {
148 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
149 perror("rdflib");
150 exit(1);
152 fwrite(sig_modname, 1, strlen(sig_modname)+1, fp);
153 fwrite(rdl_signature, 1, strlen(rdl_signature), fp);
154 l = sizeof(t = time(NULL));
155 fwrite(&l, sizeof(l), 1, fp);
156 fwrite(&t, 1, l, fp);
157 fclose(fp);
158 break;
160 case 'a': /* add module */
161 if (argc < 5) {
162 fprintf(stderr,"rdflib: required parameter missing\n");
163 exit(1);
165 fp = fopen(argv[2],"ab");
166 if (! fp)
168 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
169 perror("rdflib");
170 exit(1);
173 fp2 = fopen(argv[3],"rb");
174 if (! fp2)
176 fprintf(stderr,"rdflib: could not open '%s'\n",argv[3]);
177 perror("rdflib");
178 exit(1);
181 p = argv[4];
182 do {
183 if ( fputc(*p,fp) == EOF ) {
184 fprintf(stderr,"rdflib: write error\n");
185 exit(1);
187 } while (*p++);
189 while (! feof (fp2) ) {
190 i = fgetc (fp2);
191 if (i == EOF) {
192 break;
195 if ( fputc(i, fp) == EOF ) {
196 fprintf(stderr,"rdflib: write error\n");
197 exit(1);
200 fclose(fp2);
201 fclose(fp);
202 break;
204 case 'x':
205 if (argc < 5) {
206 fprintf(stderr, "rdflib: required paramenter missing\n");
207 exit(1);
209 case 't':
210 fp = fopen(argv[2],"rb");
211 if (! fp)
213 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
214 perror("rdflib");
215 exit(1);
218 fp2 = NULL;
219 while (! feof(fp) ) {
220 /* read name */
221 p = buf;
222 while( ( *(p++) = (char) fgetc(fp) ) )
223 if (feof(fp)) break;
225 if (feof(fp)) break;
227 fp2 = NULL;
228 if (argv[1][0] == 'x') {
229 /* check against desired name */
230 if (! strcmp(buf,argv[3]) )
232 fp2 = fopen(argv[4],"wb");
233 if (! fp2)
235 fprintf(stderr,"rdflib: could not open '%s'\n",argv[4]);
236 perror("rdflib");
237 exit(1);
241 else
242 printf("%-40s ", buf);
244 /* step over the RDOFF file, extracting type information for
245 * the listing, and copying it if fp2 != NULL */
247 if (buf[0] == '.') {
249 if (argv[1][0] == 't')
250 for (i = 0; i < 6; i++)
251 printf("%c", copybytes(fp,fp2,1));
252 else
253 copybytes(fp,fp2,6);
255 l = copylong(fp,fp2);
257 if (argv[1][0] == 't') printf(" %ld bytes content\n", l);
259 copybytes(fp,fp2,l);
261 else if ((c=copybytes(fp,fp2,6)) >= '2') /* version 2 or above */
263 l = copylong(fp,fp2);
265 if (argv[1][0] == 't')
266 printf("RDOFF%c %ld bytes content\n", c, l);
267 copybytes(fp,fp2, l); /* entire object */
269 else
271 if (argv[1][0] == 't')
272 printf("RDOFF1\n");
274 * version 1 object, so we don't have an object content
275 * length field.
277 copybytes(fp,fp2, copylong(fp,fp2)); /* header */
278 copybytes(fp,fp2, copylong(fp,fp2)); /* text */
279 copybytes(fp,fp2, copylong(fp,fp2)); /* data */
282 if (fp2)
283 break;
285 fclose(fp);
286 if (fp2)
287 fclose(fp2);
288 else if (argv[1][0] == 'x')
290 fprintf(stderr,"rdflib: module '%s' not found in '%s'\n",
291 argv[3],argv[2]);
292 exit(1);
294 break;
296 case 'r': /* replace module */
297 argc--;
298 case 'd': /* delete module */
299 if (argc < 4) {
300 fprintf(stderr, "rdflib: required paramenter missing\n");
301 exit(1);
304 fp = fopen(argv[2],"rb");
305 if (! fp)
307 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
308 perror("rdflib");
309 exit(1);
312 if (argv[1][0] == 'r') {
313 fp2 = fopen(argv[4],"rb");
314 if (! fp2)
316 fprintf(stderr, "rdflib: could not open '%s'\n", argv[4]);
317 perror("rdflib");
318 exit(1);
322 fptmp = tmpfile();
323 if (! fptmp)
325 fprintf(stderr,"rdflib: could not open temporary file\n");
326 perror("rdflib");
327 exit(1);
330 /* copy library into temporary file */
331 fseek(fp, 0, SEEK_END); /* get file length */
332 l = ftell(fp);
333 fseek(fp, 0, SEEK_SET);
334 copybytes(fp, fptmp, l);
335 rewind(fptmp); /* reopen files */
336 freopen(argv[2], "wb", fp);
338 while (! feof(fptmp) ) {
339 /* read name */
340 p = buf;
341 while( ( *(p++) = (char) fgetc(fptmp) ) )
342 if (feof(fptmp)) break;
344 if (feof(fptmp)) break;
346 /* check against desired name */
347 if (! strcmp(buf, argv[3]) ) {
348 fread(p=rdbuf, 1, sizeof(rdbuf), fptmp);
349 l = *(long*)(p+6);
350 fseek(fptmp, l, SEEK_CUR);
351 break;
352 } else {
353 fwrite(buf, 1, strlen(buf)+1, fp); /* module name */
354 if ((c=copybytes(fptmp, fp, 6)) >= '2') {
355 l = copylong(fptmp, fp); /* version 2 or above */
356 copybytes(fptmp, fp, l); /* entire object */
361 if (argv[1][0] == 'r') {
362 /* copy new module into library */
363 p = argv[3];
364 do {
365 if ( fputc(*p, fp) == EOF ) {
366 fprintf(stderr, "rdflib: write error\n");
367 exit(1);
369 } while (*p++);
371 while (! feof (fp2) ) {
372 i = fgetc (fp2);
373 if (i == EOF) {
374 break;
376 if ( fputc(i, fp) == EOF ) {
377 fprintf(stderr, "rdflib: write error\n");
378 exit(1);
381 fclose(fp2);
384 /* copy rest of library if any */
385 while (! feof (fptmp) ) {
386 i = fgetc (fptmp);
387 if (i == EOF) {
388 break;
391 if ( fputc(i, fp) == EOF ) {
392 fprintf(stderr,"rdflib: write error\n");
393 exit(1);
397 fclose(fp);
398 fclose(fptmp);
399 break;
401 default:
402 fprintf(stderr,"rdflib: command '%c' not recognized\n",
403 argv[1][0]);
404 exit(1);
406 return 0;