nasm/rdoff/rdlar.c

493 lines
14 KiB
C
Raw Normal View History

/* ----------------------------------------------------------------------- *
*
* Copyright 1996-2009 The NASM Authors - All Rights Reserved
* See the file AUTHORS included with the NASM distribution for
* the specific copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ----------------------------------------------------------------------- */
2002-12-04 22:01:01 +08:00
/*
* rdlar.c - new librarian/archiver for RDOFF2.
*/
2005-01-16 06:15:51 +08:00
#include "compiler.h"
2002-12-04 22:01:01 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "rdlar.h"
#define PROGRAM_VERSION "0.1"
/** Constants **/
const char commands[] = "adnrtx";
const char modifiers[] = "cflouvV";
2002-12-04 22:01:01 +08:00
/** Global variables **/
char *progname = "rdlar";
char **_argv = NULL;
2002-12-04 22:01:01 +08:00
struct {
bool createok;
bool usefname;
bool align;
bool odate;
bool fresh;
int verbose;
2005-01-16 06:15:51 +08:00
} options = {
0, 0, 0, 0, 0, 0};
2002-12-04 22:01:01 +08:00
2005-01-16 06:15:51 +08:00
#define _ENDIANNESS 0 /* 0 for little, 1 for big */
2002-12-04 22:01:01 +08:00
/*
* Convert int32_t to little endian (if we were compiled on big-endian machine)
2002-12-04 22:01:01 +08:00
*/
static void int32_ttolocal(int32_t *l)
2002-12-04 22:01:01 +08:00
{
#if _ENDIANNESS
uint8_t t;
uint8_t *p = (uint8_t *)l;
2002-12-04 22:01:01 +08:00
t = p[0];
p[0] = p[3];
p[3] = t;
t = p[1];
p[1] = p[2];
p[2] = p[1];
#endif
}
/*
* Print version information
*/
void show_version(void)
{
puts("New RDOFF2 librarian/archiver, version " PROGRAM_VERSION);
2002-12-04 22:01:01 +08:00
}
/*
* Print usage instructions
*/
void usage(void)
{
printf("Usage: %s [-]{%s}[%s] libfile [module-name] [files]\n",
2005-01-16 06:15:51 +08:00
progname, commands, modifiers);
2002-12-04 22:01:01 +08:00
puts(" commands:\n"
" a - add module(s) to the library\n"
2005-01-16 06:15:51 +08:00
" d - delete module(s) from the library\n"
" n - create the library\n"
" r - replace module(s)\n"
" t - display contents of library\n"
" x - extract module(s)\n"
" command specific modifiers:\n"
" o - preserve original dates\n"
" u - only replace modules that are newer than library contents\n"
" generic modifiers:\n"
" c - do not warn if the library had to be created\n"
" f - use file name as a module name\n"
" v - be verbose\n"
" V - display version information");
2002-12-04 22:01:01 +08:00
}
/*
* Print an error message and exit
*/
void error_exit(int errcode, bool useperror, const char *fmt, ...)
2002-12-04 22:01:01 +08:00
{
va_list ap;
2005-01-16 06:15:51 +08:00
2002-12-04 22:01:01 +08:00
fprintf(stderr, "%s: ", progname);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
putc('\n', stderr);
2005-01-16 06:15:51 +08:00
if (useperror)
perror(progname);
2002-12-04 22:01:01 +08:00
exit(errcode);
}
/*
* Fill in and write a header
*/
void put_header(struct rdlm_hdr *hdr, FILE * libfp, char *modname)
2002-12-04 22:01:01 +08:00
{
int n = 0;
2005-01-16 06:15:51 +08:00
2002-12-04 22:01:01 +08:00
hdr->hdrsize = sizeof(*hdr);
2005-01-16 06:15:51 +08:00
if (modname)
hdr->hdrsize += (n = strlen(modname) + 1);
if (libfp == NULL)
return;
2002-12-04 22:01:01 +08:00
if (fwrite(hdr, 1, sizeof(*hdr), libfp) != sizeof(*hdr) ||
2005-01-16 06:15:51 +08:00
(modname && (fwrite(modname, 1, n, libfp) != n)))
error_exit(3, true, "could not write header");
2002-12-04 22:01:01 +08:00
}
/*
* Copy n bytes from one file to another and return last character read.
2002-12-04 22:01:01 +08:00
*/
char copybytes(FILE * fp, FILE * fp2, int n)
2002-12-04 22:01:01 +08:00
{
int i, t = 0;
2005-01-16 06:15:51 +08:00
for (i = 0; i < n; i++) {
t = fgetc(fp);
if (t == EOF)
error_exit(1, false, "premature end of file in '%s'",
2005-01-16 06:15:51 +08:00
_argv[2]);
if (fp2)
if (fputc(t, fp2) == EOF)
error_exit(1, false, "write error");
2002-12-04 22:01:01 +08:00
}
return (char)t;
2002-12-04 22:01:01 +08:00
}
/*
* Copy uint32_t from one file to another.
* Return local presentation of int32_t.
2002-12-04 22:01:01 +08:00
*/
int32_t copyint32_t(FILE * fp, FILE * fp2)
2002-12-04 22:01:01 +08:00
{
int32_t l;
2005-01-16 06:15:51 +08:00
int i, t;
uint8_t *p = (uint8_t *)&l;
2005-01-16 06:15:51 +08:00
for (i = 0; i < 4; i++) {
t = fgetc(fp);
if (t == EOF)
error_exit(1, false, "premature end of file in '%s'",
2005-01-16 06:15:51 +08:00
_argv[2]);
if (fp2)
if (fputc(t, fp2) == EOF)
error_exit(1, false, "write error");
2005-01-16 06:15:51 +08:00
*p++ = t;
2002-12-04 22:01:01 +08:00
}
int32_ttolocal(&l);
2002-12-04 22:01:01 +08:00
return l;
}
/*
* Create a new library
*/
int create_library(char *libname)
2002-12-04 22:01:01 +08:00
{
FILE *libfp;
struct rdlm_hdr hdr;
hdr.magic = RDLAMAG;
hdr.hdrsize = 0;
hdr.date = time(NULL);
hdr.owner = getuid();
hdr.group = getgid();
hdr.mode = umask(022);
hdr.size = 0;
2005-01-16 06:15:51 +08:00
2002-12-04 22:01:01 +08:00
libfp = fopen(libname, "wb");
if (!libfp)
error_exit(1, true, "could not open '%s'\n", libname);
2005-01-16 06:15:51 +08:00
2002-12-04 22:01:01 +08:00
/* Write library header */
put_header(&hdr, libfp, NULL);
2005-01-16 06:15:51 +08:00
2002-12-04 22:01:01 +08:00
fclose(libfp);
return true;
2002-12-04 22:01:01 +08:00
}
/*
* Add a module to the library
*/
int add_module(FILE * libfp, const char *fname, char *modname)
2002-12-04 22:01:01 +08:00
{
FILE *modfp;
struct rdlm_hdr hdr = { RDLMMAG, 0, 0, 0, 0, 0, 0 };
struct stat finfo;
int i;
2005-01-16 06:15:51 +08:00
2002-12-04 22:01:01 +08:00
if (options.verbose)
2005-01-16 06:15:51 +08:00
fprintf(stderr, "adding module %s\n", modname);
2002-12-04 22:01:01 +08:00
/* Initialize some fields in the module header */
if (stat(fname, &finfo) < 0)
error_exit(1, true, "could not stat '%s'", fname);
2002-12-04 22:01:01 +08:00
hdr.date = finfo.st_mtime;
hdr.owner = finfo.st_uid;
hdr.group = finfo.st_gid;
2005-01-16 06:15:51 +08:00
hdr.size = finfo.st_size;
2002-12-04 22:01:01 +08:00
modfp = fopen(fname, "rb");
if (!modfp)
error_exit(1, true, "could not open '%s'", fname);
2002-12-04 22:01:01 +08:00
/* Write module header */
put_header(&hdr, libfp, modname);
2005-01-16 06:15:51 +08:00
/* Put the module itself */
while (!feof(modfp)) {
i = fgetc(modfp);
if (i == EOF)
break;
if (fputc(i, libfp) == EOF)
error_exit(1, false, "write error");
2002-12-04 22:01:01 +08:00
}
2005-01-16 06:15:51 +08:00
2002-12-04 22:01:01 +08:00
fclose(modfp);
return true;
2002-12-04 22:01:01 +08:00
}
/*
* Main
*/
int main(int argc, char **argv)
2002-12-04 22:01:01 +08:00
{
FILE *libfp, *tmpfp, *modfp = NULL;
struct stat finfo;
struct rdlm_hdr hdr;
char buf[MAXMODNAMELEN], *p = NULL;
char c;
2002-12-04 22:01:01 +08:00
int i;
progname = argv[0];
_argv = argv;
2005-01-16 06:15:51 +08:00
2002-12-04 22:01:01 +08:00
if (argc < 2) {
2005-01-16 06:15:51 +08:00
usage();
exit(1);
2002-12-04 22:01:01 +08:00
}
rdoff_init();
2002-12-04 22:01:01 +08:00
/* Check whether some modifiers were specified */
for (i = 1; i < strlen(argv[1]); i++) {
2005-01-16 06:15:51 +08:00
switch (c = argv[1][i]) {
case 'c':
options.createok = true;
2005-01-16 06:15:51 +08:00
break;
case 'f':
options.usefname = true;
2005-01-16 06:15:51 +08:00
break;
case 'l':
options.align = true;
2005-01-16 06:15:51 +08:00
break;
case 'o':
options.odate = true;
2005-01-16 06:15:51 +08:00
break;
case 'u':
options.fresh = true;
2005-01-16 06:15:51 +08:00
break;
case 'v':
options.verbose++;
break;
case 'V':
show_version();
exit(0);
default:
if (strchr(commands, c) == NULL)
error_exit(2, false, "invalid command or modifier '%c'",
2005-01-16 06:15:51 +08:00
c);
}
2002-12-04 22:01:01 +08:00
}
2005-01-16 06:15:51 +08:00
2002-12-04 22:01:01 +08:00
if (argc < 3)
error_exit(2, false, "missing library name");
2002-12-04 22:01:01 +08:00
/* Process the command */
2005-01-16 06:15:51 +08:00
if (argv[1][0] == '-')
argv[1]++;
switch (c = argv[1][0]) {
case 'a': /* add a module */
if (argc < 4 || (!options.usefname && argc != 5))
error_exit(2, false, "invalid number of arguments");
2005-01-16 06:15:51 +08:00
/* Check if a library already exists. If not - create it */
if (access(argv[2], F_OK) < 0) {
if (!options.createok)
fprintf(stderr, "creating library %s\n", argv[2]);
create_library(argv[2]);
}
libfp = fopen(argv[2], "ab");
if (!libfp)
error_exit(1, true, "could not open '%s'", argv[2]);
2005-01-16 06:15:51 +08:00
if (!options.usefname)
add_module(libfp, argv[4], argv[3]);
else
for (i = 3; i < argc; i++)
add_module(libfp, argv[i], argv[i]);
fclose(libfp);
break;
case 'n': /* create library */
create_library(argv[2]);
break;
case 'x': /* extract module(s) */
if (!options.usefname)
argc--;
if (argc < 4)
error_exit(2, false, "required parameter missing");
2005-01-16 06:15:51 +08:00
p = options.usefname ? argv[3] : argv[4];
case 't': /* list library contents */
libfp = fopen(argv[2], "rb");
if (!libfp)
error_exit(1, true, "could not open '%s'\n", argv[2]);
2005-01-16 06:15:51 +08:00
/* Read library header */
if (fread(&hdr, 1, sizeof(hdr), libfp) != sizeof(hdr) ||
hdr.magic != RDLAMAG)
error_exit(1, false, "invalid library format");
2005-01-16 06:15:51 +08:00
/* Walk through the library looking for requested module */
while (!feof(libfp)) {
/* Read module header */
i = fread(&hdr, 1, sizeof(hdr), libfp);
if (feof(libfp))
break;
if (i != sizeof(hdr) || hdr.magic != RDLMMAG)
error_exit(1, false, "invalid module header");
2005-01-16 06:15:51 +08:00
/* Read module name */
i = hdr.hdrsize - sizeof(hdr);
if (i > sizeof(buf) || fread(buf, 1, i, libfp) != i)
error_exit(1, false, "invalid module name");
2005-01-16 06:15:51 +08:00
if (c == 'x') {
/* Check against desired name */
if (!strcmp(buf, argv[3])) {
if (options.verbose)
fprintf(stderr,
"extracting module %s to file %s\n", buf,
p);
modfp = fopen(p, "wb");
if (!modfp)
error_exit(1, true, "could not open '%s'", p);
2005-01-16 06:15:51 +08:00
}
} else {
printf("%-40s ", buf);
if (options.verbose) {
printf("%ld bytes", hdr.size);
}
putchar('\n');
2005-01-16 06:15:51 +08:00
}
copybytes(libfp, modfp, hdr.size);
if (modfp)
break;
}
fclose(libfp);
if (modfp)
fclose(modfp);
else if (c == 'x')
error_exit(1, false, "module '%s' not found in '%s'",
2005-01-16 06:15:51 +08:00
argv[3], argv[2]);
break;
case 'r': /* replace module(s) */
argc--;
if (stat(argv[4], &finfo) < 0)
error_exit(1, true, "could not stat '%s'", argv[4]);
2005-01-16 06:15:51 +08:00
case 'd': /* delete module(s) */
if (argc < 4)
error_exit(2, false, "required parameter missing");
2005-01-16 06:15:51 +08:00
libfp = fopen(argv[2], "rb");
if (!libfp)
error_exit(1, true, "could not open '%s'", argv[2]);
2005-01-16 06:15:51 +08:00
/* Copy the library into a temporary file */
tmpfp = tmpfile();
if (!tmpfp)
error_exit(1, true, "could not open temporary file");
2005-01-16 06:15:51 +08:00
stat(argv[2], &finfo);
copybytes(libfp, tmpfp, finfo.st_size);
rewind(tmpfp);
freopen(argv[2], "wb", libfp);
/* Read library header and write it to a new file */
if (fread(&hdr, 1, sizeof(hdr), tmpfp) != sizeof(hdr) ||
hdr.magic != RDLAMAG)
error_exit(1, false, "invalid library format");
2005-01-16 06:15:51 +08:00
put_header(&hdr, libfp, NULL);
/* Walk through the library looking for requested module */
while (!feof(tmpfp)) {
/* Read module header */
if (fread(&hdr, 1, sizeof(hdr), tmpfp) != sizeof(hdr) ||
hdr.magic != RDLMMAG)
error_exit(1, false, "invalid module header");
2005-01-16 06:15:51 +08:00
/* Read module name */
i = hdr.hdrsize - sizeof(hdr);
if (i > sizeof(buf) || fread(buf, 1, i, tmpfp) != i)
error_exit(1, false, "invalid module name");
2005-01-16 06:15:51 +08:00
/* Check against desired name */
if (!strcmp(buf, argv[3]) &&
(c == 'd' || !options.odate
|| finfo.st_mtime <= hdr.date)) {
if (options.verbose)
fprintf(stderr, "deleting module %s\n", buf);
fseek(tmpfp, hdr.size, SEEK_CUR);
break;
} else {
put_header(&hdr, libfp, buf);
copybytes(tmpfp, libfp, hdr.size);
}
}
if (c == 'r') {
/* Copy new module into library */
p = options.usefname ? argv[4] : argv[3];
add_module(libfp, argv[4], p);
}
/* Copy rest of library if any */
while (!feof(tmpfp)) {
if ((i = fgetc(tmpfp)) == EOF)
break;
if (fputc(i, libfp) == EOF)
error_exit(1, false, "write error");
2005-01-16 06:15:51 +08:00
}
fclose(libfp);
fclose(tmpfp);
break;
default:
error_exit(2, false, "invalid command '%c'\n", c);
2002-12-04 22:01:01 +08:00
}
2005-01-16 06:15:51 +08:00
2002-12-04 22:01:01 +08:00
return 0;
}