xref: /original-bsd/usr.bin/ar/delete.c (revision e4165910)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Hugh Smith at The University of Guelph.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)delete.c	5.6 (Berkeley) 03/12/91";
13 #endif /* not lint */
14 
15 #include <sys/param.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <dirent.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <ar.h>
22 #include "archive.h"
23 #include "extern.h"
24 #include "pathnames.h"
25 
26 extern CHDR chdr;			/* converted header */
27 extern char *archive;			/* archive name */
28 extern char *tname;                     /* temporary file "name" */
29 
30 /*-
31  * delete --
32  *	Deletes named members from the archive.
33  */
34 delete(argv)
35 	register char **argv;
36 {
37 	CF cf;
38 	off_t size;
39 	int afd, tfd;
40 	char *file;
41 
42 	afd = open_archive(O_RDWR);
43 	tfd = tmp();
44 
45 	/* Read and write to an archive; pad on both. */
46 	SETCF(afd, archive, tfd, tname, RPAD|WPAD);
47 	while (get_arobj(afd)) {
48 		if (*argv && (file = files(argv))) {
49 			if (options & AR_V)
50 				(void)printf("d - %s\n", file);
51 			skip_arobj(afd);
52 			continue;
53 		}
54 		put_arobj(&cf, (struct stat *)NULL);
55 	}
56 
57 	size = lseek(tfd, (off_t)0, SEEK_CUR);
58 	(void)lseek(tfd, (off_t)0, SEEK_SET);
59 	(void)lseek(afd, (off_t)SARMAG, SEEK_SET);
60 	SETCF(tfd, tname, afd, archive, NOPAD);
61 	copy_ar(&cf, size);
62 	(void)close(tfd);
63 	(void)ftruncate(afd, size + SARMAG);
64 	close_archive(afd);
65 
66 	if (*argv) {
67 		orphans(argv);
68 		return(1);
69 	}
70 	return(0);
71 }
72