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.
36 /* functions supported:
37 * create a library (no extra operands required)
38 * add a module from a library (requires filename and name to give mod.)
39 * replace a module in a library (requires given name and filename)
40 * delete a module from a library (requires given name)
41 * extract a module from the library (requires given name and filename)
47 " rdflib x libname [extra operands]\n\n"
48 " where x is one of:\n"
49 " c - create library\n"
50 " a - add module (operands = filename module-name)\n"
51 " x - extract (module-name filename)\n"
52 " r - replace (module-name filename)\n"
53 " d - delete (module-name)\n" " t - list\n";
55 /* Library signature */
56 const char *rdl_signature = "RDLIB2", *sig_modname = ".sig";
60 #define _ENDIANNESS 0 /* 0 for little, 1 for big */
62 static void int32_ttolocal(int32_t *l)
66 uint8_t *p = (uint8_t *)l;
75 (void)l; /* placate optimizers */
79 char copybytes(FILE * fp, FILE * fp2, int n)
83 for (i = 0; i < n; i++) {
86 fprintf(stderr, "rdflib: premature end of file in '%s'\n",
91 if (fputc(t, fp2) == EOF) {
92 fprintf(stderr, "rdflib: write error\n");
96 return (char)t; /* return last char read */
99 int32_t copyint32_t(FILE * fp, FILE * fp2)
103 uint8_t *p = (uint8_t *)&l;
105 for (i = 0; i < 4; i++) { /* skip magic no */
108 fprintf(stderr, "rdflib: premature end of file in '%s'\n",
113 if (fputc(t, fp2) == EOF) {
114 fprintf(stderr, "rdflib: write error\n");
123 int main(int argc, char **argv)
125 FILE *fp, *fp2 = NULL, *fptmp;
126 char *p, buf[256], c;
134 if (argc < 3 || !strncmp(argv[1], "-h", 2)
135 || !strncmp(argv[1], "--h", 3)) {
140 switch (argv[1][0]) {
141 case 'c': /* create library */
142 fp = fopen(argv[2], "wb");
144 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
148 fwrite(sig_modname, 1, strlen(sig_modname) + 1, fp);
149 fwrite(rdl_signature, 1, strlen(rdl_signature), fp);
150 l = sizeof(t = time(NULL));
151 fwrite(&l, sizeof(l), 1, fp);
152 fwrite(&t, 1, l, fp);
156 case 'a': /* add module */
158 fprintf(stderr, "rdflib: required parameter missing\n");
161 fp = fopen(argv[2], "ab");
163 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
168 fp2 = fopen(argv[3], "rb");
170 fprintf(stderr, "rdflib: could not open '%s'\n", argv[3]);
177 if (fputc(*p, fp) == EOF) {
178 fprintf(stderr, "rdflib: write error\n");
189 if (fputc(i, fp) == EOF) {
190 fprintf(stderr, "rdflib: write error\n");
200 fprintf(stderr, "rdflib: required parameter missing\n");
204 fp = fopen(argv[2], "rb");
206 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
215 while ((*(p++) = (char)fgetc(fp)))
223 if (argv[1][0] == 'x') {
224 /* check against desired name */
225 if (!strcmp(buf, argv[3])) {
226 fp2 = fopen(argv[4], "wb");
228 fprintf(stderr, "rdflib: could not open '%s'\n",
235 printf("%-40s ", buf);
237 /* step over the RDOFF file, extracting type information for
238 * the listing, and copying it if fp2 != NULL */
242 if (argv[1][0] == 't')
243 for (i = 0; i < 6; i++)
244 printf("%c", copybytes(fp, fp2, 1));
246 copybytes(fp, fp2, 6);
248 l = copyint32_t(fp, fp2);
250 if (argv[1][0] == 't')
251 printf(" %"PRId32" bytes content\n", l);
253 copybytes(fp, fp2, l);
254 } else if ((c = copybytes(fp, fp2, 6)) >= '2') { /* version 2 or above */
255 l = copyint32_t(fp, fp2);
257 if (argv[1][0] == 't')
258 printf("RDOFF%c %"PRId32" bytes content\n", c, l);
259 copybytes(fp, fp2, l); /* entire object */
261 if (argv[1][0] == 't')
264 * version 1 object, so we don't have an object content
267 copybytes(fp, fp2, copyint32_t(fp, fp2)); /* header */
268 copybytes(fp, fp2, copyint32_t(fp, fp2)); /* text */
269 copybytes(fp, fp2, copyint32_t(fp, fp2)); /* data */
278 else if (argv[1][0] == 'x') {
279 fprintf(stderr, "rdflib: module '%s' not found in '%s'\n",
285 case 'r': /* replace module */
287 case 'd': /* delete module */
289 fprintf(stderr, "rdflib: required parameter missing\n");
293 fp = fopen(argv[2], "rb");
295 fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]);
300 if (argv[1][0] == 'r') {
301 fp2 = fopen(argv[4], "rb");
303 fprintf(stderr, "rdflib: could not open '%s'\n", argv[4]);
311 fprintf(stderr, "rdflib: could not open temporary file\n");
316 /* copy library into temporary file */
317 fseek(fp, 0, SEEK_END); /* get file length */
319 fseek(fp, 0, SEEK_SET);
320 copybytes(fp, fptmp, l);
322 freopen(argv[2], "wb", fp);
324 while (!feof(fptmp)) {
327 while ((*(p++) = (char)fgetc(fptmp)))
334 /* check against desired name */
335 if (!strcmp(buf, argv[3])) {
336 fread(p = rdbuf, 1, sizeof(rdbuf), fptmp);
337 l = *(int32_t *)(p + 6);
338 fseek(fptmp, l, SEEK_CUR);
341 fwrite(buf, 1, strlen(buf) + 1, fp); /* module name */
342 if ((c = copybytes(fptmp, fp, 6)) >= '2') {
343 l = copyint32_t(fptmp, fp); /* version 2 or above */
344 copybytes(fptmp, fp, l); /* entire object */
349 if (argv[1][0] == 'r') {
350 /* copy new module into library */
353 if (fputc(*p, fp) == EOF) {
354 fprintf(stderr, "rdflib: write error\n");
364 if (fputc(i, fp) == EOF) {
365 fprintf(stderr, "rdflib: write error\n");
372 /* copy rest of library if any */
373 while (!feof(fptmp)) {
379 if (fputc(i, fp) == EOF) {
380 fprintf(stderr, "rdflib: write error\n");
390 fprintf(stderr, "rdflib: command '%c' not recognized\n",