blob: e057aebedbe1a3923874d19dc02a91f05222ec37 [file] [log] [blame]
Glenn L McGratheebcc1d2003-09-24 03:22:57 +00001/*
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>
Glenn L McGratha406a9c2003-09-24 05:00:29 +000030#include <string.h>
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000031#include <unistd.h>
32
33#include "libbb.h"
34
35extern 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;
Glenn L McGratha406a9c2003-09-24 05:00:29 +000044 int dir_flag = 0;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000045 mode_t mode = 0755;
46
47 /* -c exists for backwards compatability, its needed */
Glenn L McGratha406a9c2003-09-24 05:00:29 +000048 while ((i = getopt(argc, argv, "cdg:m:o:ps")) != -1) {
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000049 switch (i) {
Glenn L McGratha406a9c2003-09-24 05:00:29 +000050 case 'd': /* Create directories */
51 dir_flag = 1;
52 break;
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000053 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 break;
68 default:
69 bb_show_usage();
70 }
71 }
72
Glenn L McGratha406a9c2003-09-24 05:00:29 +000073 if (dir_flag) {
74 for (argv += optind; *argv; argv++) {
75 unsigned char *dir_name = *argv;
76 unsigned char *argv_ptr;
77
78 ret |= bb_make_directory(dir_name, mode, FILEUTILS_RECUR);
79 do {
80 argv_ptr = strrchr(dir_name, '/');
81
82 /* Skip the "." and ".." directories */
83 if ((dir_name[0] == '.') && ((dir_name[1] == '\0') || ((dir_name[1] == '.') && (dir_name[2] == '\0')))) {
84 break;
85 }
86 if (chown(dir_name, uid, gid) == -1) {
87 bb_perror_msg("cannot change ownership of %s", argv_ptr);
88 ret |= EXIT_FAILURE;
89 }
90 if (argv_ptr) {
91 *argv_ptr = '\0';
92 }
93 } while (argv_ptr);
94 }
95 return(ret);
96 }
97
Glenn L McGratheebcc1d2003-09-24 03:22:57 +000098 if ((stat(argv[argc - 1], &statbuf) == -1) && (errno != ENOENT)) {
99 bb_perror_msg_and_die("stat failed for %s: ", argv[argc - 1]);
100 }
101
102 for (i = optind; i < argc - 1; i++) {
103 unsigned char *dest;
104
105 if (S_ISDIR(statbuf.st_mode)) {
106 dest = concat_path_file(argv[argc - 1], argv[i]);
107 } else {
108 dest = argv[argc - 1];
109 }
110 ret |= copy_file(argv[i], dest, copy_flags);
111
112 /* Set the file mode */
113 if (chmod(dest, mode) == -1) {
114 bb_perror_msg("cannot change permissions of %s", dest);
115 ret |= EXIT_FAILURE;
116 }
117
118 /* Set the user and group id */
119 if (chown(dest, uid, gid) == -1) {
120 bb_perror_msg("cannot change ownership of %s", dest);
121 ret |= EXIT_FAILURE;
122 }
123 if (strip_flag) {
124 if (execlp("strip", "strip", dest, NULL) == -1) {
125 bb_error_msg("strip failed");
126 ret |= EXIT_FAILURE;
127 }
128 }
129 }
130
131 return(ret);
132}