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 int32_t content size and a int32_t 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 uint8_t type & version identifier followed by int32_t
23 * content size, followed by data.
34 /* functions supported:
35 * create a library (no extra operands required)
36 * add a module from a library (requires filename and name to give mod.)
37 * replace a module in a library (requires given name and filename)
38 * delete a module from a library (requires given name)
39 * extract a module from the library (requires given name and filename)
45 " rdflib x libname [extra operands]\n\n"
46 " where x is one of:\n"
47 " c - create library\n"
48 " a - add module (operands = filename module-name)\n"
49 " x - extract (module-name filename)\n"
50 " r - replace (module-name filename)\n"
51 " d - delete (module-name)\n" " t - list\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 int32_ttolocal(int32_t *l
)
64 uint8_t *p
= (uint8_t *)l
;
73 (void)l
; /* placate optimizers */
77 char copybytes(FILE * fp
, FILE * fp2
, int n
)
81 for (i
= 0; i
< n
; i
++) {
84 fprintf(stderr
, "rdflib: premature end of file in '%s'\n",
89 if (fputc(t
, fp2
) == EOF
) {
90 fprintf(stderr
, "rdflib: write error\n");
94 return (char)t
; /* return last char read */
97 int32_t copyint32_t(FILE * fp
, FILE * fp2
)
101 uint8_t *p
= (uint8_t *)&l
;
103 for (i
= 0; i
< 4; i
++) { /* skip magic no */
106 fprintf(stderr
, "rdflib: premature end of file in '%s'\n",
111 if (fputc(t
, fp2
) == EOF
) {
112 fprintf(stderr
, "rdflib: write error\n");
121 int main(int argc
, char **argv
)
123 FILE *fp
, *fp2
= NULL
, *fptmp
;
124 char *p
, buf
[256], c
;
132 if (argc
< 3 || !strncmp(argv
[1], "-h", 2)
133 || !strncmp(argv
[1], "--h", 3)) {
138 switch (argv
[1][0]) {
139 case 'c': /* create library */
140 fp
= fopen(argv
[2], "wb");
142 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[2]);
146 fwrite(sig_modname
, 1, strlen(sig_modname
) + 1, fp
);
147 fwrite(rdl_signature
, 1, strlen(rdl_signature
), fp
);
148 l
= sizeof(t
= time(NULL
));
149 fwrite(&l
, sizeof(l
), 1, fp
);
150 fwrite(&t
, 1, l
, fp
);
154 case 'a': /* add module */
156 fprintf(stderr
, "rdflib: required parameter missing\n");
159 fp
= fopen(argv
[2], "ab");
161 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[2]);
166 fp2
= fopen(argv
[3], "rb");
168 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[3]);
175 if (fputc(*p
, fp
) == EOF
) {
176 fprintf(stderr
, "rdflib: write error\n");
187 if (fputc(i
, fp
) == EOF
) {
188 fprintf(stderr
, "rdflib: write error\n");
198 fprintf(stderr
, "rdflib: required parameter missing\n");
202 fp
= fopen(argv
[2], "rb");
204 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[2]);
213 while ((*(p
++) = (char)fgetc(fp
)))
221 if (argv
[1][0] == 'x') {
222 /* check against desired name */
223 if (!strcmp(buf
, argv
[3])) {
224 fp2
= fopen(argv
[4], "wb");
226 fprintf(stderr
, "rdflib: could not open '%s'\n",
233 printf("%-40s ", buf
);
235 /* step over the RDOFF file, extracting type information for
236 * the listing, and copying it if fp2 != NULL */
240 if (argv
[1][0] == 't')
241 for (i
= 0; i
< 6; i
++)
242 printf("%c", copybytes(fp
, fp2
, 1));
244 copybytes(fp
, fp2
, 6);
246 l
= copyint32_t(fp
, fp2
);
248 if (argv
[1][0] == 't')
249 printf(" %"PRId32
" bytes content\n", l
);
251 copybytes(fp
, fp2
, l
);
252 } else if ((c
= copybytes(fp
, fp2
, 6)) >= '2') { /* version 2 or above */
253 l
= copyint32_t(fp
, fp2
);
255 if (argv
[1][0] == 't')
256 printf("RDOFF%c %"PRId32
" bytes content\n", c
, l
);
257 copybytes(fp
, fp2
, l
); /* entire object */
259 if (argv
[1][0] == 't')
262 * version 1 object, so we don't have an object content
265 copybytes(fp
, fp2
, copyint32_t(fp
, fp2
)); /* header */
266 copybytes(fp
, fp2
, copyint32_t(fp
, fp2
)); /* text */
267 copybytes(fp
, fp2
, copyint32_t(fp
, fp2
)); /* data */
276 else if (argv
[1][0] == 'x') {
277 fprintf(stderr
, "rdflib: module '%s' not found in '%s'\n",
283 case 'r': /* replace module */
285 case 'd': /* delete module */
287 fprintf(stderr
, "rdflib: required parameter missing\n");
291 fp
= fopen(argv
[2], "rb");
293 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[2]);
298 if (argv
[1][0] == 'r') {
299 fp2
= fopen(argv
[4], "rb");
301 fprintf(stderr
, "rdflib: could not open '%s'\n", argv
[4]);
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
);
320 freopen(argv
[2], "wb", fp
);
322 while (!feof(fptmp
)) {
325 while ((*(p
++) = (char)fgetc(fptmp
)))
332 /* check against desired name */
333 if (!strcmp(buf
, argv
[3])) {
334 fread(p
= rdbuf
, 1, sizeof(rdbuf
), fptmp
);
335 l
= *(int32_t *)(p
+ 6);
336 fseek(fptmp
, l
, SEEK_CUR
);
339 fwrite(buf
, 1, strlen(buf
) + 1, fp
); /* module name */
340 if ((c
= copybytes(fptmp
, fp
, 6)) >= '2') {
341 l
= copyint32_t(fptmp
, fp
); /* version 2 or above */
342 copybytes(fptmp
, fp
, l
); /* entire object */
347 if (argv
[1][0] == 'r') {
348 /* copy new module into library */
351 if (fputc(*p
, fp
) == EOF
) {
352 fprintf(stderr
, "rdflib: write error\n");
362 if (fputc(i
, fp
) == EOF
) {
363 fprintf(stderr
, "rdflib: write error\n");
370 /* copy rest of library if any */
371 while (!feof(fptmp
)) {
377 if (fputc(i
, fp
) == EOF
) {
378 fprintf(stderr
, "rdflib: write error\n");
388 fprintf(stderr
, "rdflib: command '%c' not recognized\n",