1 /* install - copy files and set attributes
2 Copyright (C) 1989, 1990, 1991, 1995 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> */
58 #include <sys/types.h>
64 #include "backupfile.h"
65 #include "modechange.h"
73 struct passwd
*getpwnam ();
74 struct group
*getgrnam ();
76 #ifndef _POSIX_VERSION
83 # define endgrent() ((void) 0)
87 # define endpwent() ((void) 0)
90 /* True if C is an ASCII octal digit. */
91 #define isodigit(c) ((c) >= '0' && c <= '7')
93 /* Number of bytes of a file to copy at a time. */
94 #define READ_SIZE (32 * 1024)
102 enum backup_type
get_version ();
104 static int change_attributes
__P ((char *path
, int no_need_to_chown
));
105 static int copy_file
__P ((char *from
, char *to
, int *to_created
));
106 static int install_file_in_dir
__P ((char *from
, char *to_dir
));
107 static int install_file_in_file
__P ((char *from
, char *to
));
108 static void get_ids
__P ((void));
109 static void strip
__P ((char *path
));
110 static void usage
__P ((int status
));
112 /* The name this program was run with, for error messages. */
115 /* The user name that will own the files, or NULL to make the owner
116 the current user ID. */
117 static char *owner_name
;
119 /* The user ID corresponding to `owner_name'. */
120 static uid_t owner_id
;
122 /* The group name that will own the files, or NULL to make the group
123 the current group ID. */
124 static char *group_name
;
126 /* The group ID corresponding to `group_name'. */
127 static gid_t group_id
;
129 /* The permissions to which the files will be set. The umask has
133 /* If nonzero, strip executable files after copying them. */
134 static int strip_files
;
136 /* If nonzero, install a directory instead of a regular file. */
139 /* If nonzero, display usage information and exit. */
140 static int show_help
;
142 /* If nonzero, print the version on standard output and exit. */
143 static int show_version
;
145 static struct option
const long_options
[] =
147 {"strip", no_argument
, NULL
, 's'},
148 {"directory", no_argument
, NULL
, 'd'},
149 {"group", required_argument
, NULL
, 'g'},
150 {"mode", required_argument
, NULL
, 'm'},
151 {"owner", required_argument
, NULL
, 'o'},
152 {"backup", no_argument
, NULL
, 'b'},
153 {"version-control", required_argument
, NULL
, 'V'},
154 {"help", no_argument
, &show_help
, 1},
155 {"version", no_argument
, &show_version
, 1},
160 main (int argc
, char **argv
)
164 char *symbolic_mode
= NULL
;
165 int make_backups
= 0;
168 program_name
= argv
[0];
169 setlocale (LC_ALL
, "");
170 bindtextdomain (PACKAGE
, LOCALEDIR
);
171 textdomain (PACKAGE
);
180 version
= getenv ("SIMPLE_BACKUP_SUFFIX");
182 simple_backup_suffix
= version
;
183 version
= getenv ("VERSION_CONTROL");
185 while ((optc
= getopt_long (argc
, argv
, "bcsdg:m:o:V:S:", long_options
,
207 symbolic_mode
= optarg
;
213 simple_backup_suffix
= optarg
;
225 printf ("install - %s\n", version_string
);
232 /* Check for invalid combinations of arguments. */
233 if (dir_arg
&& strip_files
)
235 _("the strip option may not be used when installing a directory"));
238 backup_type
= get_version (version
);
240 if (optind
== argc
|| (optind
== argc
- 1 && !dir_arg
))
242 error (0, 0, _("too few arguments"));
248 struct mode_change
*change
= mode_compile (symbolic_mode
, 0);
249 if (change
== MODE_INVALID
)
250 error (1, 0, _("invalid mode `%s'"), symbolic_mode
);
251 else if (change
== MODE_MEMORY_EXHAUSTED
)
252 error (1, 0, _("virtual memory exhausted"));
253 mode
= mode_adjust (0, change
);
260 for (; optind
< argc
; ++optind
)
263 make_path (argv
[optind
], mode
, mode
, owner_id
, group_id
, 0, NULL
);
268 if (optind
== argc
- 2)
270 if (!isdir (argv
[argc
- 1]))
271 errors
= install_file_in_file (argv
[argc
- 2], argv
[argc
- 1]);
273 errors
= install_file_in_dir (argv
[argc
- 2], argv
[argc
- 1]);
277 if (!isdir (argv
[argc
- 1]))
279 for (; optind
< argc
- 1; ++optind
)
281 errors
|= install_file_in_dir (argv
[optind
], argv
[argc
- 1]);
289 /* Copy file FROM onto file TO and give TO the appropriate
291 Return 0 if successful, 1 if an error occurs. */
294 install_file_in_file (char *from
, char *to
)
297 int no_need_to_chown
;
299 if (copy_file (from
, to
, &to_created
))
303 no_need_to_chown
= (to_created
304 && owner_name
== NULL
305 && group_name
== NULL
);
306 return change_attributes (to
, no_need_to_chown
);
309 /* Copy file FROM into directory TO_DIR, keeping its same name,
310 and give the copy the appropriate attributes.
311 Return 0 if successful, 1 if not. */
314 install_file_in_dir (char *from
, char *to_dir
)
320 from_base
= basename (from
);
321 to
= xmalloc ((unsigned) (strlen (to_dir
) + strlen (from_base
) + 2));
322 stpcpy (stpcpy (stpcpy (to
, to_dir
), "/"), from_base
);
323 ret
= install_file_in_file (from
, to
);
328 /* A chunk of a file being copied. */
329 static char buffer
[READ_SIZE
];
331 /* Copy file FROM onto file TO, creating TO if necessary.
332 Return 0 if the copy is successful, 1 if not. If the copy is
333 successful, set *TO_CREATED to nonzero if TO was created (if it did
334 not exist or did, but was unlinked) and to zero otherwise. If the
335 copy fails, don't modify *TO_CREATED. */
338 copy_file (char *from
, char *to
, int *to_created
)
343 struct stat from_stats
, to_stats
;
344 int target_created
= 1;
346 if (stat (from
, &from_stats
))
348 error (0, errno
, "%s", from
);
351 if (!S_ISREG (from_stats
.st_mode
))
353 error (0, 0, _("`%s' is not a regular file"), from
);
356 if (stat (to
, &to_stats
) == 0)
358 if (!S_ISREG (to_stats
.st_mode
))
360 error (0, 0, _("`%s' is not a regular file"), to
);
363 if (from_stats
.st_dev
== to_stats
.st_dev
364 && from_stats
.st_ino
== to_stats
.st_ino
)
366 error (0, 0, _("`%s' and `%s' are the same file"), from
, to
);
370 /* The destination file exists. Try to back it up if required. */
371 if (backup_type
!= none
)
373 char *tmp_backup
= find_backup_file_name (to
);
376 if (tmp_backup
== NULL
)
377 error (1, 0, "virtual memory exhausted");
378 dst_backup
= (char *) alloca (strlen (tmp_backup
) + 1);
379 strcpy (dst_backup
, tmp_backup
);
381 if (rename (to
, dst_backup
))
385 error (0, errno
, "cannot backup `%s'", to
);
391 /* If unlink fails, try to proceed anyway. */
396 fromfd
= open (from
, O_RDONLY
, 0);
399 error (0, errno
, "%s", from
);
403 /* Make sure to open the file in a mode that allows writing. */
404 tofd
= open (to
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
407 error (0, errno
, "%s", to
);
412 while ((bytes
= safe_read (fromfd
, buffer
, READ_SIZE
)) > 0)
413 if (full_write (tofd
, buffer
, bytes
) < 0)
415 error (0, errno
, "%s", to
);
421 error (0, errno
, "%s", from
);
425 if (close (fromfd
) < 0)
427 error (0, errno
, "%s", from
);
430 if (close (tofd
) < 0)
432 error (0, errno
, "%s", to
);
436 *to_created
= target_created
;
445 /* Set the attributes of file or directory PATH.
446 If NO_NEED_TO_CHOWN is nonzero, don't call chown.
447 Return 0 if successful, 1 if not. */
450 change_attributes (char *path
, int no_need_to_chown
)
454 /* chown must precede chmod because on some systems,
455 chown clears the set[ug]id bits for non-superusers,
456 resulting in incorrect permissions.
457 On System V, users can give away files with chown and then not
458 be able to chmod them. So don't give files away.
460 We don't pass -1 to chown to mean "don't change the value"
461 because SVR3 and earlier non-BSD systems don't support that.
463 We don't normally ignore errors from chown because the idea of
464 the install command is that the file is supposed to end up with
465 precisely the attributes that the user specified (or defaulted).
466 If the file doesn't end up with the group they asked for, they'll
467 want to know. But AFS returns EPERM when you try to change a
468 file's group; thus the kludge. */
470 if (!no_need_to_chown
&& chown (path
, owner_id
, group_id
)
476 if (chmod (path
, mode
))
480 error (0, err
, "%s", path
);
486 /* Strip the symbol table from the file PATH.
487 We could dig the magic number out of the file first to
488 determine whether to strip it, but the header files and
489 magic numbers vary so much from system to system that making
490 it portable would be very difficult. Not worth the effort. */
501 error (1, errno
, _("cannot fork"));
504 execlp ("strip", "strip", path
, (char *) NULL
);
505 error (1, errno
, _("cannot run strip"));
507 default: /* Parent. */
508 /* Parent process. */
509 while (pid
!= wait (&status
)) /* Wait for kid to finish. */
515 /* Return nonzero if STR is an ASCII representation of a nonzero
516 decimal integer, zero if not. */
519 is_number (char *str
)
529 /* Initialize the user and group ownership of the files to install. */
539 pw
= getpwnam (owner_name
);
542 if (!is_number (owner_name
))
543 error (1, 0, _("invalid user `%s'"), owner_name
);
544 /* FIXME: atoi won't warn about overflow. Use xstrtoul. */
545 /* FIXME: eliminate is_number altogether! */
546 owner_id
= atoi (owner_name
);
549 owner_id
= pw
->pw_uid
;
553 owner_id
= getuid ();
557 gr
= getgrnam (group_name
);
560 if (!is_number (group_name
))
561 error (1, 0, _("invalid group `%s'"), group_name
);
562 /* FIXME: atoi won't warn about overflow. Use xstrtoul. */
563 group_id
= atoi (group_name
);
566 group_id
= gr
->gr_gid
;
570 group_id
= getgid ();
577 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
582 Usage: %s [OPTION]... SOURCE DEST (1st format)\n\
583 or: %s [OPTION]... SOURCE... DIRECTORY (2nd format)\n\
584 or: %s -d [OPTION]... DIRECTORY... (3nd format)\n\
586 program_name
, program_name
, program_name
);
588 In first two formats, copy SOURCE to DEST or multiple SOURCE(s) to\n\
589 DIRECTORY, while setting permission modes and owner/group. In third\n\
590 format, make all components of the given DIRECTORY(ies).\n\
593 -d, --directory create [leading] directories, mandatory for 3rd format\n\
594 -g, --group=GROUP set group ownership, instead of process' current group\n\
595 -m, --mode=MODE set permission mode (as in chmod), instead of rw-r--r--\n\
596 -o, --owner=OWNER set ownership (super-user only)\n\
597 -s, --strip strip symbol tables, only for 1st and 2nd formats\n\
598 -b, --backup make backup before removal\n\
599 -S, --suffix=SUFFIX override the usual backup suffix\n\
600 -V, --version-control=WORD override the usual version control\n\
601 --help display this help and exit\n\
602 --version output version information and exit\n\
604 The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX. The\n\
605 version control may be set with VERSION_CONTROL, values are:\n\
607 t, numbered make numbered backups\n\
608 nil, existing numbered if numbered backups exist, simple otherwise\n\
609 never, simple always make simple backups\n"));