Annotation of src/usr.bin/ar/move.c, Revision 1.4
1.4 ! millert 1: /* $OpenBSD: move.c,v 1.3 1997/06/17 20:47:09 kstailey Exp $ */
1.1 deraadt 2: /* $NetBSD: move.c,v 1.5 1995/03/26 03:27:57 glass Exp $ */
3:
4: /*-
5: * Copyright (c) 1990, 1993, 1994
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Hugh Smith at The University of Guelph.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
1.4 ! millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: #if 0
38: static char sccsid[] = "@(#)move.c 8.3 (Berkeley) 4/2/94";
39: #else
1.4 ! millert 40: static char rcsid[] = "$OpenBSD: move.c,v 1.3 1997/06/17 20:47:09 kstailey Exp $";
1.1 deraadt 41: #endif
42: #endif /* not lint */
43:
44: #include <sys/param.h>
45: #include <sys/stat.h>
46:
47: #include <ar.h>
48: #include <dirent.h>
49: #include <err.h>
50: #include <fcntl.h>
51: #include <stdio.h>
52: #include <unistd.h>
53:
54: #include "archive.h"
55: #include "extern.h"
56: #include "pathnames.h"
57:
58: /*
59: * move --
60: * Change location of named members in archive - if 'b' or 'i' option
61: * selected then named members are placed before 'posname'. If 'a'
62: * option selected members go after 'posname'. If no options, members
63: * are moved to end of archive.
64: */
65: int
66: move(argv)
67: char **argv;
68: {
69: CF cf;
70: off_t size, tsize;
71: int afd, curfd, mods, tfd1, tfd2, tfd3;
72: char *file;
73:
74: afd = open_archive(O_RDWR);
75: mods = options & (AR_A|AR_B);
76:
77: tfd1 = tmp(); /* Files before key file. */
78: tfd2 = tmp(); /* Files selected by user. */
79: tfd3 = tmp(); /* Files after key file. */
80:
81: /*
82: * Break archive into three parts -- selected entries and entries
83: * before and after the key entry. If positioning before the key,
84: * place the key at the beginning of the after key entries and if
85: * positioning after the key, place the key at the end of the before
86: * key entries. Put it all back together at the end.
87: */
88:
89: /* Read and write to an archive; pad on both. */
90: SETCF(afd, archive, 0, tname, RPAD|WPAD);
91: for (curfd = tfd1; get_arobj(afd);) {
92: if (*argv && (file = files(argv))) {
93: if (options & AR_V)
94: (void)printf("m - %s\n", file);
95: cf.wfd = tfd2;
1.3 kstailey 96: put_arobj(&cf, NULL);
1.1 deraadt 97: continue;
98: }
99: if (mods && compare(posname)) {
100: mods = 0;
101: if (options & AR_B)
102: curfd = tfd3;
103: cf.wfd = curfd;
1.3 kstailey 104: put_arobj(&cf, NULL);
1.1 deraadt 105: if (options & AR_A)
106: curfd = tfd3;
107: } else {
108: cf.wfd = curfd;
1.3 kstailey 109: put_arobj(&cf, NULL);
1.1 deraadt 110: }
111: }
112:
113: if (mods) {
114: warnx("%s: archive member not found", posarg);
115: close_archive(afd);
116: return (1);
117: }
118: (void)lseek(afd, (off_t)SARMAG, SEEK_SET);
119:
120: SETCF(tfd1, tname, afd, archive, NOPAD);
121: tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR);
122: (void)lseek(tfd1, (off_t)0, SEEK_SET);
123: copy_ar(&cf, size);
124:
125: tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR);
126: (void)lseek(tfd2, (off_t)0, SEEK_SET);
127: cf.rfd = tfd2;
128: copy_ar(&cf, size);
129:
130: tsize += size = lseek(tfd3, (off_t)0, SEEK_CUR);
131: (void)lseek(tfd3, (off_t)0, SEEK_SET);
132: cf.rfd = tfd3;
133: copy_ar(&cf, size);
134:
135: (void)ftruncate(afd, tsize + SARMAG);
136: close_archive(afd);
137:
138: if (*argv) {
139: orphans(argv);
140: return (1);
141: }
142: return (0);
143: }