xref: /original-bsd/usr.bin/ar/move.c (revision ae291b9c)
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[] = "@(#)move.c	5.2 (Berkeley) 01/21/91";
13 #endif /* not lint */
14 
15 #include <sys/param.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <dirent.h>
20 #include <stdio.h>
21 #include <ar.h>
22 #include "archive.h"
23 #include "pathnames.h"
24 
25 extern CHDR chdr;			/* converted header */
26 extern char *archive;			/* archive name */
27 extern char *tname;                     /* temporary file "name" */
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 move(argv)
37 	char **argv;
38 {
39 	extern char *posname;		/* positioning file name */
40 	CF cf;
41 	off_t size, tsize;
42 	int afd, curfd, eval, mods, tfd1, tfd2, tfd3;
43 
44 	afd = open_archive(O_RDWR);
45 	mods = options & (AR_A|AR_B);
46 
47 	tfd1 = tmp();			/* Files before key file. */
48 	tfd2 = tmp();			/* Files selected by user. */
49 	tfd3 = tmp();			/* Files after key file. */
50 
51 	/*
52 	 * Break archive into three parts -- selected entries and entries
53 	 * before and after the key entry.  If positioning before the key,
54 	 * place the key at the beginning of the after key entries and if
55 	 * positioning after the key, place the key at the end of the before
56 	 * key entries.  Put it all back together at the end.
57 	 */
58 	SETCF(afd, archive, 0, tname, RPAD|WPAD);
59 	for (curfd = tfd1; get_header(afd);) {
60 		if (*argv && files(argv)) {
61 			if (options & AR_V)
62 				(void)printf("m - %s\n", chdr.name);
63 			cf.wfd = tfd2;
64 			put_header(&cf, (struct stat *)NULL);
65 			copyfile(&cf, chdr.size);
66 			continue;
67 		}
68 		if (mods && compare(posname)) {
69 			mods = 0;
70 			if (options & AR_B)
71 				curfd = tfd3;
72 			cf.wfd = curfd;
73 			put_header(&cf, (struct stat *)NULL);
74 			copyfile(&cf, chdr.size);
75 			if (options & AR_A)
76 				curfd = tfd3;
77 		} else {
78 			cf.wfd = curfd;
79 			put_header(&cf, (struct stat *)NULL);
80 			copyfile(&cf, chdr.size);
81 		}
82 	}
83 
84 	if (mods) {
85 		(void)fprintf(stderr, "ar: %s: archive member not found.\n",
86 		    posname);
87 		close_archive(afd);
88 		return(1);
89 	}
90 	(void)lseek(afd, (off_t)SARMAG, SEEK_SET);
91 
92 	eval = 0;
93 	ORPHANS;
94 
95 	SETCF(tfd1, tname, afd, archive, RPAD|WPAD);
96 	tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR);
97 	(void)lseek(tfd1, (off_t)0, SEEK_SET);
98 	copyfile(&cf, size);
99 
100 	tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR);
101 	(void)lseek(tfd2, (off_t)0, SEEK_SET);
102 	cf.rfd = tfd2;
103 	copyfile(&cf, size);
104 
105 	tsize += size = lseek(tfd3, (off_t)0, SEEK_CUR);
106 	(void)lseek(tfd3, (off_t)0, SEEK_SET);
107 	cf.rfd = tfd3;
108 	copyfile(&cf, size);
109 
110 	(void)ftruncate(afd, tsize + SARMAG);
111 	close_archive(afd);
112 	return(eval);
113 }
114