diff options
Diffstat (limited to 'usr.bin/ar')
-rw-r--r-- | usr.bin/ar/Makefile | 16 | ||||
-rw-r--r-- | usr.bin/ar/append.c | 89 | ||||
-rw-r--r-- | usr.bin/ar/ar.1 | 257 | ||||
-rw-r--r-- | usr.bin/ar/ar.5.5 | 145 | ||||
-rw-r--r-- | usr.bin/ar/ar.c | 237 | ||||
-rw-r--r-- | usr.bin/ar/archive.c | 325 | ||||
-rw-r--r-- | usr.bin/ar/archive.h | 105 | ||||
-rw-r--r-- | usr.bin/ar/contents.c | 96 | ||||
-rw-r--r-- | usr.bin/ar/delete.c | 96 | ||||
-rw-r--r-- | usr.bin/ar/extern.h | 54 | ||||
-rw-r--r-- | usr.bin/ar/extract.c | 128 | ||||
-rw-r--r-- | usr.bin/ar/misc.c | 148 | ||||
-rw-r--r-- | usr.bin/ar/move.c | 140 | ||||
-rw-r--r-- | usr.bin/ar/pathnames.h | 40 | ||||
-rw-r--r-- | usr.bin/ar/print.c | 90 | ||||
-rw-r--r-- | usr.bin/ar/replace.c | 176 |
16 files changed, 2142 insertions, 0 deletions
diff --git a/usr.bin/ar/Makefile b/usr.bin/ar/Makefile new file mode 100644 index 0000000..f79edaa --- /dev/null +++ b/usr.bin/ar/Makefile @@ -0,0 +1,16 @@ +# @(#)Makefile 8.1 (Berkeley) 6/6/93 + +PROG= ar +CFLAGS+=-I${.CURDIR} +SRCS= append.c ar.c archive.c contents.c delete.c extract.c misc.c \ + move.c print.c replace.c +MAN1= ar.0 +CLEANFILES=ar.5.0 + +ar.0: ar.5.0 + +afterinstall: + install -c -o ${MANOWN} -g ${MANGRP} -m ${MANMODE} ar.5.0 \ + ${DESTDIR}${MANDIR}5/ar.0 + +.include <bsd.prog.mk> diff --git a/usr.bin/ar/append.c b/usr.bin/ar/append.c new file mode 100644 index 0000000..89db986 --- /dev/null +++ b/usr.bin/ar/append.c @@ -0,0 +1,89 @@ +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Hugh Smith at The University of Guelph. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +static char sccsid[] = "@(#)append.c 8.3 (Berkeley) 4/2/94"; +#endif /* not lint */ + +#include <sys/param.h> +#include <sys/stat.h> + +#include <err.h> +#include <fcntl.h> +#include <unistd.h> +#include <dirent.h> +#include <stdio.h> +#include <string.h> + +#include "archive.h" +#include "extern.h" + +/* + * append -- + * Append files to the archive - modifies original archive or creates + * a new archive if named archive does not exist. + */ +int +append(argv) + char **argv; +{ + int afd, fd, eval; + char *file; + CF cf; + struct stat sb; + + afd = open_archive(O_CREAT|O_RDWR); + if (lseek(afd, (off_t)0, SEEK_END) == (off_t)-1) + error(archive); + + /* Read from disk, write to an archive; pad on write. */ + SETCF(0, 0, afd, archive, WPAD); + for (eval = 0; file = *argv++;) { + if ((fd = open(file, O_RDONLY)) < 0) { + warn("%s", file); + eval = 1; + continue; + } + if (options & AR_V) + (void)printf("q - %s\n", file); + cf.rfd = fd; + cf.rname = file; + put_arobj(&cf, &sb); + (void)close(fd); + } + close_archive(afd); + return (eval); +} diff --git a/usr.bin/ar/ar.1 b/usr.bin/ar/ar.1 new file mode 100644 index 0000000..026aed6 --- /dev/null +++ b/usr.bin/ar/ar.1 @@ -0,0 +1,257 @@ +.\" Copyright (c) 1990, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Hugh Smith at The University of Guelph. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. 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. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)ar.1 8.1 (Berkeley) 6/29/93 +.\" +.TH AR 1 "June 29, 1993" +.AT 3 +.SH NAME +ar \- create and maintain library archives +.SH SYNOPSIS +.nf +.ft B +ar -d [-Tv] archive file ... +ar -m [-Tv] archive file ... +ar -m [-abiTv] position archive file ... +ar -p [-Tv] archive [file ...] +ar -q [-cTv] archive file ... +ar -r [-cuTv] archive file ... +ar -r [-abciuTv] position archive file ... +ar -t [-Tv] archive [file ...] +ar -x [-ouTv] archive [file ...] +.fi +.ft R +.SH DESCRIPTION +The +.I ar +utility creates and maintains groups of files combined into an archive. +Once an archive has been created, new files can be added and existing +files can be extracted, deleted, or replaced. +.PP +Files are named in the archive by a single component, i.e., if a file +referenced by a path containing a slash (``/'') is archived it will be +named by the last component of that path. +When matching paths listed on the command line against file names stored +in the archive, only the last component of the path will be compared. +.PP +All informational and error messages use the path listed on the command +line, if any was specified; otherwise the name in the archive is used. +If multiple files in the archive have the same name, and paths are listed +on the command line to ``select'' archive files for an operation, only the +.B first +file with a matching name will be selected. +.PP +The normal use of +.I ar +is for the creation and maintenance of libraries suitable for use with +the loader (see +.IR ld (1)), +although it is not restricted to this purpose. +The options are as follows: +.TP +\-a +A positioning modifier used with the options \-r and \-m. +The files are entered or moved +.B after +the archive member +.IR position , +which must be specified. +.TP +\-b +A positioning modifier used with the options \-r and \-m. +The files are entered or moved +.B before +the archive member +.IR position , +which must be specified. +.TP +\-c +Whenever an archive is created, an informational message to that effect +is written to standard error. +If the \-c option is specified, +.I ar +creates the archive silently. +.TP +\-d +Delete the specified archive files. +.TP +\-i +Identical to the \-b option. +.TP +\-m +Move the specified archive files within the archive. +If one of the options \-a, \-b or \-i is specified, the files are moved +before or after the +.I position +file in the archive. +If none of those options are specified, the files are moved +to the end of the archive. +.TP +\-o +Set the access and modification times of extracted files to the +modification time of the file when it was entered into the archive. +This will fail if the user is not the owner of the extracted file +or the super-user. +.TP +\-p +Write the contents of the specified archive files to the standard output. +If no files are specified, the contents of all the files in the archive +are written in the order they appear in the archive. +.TP +\-q +(Quickly) append the specified files to the archive. +If the archive does not exist a new archive file is created. +Much faster than the \-r option, when creating a large archive +piece-by-piece, as no checking is done to see if the files already +exist in the archive. +.TP +\-r +Replace or add the specified files to the archive. +If the archive does not exist a new archive file is created. +Files that replace existing files do not change the order of the files +within the archive. +New files are appended to the archive unless one of the options \-a, \-b +or \-i is specified. +.TP +\-T +Select and/or name archive members using only the first fifteen characters +of the archive member or command line file name. +The historic archive format had sixteen bytes for the name, but some +historic archiver and loader implementations were unable to handle names +that used the entire space. +This means that file names that are not unique in their first fifteen +characters can subsequently be confused. +A warning message is printed to the standard error output if any file +names are truncated. +(See +.IR ar (5) +for more information.) +.TP +\-t +List the specified files in the order in which they appear in the archive, +each on a separate line. +If no files are specified, all files in the archive are listed. +.TP +\-u +Update files. +When used with the \-r option, files in the archive will be replaced +only if the disk file has a newer modification time than the file in +the archive. +When used with the \-x option, files in the archive will be extracted +only if the archive file has a newer modification time than the file +on disk. +.TP +\-v +Provide verbose output. +When used with the \-d, \-m, \-q or \-x options, +.I ar +gives a file-by-file description of the archive modification. +This description consists of three, white-space separated fields: the +option letter, a dash (``-'') and the file name. +When used with the \-r option, +.I ar +displays the description as above, but the initial letter is an ``a'' if +the file is added to the archive and an ``r'' if the file replaces a file +already in the archive. +.IP +When used with the \-p option, +the name of each printed file, +enclosed in less-than (``<'') and greater-than (``>'') characters, +is written to the standard output before +the contents of the file; +it is preceded by a single newline character, and +followed by two newline characters. +.IP +When used with the \-t option, +.I ar +displays an ``ls -l'' style listing of information about the members of +the archive. +This listing consists of eight, white-space separated fields: +the file permissions (see +.IR strmode (3)), +the decimal user and group ID's separated by a single slash (``/''), +the file size (in bytes), the file modification time (in the +.IR date (1) +format ``%b %e %H:%M %Y''), and the name of the file. +.TP +\-x +Extract the specified archive members into the files named by the command +line arguments. +If no members are specified, all the members of the archive are extracted into +the current directory. +.IP +If the file does not exist, it is created; if it does exist, the owner +and group will be unchanged. +The file access and modification times are the time of the extraction +(but see the \-o option). +The file permissions will be set to those of the file when it was entered +into the archive; this will fail if the user is not the owner of the +extracted file or the super-user. +.PP +The +.I ar +utility exits 0 on success, and >0 if an error occurs. +.SH ENVIRONMENT +.TP +TMPDIR +The pathname of the directory to use when creating temporary files. +.SH FILES +.TP 14 +/tmp +default temporary file directory +.TP 14 +ar.XXXXXX +temporary file names +.SH COMPATIBILITY +By default, +.I ar +writes archives that may be incompatible with historic archives, as +the format used for storing archive members with names longer than +fifteen characters has changed. +This implementation of +.I ar +is backward compatible with previous versions of +.I ar +in that it can read and write (using the \-T option) historic archives. +The \-T option is provided for compatibility only, and will be deleted +in a future release. +See +.IR ar (5) +for more information. +.SH STANDARDS +The +.I ar +utility is expected to offer a superset of the POSIX 1003.2 functionality. +.SH "SEE ALSO" +ld(1), ranlib(1), strmode(3), ar(5) diff --git a/usr.bin/ar/ar.5.5 b/usr.bin/ar/ar.5.5 new file mode 100644 index 0000000..29c8f8c --- /dev/null +++ b/usr.bin/ar/ar.5.5 @@ -0,0 +1,145 @@ +.\" Copyright (c) 1990, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. 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. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)ar.5.5 8.1 (Berkeley) 6/9/93 +.\" +.Dd June 9, 1993 +.Dt AR 5 +.Os +.Sh NAME +.Nm ar +.Nd archive (library) file format +.Sh SYNOPSIS +.Fd #include <ar.h> +.Sh DESCRIPTION +The archive command +.Nm ar +combines several files into one. +Archives are mainly used as libraries of object files intended to be +loaded using the link-editor +.Xr ld 1 . +.Pp +A file created with +.Nm ar +begins with the ``magic'' string "!<arch>\en". +The rest of the archive is made up of objects, each of which is composed +of a header for a file, a possible file name, and the file contents. +The header is portable between machine architectures, and, if the file +contents are printable, the archive is itself printable. +.Pp +The header is made up of six variable length +.Tn ASCII +fields, followed by a +two character trailer. +The fields are the object name (16 characters), the file last modification +time (12 characters), the user and group id's (each 6 characters), the file +mode (8 characters) and the file size (10 characters). +All numeric fields are in decimal, except for the file mode which is in +octal. +.Pp +The modification time is the file +.Fa st_mtime +field, i.e., +.Dv CUT +seconds since +the epoch. +The user and group id's are the file +.Fa st_uid +and +.Fa st_gid +fields. +The file mode is the file +.Fa st_mode +field. +The file size is the file +.Fa st_size +field. +The two-byte trailer is the string "\`\en". +.Pp +Only the name field has any provision for overflow. +If any file name is more than 16 characters in length or contains an +embedded space, the string "#1/" followed by the +.Tn ASCII +length of the +name is written in the name field. +The file size (stored in the archive header) is incremented by the length +of the name. +The name is then written immediately following the archive header. +.Pp +Any unused characters in any of these fields are written as space +characters. +If any fields are their particular maximum number of characters in +length, there will be no separation between the fields. +.Pp +Objects in the archive are always an even number of bytes long; files +which are an odd number of bytes long are padded with a newline (``\en'') +character, although the size in the header does not reflect this. +.Sh SEE ALSO +.Xr ar 1 , +.Xr stat 2 +.Sh HISTORY +There have been at least four +.Nm ar +formats. +The first was denoted by the leading ``magic'' number 0177555 (stored as +type int). +These archives were almost certainly created on a 16-bit machine, and +contain headers made up of five fields. +The fields are the object name (8 characters), the file last modification +time (type long), the user id (type char), the file mode (type char) and +the file size (type unsigned int). +Files were padded to an even number of bytes. +.Pp +The second was denoted by the leading ``magic'' number 0177545 (stored as +type int). +These archives may have been created on either 16 or 32-bit machines, and +contain headers made up of six fields. +The fields are the object name (14 characters), the file last modification +time (type long), the user and group id's (each type char), the file mode +(type int) and the file size (type long). +Files were padded to an even number of bytes. +For more information on converting from this format see +.Xr arcv 8 . +.Pp +The current archive format (without support for long character names and +names with embedded spaces) was introduced in +.Bx 4.0 . +The headers were the same as the current format, with the exception that +names longer than 16 characters were truncated, and names with embedded +spaces (and often trailing spaces) were not supported. +It has been extended for these reasons, +as described above. +This format first appeared in 4.4BSD. +.Sh COMPATIBILITY +No archive format is currently specified by any standard. +.At V +has historically distributed archives in a different format from +all of the above. diff --git a/usr.bin/ar/ar.c b/usr.bin/ar/ar.c new file mode 100644 index 0000000..38089e8 --- /dev/null +++ b/usr.bin/ar/ar.c @@ -0,0 +1,237 @@ +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Hugh Smith at The University of Guelph. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +static char copyright[] = +"@(#) Copyright (c) 1990, 1993, 1994\n\ + The Regents of the University of California. All rights reserved.\n"; +#endif /* not lint */ + +#ifndef lint +static char sccsid[] = "@(#)ar.c 8.3 (Berkeley) 4/2/94"; +#endif /* not lint */ + +#include <sys/param.h> + +#include <ar.h> +#include <dirent.h> +#include <err.h> +#include <paths.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "archive.h" +#include "extern.h" + +CHDR chdr; +u_int options; +char *archive, *envtmp, *posarg, *posname; +static void badoptions __P((char *)); +static void usage __P((void)); + +/* + * main -- + * main basically uses getopt to parse options and calls the appropriate + * functions. Some hacks that let us be backward compatible with 4.3 ar + * option parsing and sanity checking. + */ +int +main(argc, argv) + int argc; + char **argv; +{ + int c; + char *p; + int (*fcall) __P((char **)); + + if (argc < 3) + usage(); + + /* + * Historic versions didn't require a '-' in front of the options. + * Fix it, if necessary. + */ + if (*argv[1] != '-') { + if (!(p = malloc((u_int)(strlen(argv[1]) + 2)))) + err(1, NULL); + *p = '-'; + (void)strcpy(p + 1, argv[1]); + argv[1] = p; + } + + while ((c = getopt(argc, argv, "abcdilmopqrTtuvx")) != EOF) { + switch(c) { + case 'a': + options |= AR_A; + break; + case 'b': + case 'i': + options |= AR_B; + break; + case 'c': + options |= AR_C; + break; + case 'd': + options |= AR_D; + fcall = delete; + break; + case 'l': /* not documented, compatibility only */ + envtmp = "."; + break; + case 'm': + options |= AR_M; + fcall = move; + break; + case 'o': + options |= AR_O; + break; + case 'p': + options |= AR_P; + fcall = print; + break; + case 'q': + options |= AR_Q; + fcall = append; + break; + case 'r': + options |= AR_R; + fcall = replace; + break; + case 'T': + options |= AR_TR; + break; + case 't': + options |= AR_T; + fcall = contents; + break; + case 'u': + options |= AR_U; + break; + case 'v': + options |= AR_V; + break; + case 'x': + options |= AR_X; + fcall = extract; + break; + default: + usage(); + } + } + + argv += optind; + argc -= optind; + + /* One of -dmpqrtx required. */ + if (!(options & (AR_D|AR_M|AR_P|AR_Q|AR_R|AR_T|AR_X))) { + warnx("one of options -dmpqrtx is required"); + usage(); + } + /* Only one of -a and -bi allowed. */ + if (options & AR_A && options & AR_B) { + warnx("only one of -a and -[bi] options allowed"); + usage(); + } + /* -ab require a position argument. */ + if (options & (AR_A|AR_B)) { + if (!(posarg = *argv++)) { + warnx("no position operand specified"); + usage(); + } + posname = rname(posarg); + } + /* -d only valid with -Tv. */ + if (options & AR_D && options & ~(AR_D|AR_TR|AR_V)) + badoptions("-d"); + /* -m only valid with -abiTv. */ + if (options & AR_M && options & ~(AR_A|AR_B|AR_M|AR_TR|AR_V)) + badoptions("-m"); + /* -p only valid with -Tv. */ + if (options & AR_P && options & ~(AR_P|AR_TR|AR_V)) + badoptions("-p"); + /* -q only valid with -cTv. */ + if (options & AR_Q && options & ~(AR_C|AR_Q|AR_TR|AR_V)) + badoptions("-q"); + /* -r only valid with -abcuTv. */ + if (options & AR_R && options & ~(AR_A|AR_B|AR_C|AR_R|AR_U|AR_TR|AR_V)) + badoptions("-r"); + /* -t only valid with -Tv. */ + if (options & AR_T && options & ~(AR_T|AR_TR|AR_V)) + badoptions("-t"); + /* -x only valid with -ouTv. */ + if (options & AR_X && options & ~(AR_O|AR_U|AR_TR|AR_V|AR_X)) + badoptions("-x"); + + if (!(archive = *argv++)) { + warnx("no archive specified"); + usage(); + } + + /* -dmqr require a list of archive elements. */ + if (options & (AR_D|AR_M|AR_Q|AR_R) && !*argv) { + warnx("no archive members specified"); + usage(); + } + + exit((*fcall)(argv)); +} + +static void +badoptions(arg) + char *arg; +{ + + warnx("illegal option combination for %s", arg); + usage(); +} + +static void +usage() +{ + + (void)fprintf(stderr, "usage: ar -d [-Tv] archive file ...\n"); + (void)fprintf(stderr, "\tar -m [-Tv] archive file ...\n"); + (void)fprintf(stderr, "\tar -m [-abiTv] position archive file ...\n"); + (void)fprintf(stderr, "\tar -p [-Tv] archive [file ...]\n"); + (void)fprintf(stderr, "\tar -q [-cTv] archive file ...\n"); + (void)fprintf(stderr, "\tar -r [-cuTv] archive file ...\n"); + (void)fprintf(stderr, "\tar -r [-abciuTv] position archive file ...\n"); + (void)fprintf(stderr, "\tar -t [-Tv] archive [file ...]\n"); + (void)fprintf(stderr, "\tar -x [-ouTv] archive [file ...]\n"); + exit(1); +} diff --git a/usr.bin/ar/archive.c b/usr.bin/ar/archive.c new file mode 100644 index 0000000..10897d9 --- /dev/null +++ b/usr.bin/ar/archive.c @@ -0,0 +1,325 @@ +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Hugh Smith at The University of Guelph. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +static char sccsid[] = "@(#)archive.c 8.3 (Berkeley) 4/2/94"; +#endif /* not lint */ + +#include <sys/param.h> +#include <sys/stat.h> + +#include <ar.h> +#include <dirent.h> +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "archive.h" +#include "extern.h" + +typedef struct ar_hdr HDR; +static char hb[sizeof(HDR) + 1]; /* real header */ + +int +open_archive(mode) + int mode; +{ + int created, fd, nr; + char buf[SARMAG]; + + created = 0; + if (mode & O_CREAT) { + mode |= O_EXCL; + if ((fd = open(archive, mode, DEFFILEMODE)) >= 0) { + /* POSIX.2 puts create message on stderr. */ + if (!(options & AR_C)) + warnx("creating archive %s", archive); + created = 1; + goto opened; + } + if (errno != EEXIST) + error(archive); + mode &= ~O_EXCL; + } + if ((fd = open(archive, mode, DEFFILEMODE)) < 0) + error(archive); + + /* + * Attempt to place a lock on the opened file - if we get an + * error then someone is already working on this library (or + * it's going across NFS). + */ +opened: if (flock(fd, LOCK_EX|LOCK_NB) && errno != EOPNOTSUPP) + error(archive); + + /* + * If not created, O_RDONLY|O_RDWR indicates that it has to be + * in archive format. + */ + if (!created && + ((mode & O_ACCMODE) == O_RDONLY || (mode & O_ACCMODE) == O_RDWR)) { + if ((nr = read(fd, buf, SARMAG) != SARMAG)) { + if (nr >= 0) + badfmt(); + error(archive); + } else if (bcmp(buf, ARMAG, SARMAG)) + badfmt(); + } else if (write(fd, ARMAG, SARMAG) != SARMAG) + error(archive); + return (fd); +} + +void +close_archive(fd) + int fd; +{ + + (void)close(fd); /* Implicit unlock. */ +} + +/* Convert ar header field to an integer. */ +#define AR_ATOI(from, to, len, base) { \ + memmove(buf, from, len); \ + buf[len] = '\0'; \ + to = strtol(buf, (char **)NULL, base); \ +} + +/* + * get_arobj -- + * read the archive header for this member + */ +int +get_arobj(fd) + int fd; +{ + struct ar_hdr *hdr; + int len, nr; + char *p, buf[20]; + + nr = read(fd, hb, sizeof(HDR)); + if (nr != sizeof(HDR)) { + if (!nr) + return (0); + if (nr < 0) + error(archive); + badfmt(); + } + + hdr = (struct ar_hdr *)hb; + if (strncmp(hdr->ar_fmag, ARFMAG, sizeof(ARFMAG) - 1)) + badfmt(); + + /* Convert the header into the internal format. */ +#define DECIMAL 10 +#define OCTAL 8 + + AR_ATOI(hdr->ar_date, chdr.date, sizeof(hdr->ar_date), DECIMAL); + AR_ATOI(hdr->ar_uid, chdr.uid, sizeof(hdr->ar_uid), DECIMAL); + AR_ATOI(hdr->ar_gid, chdr.gid, sizeof(hdr->ar_gid), DECIMAL); + AR_ATOI(hdr->ar_mode, chdr.mode, sizeof(hdr->ar_mode), OCTAL); + AR_ATOI(hdr->ar_size, chdr.size, sizeof(hdr->ar_size), DECIMAL); + + /* Leading spaces should never happen. */ + if (hdr->ar_name[0] == ' ') + badfmt(); + + /* + * Long name support. Set the "real" size of the file, and the + * long name flag/size. + */ + if (!bcmp(hdr->ar_name, AR_EFMT1, sizeof(AR_EFMT1) - 1)) { + chdr.lname = len = atoi(hdr->ar_name + sizeof(AR_EFMT1) - 1); + if (len <= 0 || len > MAXNAMLEN) + badfmt(); + nr = read(fd, chdr.name, len); + if (nr != len) { + if (nr < 0) + error(archive); + badfmt(); + } + chdr.name[len] = 0; + chdr.size -= len; + } else { + chdr.lname = 0; + memmove(chdr.name, hdr->ar_name, sizeof(hdr->ar_name)); + + /* Strip trailing spaces, null terminate. */ + for (p = chdr.name + sizeof(hdr->ar_name) - 1; *p == ' '; --p); + *++p = '\0'; + } + return (1); +} + +static int already_written; + +/* + * put_arobj -- + * Write an archive member to a file. + */ +void +put_arobj(cfp, sb) + CF *cfp; + struct stat *sb; +{ + int lname; + char *name; + struct ar_hdr *hdr; + off_t size; + + /* + * If passed an sb structure, reading a file from disk. Get stat(2) + * information, build a name and construct a header. (Files are named + * by their last component in the archive.) If not, then just write + * the last header read. + */ + if (sb) { + name = rname(cfp->rname); + (void)fstat(cfp->rfd, sb); + + /* + * If not truncating names and the name is too long or contains + * a space, use extended format 1. + */ + lname = strlen(name); + if (options & AR_TR) { + if (lname > OLDARMAXNAME) { + (void)fflush(stdout); + warnx("warning: %s truncated to %.*s\n", + name, OLDARMAXNAME, name); + (void)fflush(stderr); + } + (void)sprintf(hb, HDR3, name, sb->st_mtimespec.ts_sec, + sb->st_uid, sb->st_gid, sb->st_mode, sb->st_size, + ARFMAG); + lname = 0; + } else if (lname > sizeof(hdr->ar_name) || strchr(name, ' ')) + (void)sprintf(hb, HDR1, AR_EFMT1, lname, + sb->st_mtimespec.ts_sec, sb->st_uid, sb->st_gid, + sb->st_mode, sb->st_size + lname, ARFMAG); + else { + lname = 0; + (void)sprintf(hb, HDR2, name, sb->st_mtimespec.ts_sec, + sb->st_uid, sb->st_gid, sb->st_mode, sb->st_size, + ARFMAG); + } + size = sb->st_size; + } else { + lname = chdr.lname; + name = chdr.name; + size = chdr.size; + } + + if (write(cfp->wfd, hb, sizeof(HDR)) != sizeof(HDR)) + error(cfp->wname); + if (lname) { + if (write(cfp->wfd, name, lname) != lname) + error(cfp->wname); + already_written = lname; + } + copy_ar(cfp, size); + already_written = 0; +} + +/* + * copy_ar -- + * Copy size bytes from one file to another - taking care to handle the + * extra byte (for odd size files) when reading archives and writing an + * extra byte if necessary when adding files to archive. The length of + * the object is the long name plus the object itself; the variable + * already_written gets set if a long name was written. + * + * The padding is really unnecessary, and is almost certainly a remnant + * of early archive formats where the header included binary data which + * a PDP-11 required to start on an even byte boundary. (Or, perhaps, + * because 16-bit word addressed copies were faster?) Anyhow, it should + * have been ripped out long ago. + */ +void +copy_ar(cfp, size) + CF *cfp; + off_t size; +{ + static char pad = '\n'; + off_t sz; + int from, nr, nw, off, to; + char buf[8*1024]; + + if (!(sz = size)) + return; + + from = cfp->rfd; + to = cfp->wfd; + sz = size; + while (sz && (nr = read(from, buf, MIN(sz, sizeof(buf)))) > 0) { + sz -= nr; + for (off = 0; off < nr; nr -= off, off += nw) + if ((nw = write(to, buf + off, nr)) < 0) + error(cfp->wname); + } + if (sz) { + if (nr == 0) + badfmt(); + error(cfp->rname); + } + + if (cfp->flags & RPAD && size & 1 && (nr = read(from, buf, 1)) != 1) { + if (nr == 0) + badfmt(); + error(cfp->rname); + } + if (cfp->flags & WPAD && (size + already_written) & 1 && + write(to, &pad, 1) != 1) + error(cfp->wname); +} + +/* + * skip_arobj - + * Skip over an object -- taking care to skip the pad bytes. + */ +void +skip_arobj(fd) + int fd; +{ + off_t len; + + len = chdr.size + (chdr.size + chdr.lname & 1); + if (lseek(fd, len, SEEK_CUR) == (off_t)-1) + error(archive); +} diff --git a/usr.bin/ar/archive.h b/usr.bin/ar/archive.h new file mode 100644 index 0000000..23b391f --- /dev/null +++ b/usr.bin/ar/archive.h @@ -0,0 +1,105 @@ +/*- + * Copyright (c) 1991, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Hugh Smith at The University of Guelph. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * @(#)archive.h 8.3 (Berkeley) 4/2/94 + */ + +/* Ar(1) options. */ +#define AR_A 0x0001 +#define AR_B 0x0002 +#define AR_C 0x0004 +#define AR_D 0x0008 +#define AR_M 0x0010 +#define AR_O 0x0020 +#define AR_P 0x0040 +#define AR_Q 0x0080 +#define AR_R 0x0100 +#define AR_T 0x0200 +#define AR_TR 0x0400 +#define AR_U 0x0800 +#define AR_V 0x1000 +#define AR_X 0x2000 +extern u_int options; + +/* Set up file copy. */ +#define SETCF(from, fromname, to, toname, pad) { \ + cf.rfd = from; \ + cf.rname = fromname; \ + cf.wfd = to; \ + cf.wname = toname; \ + cf.flags = pad; \ +} + +/* File copy structure. */ +typedef struct { + int rfd; /* read file descriptor */ + char *rname; /* read name */ + int wfd; /* write file descriptor */ + char *wname; /* write name */ +#define NOPAD 0x00 /* don't pad */ +#define RPAD 0x01 /* pad on reads */ +#define WPAD 0x02 /* pad on writes */ + u_int flags; /* pad flags */ +} CF; + +/* Header structure internal format. */ +typedef struct { + off_t size; /* size of the object in bytes */ + long date; /* date */ + int lname; /* size of the long name in bytes */ + int gid; /* group */ + int uid; /* owner */ + u_short mode; /* permissions */ + char name[MAXNAMLEN + 1]; /* name */ +} CHDR; + +/* Header format strings. */ +#define HDR1 "%s%-13d%-12ld%-6u%-6u%-8o%-10qd%2s" +#define HDR2 "%-16.16s%-12ld%-6u%-6u%-8o%-10qd%2s" + +#define OLDARMAXNAME 15 +#define HDR3 "%-16.15s%-12ld%-6u%-6u%-8o%-10qd%2s" + + +#include <sys/cdefs.h> + +struct stat; + +void close_archive __P((int)); +void copy_ar __P((CF *, off_t)); +int get_arobj __P((int)); +int open_archive __P((int)); +void put_arobj __P((CF *, struct stat *)); +void skip_arobj __P((int)); diff --git a/usr.bin/ar/contents.c b/usr.bin/ar/contents.c new file mode 100644 index 0000000..b2db77a --- /dev/null +++ b/usr.bin/ar/contents.c @@ -0,0 +1,96 @@ +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Hugh Smith at The University of Guelph. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +static char sccsid[] = "@(#)contents.c 8.3 (Berkeley) 4/2/94"; +#endif /* not lint */ + +#include <sys/param.h> +#include <sys/stat.h> +#include <sys/time.h> + +#include <ar.h> +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <tzfile.h> +#include <unistd.h> + +#include "archive.h" +#include "extern.h" + +/* + * contents -- + * Handles t[v] option - opens the archive and then reads headers, + * skipping member contents. + */ +int +contents(argv) + char **argv; +{ + int afd, all; + struct tm *tp; + char *file, buf[25]; + + afd = open_archive(O_RDONLY); + + for (all = !*argv; get_arobj(afd);) { + if (all) + file = chdr.name; + else if (!(file = files(argv))) + goto next; + if (options & AR_V) { + (void)strmode(chdr.mode, buf); + (void)printf("%s %6d/%-6d %8qd ", + buf + 1, chdr.uid, chdr.gid, chdr.size); + tp = localtime(&chdr.date); + (void)strftime(buf, sizeof(buf), "%b %e %H:%M %Y", tp); + (void)printf("%s %s\n", buf, file); + } else + (void)printf("%s\n", file); + if (!all && !*argv) + break; +next: skip_arobj(afd); + } + close_archive(afd); + + if (*argv) { + orphans(argv); + return (1); + } + return (0); +} diff --git a/usr.bin/ar/delete.c b/usr.bin/ar/delete.c new file mode 100644 index 0000000..1ef332f --- /dev/null +++ b/usr.bin/ar/delete.c @@ -0,0 +1,96 @@ +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Hugh Smith at The University of Guelph. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +static char sccsid[] = "@(#)delete.c 8.3 (Berkeley) 4/2/94"; +#endif /* not lint */ + +#include <sys/param.h> +#include <sys/stat.h> + +#include <ar.h> +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <unistd.h> + +#include "archive.h" +#include "extern.h" +#include "pathnames.h" + +/*- + * delete -- + * Deletes named members from the archive. + */ +int +delete(argv) + char **argv; +{ + CF cf; + off_t size; + int afd, tfd; + char *file; + + afd = open_archive(O_RDWR); + tfd = tmp(); + + /* Read and write to an archive; pad on both. */ + SETCF(afd, archive, tfd, tname, RPAD|WPAD); + while (get_arobj(afd)) { + if (*argv && (file = files(argv))) { + if (options & AR_V) + (void)printf("d - %s\n", file); + skip_arobj(afd); + continue; + } + put_arobj(&cf, (struct stat *)NULL); + } + + size = lseek(tfd, (off_t)0, SEEK_CUR); + (void)lseek(tfd, (off_t)0, SEEK_SET); + (void)lseek(afd, (off_t)SARMAG, SEEK_SET); + SETCF(tfd, tname, afd, archive, NOPAD); + copy_ar(&cf, size); + (void)close(tfd); + (void)ftruncate(afd, size + SARMAG); + close_archive(afd); + + if (*argv) { + orphans(argv); + return (1); + } + return (0); +} diff --git a/usr.bin/ar/extern.h b/usr.bin/ar/extern.h new file mode 100644 index 0000000..a6d7d71 --- /dev/null +++ b/usr.bin/ar/extern.h @@ -0,0 +1,54 @@ +/*- + * Copyright (c) 1991, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * @(#)extern.h 8.3 (Berkeley) 4/2/94 + */ + +int append __P((char **)); +void badfmt __P((void)); +int compare __P((char *)); +int contents __P((char **)); +int delete __P((char **)); +void error __P((char *)); +int extract __P((char **)); +char *files __P((char **argv)); +int move __P((char **)); +void orphans __P((char **argv)); +int print __P((char **)); +int replace __P((char **)); +char *rname __P((char *)); +int tmp __P((void)); + +extern char *archive; +extern char *posarg, *posname; /* positioning file name */ +extern char *tname; /* temporary file "name" */ +extern CHDR chdr; /* converted header */ diff --git a/usr.bin/ar/extract.c b/usr.bin/ar/extract.c new file mode 100644 index 0000000..2cc1ea0 --- /dev/null +++ b/usr.bin/ar/extract.c @@ -0,0 +1,128 @@ +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Hugh Smith at The University of Guelph. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +static char sccsid[] = "@(#)extract.c 8.3 (Berkeley) 4/2/94"; +#endif /* not lint */ + +#include <sys/param.h> +#include <sys/time.h> +#include <sys/stat.h> + +#include <dirent.h> +#include <err.h> +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include "archive.h" +#include "extern.h" + +/* + * extract -- + * Extract files from the named archive - if member names given only + * extract those members otherwise extract all members. If 'o' option + * selected modify date of newly created file to be same as archive + * members date otherwise date is time of extraction. Does not modify + * archive. + */ +int +extract(argv) + char **argv; +{ + char *file; + int afd, all, eval, tfd; + struct timeval tv[2]; + struct stat sb; + CF cf; + + eval = 0; + tv[0].tv_usec = tv[1].tv_usec = 0; + + afd = open_archive(O_RDONLY); + + /* Read from an archive, write to disk; pad on read. */ + SETCF(afd, archive, 0, 0, RPAD); + for (all = !*argv; get_arobj(afd);) { + if (all) + file = chdr.name; + else if (!(file = files(argv))) { + skip_arobj(afd); + continue; + } + + if (options & AR_U && !stat(file, &sb) && + sb.st_mtime > chdr.date) + continue; + + if ((tfd = open(file, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR)) < 0) { + warn("%s", file); + skip_arobj(afd); + eval = 1; + continue; + } + + if (options & AR_V) + (void)printf("x - %s\n", file); + + cf.wfd = tfd; + cf.wname = file; + copy_ar(&cf, chdr.size); + + if (fchmod(tfd, (short)chdr.mode)) { + warn("chmod: %s", file); + eval = 1; + } + if (options & AR_O) { + tv[0].tv_sec = tv[1].tv_sec = chdr.date; + if (utimes(file, tv)) { + warn("utimes: %s", file); + eval = 1; + } + } + (void)close(tfd); + if (!all && !*argv) + break; + } + close_archive(afd); + + if (*argv) { + orphans(argv); + return (1); + } + return (0); +} diff --git a/usr.bin/ar/misc.c b/usr.bin/ar/misc.c new file mode 100644 index 0000000..d182fa9 --- /dev/null +++ b/usr.bin/ar/misc.c @@ -0,0 +1,148 @@ +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Hugh Smith at The University of Guelph. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +static char sccsid[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94"; +#endif /* not lint */ + +#include <sys/param.h> + +#include <dirent.h> +#include <err.h> +#include <errno.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "archive.h" +#include "extern.h" +#include "pathnames.h" + +char *tname = "temporary file"; /* temporary file "name" */ + +int +tmp() +{ + extern char *envtmp; + sigset_t set, oset; + static int first; + int fd; + char path[MAXPATHLEN]; + + if (!first && !envtmp) { + envtmp = getenv("TMPDIR"); + first = 1; + } + + if (envtmp) + (void)sprintf(path, "%s/%s", envtmp, _NAME_ARTMP); + else + strcpy(path, _PATH_ARTMP); + + sigfillset(&set); + (void)sigprocmask(SIG_BLOCK, &set, &oset); + if ((fd = mkstemp(path)) == -1) + error(tname); + (void)unlink(path); + (void)sigprocmask(SIG_SETMASK, &oset, NULL); + return (fd); +} + +/* + * files -- + * See if the current file matches any file in the argument list; if it + * does, remove it from the argument list. + */ +char * +files(argv) + char **argv; +{ + char **list, *p; + + for (list = argv; *list; ++list) + if (compare(*list)) { + p = *list; + for (; list[0] = list[1]; ++list) + continue; + return (p); + } + return (NULL); +} + +void +orphans(argv) + char **argv; +{ + + for (; *argv; ++argv) + warnx("%s: not found in archive", *argv); +} + +char * +rname(path) + char *path; +{ + char *ind; + + return ((ind = strrchr(path, '/')) ? ind + 1 : path); +} + +int +compare(dest) + char *dest; +{ + + if (options & AR_TR) + return (!strncmp(chdr.name, rname(dest), OLDARMAXNAME)); + return (!strcmp(chdr.name, rname(dest))); +} + +void +badfmt() +{ + + errx(1, "%s: %s", archive, strerror(EFTYPE)); +} + +void +error(name) + char *name; +{ + + errx(1, "%s", name); +} diff --git a/usr.bin/ar/move.c b/usr.bin/ar/move.c new file mode 100644 index 0000000..1bbdef4 --- /dev/null +++ b/usr.bin/ar/move.c @@ -0,0 +1,140 @@ +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Hugh Smith at The University of Guelph. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +static char sccsid[] = "@(#)move.c 8.3 (Berkeley) 4/2/94"; +#endif /* not lint */ + +#include <sys/param.h> +#include <sys/stat.h> + +#include <ar.h> +#include <dirent.h> +#include <err.h> +#include <fcntl.h> +#include <stdio.h> +#include <unistd.h> + +#include "archive.h" +#include "extern.h" +#include "pathnames.h" + +/* + * move -- + * Change location of named members in archive - if 'b' or 'i' option + * selected then named members are placed before 'posname'. If 'a' + * option selected members go after 'posname'. If no options, members + * are moved to end of archive. + */ +int +move(argv) + char **argv; +{ + CF cf; + off_t size, tsize; + int afd, curfd, mods, tfd1, tfd2, tfd3; + char *file; + + afd = open_archive(O_RDWR); + mods = options & (AR_A|AR_B); + + tfd1 = tmp(); /* Files before key file. */ + tfd2 = tmp(); /* Files selected by user. */ + tfd3 = tmp(); /* Files after key file. */ + + /* + * Break archive into three parts -- selected entries and entries + * before and after the key entry. If positioning before the key, + * place the key at the beginning of the after key entries and if + * positioning after the key, place the key at the end of the before + * key entries. Put it all back together at the end. + */ + + /* Read and write to an archive; pad on both. */ + SETCF(afd, archive, 0, tname, RPAD|WPAD); + for (curfd = tfd1; get_arobj(afd);) { + if (*argv && (file = files(argv))) { + if (options & AR_V) + (void)printf("m - %s\n", file); + cf.wfd = tfd2; + put_arobj(&cf, (struct stat *)NULL); + continue; + } + if (mods && compare(posname)) { + mods = 0; + if (options & AR_B) + curfd = tfd3; + cf.wfd = curfd; + put_arobj(&cf, (struct stat *)NULL); + if (options & AR_A) + curfd = tfd3; + } else { + cf.wfd = curfd; + put_arobj(&cf, (struct stat *)NULL); + } + } + + if (mods) { + warnx("%s: archive member not found", posarg); + close_archive(afd); + return (1); + } + (void)lseek(afd, (off_t)SARMAG, SEEK_SET); + + SETCF(tfd1, tname, afd, archive, NOPAD); + tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR); + (void)lseek(tfd1, (off_t)0, SEEK_SET); + copy_ar(&cf, size); + + tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR); + (void)lseek(tfd2, (off_t)0, SEEK_SET); + cf.rfd = tfd2; + copy_ar(&cf, size); + + tsize += size = lseek(tfd3, (off_t)0, SEEK_CUR); + (void)lseek(tfd3, (off_t)0, SEEK_SET); + cf.rfd = tfd3; + copy_ar(&cf, size); + + (void)ftruncate(afd, tsize + SARMAG); + close_archive(afd); + + if (*argv) { + orphans(argv); + return (1); + } + return (0); +} diff --git a/usr.bin/ar/pathnames.h b/usr.bin/ar/pathnames.h new file mode 100644 index 0000000..ed92db6 --- /dev/null +++ b/usr.bin/ar/pathnames.h @@ -0,0 +1,40 @@ +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Hugh Smith at The University of Guelph. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * @(#)pathnames.h 8.1 (Berkeley) 6/6/93 + */ + +#define _NAME_ARTMP "ar.XXXXXX" +#define _PATH_ARTMP "/tmp/ar.XXXXXX" diff --git a/usr.bin/ar/print.c b/usr.bin/ar/print.c new file mode 100644 index 0000000..4367628 --- /dev/null +++ b/usr.bin/ar/print.c @@ -0,0 +1,90 @@ +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Hugh Smith at The University of Guelph. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +static char sccsid[] = "@(#)print.c 8.3 (Berkeley) 4/2/94"; +#endif /* not lint */ + +#include <sys/param.h> + +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <unistd.h> + +#include "archive.h" +#include "extern.h" + +/* + * print -- + * Prints archive members on stdout - if member names given only + * print those members, otherwise print all members. + */ +int +print(argv) + char **argv; +{ + CF cf; + int afd, all; + char *file; + + afd = open_archive(O_RDONLY); + + /* Read from an archive, write to stdout; pad on read. */ + SETCF(afd, archive, STDOUT_FILENO, "stdout", RPAD); + for (all = !*argv; get_arobj(afd);) { + if (all) + file = chdr.name; + else if (!(file = files(argv))) { + skip_arobj(afd); + continue; + } + if (options & AR_V) { + (void)printf("\n<%s>\n\n", file); + (void)fflush(stdout); + } + copy_ar(&cf, chdr.size); + if (!all && !*argv) + break; + } + close_archive(afd); + + if (*argv) { + orphans(argv); + return (1); + } + return (0); +} diff --git a/usr.bin/ar/replace.c b/usr.bin/ar/replace.c new file mode 100644 index 0000000..cc12781 --- /dev/null +++ b/usr.bin/ar/replace.c @@ -0,0 +1,176 @@ +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Hugh Smith at The University of Guelph. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +static char sccsid[] = "@(#)replace.c 8.3 (Berkeley) 4/2/94"; +#endif /* not lint */ + +#include <sys/param.h> +#include <sys/stat.h> + +#include <ar.h> +#include <dirent.h> +#include <err.h> +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include "archive.h" +#include "extern.h" + +/* + * replace -- + * Replace or add named members to archive. Entries already in the + * archive are swapped in place. Others are added before or after + * the key entry, based on the a, b and i options. If the u option + * is specified, modification dates select for replacement. + */ +int +replace(argv) + char **argv; +{ + char *file; + int afd, curfd, errflg, exists, mods, sfd, tfd1, tfd2; + struct stat sb; + CF cf; + off_t size, tsize; + + errflg = 0; + /* + * If doesn't exist, simply append to the archive. There's + * a race here, but it's pretty short, and not worth fixing. + */ + exists = !stat(archive, &sb); + afd = open_archive(O_CREAT|O_RDWR); + + if (!exists) { + tfd1 = -1; + tfd2 = tmp(); + goto append; + } + + tfd1 = tmp(); /* Files before key file. */ + tfd2 = tmp(); /* Files after key file. */ + + /* + * Break archive into two parts -- entries before and after the key + * entry. If positioning before the key, place the key at the + * beginning of the after key entries and if positioning after the + * key, place the key at the end of the before key entries. Put it + * all back together at the end. + */ + mods = (options & (AR_A|AR_B)); + for (curfd = tfd1; get_arobj(afd);) { + if (*argv && (file = files(argv))) { + if ((sfd = open(file, O_RDONLY)) < 0) { + errflg = 1; + warn("%s", file); + goto useold; + } + (void)fstat(sfd, &sb); + if (options & AR_U && sb.st_mtime <= chdr.date) + goto useold; + + if (options & AR_V) + (void)printf("r - %s\n", file); + + /* Read from disk, write to an archive; pad on write */ + SETCF(sfd, file, curfd, tname, WPAD); + put_arobj(&cf, &sb); + (void)close(sfd); + skip_arobj(afd); + continue; + } + + if (mods && compare(posname)) { + mods = 0; + if (options & AR_B) + curfd = tfd2; + /* Read and write to an archive; pad on both. */ + SETCF(afd, archive, curfd, tname, RPAD|WPAD); + put_arobj(&cf, (struct stat *)NULL); + if (options & AR_A) + curfd = tfd2; + } else { + /* Read and write to an archive; pad on both. */ +useold: SETCF(afd, archive, curfd, tname, RPAD|WPAD); + put_arobj(&cf, (struct stat *)NULL); + } + } + + if (mods) { + warnx("%s: archive member not found", posarg); + close_archive(afd); + return (1); + } + + /* Append any left-over arguments to the end of the after file. */ +append: while (file = *argv++) { + if (options & AR_V) + (void)printf("a - %s\n", file); + if ((sfd = open(file, O_RDONLY)) < 0) { + errflg = 1; + warn("%s", file); + continue; + } + (void)fstat(sfd, &sb); + /* Read from disk, write to an archive; pad on write. */ + SETCF(sfd, file, + options & (AR_A|AR_B) ? tfd1 : tfd2, tname, WPAD); + put_arobj(&cf, &sb); + (void)close(sfd); + } + + (void)lseek(afd, (off_t)SARMAG, SEEK_SET); + + SETCF(tfd1, tname, afd, archive, NOPAD); + if (tfd1 != -1) { + tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR); + (void)lseek(tfd1, (off_t)0, SEEK_SET); + copy_ar(&cf, size); + } else + tsize = 0; + + tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR); + (void)lseek(tfd2, (off_t)0, SEEK_SET); + cf.rfd = tfd2; + copy_ar(&cf, size); + + (void)ftruncate(afd, tsize + SARMAG); + close_archive(afd); + return (errflg); +} |