made "test" an ash built-in.
[oweals/busybox.git] / coreutils / install.c
1 /*
2  *  Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au>
3  *
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.
8  *
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.
13  *
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.
17  *
18  *
19  * TODO: -d option, need a way of recursively making directories and changing
20  *           owner/group, will probably modify bb_make_directory(...)
21  */
22
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <getopt.h> /* struct option */
30
31 #include "busybox.h"
32 #include "libcoreutils/coreutils.h"
33
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
41
42 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
43 static const struct option install_long_options[] = {
44         { "directory",  0,      NULL,   'd' },
45         { "preserve-timestamps",        0,      NULL,   'p' },
46         { "strip",      0,      NULL,   's' },
47         { "group",      0,      NULL,   'g' },
48         { "mode",       0,      NULL,   'm' },
49         { "owner",      0,      NULL,   'o' },
50         { 0,    0,      0,      0 }
51 };
52 #endif
53
54 int install_main(int argc, char **argv)
55 {
56         mode_t mode;
57         uid_t uid;
58         gid_t gid;
59         char *gid_str = "-1";
60         char *uid_str = "-1";
61         char *mode_str = "0755";
62         int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
63         int ret = EXIT_SUCCESS, flags, i, isdir;
64
65 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
66         bb_applet_long_options = install_long_options;
67 #endif
68         bb_opt_complementally = "?:s--d:d--s";
69         /* -c exists for backwards compatibility, its needed */
70         flags = bb_getopt_ulflags(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str);     /* 'a' must be 2nd */
71
72         /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
73         if (flags & INSTALL_OPT_PRESERVE_TIME) {
74                 copy_flags |= FILEUTILS_PRESERVE_STATUS;
75         }
76         bb_parse_mode(mode_str, &mode);
77         gid = get_ug_id(gid_str, bb_xgetgrnam);
78         uid = get_ug_id(uid_str, bb_xgetpwnam);
79         umask(0);
80
81         /* Create directories
82          * dont use bb_make_directory() as it cant change uid or gid
83          * perhaps bb_make_directory() should be improved.
84          */
85         if (flags & INSTALL_OPT_DIRECTORY) {
86                 for (argv += optind; *argv; argv++) {
87                         char *old_argv_ptr = *argv + 1;
88                         char *argv_ptr;
89                         do {
90                                 argv_ptr = strchr(old_argv_ptr, '/');
91                                 old_argv_ptr = argv_ptr;
92                                 if (argv_ptr) {
93                                         *argv_ptr = '\0';
94                                         old_argv_ptr++;
95                                 }
96                                 if (mkdir(*argv, mode) == -1) {
97                                         if (errno != EEXIST) {
98                                                 bb_perror_msg("coulnt create %s", *argv);
99                                                 ret = EXIT_FAILURE;
100                                                 break;
101                                         }
102                                 }
103                                 else if (lchown(*argv, uid, gid) == -1) {
104                                         bb_perror_msg("cannot change ownership of %s", *argv);
105                                         ret = EXIT_FAILURE;
106                                         break;
107                                 }
108                                 if (argv_ptr) {
109                                         *argv_ptr = '/';
110                                 }
111                         } while (old_argv_ptr);
112                 }
113                 return(ret);
114         }
115
116         {
117                 struct stat statbuf;
118                 isdir = lstat(argv[argc - 1], &statbuf)<0
119                                         ? 0 : S_ISDIR(statbuf.st_mode);
120         }
121         for (i = optind; i < argc - 1; i++) {
122                 char *dest;
123
124                 dest = argv[argc - 1];
125                 if (isdir) dest = concat_path_file(argv[argc - 1], basename(argv[i]));
126                 ret |= copy_file(argv[i], dest, copy_flags);
127
128                 /* Set the file mode */
129                 if (chmod(dest, mode) == -1) {
130                         bb_perror_msg("cannot change permissions of %s", dest);
131                         ret = EXIT_FAILURE;
132                 }
133
134                 /* Set the user and group id */
135                 if (lchown(dest, uid, gid) == -1) {
136                         bb_perror_msg("cannot change ownership of %s", dest);
137                         ret = EXIT_FAILURE;
138                 }
139                 if (flags & INSTALL_OPT_STRIP) {
140                         if (execlp("strip", "strip", dest, NULL) == -1) {
141                                 bb_error_msg("strip failed");
142                                 ret = EXIT_FAILURE;
143                         }
144                 }
145                 if(ENABLE_FEATURE_CLEAN_UP && isdir) free(dest);
146         }
147
148         return(ret);
149 }