1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au>
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7 * TODO: -d option, need a way of recursively making directories and changing
8 * owner/group, will probably modify bb_make_directory(...)
12 #include "libcoreutils/coreutils.h"
14 #include <getopt.h> /* struct option */
16 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
17 static const struct option install_long_options[] = {
18 { "directory", 0, NULL, 'd' },
19 { "preserve-timestamps", 0, NULL, 'p' },
20 { "strip", 0, NULL, 's' },
21 { "group", 0, NULL, 'g' },
22 { "mode", 0, NULL, 'm' },
23 { "owner", 0, NULL, 'o' },
28 int install_main(int argc, char **argv);
29 int install_main(int argc, char **argv)
38 int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
39 int ret = EXIT_SUCCESS, flags, i, isdir;
44 OPT_PRESERVE_TIME = 0x4,
51 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
52 applet_long_options = install_long_options;
54 opt_complementary = "?:s--d:d--s";
55 /* -c exists for backwards compatibility, its needed */
56 flags = getopt32(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str);
58 /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
59 if (flags & OPT_PRESERVE_TIME) {
60 copy_flags |= FILEUTILS_PRESERVE_STATUS;
63 if (flags & OPT_MODE) bb_parse_mode(mode_str, &mode);
64 uid = (flags & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid();
65 gid = (flags & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid();
66 if (flags & (OPT_OWNER|OPT_GROUP)) umask(0);
69 * don't use bb_make_directory() as it can't change uid or gid
70 * perhaps bb_make_directory() should be improved.
72 if (flags & OPT_DIRECTORY) {
73 for (argv += optind; *argv; argv++) {
74 char *old_argv_ptr = *argv + 1;
77 argv_ptr = strchr(old_argv_ptr, '/');
78 old_argv_ptr = argv_ptr;
83 if (mkdir(*argv, mode | 0111) == -1) {
84 if (errno != EEXIST) {
85 bb_perror_msg("cannot create %s", *argv);
90 if ((flags & (OPT_OWNER|OPT_GROUP))
91 && lchown(*argv, uid, gid) == -1
93 bb_perror_msg("cannot change ownership of %s", *argv);
100 } while (old_argv_ptr);
105 isdir = lstat(argv[argc - 1], &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode);
107 for (i = optind; i < argc - 1; i++) {
110 dest = argv[argc - 1];
112 dest = concat_path_file(argv[argc - 1], basename(argv[i]));
113 ret |= copy_file(argv[i], dest, copy_flags);
115 /* Set the file mode */
116 if ((flags & OPT_MODE) && chmod(dest, mode) == -1) {
117 bb_perror_msg("cannot change permissions of %s", dest);
121 /* Set the user and group id */
122 if ((flags & (OPT_OWNER|OPT_GROUP))
123 && lchown(dest, uid, gid) == -1
125 bb_perror_msg("cannot change ownership of %s", dest);
128 if (flags & OPT_STRIP) {
129 if (execlp("strip", "strip", dest, NULL) == -1) {
130 bb_perror_msg("strip");
134 if (ENABLE_FEATURE_CLEAN_UP && isdir) free(dest);