1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * rdlar.c - new librarian/archiver for RDOFF2.
47 #include <sys/types.h>
52 #define PROGRAM_VERSION "0.1"
55 const char commands
[] = "adnrtx";
56 const char modifiers
[] = "cflouvV";
58 /** Global variables **/
59 char *progname
= "rdlar";
71 #define _ENDIANNESS 0 /* 0 for little, 1 for big */
74 * Convert int32_t to little endian (if we were compiled on big-endian machine)
76 static void int32_ttolocal(int32_t *l
)
80 uint8_t *p
= (uint8_t *)l
;
92 * Print version information
94 void show_version(void)
96 puts("New RDOFF2 librarian/archiver, version " PROGRAM_VERSION
"\n"
97 "Copyright (c) 2002 RET & COM Research.\n"
98 "This program is free software and distributed under GPL (version 2 or later);\n"
99 "see http://www.gnu.org/copyleft/gpl.html for details.");
103 * Print usage instructions
107 printf("Usage: %s [-]{%s}[%s] libfile [module-name] [files]\n",
108 progname
, commands
, modifiers
);
110 " a - add module(s) to the library\n"
111 " d - delete module(s) from the library\n"
112 " n - create the library\n"
113 " r - replace module(s)\n"
114 " t - display contents of library\n"
115 " x - extract module(s)\n"
116 " command specific modifiers:\n"
117 " o - preserve original dates\n"
118 " u - only replace modules that are newer than library contents\n"
119 " generic modifiers:\n"
120 " c - do not warn if the library had to be created\n"
121 " f - use file name as a module name\n"
123 " V - display version information");
127 * Print an error message and exit
129 void error_exit(int errcode
, bool useperror
, const char *fmt
, ...)
133 fprintf(stderr
, "%s: ", progname
);
135 vfprintf(stderr
, fmt
, ap
);
144 * Fill in and write a header
146 void put_header(struct rdlm_hdr
*hdr
, FILE * libfp
, char *modname
)
150 hdr
->hdrsize
= sizeof(*hdr
);
152 hdr
->hdrsize
+= (n
= strlen(modname
) + 1);
155 if (fwrite(hdr
, 1, sizeof(*hdr
), libfp
) != sizeof(*hdr
) ||
156 (modname
&& (fwrite(modname
, 1, n
, libfp
) != n
)))
157 error_exit(3, true, "could not write header");
161 * Copy n bytes from one file to another and return last character read.
163 char copybytes(FILE * fp
, FILE * fp2
, int n
)
167 for (i
= 0; i
< n
; i
++) {
170 error_exit(1, false, "premature end of file in '%s'",
173 if (fputc(t
, fp2
) == EOF
)
174 error_exit(1, false, "write error");
180 * Copy uint32_t from one file to another.
181 * Return local presentation of int32_t.
183 int32_t copyint32_t(FILE * fp
, FILE * fp2
)
187 uint8_t *p
= (uint8_t *)&l
;
189 for (i
= 0; i
< 4; i
++) {
192 error_exit(1, false, "premature end of file in '%s'",
195 if (fputc(t
, fp2
) == EOF
)
196 error_exit(1, false, "write error");
204 * Create a new library
206 int create_library(char *libname
)
213 hdr
.date
= time(NULL
);
214 hdr
.owner
= getuid();
215 hdr
.group
= getgid();
216 hdr
.mode
= umask(022);
219 libfp
= fopen(libname
, "wb");
221 error_exit(1, true, "could not open '%s'\n", libname
);
223 /* Write library header */
224 put_header(&hdr
, libfp
, NULL
);
231 * Add a module to the library
233 int add_module(FILE * libfp
, const char *fname
, char *modname
)
236 struct rdlm_hdr hdr
= { RDLMMAG
, 0, 0, 0, 0, 0, 0 };
241 fprintf(stderr
, "adding module %s\n", modname
);
243 /* Initialize some fields in the module header */
244 if (stat(fname
, &finfo
) < 0)
245 error_exit(1, true, "could not stat '%s'", fname
);
246 hdr
.date
= finfo
.st_mtime
;
247 hdr
.owner
= finfo
.st_uid
;
248 hdr
.group
= finfo
.st_gid
;
249 hdr
.size
= finfo
.st_size
;
251 modfp
= fopen(fname
, "rb");
253 error_exit(1, true, "could not open '%s'", fname
);
255 /* Write module header */
256 put_header(&hdr
, libfp
, modname
);
258 /* Put the module itself */
259 while (!feof(modfp
)) {
263 if (fputc(i
, libfp
) == EOF
)
264 error_exit(1, false, "write error");
274 int main(int argc
, char **argv
)
276 FILE *libfp
, *tmpfp
, *modfp
= NULL
;
279 char buf
[MAXMODNAMELEN
], *p
= NULL
;
291 /* Check whether some modifiers were specified */
292 for (i
= 1; i
< strlen(argv
[1]); i
++) {
293 switch (c
= argv
[1][i
]) {
295 options
.createok
= true;
298 options
.usefname
= true;
301 options
.align
= true;
304 options
.odate
= true;
307 options
.fresh
= true;
316 if (strchr(commands
, c
) == NULL
)
317 error_exit(2, false, "invalid command or modifier '%c'",
323 error_exit(2, false, "missing library name");
325 /* Process the command */
326 if (argv
[1][0] == '-')
328 switch (c
= argv
[1][0]) {
329 case 'a': /* add a module */
330 if (argc
< 4 || (!options
.usefname
&& argc
!= 5))
331 error_exit(2, false, "invalid number of arguments");
333 /* Check if a library already exists. If not - create it */
334 if (access(argv
[2], F_OK
) < 0) {
335 if (!options
.createok
)
336 fprintf(stderr
, "creating library %s\n", argv
[2]);
337 create_library(argv
[2]);
340 libfp
= fopen(argv
[2], "ab");
342 error_exit(1, true, "could not open '%s'", argv
[2]);
344 if (!options
.usefname
)
345 add_module(libfp
, argv
[4], argv
[3]);
347 for (i
= 3; i
< argc
; i
++)
348 add_module(libfp
, argv
[i
], argv
[i
]);
353 case 'n': /* create library */
354 create_library(argv
[2]);
357 case 'x': /* extract module(s) */
358 if (!options
.usefname
)
361 error_exit(2, false, "required parameter missing");
362 p
= options
.usefname
? argv
[3] : argv
[4];
363 case 't': /* list library contents */
364 libfp
= fopen(argv
[2], "rb");
366 error_exit(1, true, "could not open '%s'\n", argv
[2]);
368 /* Read library header */
369 if (fread(&hdr
, 1, sizeof(hdr
), libfp
) != sizeof(hdr
) ||
370 hdr
.magic
!= RDLAMAG
)
371 error_exit(1, false, "invalid library format");
373 /* Walk through the library looking for requested module */
374 while (!feof(libfp
)) {
375 /* Read module header */
376 i
= fread(&hdr
, 1, sizeof(hdr
), libfp
);
379 if (i
!= sizeof(hdr
) || hdr
.magic
!= RDLMMAG
)
380 error_exit(1, false, "invalid module header");
381 /* Read module name */
382 i
= hdr
.hdrsize
- sizeof(hdr
);
383 if (i
> sizeof(buf
) || fread(buf
, 1, i
, libfp
) != i
)
384 error_exit(1, false, "invalid module name");
386 /* Check against desired name */
387 if (!strcmp(buf
, argv
[3])) {
390 "extracting module %s to file %s\n", buf
,
392 modfp
= fopen(p
, "wb");
394 error_exit(1, true, "could not open '%s'", p
);
397 printf("%-40s ", buf
);
398 if (options
.verbose
) {
399 printf("%ld bytes", hdr
.size
);
404 copybytes(libfp
, modfp
, hdr
.size
);
413 error_exit(1, false, "module '%s' not found in '%s'",
417 case 'r': /* replace module(s) */
419 if (stat(argv
[4], &finfo
) < 0)
420 error_exit(1, true, "could not stat '%s'", argv
[4]);
421 case 'd': /* delete module(s) */
423 error_exit(2, false, "required parameter missing");
425 libfp
= fopen(argv
[2], "rb");
427 error_exit(1, true, "could not open '%s'", argv
[2]);
429 /* Copy the library into a temporary file */
432 error_exit(1, true, "could not open temporary file");
434 stat(argv
[2], &finfo
);
435 copybytes(libfp
, tmpfp
, finfo
.st_size
);
437 freopen(argv
[2], "wb", libfp
);
439 /* Read library header and write it to a new file */
440 if (fread(&hdr
, 1, sizeof(hdr
), tmpfp
) != sizeof(hdr
) ||
441 hdr
.magic
!= RDLAMAG
)
442 error_exit(1, false, "invalid library format");
443 put_header(&hdr
, libfp
, NULL
);
445 /* Walk through the library looking for requested module */
446 while (!feof(tmpfp
)) {
447 /* Read module header */
448 if (fread(&hdr
, 1, sizeof(hdr
), tmpfp
) != sizeof(hdr
) ||
449 hdr
.magic
!= RDLMMAG
)
450 error_exit(1, false, "invalid module header");
451 /* Read module name */
452 i
= hdr
.hdrsize
- sizeof(hdr
);
453 if (i
> sizeof(buf
) || fread(buf
, 1, i
, tmpfp
) != i
)
454 error_exit(1, false, "invalid module name");
455 /* Check against desired name */
456 if (!strcmp(buf
, argv
[3]) &&
457 (c
== 'd' || !options
.odate
458 || finfo
.st_mtime
<= hdr
.date
)) {
460 fprintf(stderr
, "deleting module %s\n", buf
);
461 fseek(tmpfp
, hdr
.size
, SEEK_CUR
);
464 put_header(&hdr
, libfp
, buf
);
465 copybytes(tmpfp
, libfp
, hdr
.size
);
470 /* Copy new module into library */
471 p
= options
.usefname
? argv
[4] : argv
[3];
472 add_module(libfp
, argv
[4], p
);
475 /* Copy rest of library if any */
476 while (!feof(tmpfp
)) {
477 if ((i
= fgetc(tmpfp
)) == EOF
)
480 if (fputc(i
, libfp
) == EOF
)
481 error_exit(1, false, "write error");
489 error_exit(2, false, "invalid command '%c'\n", c
);