aboutsummaryrefslogtreecommitdiff
path: root/coreutils/install.c
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils/install.c')
-rw-r--r--coreutils/install.c131
1 files changed, 131 insertions, 0 deletions
diff --git a/coreutils/install.c b/coreutils/install.c
new file mode 100644
index 000000000..3e003905e
--- /dev/null
+++ b/coreutils/install.c
@@ -0,0 +1,131 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au>
4 *
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6 *
7 * TODO: -d option, need a way of recursively making directories and changing
8 * owner/group, will probably modify bb_make_directory(...)
9 */
10
11#include "busybox.h"
12#include "libcoreutils/coreutils.h"
13#include <libgen.h>
14#include <getopt.h> /* struct option */
15
16#define INSTALL_OPT_CMD 1
17#define INSTALL_OPT_DIRECTORY 2
18#define INSTALL_OPT_PRESERVE_TIME 4
19#define INSTALL_OPT_STRIP 8
20#define INSTALL_OPT_GROUP 16
21#define INSTALL_OPT_MODE 32
22#define INSTALL_OPT_OWNER 64
23
24#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
25static const struct option install_long_options[] = {
26 { "directory", 0, NULL, 'd' },
27 { "preserve-timestamps", 0, NULL, 'p' },
28 { "strip", 0, NULL, 's' },
29 { "group", 0, NULL, 'g' },
30 { "mode", 0, NULL, 'm' },
31 { "owner", 0, NULL, 'o' },
32 { 0, 0, 0, 0 }
33};
34#endif
35
36int install_main(int argc, char **argv)
37{
38 mode_t mode;
39 uid_t uid;
40 gid_t gid;
41 char *gid_str = "-1";
42 char *uid_str = "-1";
43 char *mode_str = "0755";
44 int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
45 int ret = EXIT_SUCCESS, flags, i, isdir;
46
47#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
48 applet_long_options = install_long_options;
49#endif
50 opt_complementary = "?:s--d:d--s";
51 /* -c exists for backwards compatibility, its needed */
52 flags = getopt32(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str); /* 'a' must be 2nd */
53
54 /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
55 if (flags & INSTALL_OPT_PRESERVE_TIME) {
56 copy_flags |= FILEUTILS_PRESERVE_STATUS;
57 }
58 bb_parse_mode(mode_str, &mode);
59 gid = get_ug_id(gid_str, bb_xgetgrnam);
60 uid = get_ug_id(uid_str, bb_xgetpwnam);
61 umask(0);
62
63 /* Create directories
64 * don't use bb_make_directory() as it can't change uid or gid
65 * perhaps bb_make_directory() should be improved.
66 */
67 if (flags & INSTALL_OPT_DIRECTORY) {
68 for (argv += optind; *argv; argv++) {
69 char *old_argv_ptr = *argv + 1;
70 char *argv_ptr;
71 do {
72 argv_ptr = strchr(old_argv_ptr, '/');
73 old_argv_ptr = argv_ptr;
74 if (argv_ptr) {
75 *argv_ptr = '\0';
76 old_argv_ptr++;
77 }
78 if (mkdir(*argv, mode) == -1) {
79 if (errno != EEXIST) {
80 bb_perror_msg("cannot create %s", *argv);
81 ret = EXIT_FAILURE;
82 break;
83 }
84 }
85 else if (lchown(*argv, uid, gid) == -1) {
86 bb_perror_msg("cannot change ownership of %s", *argv);
87 ret = EXIT_FAILURE;
88 break;
89 }
90 if (argv_ptr) {
91 *argv_ptr = '/';
92 }
93 } while (old_argv_ptr);
94 }
95 return ret;
96 }
97
98 {
99 struct stat statbuf;
100 isdir = lstat(argv[argc - 1], &statbuf)<0
101 ? 0 : S_ISDIR(statbuf.st_mode);
102 }
103 for (i = optind; i < argc - 1; i++) {
104 char *dest;
105
106 dest = argv[argc - 1];
107 if (isdir) dest = concat_path_file(argv[argc - 1], basename(argv[i]));
108 ret |= copy_file(argv[i], dest, copy_flags);
109
110 /* Set the file mode */
111 if (chmod(dest, mode) == -1) {
112 bb_perror_msg("cannot change permissions of %s", dest);
113 ret = EXIT_FAILURE;
114 }
115
116 /* Set the user and group id */
117 if (lchown(dest, uid, gid) == -1) {
118 bb_perror_msg("cannot change ownership of %s", dest);
119 ret = EXIT_FAILURE;
120 }
121 if (flags & INSTALL_OPT_STRIP) {
122 if (execlp("strip", "strip", dest, NULL) == -1) {
123 bb_error_msg("strip failed");
124 ret = EXIT_FAILURE;
125 }
126 }
127 if(ENABLE_FEATURE_CLEAN_UP && isdir) free(dest);
128 }
129
130 return ret;
131}