aboutsummaryrefslogtreecommitdiff
path: root/coreutils/install.c
diff options
context:
space:
mode:
authorbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2003-09-24 03:22:57 +0000
committerbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2003-09-24 03:22:57 +0000
commitab96005684675240c7afc36c3547dfd4bfb99f23 (patch)
treead1e21191b4aef5ff0c0fa9c30717e77d023cc5d /coreutils/install.c
parent9c678a9d982552ed14f26255781d16c58fbf9bc9 (diff)
downloadbusybox-w32-ab96005684675240c7afc36c3547dfd4bfb99f23.tar.gz
busybox-w32-ab96005684675240c7afc36c3547dfd4bfb99f23.tar.bz2
busybox-w32-ab96005684675240c7afc36c3547dfd4bfb99f23.zip
Add the "install" applet, move get_ug_id to libbb as its used by chown,
chgrp and install. git-svn-id: svn://busybox.net/trunk/busybox@7557 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'coreutils/install.c')
-rw-r--r--coreutils/install.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/coreutils/install.c b/coreutils/install.c
new file mode 100644
index 000000000..b235817cc
--- /dev/null
+++ b/coreutils/install.c
@@ -0,0 +1,102 @@
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 <unistd.h>
31
32#include "libbb.h"
33
34extern int install_main(int argc, char **argv)
35{
36 struct stat statbuf;
37 int i;
38 int ret = EXIT_SUCCESS;
39 uid_t uid = -1;
40 gid_t gid = -1;
41 int copy_flags = 0;
42 int strip_flag = 0;
43 mode_t mode = 0755;
44
45 /* -c exists for backwards compatability, its needed */
46 while ((i = getopt(argc, argv, "cg:m:o:ps")) != -1) {
47 switch (i) {
48 case 'g': /* group */
49 gid = get_ug_id(optarg, my_getgrnam);
50 break;
51 case 'm': /* mode */
52 bb_parse_mode(optarg, &mode);
53 break;
54 case 'o': /* owner */
55 uid = get_ug_id(optarg, my_getpwnam);
56 break;
57 case 'p': /* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
58 copy_flags |= FILEUTILS_PRESERVE_STATUS;
59 break;
60 case 's': /* Strip binaries */
61 strip_flag = 1;
62 break;
63 default:
64 bb_show_usage();
65 }
66 }
67
68 if ((stat(argv[argc - 1], &statbuf) == -1) && (errno != ENOENT)) {
69 bb_perror_msg_and_die("stat failed for %s: ", argv[argc - 1]);
70 }
71
72 for (i = optind; i < argc - 1; i++) {
73 unsigned char *dest;
74
75 if (S_ISDIR(statbuf.st_mode)) {
76 dest = concat_path_file(argv[argc - 1], argv[i]);
77 } else {
78 dest = argv[argc - 1];
79 }
80 ret |= copy_file(argv[i], dest, copy_flags);
81
82 /* Set the file mode */
83 if (chmod(dest, mode) == -1) {
84 bb_perror_msg("cannot change permissions of %s", dest);
85 ret |= EXIT_FAILURE;
86 }
87
88 /* Set the user and group id */
89 if (chown(dest, uid, gid) == -1) {
90 bb_perror_msg("cannot change ownership of %s", dest);
91 ret |= EXIT_FAILURE;
92 }
93 if (strip_flag) {
94 if (execlp("strip", "strip", dest, NULL) == -1) {
95 bb_error_msg("strip failed");
96 ret |= EXIT_FAILURE;
97 }
98 }
99 }
100
101 return(ret);
102}