-/* The number of bytes to copy at once. */
-#define COPY_BUF 8192
-
-/* Copy file FROM to file TO, performing no translations.
- Return 0 if ok, -1 if error. */
-
-static int
-simple_copy (from, to)
- const char *from;
- const char *to;
-{
- int fromfd, tofd, nread;
- int saved;
- char buf[COPY_BUF];
-
- fromfd = open (from, O_RDONLY);
- if (fromfd < 0)
- return -1;
- tofd = creat (to, 0777);
- if (tofd < 0)
- {
- saved = errno;
- close (fromfd);
- errno = saved;
- return -1;
- }
- while ((nread = read (fromfd, buf, sizeof buf)) > 0)
- {
- if (write (tofd, buf, nread) != nread)
- {
- saved = errno;
- close (fromfd);
- close (tofd);
- errno = saved;
- return -1;
- }
- }
- saved = errno;
- close (fromfd);
- close (tofd);
- if (nread < 0)
- {
- errno = saved;
- return -1;
- }
- return 0;
-}
-
-#ifndef S_ISLNK
-#ifdef S_IFLNK
-#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
-#else
-#define S_ISLNK(m) 0
-#define lstat stat
-#endif
-#endif
-
-/* Rename FROM to TO, copying if TO is a link.
- Assumes that TO already exists, because FROM is a temp file.
- Return 0 if ok, -1 if error. */
-
-static int
-smart_rename (from, to)
- const char *from;
- const char *to;
-{
- struct stat s;
- int ret = 0;
-
- if (lstat (to, &s))
- return -1;
-
- /* Use rename only if TO is not a symbolic link and has
- only one hard link. */
- if (!S_ISLNK (s.st_mode) && s.st_nlink == 1)
- {
- ret = rename (from, to);
- if (ret == 0)
- {
- /* Try to preserve the permission bits and ownership of TO.
- First get the mode right except for the setuid bit. Then
- change the ownership. Then fix the setuid bit. We do
- the chmod before the chown because if the chown succeeds,
- and we are a normal user, we won't be able to do the
- chmod afterward. We don't bother to fix the setuid bit
- first because that might introduce a fleeting security
- problem, and because the chown will clear the setuid bit
- anyhow. We only fix the setuid bit if the chown
- succeeds, because we don't want to introduce an
- unexpected setuid file owned by the user running objcopy. */
- chmod (to, s.st_mode & 0777);
- if (chown (to, s.st_uid, s.st_gid) >= 0)
- chmod (to, s.st_mode & 07777);
- }
- else
- {
- /* We have to clean up here. */
- int saved = errno;
- fprintf (stderr, "%s: %s: ", program_name, to);
- errno = saved;
- perror ("rename");
- unlink (from);
- }
- }
- else
- {
- ret = simple_copy (from, to);
- if (ret != 0)
- {
- int saved = errno;
- fprintf (stderr, "%s: %s: ", program_name, to);
- errno = saved;
- perror ("simple_copy");
- }
- unlink (from);
- }
- return ret;
-}
-
-/* Set the times of the file DESTINATION to be the same as those in
- STATBUF. */
-
-static void
-set_times (destination, statbuf)
- const char *destination;
- const struct stat *statbuf;
-{
- int result;
-
- {
-#ifdef HAVE_GOOD_UTIME_H
- struct utimbuf tb;
-
- tb.actime = statbuf->st_atime;
- tb.modtime = statbuf->st_mtime;
- result = utime (destination, &tb);
-#else /* ! HAVE_GOOD_UTIME_H */
-#ifndef HAVE_UTIMES
- long tb[2];
-
- tb[0] = statbuf->st_atime;
- tb[1] = statbuf->st_mtime;
- result = utime (destination, tb);
-#else /* HAVE_UTIMES */
- struct timeval tv[2];
-
- tv[0].tv_sec = statbuf->st_atime;
- tv[0].tv_usec = 0;
- tv[1].tv_sec = statbuf->st_mtime;
- tv[1].tv_usec = 0;
- result = utimes (destination, tv);
-#endif /* HAVE_UTIMES */
-#endif /* ! HAVE_GOOD_UTIME_H */
- }
-
- if (result != 0)
- {
- fprintf (stderr, "%s: ", destination);
- perror ("can not set time");
- }
-}
-