xref: /original-bsd/usr.bin/ar/delete.c (revision e58c8952)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  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	8.3 (Berkeley) 04/02/94";
13 #endif /* not lint */
14 
15 #include <sys/param.h>
16 #include <sys/stat.h>
17 
18 #include <ar.h>
19 #include <dirent.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 
24 #include "archive.h"
25 #include "extern.h"
26 #include "pathnames.h"
27 
28 /*-
29  * delete --
30  *	Deletes named members from the archive.
31  */
32 int
33 delete(argv)
34 	char **argv;
35 {
36 	CF cf;
37 	off_t size;
38 	int afd, tfd;
39 	char *file;
40 
41 	afd = open_archive(O_RDWR);
42 	tfd = tmp();
43 
44 	/* Read and write to an archive; pad on both. */
45 	SETCF(afd, archive, tfd, tname, RPAD|WPAD);
46 	while (get_arobj(afd)) {
47 		if (*argv && (file = files(argv))) {
48 			if (options & AR_V)
49 				(void)printf("d - %s\n", file);
50 			skip_arobj(afd);
51 			continue;
52 		}
53 		put_arobj(&cf, (struct stat *)NULL);
54 	}
55 
56 	size = lseek(tfd, (off_t)0, SEEK_CUR);
57 	(void)lseek(tfd, (off_t)0, SEEK_SET);
58 	(void)lseek(afd, (off_t)SARMAG, SEEK_SET);
59 	SETCF(tfd, tname, afd, archive, NOPAD);
60 	copy_ar(&cf, size);
61 	(void)close(tfd);
62 	(void)ftruncate(afd, size + SARMAG);
63 	close_archive(afd);
64 
65 	if (*argv) {
66 		orphans(argv);
67 		return (1);
68 	}
69 	return (0);
70 }
71