Commit | Line | Data |
---|---|---|
5fa1c8be KB |
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 | |
ce478bb7 | 12 | static char sccsid[] = "@(#)move.c 5.6 (Berkeley) %G%"; |
5fa1c8be KB |
13 | #endif /* not lint */ |
14 | ||
15 | #include <sys/param.h> | |
16 | #include <sys/stat.h> | |
17 | #include <fcntl.h> | |
5fa1c8be | 18 | #include <dirent.h> |
e00adfe9 | 19 | #include <unistd.h> |
5fa1c8be KB |
20 | #include <stdio.h> |
21 | #include <ar.h> | |
22 | #include "archive.h" | |
a4cb4262 | 23 | #include "extern.h" |
5fa1c8be KB |
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 | { | |
af52bb8a | 40 | extern char *posarg, *posname; /* positioning file names */ |
5fa1c8be KB |
41 | CF cf; |
42 | off_t size, tsize; | |
a4cb4262 | 43 | int afd, curfd, mods, tfd1, tfd2, tfd3; |
af52bb8a | 44 | char *file; |
5fa1c8be KB |
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 | */ | |
e00adfe9 KB |
60 | |
61 | /* Read and write to an archive; pad on both. */ | |
5fa1c8be | 62 | SETCF(afd, archive, 0, tname, RPAD|WPAD); |
ce478bb7 | 63 | for (curfd = tfd1; get_arobj(afd);) { |
a4cb4262 | 64 | if (*argv && (file = files(argv))) { |
5fa1c8be | 65 | if (options & AR_V) |
af52bb8a | 66 | (void)printf("m - %s\n", file); |
5fa1c8be | 67 | cf.wfd = tfd2; |
ce478bb7 | 68 | put_arobj(&cf, (struct stat *)NULL); |
5fa1c8be KB |
69 | continue; |
70 | } | |
71 | if (mods && compare(posname)) { | |
72 | mods = 0; | |
73 | if (options & AR_B) | |
74 | curfd = tfd3; | |
75 | cf.wfd = curfd; | |
ce478bb7 | 76 | put_arobj(&cf, (struct stat *)NULL); |
5fa1c8be KB |
77 | if (options & AR_A) |
78 | curfd = tfd3; | |
79 | } else { | |
80 | cf.wfd = curfd; | |
ce478bb7 | 81 | put_arobj(&cf, (struct stat *)NULL); |
5fa1c8be KB |
82 | } |
83 | } | |
84 | ||
85 | if (mods) { | |
86 | (void)fprintf(stderr, "ar: %s: archive member not found.\n", | |
af52bb8a | 87 | posarg); |
5fa1c8be KB |
88 | close_archive(afd); |
89 | return(1); | |
90 | } | |
91 | (void)lseek(afd, (off_t)SARMAG, SEEK_SET); | |
92 | ||
e00adfe9 | 93 | SETCF(tfd1, tname, afd, archive, NOPAD); |
5fa1c8be KB |
94 | tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR); |
95 | (void)lseek(tfd1, (off_t)0, SEEK_SET); | |
ce478bb7 | 96 | copy_ar(&cf, size); |
5fa1c8be KB |
97 | |
98 | tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR); | |
99 | (void)lseek(tfd2, (off_t)0, SEEK_SET); | |
100 | cf.rfd = tfd2; | |
ce478bb7 | 101 | copy_ar(&cf, size); |
5fa1c8be KB |
102 | |
103 | tsize += size = lseek(tfd3, (off_t)0, SEEK_CUR); | |
104 | (void)lseek(tfd3, (off_t)0, SEEK_SET); | |
105 | cf.rfd = tfd3; | |
ce478bb7 | 106 | copy_ar(&cf, size); |
5fa1c8be KB |
107 | |
108 | (void)ftruncate(afd, tsize + SARMAG); | |
109 | close_archive(afd); | |
a4cb4262 KB |
110 | |
111 | if (*argv) { | |
112 | orphans(argv); | |
113 | return(1); | |
114 | } | |
115 | return(0); | |
5fa1c8be | 116 | } |