chiark / gitweb /
Initial revision
[ssr] / StraySrc / Utilities / c / inst
1 /*
2  * inst.c
3  *
4  * Copy files about an Acorn system
5  *
6  * © 1998 Straylight/Edgeware
7  */
8
9 /*----- Licensing note ----------------------------------------------------*
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2, or (at your option)
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, write to the Free Software Foundation,
23  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 /*----- Header files ------------------------------------------------------*/
27
28 #include <stddef.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "swis.h"
34 #include "swiv.h"
35
36 #include "cmdr.h"
37
38 /*----- Install programs --------------------------------------------------*/
39
40 static int exitstat = 0;                /* Exit status */
41
42 /* -- @copy@ --- *
43  *
44  * Arguments:   @const char *s@ = pointer to source filename
45  *              @const char *d@ = pointer to destination filename
46  *
47  * Returns:     ---
48  *
49  * Use:         Copies a file.
50  */
51
52 static void copy(const char *s, const char *d)
53 {
54   _kernel_oserror *e;
55
56   e = _swix(OS_FSControl, _inr(0, 3), 26, s, d, 0x1002);
57   if (e) {
58     fprintf(stderr, "inst: error copying `%s' to `%s': %s\n",
59             s, d, e->errmess);
60     exitstat = 1;
61   }
62 }
63
64 /* --- @main@ --- *
65  *
66  * Arguments:   @int argc@ = number of arguments
67  *              @char *argv[]@ = list of arguments
68  *
69  * Returns:     Zero if all went well
70  *
71  * Use:         Installs files in the right places.  This is a bit of a
72  *              rip-off of unix `cp'.
73  */
74
75 int main(int argc, char *argv[])
76 {
77   const char *d;
78   int ty;
79
80   /* --- Expand wildcards in the arguments --- */
81
82   cmdreplace(&argc, &argv);
83
84   /* --- Report an error if there aren't enough arguments --- */
85
86   if (argc < 3) {
87     fprintf(stderr,
88             "Usage:\n"
89             "        inst SRCFILE DSTFILE\n"
90             "        inst SRCFILE [SRCFILE...] DSTDIR\n");
91     exit(1);
92   }
93
94   /* --- Sort out how to copy things --- */
95
96   d = argv[argc - 1];
97   ty = _swi(OS_File, _inr(0, 1) | _return(0), 17, d);
98
99   if (ty < 2 && argc > 3) {
100     fprintf(stderr,
101             "inst: too many files; destination is not a directory\n");
102     exit(1);
103   }
104
105   /* --- Do the copying --- */
106
107   if ((ty & 1 || ty == 0) && argc == 3)
108     copy(argv[1], argv[2]);
109   else {
110     char buf[1024];
111     size_t sz = strlen(d);
112     int i;
113
114     memcpy(buf, d, sz);
115     buf[sz] = '.';
116
117     for (i = 1; i < argc - 1; i++) {
118       char *p = strrchr(argv[i], '.');
119       if (p) p++; else p = argv[i];
120       strcpy(buf + sz + 1, p);
121       copy(argv[i], buf);
122     }
123   }
124
125   /* --- Done --- */
126
127   return (exitstat);
128 }
129
130 /*----- That's all, folks -------------------------------------------------*/