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