4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
29 #include <sys/param.h>
31 #include <sys/errno.h>
32 #include <sys/types.h>
41 #define FILE_BUFF 40960
43 static int suppress
= 0;
45 static void usage(void);
46 static void file_copy(char *src_file
, char *dest_file
);
47 static void chown_file(const char *file
, const char *group
, const char *owner
);
48 static char *find_basename(const char *str
);
49 static int creatdir(char *fn
);
55 (void) fprintf(stderr
,
56 "usage: install [-sd][-m mode][-g group][-u owner] "
61 file_copy(char *src_file
, char *dest_file
)
66 static char file_buff
[FILE_BUFF
];
68 if ((src_fd
= open(src_file
, O_RDONLY
)) == -1) {
69 (void) fprintf(stderr
, "install:file_copy: %s failed "
70 "(%d): %s\n", src_file
, errno
, strerror(errno
));
74 if ((dest_fd
= open(dest_file
, O_CREAT
|O_WRONLY
|O_TRUNC
, 0755)) == -1) {
75 (void) fprintf(stderr
, "install:file_copy: %s failed "
76 "(%d): %s\n", dest_file
, errno
, strerror(errno
));
80 while ((count
= read(src_fd
, file_buff
, FILE_BUFF
)) > 0) {
81 (void) write(dest_fd
, file_buff
, count
);
85 (void) fprintf(stderr
, "install:file_copy:read failed "
86 "(%d): %s\n", errno
, strerror(errno
));
91 (void) printf("%s installed as %s\n", src_file
, dest_file
);
94 (void) close(dest_fd
);
99 chown_file(const char *file
, const char *group
, const char *owner
)
101 gid_t grp
= (gid_t
)-1;
102 uid_t own
= (uid_t
)-1;
105 grp
= stdfind(group
, groupnames
);
107 (void) fprintf(stderr
, "unknown group(%s)\n", group
);
111 own
= stdfind(owner
, usernames
);
113 (void) fprintf(stderr
, "unknown owner(%s)\n", owner
);
119 if (chown(file
, own
, grp
) == -1) {
120 (void) fprintf(stderr
, "install:chown_file: failed "
121 "(%d): %s\n", errno
, strerror(errno
));
128 find_basename(const char *str
)
135 for (i
= len
-1; i
>= 0; i
--)
137 return ((char *)(str
+ i
+ 1));
138 return ((char *)str
);
146 if (mkdirp(fn
, 0755) == -1) {
149 } else if (!suppress
) {
150 (void) printf("directory %s created\n", fn
);
157 main(int argc
, char **argv
)
165 char *ins_file
= NULL
;
167 char dest_file
[MAXPATHLEN
];
170 while ((c
= getopt(argc
, argv
, "f:sm:du:g:")) != EOF
) {
185 mode
= strtol(optarg
, NULL
, 8);
201 if (argc
== optind
) {
206 if (!dirflg
&& (dirb
== NULL
)) {
207 (void) fprintf(stderr
,
208 "install: no destination directory specified.\n");
212 for (c
= optind
; c
< argc
; c
++) {
216 rv
= creatdir(ins_file
);
218 (void) fprintf(stderr
,
219 "install: creatdir %s (%d): %s\n",
220 ins_file
, errno
, strerror(errno
));
223 (void) strlcpy(dest_file
, ins_file
, MAXPATHLEN
);
226 (void) strcat(strcat(strcpy(dest_file
, dirb
), "/"),
227 find_basename(ins_file
));
228 file_copy(ins_file
, dest_file
);
232 chown_file(dest_file
, group
, owner
);
236 if (chmod(dest_file
, mode
) == -1) {
237 (void) fprintf(stderr
,
238 "install: chmod of %s to mode %o failed "
240 dest_file
, mode
, errno
, strerror(errno
));