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 * 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.
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)
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"
47 #define _ENDIANNESS 0 /* 0 for little, 1 for big */
49 static void longtolocal(long * l
)
53 unsigned char * p
= (unsigned char *) l
;
64 char copybytes(FILE *fp
, FILE *fp2
, int n
)
68 for (i
= 0 ; i
< n
; i
++ )
73 fprintf(stderr
,"rdflib: premature end of file in '%s'\n",
78 if (fputc(t
, fp2
) == EOF
)
80 fprintf(stderr
,"rdflib: write error\n");
84 return (char) t
; /* return last char read */
87 long copylong(FILE *fp
, FILE *fp2
)
91 unsigned char * p
= (unsigned char *) &l
;
94 for (i
= 0 ; i
< 4; i
++ ) /* skip magic no */
99 fprintf(stderr
,"rdflib: premature end of file in '%s'\n",
104 if (fputc(t
, fp2
) == EOF
)
106 fprintf(stderr
,"rdflib: write error\n");
115 int main(int argc
, char **argv
)
117 FILE *fp
, *fp2
, *fptmp
;
118 char *p
, buf
[256], c
;
121 char tmptempl
[L_tmpnam
], rdbuf
[10];
125 if (argc
< 3 || !strncmp(argv
[1],"-h",2) || !strncmp(argv
[1],"--h",3))
133 case 'c': /* create library */
134 fp
= fopen(argv
[2],"wb");
136 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[2]);
143 case 'a': /* add module */
145 fprintf(stderr
,"rdflib: required parameter missing\n");
148 fp
= fopen(argv
[2],"ab");
151 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[2]);
156 fp2
= fopen(argv
[3],"rb");
159 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[3]);
166 if ( fputc(*p
,fp
) == EOF
) {
167 fprintf(stderr
,"rdflib: write error\n");
172 while (! feof (fp2
) ) {
178 if ( fputc(i
, fp
) == EOF
) {
179 fprintf(stderr
,"rdflib: write error\n");
189 fprintf(stderr
, "rdflib: required paramenter missing\n");
193 fp
= fopen(argv
[2],"rb");
196 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[2]);
202 while (! feof(fp
) ) {
205 while( ( *(p
++) = (char) fgetc(fp
) ) )
211 if (argv
[1][0] == 'x') {
212 /* check against desired name */
213 if (! strcmp(buf
,argv
[3]) )
215 fp2
= fopen(argv
[4],"wb");
218 fprintf(stderr
,"rdflib: could not open '%s'\n",argv
[4]);
225 printf("%-40s ", buf
);
227 /* step over the RDOFF file, extracting type information for
228 * the listing, and copying it if fp2 != NULL */
232 if (argv
[1][0] == 't')
233 for (i
= 0; i
< 6; i
++)
234 printf("%c", copybytes(fp
,fp2
,1));
238 l
= copylong(fp
,fp2
);
240 if (argv
[1][0] == 't') printf(" %ld bytes content\n", 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 */
254 if (argv
[1][0] == 't')
257 * version 1 object, so we don't have an object content
260 copybytes(fp
,fp2
, copylong(fp
,fp2
)); /* header */
261 copybytes(fp
,fp2
, copylong(fp
,fp2
)); /* text */
262 copybytes(fp
,fp2
, copylong(fp
,fp2
)); /* data */
271 else if (argv
[1][0] == 'x')
273 fprintf(stderr
,"rdflib: module '%s' not found in '%s'\n",
279 case 'r': /* replace module */
281 case 'd': /* delete module */
283 fprintf(stderr
, "rdflib: required paramenter missing\n");
287 fp
= fopen(argv
[2],"rb");
290 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[2]);
295 if (argv
[1][0] == 'r') {
296 fp2
= fopen(argv
[4],"rb");
299 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[4]);
306 fptmp
= fopen(tmptempl
,"wb");
309 fprintf(stderr
,"rdflib: could not open temporary file\n");
314 /* copy library into temporary file */
315 fseek(fp
, 0, SEEK_END
); /* get file length */
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
) ) {
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
);
334 fseek(fptmp
, l
, SEEK_CUR
);
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 */
349 if ( fputc(*p
, fp
) == EOF
) {
350 fprintf(stderr
, "rdflib: write error\n");
355 while (! feof (fp2
) ) {
360 if ( fputc(i
, fp
) == EOF
) {
361 fprintf(stderr
, "rdflib: write error\n");
368 /* copy rest of library if any */
369 while (! feof (fptmp
) ) {
375 if ( fputc(i
, fp
) == EOF
) {
376 fprintf(stderr
,"rdflib: write error\n");
387 fprintf(stderr
,"rdflib: command '%c' not recognized\n",