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";
41 #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.");
73 * Print usage instructions
77 printf("Usage: %s [-]{%s}[%s] libfile [module-name] [files]\n",
78 progname
, commands
, modifiers
);
80 " a - add module(s) to the library\n"
81 " d - delete module(s) from the library\n"
82 " n - create the library\n"
83 " r - replace module(s)\n"
84 " t - display contents of library\n"
85 " x - extract module(s)\n"
86 " command specific modifiers:\n"
87 " o - preserve original dates\n"
88 " u - only replace modules that are newer than library contents\n"
89 " generic modifiers:\n"
90 " c - do not warn if the library had to be created\n"
91 " f - use file name as a module name\n"
93 " V - display version information");
97 * Print an error message and exit
99 void error_exit(int errcode
, bool useperror
, const char *fmt
, ...)
103 fprintf(stderr
, "%s: ", progname
);
105 vfprintf(stderr
, fmt
, ap
);
114 * Fill in and write a header
116 void put_header(struct rdlm_hdr
*hdr
, FILE * libfp
, char *modname
)
120 hdr
->hdrsize
= sizeof(*hdr
);
122 hdr
->hdrsize
+= (n
= strlen(modname
) + 1);
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'",
143 if (fputc(t
, fp2
) == EOF
)
144 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
;
159 for (i
= 0; i
< 4; i
++) {
162 error_exit(1, FALSE
, "premature end of file in '%s'",
165 if (fputc(t
, fp2
) == EOF
)
166 error_exit(1, FALSE
, "write error");
174 * Create a new library
176 int create_library(char *libname
)
183 hdr
.date
= time(NULL
);
184 hdr
.owner
= getuid();
185 hdr
.group
= getgid();
186 hdr
.mode
= umask(022);
189 libfp
= fopen(libname
, "wb");
191 error_exit(1, TRUE
, "could not open '%s'\n", libname
);
193 /* Write library header */
194 put_header(&hdr
, libfp
, NULL
);
201 * Add a module to the library
203 int add_module(FILE * libfp
, const char *fname
, char *modname
)
206 struct rdlm_hdr hdr
= { RDLMMAG
, 0, 0, 0, 0, 0, 0 };
211 fprintf(stderr
, "adding module %s\n", modname
);
213 /* Initialize some fields in the module header */
214 if (stat(fname
, &finfo
) < 0)
215 error_exit(1, TRUE
, "could not stat '%s'", fname
);
216 hdr
.date
= finfo
.st_mtime
;
217 hdr
.owner
= finfo
.st_uid
;
218 hdr
.group
= finfo
.st_gid
;
219 hdr
.size
= finfo
.st_size
;
221 modfp
= fopen(fname
, "rb");
223 error_exit(1, TRUE
, "could not open '%s'", fname
);
225 /* Write module header */
226 put_header(&hdr
, libfp
, modname
);
228 /* Put the module itself */
229 while (!feof(modfp
)) {
233 if (fputc(i
, libfp
) == EOF
)
234 error_exit(1, FALSE
, "write error");
244 int main(int argc
, char *argv
[])
246 FILE *libfp
, *tmpfp
, *modfp
= NULL
;
249 char buf
[MAXMODNAMELEN
], *p
= NULL
;
261 /* Check whether some modifiers were specified */
262 for (i
= 1; i
< strlen(argv
[1]); i
++) {
263 switch (c
= argv
[1][i
]) {
265 options
.createok
= TRUE
;
268 options
.usefname
= TRUE
;
271 options
.align
= TRUE
;
274 options
.odate
= TRUE
;
277 options
.fresh
= TRUE
;
286 if (strchr(commands
, c
) == NULL
)
287 error_exit(2, FALSE
, "invalid command or modifier '%c'",
293 error_exit(2, FALSE
, "missing library name");
295 /* Process the command */
296 if (argv
[1][0] == '-')
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
);
349 if (i
!= sizeof(hdr
) || hdr
.magic
!= RDLMMAG
)
350 error_exit(1, FALSE
, "invalid module header");
351 /* Read module name */
352 i
= hdr
.hdrsize
- sizeof(hdr
);
353 if (i
> sizeof(buf
) || 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])) {
360 "extracting module %s to file %s\n", buf
,
362 modfp
= fopen(p
, "wb");
364 error_exit(1, TRUE
, "could not open '%s'", p
);
367 printf("%-40s ", buf
);
368 if (options
.verbose
) {
369 printf("%ld bytes", hdr
.size
);
374 copybytes(libfp
, modfp
, hdr
.size
);
383 error_exit(1, FALSE
, "module '%s' not found in '%s'",
387 case 'r': /* replace module(s) */
389 if (stat(argv
[4], &finfo
) < 0)
390 error_exit(1, TRUE
, "could not stat '%s'", argv
[4]);
391 case 'd': /* delete module(s) */
393 error_exit(2, FALSE
, "required parameter missing");
395 libfp
= fopen(argv
[2], "rb");
397 error_exit(1, TRUE
, "could not open '%s'", argv
[2]);
399 /* Copy the library into a temporary file */
402 error_exit(1, TRUE
, "could not open temporary file");
404 stat(argv
[2], &finfo
);
405 copybytes(libfp
, tmpfp
, finfo
.st_size
);
407 freopen(argv
[2], "wb", libfp
);
409 /* Read library header and write it to a new file */
410 if (fread(&hdr
, 1, sizeof(hdr
), tmpfp
) != sizeof(hdr
) ||
411 hdr
.magic
!= RDLAMAG
)
412 error_exit(1, FALSE
, "invalid library format");
413 put_header(&hdr
, libfp
, NULL
);
415 /* Walk through the library looking for requested module */
416 while (!feof(tmpfp
)) {
417 /* Read module header */
418 if (fread(&hdr
, 1, sizeof(hdr
), tmpfp
) != sizeof(hdr
) ||
419 hdr
.magic
!= RDLMMAG
)
420 error_exit(1, FALSE
, "invalid module header");
421 /* Read module name */
422 i
= hdr
.hdrsize
- sizeof(hdr
);
423 if (i
> sizeof(buf
) || fread(buf
, 1, i
, tmpfp
) != i
)
424 error_exit(1, FALSE
, "invalid module name");
425 /* Check against desired name */
426 if (!strcmp(buf
, argv
[3]) &&
427 (c
== 'd' || !options
.odate
428 || finfo
.st_mtime
<= hdr
.date
)) {
430 fprintf(stderr
, "deleting module %s\n", buf
);
431 fseek(tmpfp
, hdr
.size
, SEEK_CUR
);
434 put_header(&hdr
, libfp
, buf
);
435 copybytes(tmpfp
, libfp
, hdr
.size
);
440 /* Copy new module into library */
441 p
= options
.usefname
? argv
[4] : argv
[3];
442 add_module(libfp
, argv
[4], p
);
445 /* Copy rest of library if any */
446 while (!feof(tmpfp
)) {
447 if ((i
= fgetc(tmpfp
)) == EOF
)
450 if (fputc(i
, libfp
) == EOF
)
451 error_exit(1, FALSE
, "write error");
459 error_exit(2, FALSE
, "invalid command '%c'\n", c
);