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.
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)
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"
53 /* Library signature */
54 const char *rdl_signature
= "RDLIB2", *sig_modname
= ".sig";
58 #define _ENDIANNESS 0 /* 0 for little, 1 for big */
60 static void longtolocal(long * l
)
64 unsigned char * p
= (unsigned char *) l
;
75 char copybytes(FILE *fp
, FILE *fp2
, int n
)
79 for (i
= 0 ; i
< n
; i
++ )
84 fprintf(stderr
,"rdflib: premature end of file in '%s'\n",
89 if (fputc(t
, fp2
) == EOF
)
91 fprintf(stderr
,"rdflib: write error\n");
95 return (char) t
; /* return last char read */
98 long copylong(FILE *fp
, FILE *fp2
)
102 unsigned char * p
= (unsigned char *) &l
;
105 for (i
= 0 ; i
< 4; i
++ ) /* skip magic no */
110 fprintf(stderr
,"rdflib: premature end of file in '%s'\n",
115 if (fputc(t
, fp2
) == EOF
)
117 fprintf(stderr
,"rdflib: write error\n");
126 int main(int argc
, char **argv
)
128 FILE *fp
, *fp2
= NULL
, *fptmp
;
129 char *p
, buf
[256], c
;
137 if (argc
< 3 || !strncmp(argv
[1],"-h",2) || !strncmp(argv
[1],"--h",3))
145 case 'c': /* create library */
146 fp
= fopen(argv
[2],"wb");
148 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[2]);
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
);
160 case 'a': /* add module */
162 fprintf(stderr
,"rdflib: required parameter missing\n");
165 fp
= fopen(argv
[2],"ab");
168 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[2]);
173 fp2
= fopen(argv
[3],"rb");
176 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[3]);
183 if ( fputc(*p
,fp
) == EOF
) {
184 fprintf(stderr
,"rdflib: write error\n");
189 while (! feof (fp2
) ) {
195 if ( fputc(i
, fp
) == EOF
) {
196 fprintf(stderr
,"rdflib: write error\n");
206 fprintf(stderr
, "rdflib: required paramenter missing\n");
210 fp
= fopen(argv
[2],"rb");
213 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[2]);
219 while (! feof(fp
) ) {
222 while( ( *(p
++) = (char) fgetc(fp
) ) )
228 if (argv
[1][0] == 'x') {
229 /* check against desired name */
230 if (! strcmp(buf
,argv
[3]) )
232 fp2
= fopen(argv
[4],"wb");
235 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[4]);
242 printf("%-40s ", buf
);
244 /* step over the RDOFF file, extracting type information for
245 * the listing, and copying it if fp2 != NULL */
249 if (argv
[1][0] == 't')
250 for (i
= 0; i
< 6; i
++)
251 printf("%c", copybytes(fp
,fp2
,1));
255 l
= copylong(fp
,fp2
);
257 if (argv
[1][0] == 't') printf(" %ld bytes content\n", 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 */
271 if (argv
[1][0] == 't')
274 * version 1 object, so we don't have an object content
277 copybytes(fp
,fp2
, copylong(fp
,fp2
)); /* header */
278 copybytes(fp
,fp2
, copylong(fp
,fp2
)); /* text */
279 copybytes(fp
,fp2
, copylong(fp
,fp2
)); /* data */
288 else if (argv
[1][0] == 'x')
290 fprintf(stderr
,"rdflib: module '%s' not found in '%s'\n",
296 case 'r': /* replace module */
298 case 'd': /* delete module */
300 fprintf(stderr
, "rdflib: required paramenter missing\n");
304 fp
= fopen(argv
[2],"rb");
307 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[2]);
312 if (argv
[1][0] == 'r') {
313 fp2
= fopen(argv
[4],"rb");
316 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[4]);
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 rewind(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");
402 fprintf(stderr
,"rdflib: command '%c' not recognized\n",