NASM 0.94
[nasm.git] / rdoff / rdflib.c
blob584656265c194d138c3da10e31f3a9e332fbc27c
1 /* rdflib - manipulate RDOFF library files (.rdl) */
3 /* an rdoff library is simply a sequence of RDOFF object files, each
4 preceded by the name of the module, an ASCII string of up to 255
5 characters, terminated by a zero. There may be an optional
6 directory placed on the end of the file. The format of the
7 directory will be 'RDL' followed by a version number, followed by
8 the length of the directory, and then the directory, the format of
9 which has not yet been designed. */
11 #include <stdio.h>
12 #include <errno.h>
13 #include <string.h>
15 /* functions supported:
16 create a library (no extra operands required)
17 add a module from a library (requires filename and name to give mod.)
18 remove a module from a library (requires given name)
19 extract a module from the library (requires given name and filename)
20 list modules */
22 const char *usage =
23 "usage:\n"
24 " rdflib x libname [extra operands]\n\n"
25 " where x is one of:\n"
26 " c - create library\n"
27 " a - add module (operands = filename module-name)\n"
28 " r - remove (module-name)\n"
29 " x - extract (module-name filename)\n"
30 " t - list\n";
32 char **_argv;
34 #define _ENDIANNESS 0 /* 0 for little, 1 for big */
36 static void longtolocal(long * l)
38 #if _ENDIANNESS
39 unsigned char t;
40 unsigned char * p = (unsigned char *) l;
42 t = p[0];
43 p[0] = p[3];
44 p[3] = t;
45 t = p[1];
46 p[1] = p[2];
47 p[2] = p[1];
48 #endif
51 void copybytes(FILE *fp, FILE *fp2, int n)
53 int i,t;
55 for (i = 0 ; i < n; i++ )
57 t = fgetc(fp);
58 if (t == EOF)
60 fprintf(stderr,"ldrdf: premature end of file in '%s'\n",
61 _argv[2]);
62 exit(1);
64 if (fp2)
65 if (fputc(t, fp2) == EOF)
67 fprintf(stderr,"ldrdf: write error\n");
68 exit(1);
73 long copylong(FILE *fp, FILE *fp2)
75 long l;
76 int i,t;
77 unsigned char * p = (unsigned char *) &l;
80 for (i = 0 ; i < 4; i++ ) /* skip magic no */
82 t = fgetc(fp);
83 if (t == EOF)
85 fprintf(stderr,"ldrdf: premature end of file in '%s'\n",
86 _argv[2]);
87 exit(1);
89 if (fp2)
90 if (fputc(t, fp2) == EOF)
92 fprintf(stderr,"ldrdf: write error\n");
93 exit(1);
95 *p++ = t;
97 longtolocal (&l);
98 return l;
101 int main(int argc, char **argv)
103 FILE *fp, *fp2;
104 char *p, buf[256];
105 int i;
107 _argv = argv;
109 if (argc < 3 || !strncmp(argv[1],"-h",2) || !strncmp(argv[1],"--h",3))
111 printf(usage);
112 exit(1);
115 switch(argv[1][0])
117 case 'c': /* create library */
118 fp = fopen(argv[2],"wb");
119 if (! fp) {
120 fprintf(stderr,"ldrdf: could not open '%s'\n",argv[2]);
121 perror("ldrdf");
122 exit(1);
124 fclose(fp);
125 break;
127 case 'a': /* add module */
128 if (argc < 5) {
129 fprintf(stderr,"ldrdf: required parameter missing\n");
130 exit(1);
132 fp = fopen(argv[2],"ab");
133 if (! fp)
135 fprintf(stderr,"ldrdf: could not open '%s'\n",argv[2]);
136 perror("ldrdf");
137 exit(1);
140 fp2 = fopen(argv[3],"rb");
141 if (! fp)
143 fprintf(stderr,"ldrdf: could not open '%s'\n",argv[3]);
144 perror("ldrdf");
145 exit(1);
148 p = argv[4];
149 do {
150 if ( fputc(*p,fp) == EOF ) {
151 fprintf(stderr,"ldrdf: write error\n");
152 exit(1);
154 } while (*p++);
156 while (! feof (fp2) ) {
157 i = fgetc (fp2);
158 if (i == EOF) {
159 break;
162 if ( fputc(i, fp) == EOF ) {
163 fprintf(stderr,"ldrdf: write error\n");
164 exit(1);
167 fclose(fp2);
168 fclose(fp);
169 break;
171 case 'x':
172 if (argc < 5) {
173 fprintf(stderr,"ldrdf: required parameter missing\n");
174 exit(1);
177 fp = fopen(argv[2],"rb");
178 if (! fp)
180 fprintf(stderr,"ldrdf: could not open '%s'\n",argv[2]);
181 perror("ldrdf");
182 exit(1);
185 fp2 = NULL;
186 while (! feof(fp) ) {
187 /* read name */
188 p = buf;
189 while( ( *(p++) = (char) fgetc(fp) ) )
190 if (feof(fp)) break;
192 if (feof(fp)) break;
194 /* check against desired name */
195 if (! strcmp(buf,argv[3]) )
197 fp2 = fopen(argv[4],"wb");
198 if (! fp2)
200 fprintf(stderr,"ldrdf: could not open '%s'\n", argv[4]);
201 perror("ldrdf");
202 exit(1);
205 else
206 fp2 = NULL;
208 /* step over the RDOFF file, copying it if fp2 != NULL */
209 copybytes(fp,fp2,6); /* magic number */
210 copybytes(fp,fp2, copylong(fp,fp2)); /* header */
211 copybytes(fp,fp2, copylong(fp,fp2)); /* text */
212 copybytes(fp,fp2, copylong(fp,fp2)); /* data */
214 if (fp2)
215 break;
217 fclose(fp);
218 if (fp2)
219 fclose(fp2);
220 else
222 fprintf(stderr,"ldrdf: module '%s' not found in '%s'\n",
223 argv[3],argv[2]);
224 exit(1);
226 break;
228 default:
229 fprintf(stderr,"ldrdf: command '%c' not recognised\n",
230 argv[1][0]);
231 exit(1);
233 return 0;