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" " t - list\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
++) {
81 fprintf(stderr
, "rdflib: premature end of file in '%s'\n",
86 if (fputc(t
, fp2
) == EOF
) {
87 fprintf(stderr
, "rdflib: write error\n");
91 return (char)t
; /* return last char read */
94 long copylong(FILE * fp
, FILE * fp2
)
98 unsigned char *p
= (unsigned char *)&l
;
100 for (i
= 0; i
< 4; i
++) { /* skip magic no */
103 fprintf(stderr
, "rdflib: premature end of file in '%s'\n",
108 if (fputc(t
, fp2
) == EOF
) {
109 fprintf(stderr
, "rdflib: write error\n");
118 int main(int argc
, char **argv
)
120 FILE *fp
, *fp2
= NULL
, *fptmp
;
121 char *p
, buf
[256], c
;
129 if (argc
< 3 || !strncmp(argv
[1], "-h", 2)
130 || !strncmp(argv
[1], "--h", 3)) {
135 switch (argv
[1][0]) {
136 case 'c': /* create library */
137 fp
= fopen(argv
[2], "wb");
139 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[2]);
143 fwrite(sig_modname
, 1, strlen(sig_modname
) + 1, fp
);
144 fwrite(rdl_signature
, 1, strlen(rdl_signature
), fp
);
145 l
= sizeof(t
= time(NULL
));
146 fwrite(&l
, sizeof(l
), 1, fp
);
147 fwrite(&t
, 1, l
, fp
);
151 case 'a': /* add module */
153 fprintf(stderr
, "rdflib: required parameter missing\n");
156 fp
= fopen(argv
[2], "ab");
158 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[2]);
163 fp2
= fopen(argv
[3], "rb");
165 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[3]);
172 if (fputc(*p
, fp
) == EOF
) {
173 fprintf(stderr
, "rdflib: write error\n");
184 if (fputc(i
, fp
) == EOF
) {
185 fprintf(stderr
, "rdflib: write error\n");
195 fprintf(stderr
, "rdflib: required parameter missing\n");
199 fp
= fopen(argv
[2], "rb");
201 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[2]);
210 while ((*(p
++) = (char)fgetc(fp
)))
218 if (argv
[1][0] == 'x') {
219 /* check against desired name */
220 if (!strcmp(buf
, argv
[3])) {
221 fp2
= fopen(argv
[4], "wb");
223 fprintf(stderr
, "rdflib: could not open '%s'\n",
230 printf("%-40s ", buf
);
232 /* step over the RDOFF file, extracting type information for
233 * the listing, and copying it if fp2 != NULL */
237 if (argv
[1][0] == 't')
238 for (i
= 0; i
< 6; i
++)
239 printf("%c", copybytes(fp
, fp2
, 1));
241 copybytes(fp
, fp2
, 6);
243 l
= copylong(fp
, fp2
);
245 if (argv
[1][0] == 't')
246 printf(" %ld bytes content\n", l
);
248 copybytes(fp
, fp2
, l
);
249 } else if ((c
= copybytes(fp
, fp2
, 6)) >= '2') { /* version 2 or above */
250 l
= copylong(fp
, fp2
);
252 if (argv
[1][0] == 't')
253 printf("RDOFF%c %ld bytes content\n", c
, l
);
254 copybytes(fp
, fp2
, l
); /* entire object */
256 if (argv
[1][0] == 't')
259 * version 1 object, so we don't have an object content
262 copybytes(fp
, fp2
, copylong(fp
, fp2
)); /* header */
263 copybytes(fp
, fp2
, copylong(fp
, fp2
)); /* text */
264 copybytes(fp
, fp2
, copylong(fp
, fp2
)); /* data */
273 else if (argv
[1][0] == 'x') {
274 fprintf(stderr
, "rdflib: module '%s' not found in '%s'\n",
280 case 'r': /* replace module */
282 case 'd': /* delete module */
284 fprintf(stderr
, "rdflib: required parameter missing\n");
288 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");
298 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[4]);
306 fprintf(stderr
, "rdflib: could not open temporary file\n");
311 /* copy library into temporary file */
312 fseek(fp
, 0, SEEK_END
); /* get file length */
314 fseek(fp
, 0, SEEK_SET
);
315 copybytes(fp
, fptmp
, l
);
317 freopen(argv
[2], "wb", fp
);
319 while (!feof(fptmp
)) {
322 while ((*(p
++) = (char)fgetc(fptmp
)))
329 /* check against desired name */
330 if (!strcmp(buf
, argv
[3])) {
331 fread(p
= rdbuf
, 1, sizeof(rdbuf
), fptmp
);
332 l
= *(long *)(p
+ 6);
333 fseek(fptmp
, l
, SEEK_CUR
);
336 fwrite(buf
, 1, strlen(buf
) + 1, fp
); /* module name */
337 if ((c
= copybytes(fptmp
, fp
, 6)) >= '2') {
338 l
= copylong(fptmp
, fp
); /* version 2 or above */
339 copybytes(fptmp
, fp
, l
); /* entire object */
344 if (argv
[1][0] == 'r') {
345 /* copy new module into library */
348 if (fputc(*p
, fp
) == EOF
) {
349 fprintf(stderr
, "rdflib: write error\n");
359 if (fputc(i
, fp
) == EOF
) {
360 fprintf(stderr
, "rdflib: write error\n");
367 /* copy rest of library if any */
368 while (!feof(fptmp
)) {
374 if (fputc(i
, fp
) == EOF
) {
375 fprintf(stderr
, "rdflib: write error\n");
385 fprintf(stderr
, "rdflib: command '%c' not recognized\n",