Document new Cygwin compatibility
[nasm/avx512.git] / rdoff / rdflib.c
blob1f19913eb208be636ade9b0ee68a0c3a54828d0e
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 <errno.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <time.h>
32 /* functions supported:
33 * create a library (no extra operands required)
34 * add a module from a library (requires filename and name to give mod.)
35 * replace a module in a library (requires given name and filename)
36 * delete a module from a library (requires given name)
37 * extract a module from the library (requires given name and filename)
38 * list modules
41 const char *usage =
42 "usage:\n"
43 " rdflib x libname [extra operands]\n\n"
44 " where x is one of:\n"
45 " c - create library\n"
46 " a - add module (operands = filename module-name)\n"
47 " x - extract (module-name filename)\n"
48 " r - replace (module-name filename)\n"
49 " d - delete (module-name)\n"
50 " 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++ )
80 t = fgetc(fp);
81 if (t == EOF)
83 fprintf(stderr,"rdflib: premature end of file in '%s'\n",
84 _argv[2]);
85 exit(1);
87 if (fp2)
88 if (fputc(t, fp2) == EOF)
90 fprintf(stderr,"rdflib: write error\n");
91 exit(1);
94 return (char) t; /* return last char read */
97 long copylong(FILE *fp, FILE *fp2)
99 long l;
100 int i,t;
101 unsigned char * p = (unsigned char *) &l;
104 for (i = 0 ; i < 4; i++ ) /* skip magic no */
106 t = fgetc(fp);
107 if (t == EOF)
109 fprintf(stderr,"rdflib: premature end of file in '%s'\n",
110 _argv[2]);
111 exit(1);
113 if (fp2)
114 if (fputc(t, fp2) == EOF)
116 fprintf(stderr,"rdflib: write error\n");
117 exit(1);
119 *p++ = t;
121 longtolocal (&l);
122 return l;
125 int main(int argc, char **argv)
127 FILE *fp, *fp2 = NULL, *fptmp;
128 char *p, buf[256], c;
129 int i;
130 long l;
131 time_t t;
132 char tmptempl[L_tmpnam], rdbuf[10];
134 _argv = argv;
136 if (argc < 3 || !strncmp(argv[1],"-h",2) || !strncmp(argv[1],"--h",3))
138 printf(usage);
139 exit(1);
142 switch(argv[1][0])
144 case 'c': /* create library */
145 fp = fopen(argv[2],"wb");
146 if (! fp) {
147 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
148 perror("rdflib");
149 exit(1);
151 fwrite(sig_modname, 1, strlen(sig_modname)+1, fp);
152 fwrite(rdl_signature, 1, strlen(rdl_signature), fp);
153 l = sizeof(t = time(NULL));
154 fwrite(&l, sizeof(l), 1, fp);
155 fwrite(&t, 1, l, fp);
156 fclose(fp);
157 break;
159 case 'a': /* add module */
160 if (argc < 5) {
161 fprintf(stderr,"rdflib: required parameter missing\n");
162 exit(1);
164 fp = fopen(argv[2],"ab");
165 if (! fp)
167 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
168 perror("rdflib");
169 exit(1);
172 fp2 = fopen(argv[3],"rb");
173 if (! fp2)
175 fprintf(stderr,"rdflib: could not open '%s'\n",argv[3]);
176 perror("rdflib");
177 exit(1);
180 p = argv[4];
181 do {
182 if ( fputc(*p,fp) == EOF ) {
183 fprintf(stderr,"rdflib: write error\n");
184 exit(1);
186 } while (*p++);
188 while (! feof (fp2) ) {
189 i = fgetc (fp2);
190 if (i == EOF) {
191 break;
194 if ( fputc(i, fp) == EOF ) {
195 fprintf(stderr,"rdflib: write error\n");
196 exit(1);
199 fclose(fp2);
200 fclose(fp);
201 break;
203 case 'x':
204 if (argc < 5) {
205 fprintf(stderr, "rdflib: required paramenter missing\n");
206 exit(1);
208 case 't':
209 fp = fopen(argv[2],"rb");
210 if (! fp)
212 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
213 perror("rdflib");
214 exit(1);
217 fp2 = NULL;
218 while (! feof(fp) ) {
219 /* read name */
220 p = buf;
221 while( ( *(p++) = (char) fgetc(fp) ) )
222 if (feof(fp)) break;
224 if (feof(fp)) break;
226 fp2 = NULL;
227 if (argv[1][0] == 'x') {
228 /* check against desired name */
229 if (! strcmp(buf,argv[3]) )
231 fp2 = fopen(argv[4],"wb");
232 if (! fp2)
234 fprintf(stderr,"rdflib: could not open '%s'\n",argv[4]);
235 perror("rdflib");
236 exit(1);
240 else
241 printf("%-40s ", buf);
243 /* step over the RDOFF file, extracting type information for
244 * the listing, and copying it if fp2 != NULL */
246 if (buf[0] == '.') {
248 if (argv[1][0] == 't')
249 for (i = 0; i < 6; i++)
250 printf("%c", copybytes(fp,fp2,1));
251 else
252 copybytes(fp,fp2,6);
254 l = copylong(fp,fp2);
256 if (argv[1][0] == 't') printf(" %ld bytes content\n", l);
258 copybytes(fp,fp2,l);
260 else if ((c=copybytes(fp,fp2,6)) >= '2') /* version 2 or above */
262 l = copylong(fp,fp2);
264 if (argv[1][0] == 't')
265 printf("RDOFF%c %ld bytes content\n", c, l);
266 copybytes(fp,fp2, l); /* entire object */
268 else
270 if (argv[1][0] == 't')
271 printf("RDOFF1\n");
273 * version 1 object, so we don't have an object content
274 * length field.
276 copybytes(fp,fp2, copylong(fp,fp2)); /* header */
277 copybytes(fp,fp2, copylong(fp,fp2)); /* text */
278 copybytes(fp,fp2, copylong(fp,fp2)); /* data */
281 if (fp2)
282 break;
284 fclose(fp);
285 if (fp2)
286 fclose(fp2);
287 else if (argv[1][0] == 'x')
289 fprintf(stderr,"rdflib: module '%s' not found in '%s'\n",
290 argv[3],argv[2]);
291 exit(1);
293 break;
295 case 'r': /* replace module */
296 argc--;
297 case 'd': /* delete module */
298 if (argc < 4) {
299 fprintf(stderr, "rdflib: required paramenter missing\n");
300 exit(1);
303 fp = fopen(argv[2],"rb");
304 if (! fp)
306 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
307 perror("rdflib");
308 exit(1);
311 if (argv[1][0] == 'r') {
312 fp2 = fopen(argv[4],"rb");
313 if (! fp2)
315 fprintf(stderr, "rdflib: could not open '%s'\n", argv[4]);
316 perror("rdflib");
317 exit(1);
321 tmpnam(tmptempl);
322 fptmp = fopen(tmptempl,"wb");
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 freopen(tmptempl, "rb", 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 unlink(tmptempl);
400 break;
402 default:
403 fprintf(stderr,"rdflib: command '%c' not recognized\n",
404 argv[1][0]);
405 exit(1);
407 return 0;