Ignore:
Timestamp:
Dec 17, 2005, 6:31:29 AM (20 years ago)
Author:
bird
Message:

ln and install builtins (from BSD as usual).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gmake/kmkbuiltin/install.c

    r367 r368  
    7979#define BACKUP_SUFFIX   ".old"
    8080
    81 struct passwd *pp;
    82 struct group *gp;
    83 gid_t gid;
    84 uid_t uid;
    85 int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose;
    86 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
    87 const char *suffix = BACKUP_SUFFIX;
    88 
    89 void    copy(int, const char *, int, const char *, off_t);
    90 int     compare(int, const char *, size_t, int, const char *, size_t);
    91 int     create_newfile(const char *, int, struct stat *);
    92 int     create_tempfile(const char *, char *, size_t);
    93 void    install(const char *, const char *, u_long, u_int);
    94 void    install_dir(char *);
    95 u_long  numeric_id(const char *, const char *);
    96 void    strip(const char *);
    97 int     trymmap(int);
    98 void    usage(void);
     81#ifndef O_BINARY
     82# define O_BINARY 0
     83#endif
     84
     85static struct passwd *pp;
     86static struct group *gp;
     87static gid_t gid;
     88static uid_t uid;
     89static int dobackup, docompare, dodir, dopreserve, dostrip, nommap, safecopy, verbose;
     90static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
     91static const char *suffix = BACKUP_SUFFIX;
     92
     93static void     copy(int, const char *, int, const char *, off_t);
     94static int      compare(int, const char *, size_t, int, const char *, size_t);
     95static int      create_newfile(const char *, int, struct stat *);
     96static int      create_tempfile(const char *, char *, size_t);
     97static void     install(const char *, const char *, u_long, u_int);
     98static void     install_dir(char *);
     99static u_long   numeric_id(const char *, const char *);
     100static void     strip(const char *);
     101static int      trymmap(int);
     102static void     usage(void);
    99103
    100104int
    101 main(int argc, char *argv[])
     105kmk_builtin_install(int argc, char *argv[])
    102106{
    103107        struct stat from_sb, to_sb;
     
    128132                        dodir = 1;
    129133                        break;
    130                 case 'f':
     134                    case 'f':
     135#ifdef UF_IMMUTABLE
    131136                        flags = optarg;
    132137                        if (strtofflags(&flags, &fset, NULL))
    133138                                errx(EX_USAGE, "%s: invalid flag", flags);
    134139                        iflags |= SETFLAGS;
     140#endif
    135141                        break;
    136142                case 'g':
     
    140146                        nommap = 1;
    141147                        break;
    142                 case 'm':
     148                case 'm':
     149#ifdef __EMX__
     150                        if (!(set = bsd_setmode(optarg)))
     151#else
    143152                        if (!(set = setmode(optarg)))
     153#endif
    144154                                errx(EX_USAGE, "invalid file mode: %s",
    145155                                     optarg);
     
    230240                if (to_sb.st_dev == from_sb.st_dev &&
    231241                    to_sb.st_ino == from_sb.st_ino)
    232                         errx(EX_USAGE, 
     242                        errx(EX_USAGE,
    233243                            "%s and %s are the same file", *argv, to_name);
    234244        }
     
    306316        tempcopy = safecopy && target;
    307317
    308         if (!devnull && (from_fd = open(from_name, O_RDONLY, 0)) < 0)
     318        if (!devnull && (from_fd = open(from_name, O_RDONLY | O_BINARY, 0)) < 0)
    309319                err(EX_OSERR, "%s", from_name);
    310320
    311321        /* If we don't strip, we can compare first. */
    312322        if (docompare && !dostrip && target) {
    313                 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
     323                if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0)
    314324                        err(EX_OSERR, "%s", to_name);
    315325                if (devnull)
     
    352362                 */
    353363                close(to_fd);
    354                 to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY, 0);
     364                to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY | O_BINARY, 0);
    355365                if (to_fd < 0)
    356366                        err(EX_OSERR, "stripping %s", to_name);
     
    364374
    365375                /* Re-open to_fd using the real target name. */
    366                 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
     376                if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0)
    367377                        err(EX_OSERR, "%s", to_name);
    368378
     
    400410         */
    401411        if (tempcopy && !files_match) {
     412#ifdef UF_IMMUTABLE
    402413                /* Try to turn off the immutable bits. */
    403414                if (to_sb.st_flags & NOCHANGEBITS)
    404415                        (void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
     416#endif
    405417                if (dobackup) {
    406418                        if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s", to_name,
     
    432444                /* Re-open to_fd so we aren't hosed by the rename(2). */
    433445                (void) close(to_fd);
    434                 if ((to_fd = open(to_name, O_RDONLY, 0)) < 0)
     446                if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0)
    435447                        err(EX_OSERR, "%s", to_name);
    436448        }
     
    458470         * chown may lose the setuid bits.
    459471         */
     472#ifdef UF_IMMUTABLE
    460473        if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
    461474            (uid != (uid_t)-1 && uid != to_sb.st_uid) ||
     
    465478                        (void)fchflags(to_fd, to_sb.st_flags & ~NOCHANGEBITS);
    466479        }
     480#endif
    467481
    468482        if ((gid != (gid_t)-1 && gid != to_sb.st_gid) ||
     
    490504         * then warn if the the fs doesn't support it, otherwise fail.
    491505         */
     506#ifdef UF_IMMUTABLE
    492507        if (!devnull && (flags & SETFLAGS ||
    493508            (from_sb.st_flags & ~UF_NODUMP) != to_sb.st_flags) &&
     
    505520                }
    506521        }
     522#endif
    507523
    508524        (void)close(to_fd);
     
    528544
    529545        if (from_len <= MAX_CMP_SIZE) {
     546#if !defined(__EMX__) && !defined(_MSC_VER)
    530547                done_compare = 0;
    531548                if (trymmap(from_fd) && trymmap(to_fd)) {
     
    544561                        done_compare = 1;
    545562                }
     563#endif
    546564        out:
    547565                if (!done_compare) {
     
    612630                 * it might work.
    613631                 */
     632#ifdef UF_IMMUTABLE
    614633                if (sbp->st_flags & NOCHANGEBITS)
    615634                        (void)chflags(path, sbp->st_flags & ~NOCHANGEBITS);
     635#endif
    616636
    617637                if (dobackup) {
     
    632652        }
    633653
    634         newfd = open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
     654        newfd = open(path, O_CREAT | O_RDWR | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
    635655        if (newfd < 0 && saved_errno != 0)
    636656                errno = saved_errno;
     
    663683         */
    664684        done_copy = 0;
     685#if !defined(__EMX__) && !defined(_MSC_VER)
    665686        if (size <= 8 * 1048576 && trymmap(from_fd) &&
    666687            (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
     
    674695                done_copy = 1;
    675696        }
     697#endif
    676698        if (!done_copy) {
    677699                while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
Note: See TracChangeset for help on using the changeset viewer.