NASM 0.98.14
[nasm/avx512.git] / rdoff / rdflib.c
blob5a8cf690c77afb0736f9afb4e861ad5bd1f1b5ec
1 /* rdflib - manipulate RDOFF library files (.rdl) */
3 /*
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
9 * directory placed on the end of the file. The format of the
10 * directory will be 'RDLDD' followed by a version number, followed by
11 * the length of the directory, and then the directory, the format of
12 * which has not yet been designed. The module name of the directory
13 * must be '.dir'.
15 * All module names beginning with '.' are reserved
16 * for possible future extensions. The linker ignores all such modules,
17 * assuming they have the format of a six byte type & version identifier
18 * followed by long content size, followed by data.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <string.h>
26 /* functions supported:
27 create a library (no extra operands required)
28 add a module from a library (requires filename and name to give mod.)
29 remove a module from a library (requires given name) (not implemented)
30 extract a module from the library (requires given name and filename)
31 list modules */
33 const char *usage =
34 "usage:\n"
35 " rdflib x libname [extra operands]\n\n"
36 " where x is one of:\n"
37 " c - create library\n"
38 " a - add module (operands = filename module-name)\n"
39 " r - remove (module-name) [not implemented]\n"
40 " x - extract (module-name filename)\n"
41 " t - list\n";
43 char **_argv;
45 #define _ENDIANNESS 0 /* 0 for little, 1 for big */
47 static void longtolocal(long * l)
49 #if _ENDIANNESS
50 unsigned char t;
51 unsigned char * p = (unsigned char *) l;
53 t = p[0];
54 p[0] = p[3];
55 p[3] = t;
56 t = p[1];
57 p[1] = p[2];
58 p[2] = p[1];
59 #endif
62 char copybytes(FILE *fp, FILE *fp2, int n)
64 int i, t = 0;
66 for (i = 0 ; i < n; i++ )
68 t = fgetc(fp);
69 if (t == EOF)
71 fprintf(stderr,"rdflib: premature end of file in '%s'\n",
72 _argv[2]);
73 exit(1);
75 if (fp2)
76 if (fputc(t, fp2) == EOF)
78 fprintf(stderr,"rdflib: write error\n");
79 exit(1);
82 return (char) t; /* return last char read */
85 long copylong(FILE *fp, FILE *fp2)
87 long l;
88 int i,t;
89 unsigned char * p = (unsigned char *) &l;
92 for (i = 0 ; i < 4; i++ ) /* skip magic no */
94 t = fgetc(fp);
95 if (t == EOF)
97 fprintf(stderr,"rdflib: premature end of file in '%s'\n",
98 _argv[2]);
99 exit(1);
101 if (fp2)
102 if (fputc(t, fp2) == EOF)
104 fprintf(stderr,"rdflib: write error\n");
105 exit(1);
107 *p++ = t;
109 longtolocal (&l);
110 return l;
113 int main(int argc, char **argv)
115 FILE *fp, *fp2;
116 char *p, buf[256], c;
117 int i;
118 long l;
120 _argv = argv;
122 if (argc < 3 || !strncmp(argv[1],"-h",2) || !strncmp(argv[1],"--h",3))
124 printf(usage);
125 exit(1);
128 switch(argv[1][0])
130 case 'c': /* create library */
131 fp = fopen(argv[2],"wb");
132 if (! fp) {
133 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
134 perror("rdflib");
135 exit(1);
137 fclose(fp);
138 break;
140 case 'a': /* add module */
141 if (argc < 5) {
142 fprintf(stderr,"rdflib: required parameter missing\n");
143 exit(1);
145 fp = fopen(argv[2],"ab");
146 if (! fp)
148 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
149 perror("rdflib");
150 exit(1);
153 fp2 = fopen(argv[3],"rb");
154 if (! fp)
156 fprintf(stderr,"rdflib: could not open '%s'\n",argv[3]);
157 perror("rdflib");
158 exit(1);
161 p = argv[4];
162 do {
163 if ( fputc(*p,fp) == EOF ) {
164 fprintf(stderr,"rdflib: write error\n");
165 exit(1);
167 } while (*p++);
169 while (! feof (fp2) ) {
170 i = fgetc (fp2);
171 if (i == EOF) {
172 break;
175 if ( fputc(i, fp) == EOF ) {
176 fprintf(stderr,"rdflib: write error\n");
177 exit(1);
180 fclose(fp2);
181 fclose(fp);
182 break;
184 case 'x':
185 if (argc < 5) {
186 fprintf(stderr,"rdflib: required parameter missing\n");
187 exit(1);
189 case 't':
190 if (argc < 3) {
191 fprintf(stderr, "rdflib: required paramenter missing\n");
192 exit(1);
194 fp = fopen(argv[2],"rb");
195 if (! fp)
197 fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]);
198 perror("rdflib");
199 exit(1);
202 fp2 = NULL;
203 while (! feof(fp) ) {
204 /* read name */
205 p = buf;
206 while( ( *(p++) = (char) fgetc(fp) ) )
207 if (feof(fp)) break;
209 if (feof(fp)) break;
211 fp2 = NULL;
212 if (argv[1][0] == 'x') {
213 /* check against desired name */
214 if (! strcmp(buf,argv[3]) )
216 fp2 = fopen(argv[4],"wb");
217 if (! fp2)
219 fprintf(stderr,"rdflib: could not open '%s'\n",argv[4]);
220 perror("rdflib");
221 exit(1);
225 else
226 printf("%-40s ", buf);
228 /* step over the RDOFF file, extracting type information for
229 * the listing, and copying it if fp2 != NULL */
231 if (buf[0] == '.') {
233 if (argv[1][0] == 't')
234 for (i = 0; i < 6; i++)
235 printf("%c", copybytes(fp,fp2,1));
236 else
237 copybytes(fp,fp2,6);
239 l = copylong(fp,fp2);
241 if (argv[1][0] == 't') printf(" %ld bytes content\n", l);
243 copybytes(fp,fp2,l);
245 else if ((c=copybytes(fp,fp2,6)) >= '2') /* version 2 or above */
247 l = copylong(fp,fp2);
249 if (argv[1][0] == 't')
250 printf("RDOFF%c %ld bytes content\n", c, l);
251 copybytes(fp,fp2, l); /* entire object */
253 else
255 if (argv[1][0] == 't')
256 printf("RDOFF1\n");
258 * version 1 object, so we don't have an object content
259 * length field.
261 copybytes(fp,fp2, copylong(fp,fp2)); /* header */
262 copybytes(fp,fp2, copylong(fp,fp2)); /* text */
263 copybytes(fp,fp2, copylong(fp,fp2)); /* data */
266 if (fp2)
267 break;
269 fclose(fp);
270 if (fp2)
271 fclose(fp2);
272 else if (argv[1][0] == 'x')
274 fprintf(stderr,"rdflib: module '%s' not found in '%s'\n",
275 argv[3],argv[2]);
276 exit(1);
278 break;
280 default:
281 fprintf(stderr,"rdflib: command '%c' not recognised\n",
282 argv[1][0]);
283 exit(1);
285 return 0;