tar -Z, uncompress support
[oweals/busybox.git] / coreutils / install.c
1 /*
2  *  Copyright (C) 2003 by Glenn McGrath <bug1@optushome.com.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  *       Use bb_getopt_ulflags(...) ?
22  *
23  */
24
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <errno.h>
28 #include <getopt.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #include "libbb.h"
34
35 extern int install_main(int argc, char **argv)
36 {
37         struct stat statbuf;
38         int i;
39         int ret = EXIT_SUCCESS;
40         uid_t uid = -1;
41         gid_t gid = -1;
42         int copy_flags = 0;
43         int strip_flag = 0;
44         int dir_flag = 0;
45         mode_t mode = 0755;
46
47         /* -c exists for backwards compatability, its needed */
48         while ((i = getopt(argc, argv, "cdg:m:o:ps")) != -1) {
49                 switch (i) {
50                 case 'd':       /* Create directories */
51                         dir_flag = 1;
52                         break;
53                 case 'g':       /* group */
54                         gid = get_ug_id(optarg, my_getgrnam);
55                         break;
56                 case 'm':       /* mode */
57                         bb_parse_mode(optarg, &mode);
58                         break;
59                 case 'o':       /* owner */
60                         uid = get_ug_id(optarg, my_getpwnam);
61                         break;
62                 case 'p':       /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
63                         copy_flags |= FILEUTILS_PRESERVE_STATUS;
64                         break;
65                 case 's':       /* Strip binaries */
66                         strip_flag = 1;
67                         /* Fall through */
68                 case 'c':
69                         /* do nothing */
70                         break;
71                 default:
72                         bb_show_usage();
73                 }
74         }
75
76         if (dir_flag) {
77                 for (argv += optind; *argv; argv++) {
78                         unsigned char *dir_name = *argv;
79                         unsigned char *argv_ptr;
80
81                         ret |= bb_make_directory(dir_name, mode, FILEUTILS_RECUR);
82                         do {
83                                 argv_ptr = strrchr(dir_name, '/');
84
85                                 /* Skip the "." and ".." directories */
86                                 if ((dir_name[0] == '.') && ((dir_name[1] == '\0') || ((dir_name[1] == '.') && (dir_name[2] == '\0')))) {
87                                         break;
88                                 }
89                                 if (chown(dir_name, uid, gid) == -1) {
90                                         bb_perror_msg("cannot change ownership of %s", argv_ptr);
91                                         ret |= EXIT_FAILURE;
92                                 }
93                                 if (argv_ptr) {
94                                         *argv_ptr = '\0';
95                                 }
96                         } while (argv_ptr);
97                 }
98                 return(ret);
99         }
100
101         if ((stat(argv[argc - 1], &statbuf) == -1) && (errno != ENOENT)) {
102                 bb_perror_msg_and_die("stat failed for %s: ", argv[argc - 1]);
103         }
104
105         for (i = optind; i < argc - 1; i++) {
106                 unsigned char *dest;
107
108                 if (S_ISDIR(statbuf.st_mode)) {
109                         dest = concat_path_file(argv[argc - 1], argv[i]);
110                 } else {
111                         dest = argv[argc - 1];
112                 }
113                 ret |= copy_file(argv[i], dest, copy_flags);
114
115                 /* Set the file mode */
116                 if (chmod(dest, mode) == -1) {
117                         bb_perror_msg("cannot change permissions of %s", dest);
118                         ret |= EXIT_FAILURE;
119                 }
120
121                 /* Set the user and group id */
122                 if (chown(dest, uid, gid) == -1) {
123                         bb_perror_msg("cannot change ownership of %s", dest);
124                         ret |= EXIT_FAILURE;                    
125                 }
126                 if (strip_flag) {
127                         if (execlp("strip", "strip", dest, NULL) == -1) {
128                                 bb_error_msg("strip failed");
129                                 ret |= EXIT_FAILURE;                    
130                         }
131                 }
132         }
133         
134         return(ret);
135 }