prettyness police
[unix-history] / usr / src / usr.bin / ar / move.c
CommitLineData
5fa1c8be 1/*-
4e6c9859
KB
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5fa1c8be
KB
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
706c0358 12static char sccsid[] = "@(#)move.c 8.2 (Berkeley) %G%";
5fa1c8be
KB
13#endif /* not lint */
14
15#include <sys/param.h>
16#include <sys/stat.h>
706c0358
JSP
17
18#include <ar.h>
5fa1c8be 19#include <dirent.h>
706c0358
JSP
20#include <err.h>
21#include <fcntl.h>
5fa1c8be 22#include <stdio.h>
706c0358
JSP
23#include <unistd.h>
24
5fa1c8be 25#include "archive.h"
a4cb4262 26#include "extern.h"
5fa1c8be
KB
27#include "pathnames.h"
28
5fa1c8be
KB
29/*
30 * move --
31 * Change location of named members in archive - if 'b' or 'i' option
32 * selected then named members are placed before 'posname'. If 'a'
33 * option selected members go after 'posname'. If no options, members
34 * are moved to end of archive.
35 */
706c0358 36int
5fa1c8be
KB
37move(argv)
38 char **argv;
39{
5fa1c8be
KB
40 CF cf;
41 off_t size, tsize;
a4cb4262 42 int afd, curfd, mods, tfd1, tfd2, tfd3;
af52bb8a 43 char *file;
5fa1c8be
KB
44
45 afd = open_archive(O_RDWR);
46 mods = options & (AR_A|AR_B);
47
48 tfd1 = tmp(); /* Files before key file. */
49 tfd2 = tmp(); /* Files selected by user. */
50 tfd3 = tmp(); /* Files after key file. */
51
52 /*
53 * Break archive into three parts -- selected entries and entries
54 * before and after the key entry. If positioning before the key,
55 * place the key at the beginning of the after key entries and if
56 * positioning after the key, place the key at the end of the before
57 * key entries. Put it all back together at the end.
58 */
e00adfe9
KB
59
60 /* Read and write to an archive; pad on both. */
5fa1c8be 61 SETCF(afd, archive, 0, tname, RPAD|WPAD);
ce478bb7 62 for (curfd = tfd1; get_arobj(afd);) {
a4cb4262 63 if (*argv && (file = files(argv))) {
5fa1c8be 64 if (options & AR_V)
af52bb8a 65 (void)printf("m - %s\n", file);
5fa1c8be 66 cf.wfd = tfd2;
ce478bb7 67 put_arobj(&cf, (struct stat *)NULL);
5fa1c8be
KB
68 continue;
69 }
70 if (mods && compare(posname)) {
71 mods = 0;
72 if (options & AR_B)
73 curfd = tfd3;
74 cf.wfd = curfd;
ce478bb7 75 put_arobj(&cf, (struct stat *)NULL);
5fa1c8be
KB
76 if (options & AR_A)
77 curfd = tfd3;
78 } else {
79 cf.wfd = curfd;
ce478bb7 80 put_arobj(&cf, (struct stat *)NULL);
5fa1c8be
KB
81 }
82 }
83
84 if (mods) {
706c0358 85 warnx("%s: archive member not found", posarg);
5fa1c8be 86 close_archive(afd);
706c0358 87 return (1);
5fa1c8be
KB
88 }
89 (void)lseek(afd, (off_t)SARMAG, SEEK_SET);
90
e00adfe9 91 SETCF(tfd1, tname, afd, archive, NOPAD);
5fa1c8be
KB
92 tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR);
93 (void)lseek(tfd1, (off_t)0, SEEK_SET);
ce478bb7 94 copy_ar(&cf, size);
5fa1c8be
KB
95
96 tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR);
97 (void)lseek(tfd2, (off_t)0, SEEK_SET);
98 cf.rfd = tfd2;
ce478bb7 99 copy_ar(&cf, size);
5fa1c8be
KB
100
101 tsize += size = lseek(tfd3, (off_t)0, SEEK_CUR);
102 (void)lseek(tfd3, (off_t)0, SEEK_SET);
103 cf.rfd = tfd3;
ce478bb7 104 copy_ar(&cf, size);
5fa1c8be
KB
105
106 (void)ftruncate(afd, tsize + SARMAG);
107 close_archive(afd);
a4cb4262
KB
108
109 if (*argv) {
110 orphans(argv);
706c0358 111 return (1);
a4cb4262 112 }
706c0358 113 return (0);
5fa1c8be 114}