ack/util/arch/archiver.c

927 lines
18 KiB
C
Raw Permalink Normal View History

1987-03-09 19:15:41 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
2019-03-24 09:37:49 +00:00
/* ar - archiver Author: Michiel Huisjes */
1987-01-12 17:21:59 +00:00
/* Made into arch/aal by Ceriel Jacobs
*/
2019-03-24 09:37:49 +00:00
1987-01-12 17:21:59 +00:00
/*
1991-08-27 09:44:53 +00:00
* Usage: [arch|aal] [qdprtx][vlcu] archive [file] ...
2019-03-24 09:37:49 +00:00
* possible key
* d: delete
* p: print named files
* q: append
* r: replace (append when not in archive)
* t: print contents of archive
* x: extract
* possible args
* c: don't give "create" message
* u: replace only if dated later than member in archive
* v: verbose
#ifdef DISTRIBUTION
* D: make distribution: use distr_time, uid=2, gid=2, mode=0644
#endif
#ifdef AAL
* s: ignored; for compatibility with GNU ar (says to build index table)
#endif
1987-01-12 17:21:59 +00:00
*/
#include <fcntl.h>
#include <stdlib.h>
2019-03-24 09:37:49 +00:00
#include <time.h>
#include <stdio.h>
#include <string.h>
1987-01-12 17:21:59 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
2019-03-24 09:37:49 +00:00
#include "print.h"
#include "system.h"
#include "object.h"
#include "arch.h"
#include "ranlib.h"
/* UNIX specific */
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
#ifndef unix
#define unix
#endif
#endif
#ifdef unix
#include <unistd.h>
2019-03-24 09:37:49 +00:00
#else
#define getuid() 0
#define getgid() 0
#endif
1991-08-27 09:44:53 +00:00
#ifdef AAL
2019-03-24 09:37:49 +00:00
#include "out.h"
#define MAGIC_NUMBER AALMAG
long offset;
struct ranlib *tab;
2019-03-24 09:37:49 +00:00
unsigned int tnum = 0;
char *tstrtab;
unsigned int tssiz = 0;
unsigned int tabsz, strtabsz;
1987-01-12 17:21:59 +00:00
#else
2019-03-24 09:37:49 +00:00
#define MAGIC_NUMBER ARMAG
1987-01-12 17:21:59 +00:00
#endif
2019-03-24 09:37:49 +00:00
#define odd(nr) (nr & 01)
#define even(nr) (odd(nr) ? nr + 1 : nr)
1987-01-12 17:21:59 +00:00
typedef char BOOL;
2019-03-24 09:37:49 +00:00
#define FALSE 0
#define TRUE 1
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
#define READ 0
#define APPEND 2
#define CREATE 1
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
#define MEMBER struct ar_hdr
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
#define NIL_PTR ((char *) 0)
#define NIL_MEM ((MEMBER *) 0)
#define NIL_LONG ((long *) 0)
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
#define IO_SIZE (10 * 1024)
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
#define equal(str1, str2) (!strncmp((str1), (str2), AR_NAME_MAX))
1995-06-28 09:46:39 +00:00
1987-01-12 17:21:59 +00:00
BOOL verbose;
BOOL app_fl;
BOOL ex_fl;
BOOL show_fl;
2019-03-24 09:37:49 +00:00
/* print files found in archive. */
1987-01-12 17:21:59 +00:00
BOOL pr_fl;
1991-08-27 09:44:53 +00:00
BOOL u_fl;
1987-01-12 17:21:59 +00:00
BOOL rep_fl;
BOOL del_fl;
1987-01-19 09:36:35 +00:00
BOOL nocr_fl;
1987-01-12 17:21:59 +00:00
BOOL local_fl;
#ifdef DISTRIBUTION
BOOL distr_fl;
2019-03-24 09:37:49 +00:00
time_t distr_time;
#endif
#ifndef S_ISUID
#define S_ISUID 0
#endif
#ifndef S_ISGID
#define S_ISGID 0
#endif
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
#define MODE_COUNT 11
1987-01-12 17:21:59 +00:00
char io_buffer[IO_SIZE];
char *progname;
char *temp_arch;
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
void do_object(FILE* f, long size);
void do_names(struct outhead *headp);
void enter_name(struct outname *namep);
void write_symdef(FILE *ar);
void error(BOOL quit, char *str1, char *str2);
FILE* open_archive(char *name, int mode);
void
catch(int param);
MEMBER *get_member(FILE*);
void get(int argc, char *argv[]);
void add(char *name, FILE* ar, FILE* dst, char *mess);
void extract(FILE* ar, MEMBER *member);
void copy_member(MEMBER *member, FILE* from, FILE* to, BOOL extracting);
char *get_mode(int mode);
void wr_fatal(void);
void rd_fatal(void);
void mwrite(FILE* f, void* address, size_t bytes);
void show(char *s, char *name);
/* Conversion utilities. */
static mode_t ar2mode(short mode);
static short mode2ar(mode_t mode);
/** Maps an AR mode to the current system mode. */
struct modemap
{
short ar_mode;
mode_t mode;
};
/** Mapping table to map an AR mode to a system mode. */
static const struct modemap armodes[MODE_COUNT] =
{
{ AR_IRUSR, S_IRUSR },
{ AR_IWUSR, S_IWUSR },
{ AR_IXUSR, S_IXUSR },
{ AR_IRGRP, S_IRGRP },
{ AR_IWGRP, S_IWGRP },
{ AR_IXGRP, S_IXGRP },
{ AR_IROTH, S_IROTH },
{ AR_IWOTH, S_IWOTH },
{ AR_IXOTH, S_IXOTH },
2019-03-24 09:37:49 +00:00
{ AR_ISUID, S_ISUID },
{ AR_ISGID, S_ISGID } };
/** Convert an "ar" mode to a system specific
* mode.
*/
static mode_t ar2mode(short mode)
{
int i;
mode_t result = 0;
for (i = 0; i < MODE_COUNT; i++)
{
if (mode & armodes[i].ar_mode)
{
result = result | armodes[i].mode;
}
}
return result;
}
/** Convert a system specified mode to
* an ar compatible mode.
*/
static short mode2ar(mode_t mode)
{
int i;
short result = 0;
for (i = 0; i < MODE_COUNT; i++)
{
if (mode & armodes[i].mode)
{
result = result | armodes[i].ar_mode;
}
}
return result;
}
static void usage(void)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
error(TRUE, "usage: %s [qdprtxl][vc] archive [file] ...\n", progname);
1987-01-12 17:21:59 +00:00
}
/*VARARGS2*/
2019-03-24 09:37:49 +00:00
void error(BOOL quit, char *str1, char *str2)
1987-01-12 17:21:59 +00:00
{
char errbuf[256];
2019-03-24 09:37:49 +00:00
sprint(errbuf, str1, str2);
fwrite(errbuf, 1, strlen(errbuf), stderr);
2019-03-24 09:37:49 +00:00
if (quit)
{
remove(temp_arch);
exit(1);
}
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
/** Opens the specified archive. */
FILE* open_archive(char *name, int mode)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
unsigned short magic = 0;
FILE* file = NULL;
if (mode == CREATE)
{
file = fopen(name, "wb+");
if (file == NULL)
error(TRUE, "cannot create %s\n", name);
magic = MAGIC_NUMBER;
wr_int2(file, magic);
return file;
}
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
file = fopen(name, "rb");
if (file == NULL)
{
/* mode APPEND and files does not exist. */
if (mode == APPEND)
{
fclose(open_archive(name, CREATE));
if (!nocr_fl)
error(FALSE, "creating %s\n", name);
return open_archive(name, APPEND);
}
error(TRUE, "cannot open %s\n", name);
}
else
/* file already exists, simply open it for appending */
{
if (mode == APPEND)
{
fclose(file);
file = fopen(name, "a+b");
}
}
fseek(file, 0, SEEK_SET);
magic = rd_unsigned2(file);
if (magic != AALMAG && magic != ARMAG)
error(TRUE, "%s is not in ar format\n", name);
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
return file;
1987-01-12 17:21:59 +00:00
}
void
2019-03-24 09:37:49 +00:00
catch(int param)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
remove(temp_arch);
exit(2);
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
int main(int argc, char *argv[])
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
register char *ptr;
int needs_arg = 0;
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
progname = argv[0];
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
if (argc < 3)
usage();
2019-03-24 09:37:49 +00:00
for (ptr = argv[1]; *ptr; ptr++)
{
switch (*ptr)
{
case 't':
1987-01-12 17:21:59 +00:00
show_fl = TRUE;
break;
2019-03-24 09:37:49 +00:00
case 'v':
1987-01-12 17:21:59 +00:00
verbose = TRUE;
break;
2019-03-24 09:37:49 +00:00
case 'x':
1987-01-12 17:21:59 +00:00
ex_fl = TRUE;
break;
2019-03-24 09:37:49 +00:00
case 'q':
1989-03-29 14:58:42 +00:00
needs_arg = 1;
1987-01-12 17:21:59 +00:00
app_fl = TRUE;
break;
2019-03-24 09:37:49 +00:00
case 'c':
1987-01-19 09:36:35 +00:00
nocr_fl = TRUE;
break;
1991-08-27 09:44:53 +00:00
case 'u':
u_fl = TRUE;
break;
2019-03-24 09:37:49 +00:00
case 'p':
1989-03-29 14:58:42 +00:00
needs_arg = 1;
1987-01-12 17:21:59 +00:00
pr_fl = TRUE;
break;
2019-03-24 09:37:49 +00:00
case 'd':
1989-03-29 14:58:42 +00:00
needs_arg = 1;
1987-01-12 17:21:59 +00:00
del_fl = TRUE;
break;
2019-03-24 09:37:49 +00:00
case 'r':
1989-03-29 14:58:42 +00:00
needs_arg = 1;
1987-01-12 17:21:59 +00:00
rep_fl = TRUE;
break;
#ifdef DISTRIBUTION
2019-03-24 09:37:49 +00:00
case 'D' :
distr_fl = TRUE;
break;
#endif
#ifdef AAL
case 's':
break;
#endif
2019-03-24 09:37:49 +00:00
default:
1987-01-12 17:21:59 +00:00
usage();
2019-03-24 09:37:49 +00:00
}
1987-01-12 17:21:59 +00:00
}
1989-03-29 14:58:42 +00:00
2019-03-24 09:37:49 +00:00
if (needs_arg && argc <= 3)
usage();
#ifdef DISTRIBUTION
2019-03-24 09:37:49 +00:00
if (distr_fl)
{
static struct stat statbuf;
2019-03-24 09:37:49 +00:00
stat(progname, &statbuf);
distr_time = statbuf.st_mtime;
}
#endif
temp_arch = sys_maketempfile("aal", "dat");
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
if (app_fl + ex_fl + del_fl + rep_fl + show_fl + pr_fl != 1)
usage();
2019-03-24 09:37:49 +00:00
if (u_fl && !rep_fl)
usage();
1991-08-27 09:44:53 +00:00
2019-03-24 09:37:49 +00:00
if (rep_fl || del_fl
1987-01-12 17:21:59 +00:00
#ifdef AAL
2019-03-24 09:37:49 +00:00
|| app_fl
1987-01-12 17:21:59 +00:00
#endif
2019-03-24 09:37:49 +00:00
)
{
/*fclose(mkstemp(temp_arch));*/
}
#ifdef AAL
2019-03-24 09:37:49 +00:00
tab = (struct ranlib *) malloc(512 * sizeof(struct ranlib));
tstrtab = malloc(4096);
if (!tab || !tstrtab)
error(TRUE, "Out of core\n", NULL);
tabsz = 512;
strtabsz = 4096;
#endif
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
signal(SIGINT,
catch);
get(argc, argv);
2019-03-24 09:37:49 +00:00
return 0;
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
/* Read next member of in the archive file "f". */
MEMBER *get_member(FILE *f)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
static MEMBER member;
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
again:
if (rd_arhdr(f, &member) == 0)
1987-01-12 17:21:59 +00:00
return NIL_MEM;
2019-03-24 09:37:49 +00:00
if (member.ar_size < 0)
{
error(TRUE, "archive has member with negative size\n",NULL);
}
if (equal(SYMDEF, member.ar_name))
{
fseek(f, member.ar_size, SEEK_CUR);
goto again;
}
return &member;
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
void get(int argc, char *argv[])
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
register MEMBER *member;
FILE *ar_f;
int i = 0;
char buffer[FILENAME_MAX];
size_t read_chars;
FILE* temp_fd;
ar_f = open_archive(argv[2], (show_fl || pr_fl || ex_fl) ? READ : APPEND);
if (rep_fl || del_fl
1987-01-12 17:21:59 +00:00
#ifdef AAL
2019-03-24 09:37:49 +00:00
|| app_fl
1987-01-12 17:21:59 +00:00
#endif
2019-03-24 09:37:49 +00:00
)
1987-01-12 17:21:59 +00:00
temp_fd = open_archive(temp_arch, CREATE);
2019-03-24 09:37:49 +00:00
while ((member = get_member(ar_f)) != NIL_MEM)
{
if (argc > 3)
{
for (i = 3; i < argc; i++)
{
sys_basename(argv[i],buffer);
if (equal(buffer, member->ar_name))
2019-03-25 16:58:36 +00:00
break;
2019-03-24 09:37:49 +00:00
}
if (i == argc || app_fl)
{
if (rep_fl || del_fl
1987-01-12 17:21:59 +00:00
#ifdef AAL
2019-03-24 09:37:49 +00:00
|| app_fl
1987-01-12 17:21:59 +00:00
#endif
2019-03-24 09:37:49 +00:00
)
{
1987-01-12 17:21:59 +00:00
#ifdef AAL
2019-03-24 09:37:49 +00:00
if (i != argc)
{
print("%s: already in archive\n", argv[i]);
argv[i] = "";
}
1987-01-12 17:21:59 +00:00
#endif
2019-03-24 09:37:49 +00:00
wr_arhdr(temp_fd, member);
copy_member(member, ar_f, temp_fd, FALSE);
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
else
{
#ifndef AAL
if (app_fl && i != argc)
{
print("%s: already in archive\n", argv[i]);
argv[i] = "";
}
#endif
2019-03-24 09:37:49 +00:00
fseek(ar_f, even(member->ar_size),SEEK_CUR);
}
continue;
1987-01-12 17:21:59 +00:00
}
}
2019-03-24 09:37:49 +00:00
if (ex_fl || pr_fl)
extract(ar_f,member);
else
{
if (rep_fl)
2019-03-25 16:58:36 +00:00
add(argv[i], ar_f, temp_fd, "r - %s\n");
2019-03-24 09:37:49 +00:00
else if (show_fl)
{
char buf[sizeof(member->ar_name) + 2];
register char *p = buf, *q = member->ar_name;
while (q <= &member->ar_name[sizeof(member->ar_name)-1] && *q)
{
*p++ = *q++;
}
*p++ = '\n';
*p = '\0';
if (verbose)
{
char *mode = get_mode(member->ar_mode);
char *date = ctime(&(member->ar_date));
*(date + 16) = '\0';
*(date + 24) = '\0';
print("%s%3u/%u%7ld %s %s %s",
mode,
(unsigned) (member->ar_uid & 0377),
(unsigned) (member->ar_gid & 0377),
member->ar_size,
date+4,
date+20,
buf);
}
else print(buf);
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
else if (del_fl)
{
show("d - %s\n", member->ar_name);
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
fseek(ar_f, even(member->ar_size), SEEK_CUR);
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
argv[i] = "";
2019-03-25 16:58:36 +00:00
} /* end while */
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
if (argc > 3)
{
for (i = 3; i < argc; i++)
if (argv[i][0] != '\0')
{
1987-01-12 17:21:59 +00:00
#ifndef AAL
if (app_fl)
2019-03-24 09:37:49 +00:00
add(argv[i], ar_f, "a - %s\n");
1987-01-12 17:21:59 +00:00
else
#endif
if (rep_fl
#ifdef AAL
2019-03-24 09:37:49 +00:00
|| app_fl
1987-01-12 17:21:59 +00:00
#endif
)
2019-03-24 09:37:49 +00:00
add(argv[i], ar_f, temp_fd, "a - %s\n");
else
{
1987-01-12 17:21:59 +00:00
print("%s: not found\n", argv[i]);
}
}
2019-03-24 09:37:49 +00:00
}
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
if (rep_fl || del_fl
1987-01-12 17:21:59 +00:00
#ifdef AAL
2019-03-24 09:37:49 +00:00
|| app_fl
1987-01-12 17:21:59 +00:00
#endif
2019-03-24 09:37:49 +00:00
)
{
signal(SIGINT, SIG_IGN);
fclose(ar_f);
fclose(temp_fd);
ar_f = open_archive(argv[2], CREATE);
temp_fd = open_archive(temp_arch, APPEND);
1987-01-12 17:21:59 +00:00
#ifdef AAL
2019-03-24 09:37:49 +00:00
write_symdef(ar_f);
1987-01-12 17:21:59 +00:00
#endif
2019-03-24 09:37:49 +00:00
while ((read_chars = fread(io_buffer, 1, IO_SIZE, temp_fd)) > 0)
mwrite(ar_f, io_buffer, read_chars);
fclose(temp_fd);
remove(temp_arch);
}
fclose(ar_f);
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
/** Add an entry into the archive.
*
* @param[in] name path specification of the file to add.
* @param[in] ar Original ar file for update.
* @param[in] dst Archive name that will have its file added.
*
*/
void add(char *name, FILE* ar, FILE* dst, char *mess)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
static MEMBER member;
size_t read_chars;
struct stat status;
char buffer[FILENAME_MAX];
FILE* src_fd = NULL;
if (stat(name, &status) < 0)
{
error(FALSE, "cannot find %s\n", name);
return;
}
else if (S_ISDIR(status.st_mode))
{
error(FALSE, "%s is a directory (ignored)\n", name);
return;
}
else if (u_fl && status.st_mtime <= member.ar_date)
{
wr_arhdr(dst, &member);
copy_member(&member, ar, dst, FALSE);
return;
}
else if ((src_fd = fopen(name, "rb")) == NULL)
{
error(FALSE, "cannot open %s\n", name);
return;
}
sys_basename(name, buffer);
strncpy (member.ar_name, buffer, sizeof(member.ar_name));
member.ar_uid = status.st_uid;
member.ar_gid = status.st_gid;
member.ar_mode = mode2ar(status.st_mode);
member.ar_date = status.st_mtime;
member.ar_size = status.st_size;
#ifdef DISTRIBUTION
2019-03-24 09:37:49 +00:00
if (distr_fl)
{
member.ar_uid = 2;
member.ar_gid = 2;
member.ar_mode = AR_IUSR | AR_IWUSR | AR_IRGRP | AR_IROTH;
member.ar_date = distr_time;
}
#endif
2019-03-24 09:37:49 +00:00
wr_arhdr(dst, &member);
1987-01-12 17:21:59 +00:00
#ifdef AAL
2019-03-24 09:37:49 +00:00
do_object(src_fd, member.ar_size);
fseek(src_fd, 0L, SEEK_SET);
offset += AR_TOTAL + even(member.ar_size);
1987-01-12 17:21:59 +00:00
#endif
2019-03-24 09:37:49 +00:00
while (status.st_size > 0)
{
size_t x = IO_SIZE;
1987-01-12 17:21:59 +00:00
read_chars = x;
2019-03-24 09:37:49 +00:00
if (status.st_size < x)
{
x = status.st_size;
read_chars = x;
status.st_size = 0;
x = even(x);
}
else status.st_size -= x;
if (fread(io_buffer, 1, read_chars, src_fd) != read_chars)
{
error(FALSE,"%s seems to shrink\n", name);
break;
}
mwrite(dst, io_buffer, x);
}
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
if (verbose)
show(mess, member.ar_name);
fclose(src_fd);
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
/** Extract an archive entry pointed to by member to
* either standard output or to a file.
*
*/
void extract(FILE* ar, MEMBER *member)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
FILE* file = stdout;
char buf[sizeof(member->ar_name) + 1];
strncpy(buf, member->ar_name, sizeof(member->ar_name));
buf[sizeof(member->ar_name)] = 0;
if (pr_fl == FALSE)
{
file = fopen(buf, "wb");
if (file == NULL)
{
error(FALSE, "cannot create %s\n", buf);
file = NULL;
}
};
if (verbose)
{
if (pr_fl == FALSE)
show("x - %s\n", buf);
else
show("\n<%s>\n\n", buf);
}
copy_member(member, ar, file, TRUE);
if (file != NULL)
fclose(file);
if (pr_fl == FALSE)
chmod(buf, ar2mode(member->ar_mode));
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
void copy_member(MEMBER *member, FILE* from, FILE* to, BOOL extracting)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
size_t rest;
long mem_size = member->ar_size;
BOOL is_odd = odd(mem_size) ? TRUE : FALSE;
1987-01-12 17:21:59 +00:00
#ifdef AAL
2019-03-24 09:37:49 +00:00
if (!extracting)
{
long pos = ftell(from);
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
do_object(from, mem_size);
offset += AR_TOTAL + even(mem_size);
fseek(from, pos, SEEK_SET);
}
1987-01-12 17:21:59 +00:00
#endif
2019-03-24 09:37:49 +00:00
do
{
rest = mem_size > (size_t) IO_SIZE ? IO_SIZE : (size_t) mem_size;
if (fread(io_buffer, 1, rest, from) != rest)
{
char buf[sizeof(member->ar_name) + 1];
strncpy(buf, member->ar_name, sizeof(member->ar_name));
buf[sizeof(member->ar_name)] = 0;
error(TRUE, "read error on %s\n", buf);
}
if (to != NULL)
mwrite(to, io_buffer, rest);
mem_size -= (long) rest;
} while (mem_size > 0L);
if (is_odd)
{
fseek(from, 1L, SEEK_CUR);
if ((to != NULL) && (extracting == FALSE))
fseek(to, 1L, SEEK_CUR);
}
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
char *get_mode(int mode)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
static char mode_buf[11];
register int tmp = mode;
int i;
mode_buf[9] = ' ';
for (i = 0; i < 3; i++)
{
mode_buf[i * 3] = (tmp & AR_IRUSR) ? 'r' : '-';
mode_buf[i * 3 + 1] = (tmp & AR_IWUSR) ? 'w' : '-';
mode_buf[i * 3 + 2] = (tmp & AR_IXUSR) ? 'x' : '-';
tmp <<= 3;
}
if (mode & AR_ISUID)
mode_buf[2] = 's';
if (mode & AR_ISGID)
mode_buf[5] = 's';
return mode_buf;
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
void wr_fatal(void)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
error(TRUE, "write error\n", NULL);
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
void rd_fatal(void)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
error(TRUE, "read error\n", NULL);
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
void mwrite(FILE* f, void* address, size_t bytes)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
if (fwrite(address, 1, bytes, f) != bytes)
error(TRUE, "write error\n", NULL);
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
void show(char *s, char *name)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
MEMBER x;
char buf[sizeof(x.ar_name) + 1];
register char *p = buf, *q = name;
while (q <= &name[sizeof(x.ar_name) - 1] && *q)
*p++ = *q++;
*p++ = '\0';
print(s, buf);
1987-01-12 17:21:59 +00:00
}
#ifdef AAL
/*
* Write out the ranlib table: first 4 bytes telling how many ranlib structs
* there are, followed by the ranlib structs,
* then 4 bytes giving the size of the string table, followed by the string
* table itself.
*/
2019-03-24 09:37:49 +00:00
void write_symdef(FILE *ar)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
register struct ranlib *ran;
register int i;
register long delta;
time_t time_value;
MEMBER arbuf;
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
if (!tnum)
return;
1987-01-12 17:21:59 +00:00
if (odd(tssiz))
tstrtab[tssiz++] = '\0';
for (i = 0; i < sizeof(arbuf.ar_name); i++)
arbuf.ar_name[i] = '\0';
strcpy(arbuf.ar_name, SYMDEF);
2019-03-24 09:37:49 +00:00
arbuf.ar_size = 4 + 2 * 4 * (long) tnum + 4 + (long) tssiz;
time(&time_value);
arbuf.ar_date = (long) time_value;
1987-01-12 17:21:59 +00:00
arbuf.ar_uid = getuid();
arbuf.ar_gid = getgid();
2019-03-24 09:37:49 +00:00
arbuf.ar_mode = AR_IRUSR | AR_IRGRP | AR_IROTH;
#ifdef DISTRIBUTION
2019-03-24 09:37:49 +00:00
if (distr_fl)
{
arbuf.ar_uid = 2;
arbuf.ar_gid = 2;
arbuf.ar_date = distr_time;
}
#endif
2019-03-24 09:37:49 +00:00
wr_arhdr(ar, &arbuf);
wr_int4(ar, (long) tnum);
1987-01-12 17:21:59 +00:00
/*
* Account for the space occupied by the magic number
* and the ranlib table.
*/
delta = 2 + AR_TOTAL + arbuf.ar_size;
2019-03-24 09:37:49 +00:00
for (ran = tab; ran < &tab[tnum]; ran++)
{
1987-01-12 17:21:59 +00:00
ran->ran_pos += delta;
}
2019-03-24 09:37:49 +00:00
wr_ranlib(ar, tab, (long) tnum);
wr_int4(ar, (long) tssiz);
2019-03-24 09:37:49 +00:00
wr_bytes(ar, tstrtab, (long) tssiz);
1987-01-12 17:21:59 +00:00
}
/*
* Return whether the bytes in `buf' form a good object header.
1987-01-12 17:21:59 +00:00
* The header is put in `headp'.
*/
2019-03-24 09:37:49 +00:00
int is_outhead(struct outhead *headp)
1987-01-12 17:21:59 +00:00
{
return !BADMAGIC(*headp) && headp->oh_nname != 0;
}
2019-03-24 09:37:49 +00:00
void do_object(FILE* f, long size)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
struct outhead headbuf;
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
if (size < SZ_HEAD)
{
1987-01-12 17:21:59 +00:00
/* It can't be an object file. */
return;
}
/*
* Read a header to see if it is an object file.
*/
2019-03-24 09:37:49 +00:00
if (!rd_fdopen(f))
{
1987-01-12 17:21:59 +00:00
rd_fatal();
}
rd_ohead(&headbuf);
2019-03-24 09:37:49 +00:00
if (!is_outhead(&headbuf))
{
1987-01-12 17:21:59 +00:00
return;
}
do_names(&headbuf);
}
/*
* First skip the names and read in the string table, then seek back to the
* name table and read and write the names one by one. Update the ranlib table
* accordingly.
*/
2019-03-24 09:37:49 +00:00
void do_names(struct outhead *headp)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
register char *strings = NULL;
register int nnames = headp->oh_nname;
1987-01-12 17:21:59 +00:00
#define NNAMES 100
2019-03-24 09:37:49 +00:00
struct outname namebuf[NNAMES];
1988-07-12 20:16:13 +00:00
long xxx = OFF_CHAR(*headp);
1987-01-12 17:21:59 +00:00
2019-03-24 09:37:49 +00:00
if ( (headp->oh_nchar != (unsigned int) headp->oh_nchar)
|| ((strings = malloc((unsigned int) headp->oh_nchar))) == NULL)
{
error(TRUE, "string table too big\n", NULL);
1987-01-12 17:21:59 +00:00
}
rd_string(strings, headp->oh_nchar);
2019-03-24 09:37:49 +00:00
while (nnames)
{
1987-01-12 17:21:59 +00:00
int i = nnames >= NNAMES ? NNAMES : nnames;
register struct outname *p = namebuf;
nnames -= i;
rd_name(namebuf, i);
2019-03-24 09:37:49 +00:00
while (i--)
{
1988-07-12 20:16:13 +00:00
long off = p->on_foff - xxx;
2019-03-24 09:37:49 +00:00
if (p->on_foff == (long) 0)
{
1988-07-12 20:16:13 +00:00
p++;
1987-01-12 17:21:59 +00:00
continue; /* An unrecognizable name. */
1988-07-12 20:16:13 +00:00
}
p->on_mptr = strings + off;
1987-01-12 17:21:59 +00:00
/*
* Only enter names that are exported and are really
* defined. Also enter common names. Note, that
* this might cause problems when the name is really
* defined in a later file, with a value != 0.
* However, this problem also exists on the Unix
* ranlib archives.
1987-01-12 17:21:59 +00:00
*/
2019-03-24 09:37:49 +00:00
if ((p->on_type & S_EXT) && (p->on_type & S_TYP) != S_UND)
1987-01-12 17:21:59 +00:00
enter_name(p);
p++;
}
}
free(strings);
}
2019-03-24 09:37:49 +00:00
void enter_name(struct outname *namep)
1987-01-12 17:21:59 +00:00
{
2019-03-24 09:37:49 +00:00
register char *cp;
if (tnum >= tabsz)
{
tab = (struct ranlib *) realloc((char *) tab,
(tabsz += 512) * sizeof(struct ranlib));
if (!tab)
error(TRUE, "Out of core\n", NULL);
1987-01-12 17:21:59 +00:00
}
tab[tnum].ran_off = tssiz;
tab[tnum].ran_pos = offset;
2019-03-24 09:37:49 +00:00
for (cp = namep->on_mptr;; cp++)
{
if (tssiz >= strtabsz)
{
tstrtab = realloc(tstrtab, (strtabsz += 4096));
2019-03-24 09:37:49 +00:00
if (!tstrtab)
error(TRUE, "string table overflow\n", NULL);
1987-01-12 17:21:59 +00:00
}
2019-03-24 09:37:49 +00:00
tstrtab[tssiz++] = *cp;
if (!*cp)
break;
}
1987-01-12 17:21:59 +00:00
tnum++;
}
#endif /* AAL */