xref: /original-bsd/usr.bin/ar/move.c (revision 7e5c8007)
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[] = "@(#)move.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 <err.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 
25 #include "archive.h"
26 #include "extern.h"
27 #include "pathnames.h"
28 
29 /*
30  * move --
31  *	Change location of named members in archive - if 'b' or 'i' option
32  *	selected then named members are placed before 'posname'.  If 'a'
33  *	option selected members go after 'posname'.  If no options, members
34  *	are moved to end of archive.
35  */
36 int
37 move(argv)
38 	char **argv;
39 {
40 	CF cf;
41 	off_t size, tsize;
42 	int afd, curfd, mods, tfd1, tfd2, tfd3;
43 	char *file;
44 
45 	afd = open_archive(O_RDWR);
46 	mods = options & (AR_A|AR_B);
47 
48 	tfd1 = tmp();			/* Files before key file. */
49 	tfd2 = tmp();			/* Files selected by user. */
50 	tfd3 = tmp();			/* Files after key file. */
51 
52 	/*
53 	 * Break archive into three parts -- selected entries and entries
54 	 * before and after the key entry.  If positioning before the key,
55 	 * place the key at the beginning of the after key entries and if
56 	 * positioning after the key, place the key at the end of the before
57 	 * key entries.  Put it all back together at the end.
58 	 */
59 
60 	/* Read and write to an archive; pad on both. */
61 	SETCF(afd, archive, 0, tname, RPAD|WPAD);
62 	for (curfd = tfd1; get_arobj(afd);) {
63 		if (*argv && (file = files(argv))) {
64 			if (options & AR_V)
65 				(void)printf("m - %s\n", file);
66 			cf.wfd = tfd2;
67 			put_arobj(&cf, (struct stat *)NULL);
68 			continue;
69 		}
70 		if (mods && compare(posname)) {
71 			mods = 0;
72 			if (options & AR_B)
73 				curfd = tfd3;
74 			cf.wfd = curfd;
75 			put_arobj(&cf, (struct stat *)NULL);
76 			if (options & AR_A)
77 				curfd = tfd3;
78 		} else {
79 			cf.wfd = curfd;
80 			put_arobj(&cf, (struct stat *)NULL);
81 		}
82 	}
83 
84 	if (mods) {
85 		warnx("%s: archive member not found", posarg);
86 		close_archive(afd);
87 		return (1);
88 	}
89 	(void)lseek(afd, (off_t)SARMAG, SEEK_SET);
90 
91 	SETCF(tfd1, tname, afd, archive, NOPAD);
92 	tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR);
93 	(void)lseek(tfd1, (off_t)0, SEEK_SET);
94 	copy_ar(&cf, size);
95 
96 	tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR);
97 	(void)lseek(tfd2, (off_t)0, SEEK_SET);
98 	cf.rfd = tfd2;
99 	copy_ar(&cf, size);
100 
101 	tsize += size = lseek(tfd3, (off_t)0, SEEK_CUR);
102 	(void)lseek(tfd3, (off_t)0, SEEK_SET);
103 	cf.rfd = tfd3;
104 	copy_ar(&cf, size);
105 
106 	(void)ftruncate(afd, tsize + SARMAG);
107 	close_archive(afd);
108 
109 	if (*argv) {
110 		orphans(argv);
111 		return (1);
112 	}
113 	return (0);
114 }
115