Statistics
| Branch: | Revision:

scoutos / prex-0.9.0 / usr / sbin / install / install.c @ 03e9c04a

History | View | Annotate | Download (3.66 KB)

1
/*
2
 * Copyright (c) 1988, 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
 * David Hitz of Auspex Systems Inc.
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. Neither the name of the University nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32

    
33
/* This code is derived from cp.c */
34
/* modified for Prex by Kohsuke Ohtani. */
35

    
36
/*
37
 * install.c - software installer.
38
 *
39
 * Required capabilities:
40
 *      CAP_SYSFILES
41
 */
42

    
43
#include <sys/stat.h>
44
#include <sys/fcntl.h>
45

    
46
#include <unistd.h>
47
#include <err.h>
48
#include <string.h>
49
#include <stdlib.h>
50
#include <errno.h>
51
#include <limits.h>
52
#include <libgen.h>
53
#include <stdio.h>
54

    
55
static void usage(void);
56
static void error(void);
57
static int copy(char *from, char *to);
58

    
59

    
60
static char iobuf[BUFSIZ];
61

    
62
int
63
main(int argc, char *argv[])
64
{
65
        int r, ch, checkch, i;
66
        char target[] = "/bin";
67
        char *src;
68
        struct stat to_stat;
69

    
70
        if (argc < 2)
71
                usage();
72
        if (!strcmp(argv[1], "-?"))
73
                usage();
74

    
75
        r = stat(target, &to_stat);
76
        if (r == -1 && errno != ENOENT)
77
                error();
78
        if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
79
                error();
80
        } else {
81
                r = 0;
82
                for (i = 1; i < argc; i++) {
83
                        src = argv[i];
84
                        /*
85
                         * User confirmation is required for security.
86
                         */
87
                        fputs("Are you sure you want to install ", stdout);
88
                        fputs(basename(src), stdout);
89
                        fputs("? (y/n) ", stdout);
90
                        checkch = ch = getchar();
91
                        while (ch != '\n' && ch != EOF)
92
                                ch = getchar();
93
                        if (checkch != 'y')
94
                                exit(0);
95

    
96
                        r = copy(src, target);
97
                        if (r)
98
                                error();
99
                }
100
        }
101
        exit(r);
102
}
103

    
104
static void
105
usage(void)
106
{
107
        fputs("usage: install file\n", stderr);
108
        exit(1);
109
        /* NOTREACHED */
110
}
111

    
112
static int
113
copy(char *from, char *to)
114
{
115
        char path[PATH_MAX];
116
        int fold, fnew, n;
117
        struct stat stbuf;
118
        mode_t mode;
119
        char *p;
120

    
121
        p = strrchr(from, '/');
122
        p = p ? p + 1 : from;
123
        strlcpy(path, to, sizeof(path));
124
        strlcat(path, "/", sizeof(path));
125
        strlcat(path, p, sizeof(path));
126
        to = path;
127

    
128
        if ((fold = open(from, O_RDONLY)) == -1)
129
                return 1;
130
        fstat(fold, &stbuf);
131
        mode = stbuf.st_mode;
132

    
133
        if ((fnew = creat(to, mode)) == -1) {
134
                close(fold);
135
                return 1;
136
        }
137
        while ((n = read(fold, iobuf, BUFSIZ)) > 0) {
138
                if (write(fnew, iobuf, (size_t)n) != n) {
139
                        close(fold);
140
                        close(fnew);
141
                        return 1;
142
                }
143
        }
144
        close(fold);
145
        close(fnew);
146
        return 0;
147
}
148

    
149
static void
150
error(void)
151
{
152
        perror("install");
153
}