2 * rdlar.c - new librarian/archiver for RDOFF2.
3 * Copyright (c) 2002 RET & COM Research.
14 #include <sys/types.h>
19 #define PROGRAM_VERSION "0.1"
22 typedef enum { FALSE
, TRUE
} bool;
25 const char commands
[] = "adnrtx";
26 const char modifiers
[] = "cflouvV";
28 /** Global variables **/
29 char *progname
= "rdlar";
38 } options
= { 0, 0, 0, 0, 0, 0 };
40 #define _ENDIANNESS 0 /* 0 for little, 1 for big */
44 * Convert long to little endian (if we were compiled on big-endian machine)
46 static void longtolocal(long * l
)
50 unsigned char * p
= (unsigned char *) l
;
62 * Print version information
64 void show_version(void)
66 puts("New RDOFF2 librarian/archiver, version " PROGRAM_VERSION
"\n"
67 "Copyright (c) 2002 RET & COM Research.\n"
68 "This program is free software and distributed under GPL (version 2 or later);\n"
69 "see http://www.gnu.org/copyleft/gpl.html for details.");
74 * Print usage instructions
78 printf("Usage: %s [-]{%s}[%s] libfile [module-name] [files]\n",
79 progname
, commands
, modifiers
);
81 " a - add module(s) to the library\n"
82 " d - delete module(s) from the library\n"
83 " n - create the library\n"
84 " r - replace module(s)\n"
85 " t - display contents of library\n"
86 " x - extract module(s)\n"
87 " command specific modifiers:\n"
88 " o - preserve original dates\n"
89 " u - only replace modules that are newer than library contents\n"
90 " generic modifiers:\n"
91 " c - do not warn if the library had to be created\n"
92 " f - use file name as a module name\n"
94 " V - display version information");
99 * Print an error message and exit
101 void error_exit(int errcode
, bool useperror
, const char *fmt
, ...)
105 fprintf(stderr
, "%s: ", progname
);
107 vfprintf(stderr
, fmt
, ap
);
110 if (useperror
) perror(progname
);
116 * Fill in and write a header
118 void put_header(struct rdlm_hdr
*hdr
, FILE *libfp
, char *modname
)
122 hdr
->hdrsize
= sizeof(*hdr
);
123 if (modname
) hdr
->hdrsize
+= (n
= strlen(modname
) + 1);
124 if (libfp
== NULL
) return;
125 if (fwrite(hdr
, 1, sizeof(*hdr
), libfp
) != sizeof(*hdr
) ||
126 (modname
&& (fwrite(modname
, 1, n
, libfp
) != n
)))
127 error_exit(3, TRUE
, "could not write header");
131 * Copy n bytes from one file to another and return last character read.
133 char copybytes(FILE *fp
, FILE *fp2
, int n
)
137 for (i
= 0 ; i
< n
; i
++) {
140 error_exit(1, FALSE
, "premature end of file in '%s'", _argv
[2]);
142 if (fputc(t
, fp2
) == EOF
)
143 error_exit(1, FALSE
, "write error");
150 * Copy unsigned long from one file to another.
151 * Return local presentation of long.
153 long copylong(FILE *fp
, FILE *fp2
)
157 unsigned char * p
= (unsigned char *) &l
;
160 for (i
= 0 ; i
< 4; i
++ ) {
163 error_exit(1, FALSE
, "premature end of file in '%s'", _argv
[2]);
165 if (fputc(t
, fp2
) == EOF
)
166 error_exit(1, FALSE
, "write error");
175 * Create a new library
177 int create_library(char *libname
)
184 hdr
.date
= time(NULL
);
185 hdr
.owner
= getuid();
186 hdr
.group
= getgid();
187 hdr
.mode
= umask(022);
190 libfp
= fopen(libname
, "wb");
192 error_exit(1, TRUE
, "could not open '%s'\n", libname
);
194 /* Write library header */
195 put_header(&hdr
, libfp
, NULL
);
203 * Add a module to the library
205 int add_module(FILE *libfp
, const char *fname
, char *modname
)
208 struct rdlm_hdr hdr
= { RDLMMAG
, 0, 0, 0, 0, 0, 0 };
213 fprintf(stderr
, "adding module %s\n", modname
);
215 /* Initialize some fields in the module header */
216 if (stat(fname
, &finfo
) < 0)
217 error_exit(1, TRUE
, "could not stat '%s'", fname
);
218 hdr
.date
= finfo
.st_mtime
;
219 hdr
.owner
= finfo
.st_uid
;
220 hdr
.group
= finfo
.st_gid
;
221 hdr
.size
= finfo
.st_size
;
223 modfp
= fopen(fname
, "rb");
225 error_exit(1, TRUE
, "could not open '%s'", fname
);
227 /* Write module header */
228 put_header(&hdr
, libfp
, modname
);
230 /* Put the module itself */
231 while (! feof(modfp
)) {
234 if (fputc(i
, libfp
) == EOF
)
235 error_exit(1, FALSE
, "write error");
246 int main(int argc
, char *argv
[])
248 FILE *libfp
, *tmpfp
, *modfp
= NULL
;
251 char buf
[MAXMODNAMELEN
], *p
= NULL
;
263 /* Check whether some modifiers were specified */
264 for (i
= 1; i
< strlen(argv
[1]); i
++) {
265 switch (c
= argv
[1][i
]) {
267 options
.createok
= TRUE
;
270 options
.usefname
= TRUE
;
273 options
.align
= TRUE
;
276 options
.odate
= TRUE
;
279 options
.fresh
= TRUE
;
288 if (strchr(commands
, c
) == NULL
)
289 error_exit(2, FALSE
, "invalid command or modifier '%c'", c
);
294 error_exit(2, FALSE
, "missing library name");
296 /* Process the command */
297 if (argv
[1][0] == '-') argv
[1]++;
298 switch(c
= argv
[1][0]) {
299 case 'a': /* add a module */
300 if (argc
< 4 || (!options
.usefname
&& argc
!= 5))
301 error_exit(2, FALSE
, "invalid number of arguments");
303 /* Check if a library already exists. If not - create it */
304 if (access(argv
[2], F_OK
) < 0) {
305 if (!options
.createok
)
306 fprintf(stderr
, "creating library %s\n", argv
[2]);
307 create_library(argv
[2]);
310 libfp
= fopen(argv
[2], "ab");
312 error_exit(1, TRUE
, "could not open '%s'", argv
[2]);
314 if (!options
.usefname
)
315 add_module(libfp
, argv
[4], argv
[3]);
317 for (i
= 3; i
< argc
; i
++)
318 add_module(libfp
, argv
[i
], argv
[i
]);
323 case 'n': /* create library */
324 create_library(argv
[2]);
327 case 'x': /* extract module(s) */
328 if (!options
.usefname
)
331 error_exit(2, FALSE
, "required parameter missing");
332 p
= options
.usefname
? argv
[3] : argv
[4];
333 case 't': /* list library contents */
334 libfp
= fopen(argv
[2],"rb");
336 error_exit(1, TRUE
, "could not open '%s'\n", argv
[2]);
338 /* Read library header */
339 if (fread(&hdr
, 1, sizeof(hdr
), libfp
) != sizeof(hdr
) ||
340 hdr
.magic
!= RDLAMAG
)
341 error_exit(1, FALSE
, "invalid library format");
343 /* Walk through the library looking for requested module */
344 while (!feof(libfp
)) {
345 /* Read module header */
346 i
= fread(&hdr
, 1, sizeof(hdr
), libfp
);
347 if (feof(libfp
)) break;
348 if (i
!= sizeof(hdr
) || hdr
.magic
!= RDLMMAG
)
349 error_exit(1, FALSE
, "invalid module header");
350 /* Read module name */
351 i
= hdr
.hdrsize
- sizeof(hdr
);
352 if (i
> sizeof(buf
) ||
353 fread(buf
, 1, i
, libfp
) != i
)
354 error_exit(1, FALSE
, "invalid module name");
356 /* Check against desired name */
357 if (!strcmp(buf
, argv
[3])) {
359 fprintf(stderr
, "extracting module %s to file %s\n",
361 modfp
= fopen(p
, "wb");
363 error_exit(1, TRUE
, "could not open '%s'", p
);
366 printf("%-40s ", buf
);
367 if (options
.verbose
) {
368 printf("%ld bytes", hdr
.size
);
373 copybytes(libfp
, modfp
, hdr
.size
);
381 error_exit(1, FALSE
, "module '%s' not found in '%s'",
385 case 'r': /* replace module(s) */
387 if (stat(argv
[4], &finfo
) < 0)
388 error_exit(1, TRUE
, "could not stat '%s'", argv
[4]);
389 case 'd': /* delete module(s) */
391 error_exit(2, FALSE
, "required parameter missing");
393 libfp
= fopen(argv
[2], "rb");
395 error_exit(1, TRUE
, "could not open '%s'", argv
[2]);
397 /* Copy the library into a temporary file */
400 error_exit(1, TRUE
, "could not open temporary file");
402 stat(argv
[2], &finfo
);
403 copybytes(libfp
, tmpfp
, finfo
.st_size
);
405 freopen(argv
[2], "wb", libfp
);
407 /* Read library header and write it to a new file */
408 if (fread(&hdr
, 1, sizeof(hdr
), tmpfp
) != sizeof(hdr
) ||
409 hdr
.magic
!= RDLAMAG
)
410 error_exit(1, FALSE
, "invalid library format");
411 put_header(&hdr
, libfp
, NULL
);
413 /* Walk through the library looking for requested module */
414 while (!feof(tmpfp
)) {
415 /* Read module header */
416 if (fread(&hdr
, 1, sizeof(hdr
), tmpfp
) != sizeof(hdr
) ||
417 hdr
.magic
!= RDLMMAG
)
418 error_exit(1, FALSE
, "invalid module header");
419 /* Read module name */
420 i
= hdr
.hdrsize
- sizeof(hdr
);
421 if (i
> sizeof(buf
) ||
422 fread(buf
, 1, i
, tmpfp
) != i
)
423 error_exit(1, FALSE
, "invalid module name");
424 /* Check against desired name */
425 if (!strcmp(buf
, argv
[3]) &&
426 (c
== 'd' || !options
.odate
|| finfo
.st_mtime
<= hdr
.date
)) {
428 fprintf(stderr
, "deleting module %s\n", buf
);
429 fseek(tmpfp
, hdr
.size
, SEEK_CUR
);
432 put_header(&hdr
, libfp
, buf
);
433 copybytes(tmpfp
, libfp
, hdr
.size
);
438 /* Copy new module into library */
439 p
= options
.usefname
? argv
[4] : argv
[3];
440 add_module(libfp
, argv
[4], p
);
443 /* Copy rest of library if any */
444 while (!feof(tmpfp
)) {
445 if ((i
= fgetc(tmpfp
)) == EOF
)
448 if (fputc(i
, libfp
) == EOF
)
449 error_exit(1, FALSE
, "write error");
457 error_exit(2, FALSE
, "invalid command '%c'\n", c
);