1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * TODO: -d option, need a way of recursively making directories and changing
21 * owner/group, will probably modify bb_make_directory(...)
25 #include <sys/types.h>
30 #include <getopt.h> /* struct option */
33 #include "libcoreutils/coreutils.h"
35 #define INSTALL_OPT_CMD 1
36 #define INSTALL_OPT_DIRECTORY 2
37 #define INSTALL_OPT_PRESERVE_TIME 4
38 #define INSTALL_OPT_STRIP 8
39 #define INSTALL_OPT_GROUP 16
40 #define INSTALL_OPT_MODE 32
41 #define INSTALL_OPT_OWNER 64
43 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
44 static const struct option install_long_options[] = {
45 { "directory", 0, NULL, 'd' },
46 { "preserve-timestamps", 0, NULL, 'p' },
47 { "strip", 0, NULL, 's' },
48 { "group", 0, NULL, 'g' },
49 { "mode", 0, NULL, 'm' },
50 { "owner", 0, NULL, 'o' },
55 int install_main(int argc, char **argv)
62 char *mode_str = "0755";
63 int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
64 int ret = EXIT_SUCCESS, flags, i, isdir;
66 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
67 bb_applet_long_options = install_long_options;
69 bb_opt_complementally = "?:s--d:d--s";
70 /* -c exists for backwards compatibility, its needed */
71 flags = bb_getopt_ulflags(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str); /* 'a' must be 2nd */
73 /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
74 if (flags & INSTALL_OPT_PRESERVE_TIME) {
75 copy_flags |= FILEUTILS_PRESERVE_STATUS;
77 bb_parse_mode(mode_str, &mode);
78 gid = get_ug_id(gid_str, bb_xgetgrnam);
79 uid = get_ug_id(uid_str, bb_xgetpwnam);
83 * dont use bb_make_directory() as it cant change uid or gid
84 * perhaps bb_make_directory() should be improved.
86 if (flags & INSTALL_OPT_DIRECTORY) {
87 for (argv += optind; *argv; argv++) {
88 char *old_argv_ptr = *argv + 1;
91 argv_ptr = strchr(old_argv_ptr, '/');
92 old_argv_ptr = argv_ptr;
97 if (mkdir(*argv, mode) == -1) {
98 if (errno != EEXIST) {
99 bb_perror_msg("coulnt create %s", *argv);
104 else if (lchown(*argv, uid, gid) == -1) {
105 bb_perror_msg("cannot change ownership of %s", *argv);
112 } while (old_argv_ptr);
119 isdir = lstat(argv[argc - 1], &statbuf)<0
120 ? 0 : S_ISDIR(statbuf.st_mode);
122 for (i = optind; i < argc - 1; i++) {
125 dest = argv[argc - 1];
126 if (isdir) dest = concat_path_file(argv[argc - 1], basename(argv[i]));
127 ret |= copy_file(argv[i], dest, copy_flags);
129 /* Set the file mode */
130 if (chmod(dest, mode) == -1) {
131 bb_perror_msg("cannot change permissions of %s", dest);
135 /* Set the user and group id */
136 if (lchown(dest, uid, gid) == -1) {
137 bb_perror_msg("cannot change ownership of %s", dest);
140 if (flags & INSTALL_OPT_STRIP) {
141 if (execlp("strip", "strip", dest, NULL) == -1) {
142 bb_error_msg("strip failed");
146 if(ENABLE_FEATURE_CLEAN_UP && isdir) free(dest);