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