2 * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Library General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * TODO: -d option, need a way of recursively making directories and changing
20 * owner/group, will probably modify bb_make_directory(...)
24 #include <sys/types.h>
32 #include "libcoreutils/coreutils.h"
34 #define INSTALL_OPT_CMD 1
35 #define INSTALL_OPT_DIRECTORY 2
36 #define INSTALL_OPT_PRESERVE_TIME 4
37 #define INSTALL_OPT_STRIP 8
38 #define INSTALL_OPT_GROUP 16
39 #define INSTALL_OPT_MODE 32
40 #define INSTALL_OPT_OWNER 64
42 static const struct option install_long_options[] = {
43 { "directory", 0, NULL, 'd' },
44 { "preserve-timestamps", 0, NULL, 'p' },
45 { "strip", 0, NULL, 's' },
46 { "group", 0, NULL, 'g' },
47 { "mode", 0, NULL, 'm' },
48 { "owner", 0, NULL, 'o' },
52 extern int install_main(int argc, char **argv)
60 char *mode_str = "0755";
61 int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
62 int ret = EXIT_SUCCESS;
66 bb_applet_long_options = install_long_options;
67 bb_opt_complementaly = "s~d:d~s";
68 /* -c exists for backwards compatability, its needed */
69 flags = bb_getopt_ulflags(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str); /* 'a' must be 2nd */
71 /* Check valid options were given */
72 if(flags & 0x80000000UL) {
76 /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
77 if (flags & INSTALL_OPT_PRESERVE_TIME) {
78 copy_flags |= FILEUTILS_PRESERVE_STATUS;
80 bb_parse_mode(mode_str, &mode);
81 gid = get_ug_id(gid_str, my_getgrnam);
82 uid = get_ug_id(uid_str, my_getpwnam);
86 * dont use bb_make_directory() as it cant change uid or gid
87 * perhaps bb_make_directory() should be improved.
89 if (flags & INSTALL_OPT_DIRECTORY) {
90 for (argv += optind; *argv; argv++) {
91 char *old_argv_ptr = *argv + 1;
94 argv_ptr = strchr(old_argv_ptr, '/');
95 old_argv_ptr = argv_ptr;
100 if (mkdir(*argv, mode) == -1) {
101 if (errno != EEXIST) {
102 bb_perror_msg("coulnt create %s", *argv);
107 else if (lchown(*argv, uid, gid) == -1) {
108 bb_perror_msg("cannot change ownership of %s", *argv);
115 } while (old_argv_ptr);
120 cp_mv_stat2(argv[argc - 1], &statbuf, lstat);
121 for (i = optind; i < argc - 1; i++) {
124 if (S_ISDIR(statbuf.st_mode)) {
125 dest = concat_path_file(argv[argc - 1], basename(argv[i]));
127 dest = argv[argc - 1];
129 ret |= copy_file(argv[i], dest, copy_flags);
131 /* Set the file mode */
132 if (chmod(dest, mode) == -1) {
133 bb_perror_msg("cannot change permissions of %s", dest);
137 /* Set the user and group id */
138 if (lchown(dest, uid, gid) == -1) {
139 bb_perror_msg("cannot change ownership of %s", dest);
142 if (flags & INSTALL_OPT_STRIP) {
143 if (execlp("strip", "strip", dest, NULL) == -1) {
144 bb_error_msg("strip failed");