1 /* rdflib - manipulate RDOFF library files (.rdl) */
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.
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)
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"
52 /* Library signature */
53 const char *rdl_signature
= "RDLIB2", *sig_modname
= ".sig";
57 #define _ENDIANNESS 0 /* 0 for little, 1 for big */
59 static void longtolocal(long * l
)
63 unsigned char * p
= (unsigned char *) l
;
74 char copybytes(FILE *fp
, FILE *fp2
, int n
)
78 for (i
= 0 ; i
< n
; i
++ )
83 fprintf(stderr
,"rdflib: premature end of file in '%s'\n",
88 if (fputc(t
, fp2
) == EOF
)
90 fprintf(stderr
,"rdflib: write error\n");
94 return (char) t
; /* return last char read */
97 long copylong(FILE *fp
, FILE *fp2
)
101 unsigned char * p
= (unsigned char *) &l
;
104 for (i
= 0 ; i
< 4; i
++ ) /* skip magic no */
109 fprintf(stderr
,"rdflib: premature end of file in '%s'\n",
114 if (fputc(t
, fp2
) == EOF
)
116 fprintf(stderr
,"rdflib: write error\n");
125 int main(int argc
, char **argv
)
127 FILE *fp
, *fp2
= NULL
, *fptmp
;
128 char *p
, buf
[256], c
;
132 char tmptempl
[L_tmpnam
], rdbuf
[10];
136 if (argc
< 3 || !strncmp(argv
[1],"-h",2) || !strncmp(argv
[1],"--h",3))
144 case 'c': /* create library */
145 fp
= fopen(argv
[2],"wb");
147 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[2]);
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
);
159 case 'a': /* add module */
161 fprintf(stderr
,"rdflib: required parameter missing\n");
164 fp
= fopen(argv
[2],"ab");
167 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[2]);
172 fp2
= fopen(argv
[3],"rb");
175 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[3]);
182 if ( fputc(*p
,fp
) == EOF
) {
183 fprintf(stderr
,"rdflib: write error\n");
188 while (! feof (fp2
) ) {
194 if ( fputc(i
, fp
) == EOF
) {
195 fprintf(stderr
,"rdflib: write error\n");
205 fprintf(stderr
, "rdflib: required paramenter missing\n");
209 fp
= fopen(argv
[2],"rb");
212 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[2]);
218 while (! feof(fp
) ) {
221 while( ( *(p
++) = (char) fgetc(fp
) ) )
227 if (argv
[1][0] == 'x') {
228 /* check against desired name */
229 if (! strcmp(buf
,argv
[3]) )
231 fp2
= fopen(argv
[4],"wb");
234 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[4]);
241 printf("%-40s ", buf
);
243 /* step over the RDOFF file, extracting type information for
244 * the listing, and copying it if fp2 != NULL */
248 if (argv
[1][0] == 't')
249 for (i
= 0; i
< 6; i
++)
250 printf("%c", copybytes(fp
,fp2
,1));
254 l
= copylong(fp
,fp2
);
256 if (argv
[1][0] == 't') printf(" %ld bytes content\n", 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 */
270 if (argv
[1][0] == 't')
273 * version 1 object, so we don't have an object content
276 copybytes(fp
,fp2
, copylong(fp
,fp2
)); /* header */
277 copybytes(fp
,fp2
, copylong(fp
,fp2
)); /* text */
278 copybytes(fp
,fp2
, copylong(fp
,fp2
)); /* data */
287 else if (argv
[1][0] == 'x')
289 fprintf(stderr
,"rdflib: module '%s' not found in '%s'\n",
295 case 'r': /* replace module */
297 case 'd': /* delete module */
299 fprintf(stderr
, "rdflib: required paramenter missing\n");
303 fp
= fopen(argv
[2],"rb");
306 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[2]);
311 if (argv
[1][0] == 'r') {
312 fp2
= fopen(argv
[4],"rb");
315 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[4]);
322 fptmp
= fopen(tmptempl
,"wb");
325 fprintf(stderr
,"rdflib: could not open temporary file\n");
330 /* copy library into temporary file */
331 fseek(fp
, 0, SEEK_END
); /* get file length */
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
) ) {
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
);
350 fseek(fptmp
, l
, SEEK_CUR
);
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 */
365 if ( fputc(*p
, fp
) == EOF
) {
366 fprintf(stderr
, "rdflib: write error\n");
371 while (! feof (fp2
) ) {
376 if ( fputc(i
, fp
) == EOF
) {
377 fprintf(stderr
, "rdflib: write error\n");
384 /* copy rest of library if any */
385 while (! feof (fptmp
) ) {
391 if ( fputc(i
, fp
) == EOF
) {
392 fprintf(stderr
,"rdflib: write error\n");
403 fprintf(stderr
,"rdflib: command '%c' not recognized\n",