Print this page
10075 make usr/src/tools smatch clean
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/install.bin/install.bin.c
+++ new/usr/src/tools/install.bin/install.bin.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 */
24 24
25 +/*
26 + * Copyright (c) 2018, Joyent, Inc.
27 + */
25 28
26 29 #include <stdio.h>
27 30 #include <stdlib.h>
28 31 #include <strings.h>
29 32 #include <sys/param.h>
30 33 #include <fcntl.h>
31 34 #include <sys/errno.h>
32 35 #include <sys/types.h>
33 36 #include <sys/uio.h>
34 37 #include <unistd.h>
35 38 #include <sys/stat.h>
36 39 #include <errno.h>
37 40 #include <libgen.h>
38 41 #include "stdusers.h"
39 42
40 43
41 44 #define FILE_BUFF 40960
42 45
43 46 static int suppress = 0;
44 47
45 48 static void usage(void);
46 49 static void file_copy(char *src_file, char *dest_file);
47 50 static void chown_file(const char *file, const char *group, const char *owner);
48 51 static char *find_basename(const char *str);
49 52 static int creatdir(char *fn);
50 53
51 54
52 55 void
53 56 usage(void)
54 57 {
55 58 (void) fprintf(stderr,
56 59 "usage: install [-sd][-m mode][-g group][-u owner] "
57 60 "-f dir file ...\n");
58 61 }
59 62
60 63 void
61 64 file_copy(char *src_file, char *dest_file)
62 65 {
63 66 int src_fd;
64 67 int dest_fd;
65 68 int count;
66 69 static char file_buff[FILE_BUFF];
67 70
68 71 if ((src_fd = open(src_file, O_RDONLY)) == -1) {
69 72 (void) fprintf(stderr, "install:file_copy: %s failed "
70 73 "(%d): %s\n", src_file, errno, strerror(errno));
71 74 exit(1);
72 75 }
73 76
74 77 if ((dest_fd = open(dest_file, O_CREAT|O_WRONLY|O_TRUNC, 0755)) == -1) {
75 78 (void) fprintf(stderr, "install:file_copy: %s failed "
76 79 "(%d): %s\n", dest_file, errno, strerror(errno));
77 80 exit(1);
78 81 }
79 82
80 83 while ((count = read(src_fd, file_buff, FILE_BUFF)) > 0) {
81 84 (void) write(dest_fd, file_buff, count);
82 85 }
83 86
84 87 if (count == -1) {
85 88 (void) fprintf(stderr, "install:file_copy:read failed "
86 89 "(%d): %s\n", errno, strerror(errno));
87 90 exit(1);
88 91 }
89 92
90 93 if (!suppress)
91 94 (void) printf("%s installed as %s\n", src_file, dest_file);
92 95
93 96 (void) close(src_fd);
94 97 (void) close(dest_fd);
95 98 }
↓ open down ↓ |
61 lines elided |
↑ open up ↑ |
96 99
97 100
98 101 void
99 102 chown_file(const char *file, const char *group, const char *owner)
100 103 {
101 104 gid_t grp = (gid_t)-1;
102 105 uid_t own = (uid_t)-1;
103 106
104 107 if (group) {
105 108 grp = stdfind(group, groupnames);
106 - if (grp < 0)
109 + if (grp == (gid_t)-1)
107 110 (void) fprintf(stderr, "unknown group(%s)\n", group);
108 111 }
109 112
110 113 if (owner) {
111 114 own = stdfind(owner, usernames);
112 - if (own < 0) {
115 + if (own == (uid_t)-1) {
113 116 (void) fprintf(stderr, "unknown owner(%s)\n", owner);
114 117 exit(1);
115 118 }
116 119
117 120 }
118 121
119 122 if (chown(file, own, grp) == -1) {
120 123 (void) fprintf(stderr, "install:chown_file: failed "
121 124 "(%d): %s\n", errno, strerror(errno));
122 125 exit(1);
123 126 }
124 127 }
125 128
126 129
127 130 char *
128 131 find_basename(const char *str)
129 132 {
130 133 int i;
131 134 int len;
132 135
133 136 len = strlen(str);
134 137
135 138 for (i = len-1; i >= 0; i--)
136 139 if (str[i] == '/')
137 140 return ((char *)(str + i + 1));
138 141 return ((char *)str);
139 142 }
140 143
141 144 int
142 145 creatdir(char *fn) {
143 146
144 147 errno = 0;
145 148
146 149 if (mkdirp(fn, 0755) == -1) {
147 150 if (errno != EEXIST)
148 151 return (errno);
149 152 } else if (!suppress) {
150 153 (void) printf("directory %s created\n", fn);
151 154 }
152 155 return (0);
153 156 }
154 157
155 158
156 159 int
157 160 main(int argc, char **argv)
158 161 {
159 162 int c;
160 163 int errflg = 0;
161 164 int dirflg = 0;
162 165 char *group = NULL;
163 166 char *owner = NULL;
164 167 char *dirb = NULL;
165 168 char *ins_file = NULL;
166 169 int mode = -1;
167 170 char dest_file[MAXPATHLEN];
168 171 int rv = 0;
169 172
170 173 while ((c = getopt(argc, argv, "f:sm:du:g:")) != EOF) {
171 174 switch (c) {
172 175 case 'f':
173 176 dirb = optarg;
174 177 break;
175 178 case 'g':
176 179 group = optarg;
177 180 break;
178 181 case 'u':
179 182 owner = optarg;
180 183 break;
181 184 case 'd':
182 185 dirflg = 1;
183 186 break;
184 187 case 'm':
185 188 mode = strtol(optarg, NULL, 8);
186 189 break;
187 190 case 's':
188 191 suppress = 1;
189 192 break;
190 193 case '?':
191 194 errflg++;
192 195 break;
193 196 }
194 197 }
195 198
196 199 if (errflg) {
197 200 usage();
198 201 return (1);
199 202 }
200 203
201 204 if (argc == optind) {
202 205 usage();
203 206 return (1);
204 207 }
205 208
206 209 if (!dirflg && (dirb == NULL)) {
207 210 (void) fprintf(stderr,
208 211 "install: no destination directory specified.\n");
209 212 return (1);
210 213 }
211 214
212 215 for (c = optind; c < argc; c++) {
213 216 ins_file = argv[c];
214 217
215 218 if (dirflg) {
216 219 rv = creatdir(ins_file);
217 220 if (rv) {
218 221 (void) fprintf(stderr,
219 222 "install: creatdir %s (%d): %s\n",
220 223 ins_file, errno, strerror(errno));
221 224 return (rv);
222 225 }
223 226 (void) strlcpy(dest_file, ins_file, MAXPATHLEN);
224 227
225 228 } else {
226 229 (void) strcat(strcat(strcpy(dest_file, dirb), "/"),
227 230 find_basename(ins_file));
228 231 file_copy(ins_file, dest_file);
229 232 }
230 233
231 234 if (group || owner)
232 235 chown_file(dest_file, group, owner);
233 236
234 237 if (mode != -1) {
235 238 (void) umask(0);
236 239 if (chmod(dest_file, mode) == -1) {
237 240 (void) fprintf(stderr,
238 241 "install: chmod of %s to mode %o failed "
239 242 "(%d): %s\n",
240 243 dest_file, mode, errno, strerror(errno));
241 244 return (1);
242 245 }
243 246 }
244 247 }
245 248 return (0);
246 249 }
↓ open down ↓ |
124 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX