1 /* install - copy files and set attributes
2 Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
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, or (at your option)
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 General Public License for more details.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Copy files and set their permission modes and, if possible,
19 their owner and group. Used similarly to `cp'; typically
20 used in Makefiles to copy programs into their destination
21 directories. It can also be used to create the destination
22 directories and any leading directories, and to set the final
23 directory's modes. It refuses to copy files onto themselves.
27 Set the group ownership of the installed file or directory
28 to the group ID of GROUP (default is process's current
29 group). GROUP may also be a numeric group ID.
32 Set the permission mode for the installed file or directory
33 to MODE, which is an octal number (default is 0755).
36 If run as root, set the ownership of the installed file to
37 the user ID of OWNER (default is root). OWNER may also be
40 -c No effect. For compatibility with old Unix versions of install.
43 Strip the symbol tables from installed files.
46 Create a directory and its leading directories, if they
47 do not already exist. Set the owner, group and mode
48 as given on the command line. Any leading directories
49 that are created are also given those attributes.
50 This is different from the SunOS 4.0 install, which gives
51 directories that it creates the default attributes.
53 David MacKenzie <djm@gnu.ai.mit.edu> */
56 #if defined (CONFIG_BROKETS)
57 /* We use <config.h> instead of "config.h" so that a compilation
58 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
59 (which it would do because it found this file in $srcdir). */
69 #include <sys/types.h>
74 #include "modechange.h"
76 #if !defined (isascii) || defined (STDC_HEADERS)
81 #define ISDIGIT(c) (isascii (c) && isdigit (c))
86 struct passwd *getpwnam ();
87 struct group *getgrnam ();
98 /* True if C is an ASCII octal digit. */
99 #define isodigit(c) ((c) >= '0' && c <= '7')
101 /* Number of bytes of a file to copy at a time. */
102 #define READ_SIZE (32 * 1024)
113 static int change_attributes ();
114 static int copy_file ();
115 static int install_file_in_dir ();
116 static int install_file_in_file ();
117 static int isnumber ();
118 static void get_ids ();
119 static void strip ();
120 static void usage ();
122 /* The name this program was run with, for error messages. */
125 /* The user name that will own the files, or NULL to make the owner
126 the current user ID. */
127 static char *owner_name;
129 /* The user ID corresponding to `owner_name'. */
130 static uid_t owner_id;
132 /* The group name that will own the files, or NULL to make the group
133 the current group ID. */
134 static char *group_name;
136 /* The group ID corresponding to `group_name'. */
137 static gid_t group_id;
139 /* The permissions to which the files will be set. The umask has
143 /* If nonzero, strip executable files after copying them. */
144 static int strip_files;
146 /* If nonzero, install a directory instead of a regular file. */
149 /* If non-zero, display usage information and exit. */
150 static int show_help;
152 /* If non-zero, print the version on standard output and exit. */
153 static int show_version;
155 static struct option const long_options[] =
157 {"strip", no_argument, NULL, 's'},
158 {"directory", no_argument, NULL, 'd'},
159 {"group", required_argument, NULL, 'g'},
160 {"mode", required_argument, NULL, 'm'},
161 {"owner", required_argument, NULL, 'o'},
162 {"help", no_argument, &show_help, 1},
163 {"version", no_argument, &show_version, 1},
174 char *symbolic_mode = NULL;
176 program_name = argv[0];
184 while ((optc = getopt_long (argc, argv, "csdg:m:o:", long_options,
203 symbolic_mode = optarg;
215 printf ("%s\n", version_string);
222 /* Check for invalid combinations of arguments. */
223 if ((dir_arg && strip_files)
225 || (optind == argc - 1 && !dir_arg))
230 struct mode_change *change = mode_compile (symbolic_mode, 0);
231 if (change == MODE_INVALID)
232 error (1, 0, "invalid mode `%s'", symbolic_mode);
233 else if (change == MODE_MEMORY_EXHAUSTED)
234 error (1, 0, "virtual memory exhausted");
235 mode = mode_adjust (0, change);
242 for (; optind < argc; ++optind)
245 make_path (argv[optind], mode, mode, owner_id, group_id, NULL);
250 if (optind == argc - 2)
252 if (!isdir (argv[argc - 1]))
253 errors = install_file_in_file (argv[argc - 2], argv[argc - 1]);
255 errors = install_file_in_dir (argv[argc - 2], argv[argc - 1]);
259 if (!isdir (argv[argc - 1]))
261 for (; optind < argc - 1; ++optind)
263 errors |= install_file_in_dir (argv[optind], argv[argc - 1]);
271 /* Copy file FROM onto file TO and give TO the appropriate
273 Return 0 if successful, 1 if an error occurs. */
276 install_file_in_file (from, to)
281 int no_need_to_chown;
283 if (copy_file (from, to, &to_created))
287 no_need_to_chown = (to_created
288 && owner_name == NULL
289 && group_name == NULL);
290 return change_attributes (to, no_need_to_chown);
293 /* Copy file FROM into directory TO_DIR, keeping its same name,
294 and give the copy the appropriate attributes.
295 Return 0 if successful, 1 if not. */
298 install_file_in_dir (from, to_dir)
306 from_base = basename (from);
307 to = xmalloc ((unsigned) (strlen (to_dir) + strlen (from_base) + 2));
308 stpcpy (stpcpy (stpcpy (to, to_dir), "/"), from_base);
309 ret = install_file_in_file (from, to);
314 /* A chunk of a file being copied. */
315 static char buffer[READ_SIZE];
317 /* Copy file FROM onto file TO, creating TO if necessary.
318 Return 0 if the copy is successful, 1 if not. If the copy is
319 successful, set *TO_CREATED to non-zero if TO was created (if it did
320 not exist or did, but was unlinked) and to zero otherwise. If the
321 copy fails, don't modify *TO_CREATED. */
324 copy_file (from, to, to_created)
332 struct stat from_stats, to_stats;
333 int target_created = 1;
335 if (stat (from, &from_stats))
337 error (0, errno, "%s", from);
340 if (!S_ISREG (from_stats.st_mode))
342 error (0, 0, "`%s' is not a regular file", from);
345 if (stat (to, &to_stats) == 0)
347 if (!S_ISREG (to_stats.st_mode))
349 error (0, 0, "`%s' is not a regular file", to);
352 if (from_stats.st_dev == to_stats.st_dev
353 && from_stats.st_ino == to_stats.st_ino)
355 error (0, 0, "`%s' and `%s' are the same file", from, to);
358 /* If unlink fails, try to proceed anyway. */
363 fromfd = open (from, O_RDONLY, 0);
366 error (0, errno, "%s", from);
370 /* Make sure to open the file in a mode that allows writing. */
371 tofd = open (to, O_WRONLY | O_CREAT | O_TRUNC, 0600);
374 error (0, errno, "%s", to);
379 while ((bytes = safe_read (fromfd, buffer, READ_SIZE)) > 0)
380 if (full_write (tofd, buffer, bytes) < 0)
382 error (0, errno, "%s", to);
388 error (0, errno, "%s", from);
392 if (close (fromfd) < 0)
394 error (0, errno, "%s", from);
397 if (close (tofd) < 0)
399 error (0, errno, "%s", to);
403 *to_created = target_created;
412 /* Set the attributes of file or directory PATH.
413 If NO_NEED_TO_CHOWN is non-zero, don't call chown.
414 Return 0 if successful, 1 if not. */
417 change_attributes (path, no_need_to_chown)
419 int no_need_to_chown;
423 /* chown must precede chmod because on some systems,
424 chown clears the set[ug]id bits for non-superusers,
425 resulting in incorrect permissions.
426 On System V, users can give away files with chown and then not
427 be able to chmod them. So don't give files away.
429 We don't pass -1 to chown to mean "don't change the value"
430 because SVR3 and earlier non-BSD systems don't support that.
432 We don't normally ignore errors from chown because the idea of
433 the install command is that the file is supposed to end up with
434 precisely the attributes that the user specified (or defaulted).
435 If the file doesn't end up with the group they asked for, they'll
436 want to know. But AFS returns EPERM when you try to change a
437 file's group; thus the kludge. */
439 if (!no_need_to_chown && chown (path, owner_id, group_id)
445 if (chmod (path, mode))
449 error (0, err, "%s", path);
455 /* Strip the symbol table from the file PATH.
456 We could dig the magic number out of the file first to
457 determine whether to strip it, but the header files and
458 magic numbers vary so much from system to system that making
459 it portable would be very difficult. Not worth the effort. */
471 error (1, errno, "cannot fork");
474 execlp ("strip", "strip", path, (char *) NULL);
475 error (1, errno, "cannot run strip");
477 default: /* Parent. */
478 /* Parent process. */
479 while (pid != wait (&status)) /* Wait for kid to finish. */
485 /* Initialize the user and group ownership of the files to install. */
495 pw = getpwnam (owner_name);
498 if (!isnumber (owner_name))
499 error (1, 0, "invalid user `%s'", owner_name);
500 owner_id = atoi (owner_name);
503 owner_id = pw->pw_uid;
507 owner_id = getuid ();
511 gr = getgrnam (group_name);
514 if (!isnumber (group_name))
515 error (1, 0, "invalid group `%s'", group_name);
516 group_id = atoi (group_name);
519 group_id = gr->gr_gid;
523 group_id = getgid ();
526 /* Return nonzero if STR is an ASCII representation of a nonzero
527 decimal integer, zero if not. */
546 fprintf (stderr, "Try `%s --help' for more information.\n",
551 Usage: %s [OPTION]... SOURCE DEST (1st format)\n\
552 or: %s [OPTION]... SOURCE... DIRECTORY (2nd format)\n\
553 or: %s [OPTION]... DIRECTORY... (3nd format)\n\
555 program_name, program_name, program_name);
559 -d, --directory create [leading] directories, mandatory for 3rd format\n\
560 -g, --group=GROUP set group ownership, instead of process' current group\n\
561 -m, --mode=MODE set permission mode (as in chmod), instead of 0755\n\
562 -o, --owner=OWNER set ownership (super-user only)\n\
563 -s, --strip strip symbol tables, only for 1st and 2nd formats\n\
564 --help display this help and exit\n\
565 --version output version information and exit\n");