Annotation of src/usr.bin/ar/move.c, Revision 1.6
1.6 ! deraadt 1: /* $OpenBSD: move.c,v 1.5 2003/06/12 20:58:08 deraadt 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: #include <sys/param.h>
37: #include <sys/stat.h>
38:
39: #include <ar.h>
40: #include <dirent.h>
41: #include <err.h>
42: #include <fcntl.h>
43: #include <stdio.h>
44: #include <unistd.h>
45:
46: #include "archive.h"
47: #include "extern.h"
48: #include "pathnames.h"
49:
50: /*
51: * move --
52: * Change location of named members in archive - if 'b' or 'i' option
53: * selected then named members are placed before 'posname'. If 'a'
54: * option selected members go after 'posname'. If no options, members
55: * are moved to end of archive.
56: */
57: int
1.5 deraadt 58: move(char **argv)
1.1 deraadt 59: {
60: CF cf;
61: off_t size, tsize;
62: int afd, curfd, mods, tfd1, tfd2, tfd3;
63: char *file;
64:
65: afd = open_archive(O_RDWR);
66: mods = options & (AR_A|AR_B);
67:
68: tfd1 = tmp(); /* Files before key file. */
69: tfd2 = tmp(); /* Files selected by user. */
70: tfd3 = tmp(); /* Files after key file. */
71:
72: /*
73: * Break archive into three parts -- selected entries and entries
74: * before and after the key entry. If positioning before the key,
75: * place the key at the beginning of the after key entries and if
76: * positioning after the key, place the key at the end of the before
77: * key entries. Put it all back together at the end.
78: */
79:
80: /* Read and write to an archive; pad on both. */
81: SETCF(afd, archive, 0, tname, RPAD|WPAD);
82: for (curfd = tfd1; get_arobj(afd);) {
83: if (*argv && (file = files(argv))) {
84: if (options & AR_V)
85: (void)printf("m - %s\n", file);
86: cf.wfd = tfd2;
1.3 kstailey 87: put_arobj(&cf, NULL);
1.1 deraadt 88: continue;
89: }
90: if (mods && compare(posname)) {
91: mods = 0;
92: if (options & AR_B)
93: curfd = tfd3;
94: cf.wfd = curfd;
1.3 kstailey 95: put_arobj(&cf, NULL);
1.1 deraadt 96: if (options & AR_A)
97: curfd = tfd3;
98: } else {
99: cf.wfd = curfd;
1.3 kstailey 100: put_arobj(&cf, NULL);
1.1 deraadt 101: }
102: }
103:
104: if (mods) {
105: warnx("%s: archive member not found", posarg);
106: close_archive(afd);
107: return (1);
108: }
109: (void)lseek(afd, (off_t)SARMAG, SEEK_SET);
110:
111: SETCF(tfd1, tname, afd, archive, NOPAD);
112: tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR);
113: (void)lseek(tfd1, (off_t)0, SEEK_SET);
114: copy_ar(&cf, size);
115:
116: tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR);
117: (void)lseek(tfd2, (off_t)0, SEEK_SET);
118: cf.rfd = tfd2;
119: copy_ar(&cf, size);
120:
121: tsize += size = lseek(tfd3, (off_t)0, SEEK_CUR);
122: (void)lseek(tfd3, (off_t)0, SEEK_SET);
123: cf.rfd = tfd3;
124: copy_ar(&cf, size);
125:
126: (void)ftruncate(afd, tsize + SARMAG);
127: close_archive(afd);
128:
129: if (*argv) {
130: orphans(argv);
131: return (1);
132: }
133: return (0);
134: }