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 <sys/types.h>
17 #include <getopt.h> /* struct option */
20 #include "libcoreutils/coreutils.h"
22 #define INSTALL_OPT_CMD 1
23 #define INSTALL_OPT_DIRECTORY 2
24 #define INSTALL_OPT_PRESERVE_TIME 4
25 #define INSTALL_OPT_STRIP 8
26 #define INSTALL_OPT_GROUP 16
27 #define INSTALL_OPT_MODE 32
28 #define INSTALL_OPT_OWNER 64
30 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
31 static const struct option install_long_options[] = {
32 { "directory", 0, NULL, 'd' },
33 { "preserve-timestamps", 0, NULL, 'p' },
34 { "strip", 0, NULL, 's' },
35 { "group", 0, NULL, 'g' },
36 { "mode", 0, NULL, 'm' },
37 { "owner", 0, NULL, 'o' },
42 int install_main(int argc, char **argv)
49 char *mode_str = "0755";
50 int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
51 int ret = EXIT_SUCCESS, flags, i, isdir;
53 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
54 bb_applet_long_options = install_long_options;
56 bb_opt_complementally = "?:s--d:d--s";
57 /* -c exists for backwards compatibility, its needed */
58 flags = bb_getopt_ulflags(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str); /* 'a' must be 2nd */
60 /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
61 if (flags & INSTALL_OPT_PRESERVE_TIME) {
62 copy_flags |= FILEUTILS_PRESERVE_STATUS;
64 bb_parse_mode(mode_str, &mode);
65 gid = get_ug_id(gid_str, bb_xgetgrnam);
66 uid = get_ug_id(uid_str, bb_xgetpwnam);
70 * dont use bb_make_directory() as it cant change uid or gid
71 * perhaps bb_make_directory() should be improved.
73 if (flags & INSTALL_OPT_DIRECTORY) {
74 for (argv += optind; *argv; argv++) {
75 char *old_argv_ptr = *argv + 1;
78 argv_ptr = strchr(old_argv_ptr, '/');
79 old_argv_ptr = argv_ptr;
84 if (mkdir(*argv, mode) == -1) {
85 if (errno != EEXIST) {
86 bb_perror_msg("coulnt create %s", *argv);
91 else if (lchown(*argv, uid, gid) == -1) {
92 bb_perror_msg("cannot change ownership of %s", *argv);
99 } while (old_argv_ptr);
106 isdir = lstat(argv[argc - 1], &statbuf)<0
107 ? 0 : S_ISDIR(statbuf.st_mode);
109 for (i = optind; i < argc - 1; i++) {
112 dest = argv[argc - 1];
113 if (isdir) dest = concat_path_file(argv[argc - 1], basename(argv[i]));
114 ret |= copy_file(argv[i], dest, copy_flags);
116 /* Set the file mode */
117 if (chmod(dest, mode) == -1) {
118 bb_perror_msg("cannot change permissions of %s", dest);
122 /* Set the user and group id */
123 if (lchown(dest, uid, gid) == -1) {
124 bb_perror_msg("cannot change ownership of %s", dest);
127 if (flags & INSTALL_OPT_STRIP) {
128 if (execlp("strip", "strip", dest, NULL) == -1) {
129 bb_error_msg("strip failed");
133 if(ENABLE_FEATURE_CLEAN_UP && isdir) free(dest);