VirtualBox

Changeset 370 in kBuild


Ignore:
Timestamp:
Dec 18, 2005 3:48:02 AM (19 years ago)
Author:
bird
Message:

o Ported all kmk builtins to win32.
o Fixed serveral bugs in kmk builtins.
o Probably broke both linux, bsd and OS/2. :-)

Location:
trunk/src/gmake
Files:
6 added
16 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gmake/Makefile.kmk

    r368 r370  
    7171        kmkbuiltin/echo.c \
    7272        kmkbuiltin/mkdir.c \
    73         kmkbuiltin/setmode.c
     73        kmkbuiltin/ln.c \
     74        kmkbuiltin/install.c \
     75        kmkbuiltin/rm.c \
     76        \
     77        kmkbuiltin/err.c \
     78        kmkbuiltin/setmode.c \
     79        kmkbuiltin/strmode.c
    7480ifneq ($(BUILD_TARGET),win32)
    7581kmk_SOURCES += \
    76         kmkbuiltin/rm.c \
    7782        kmkbuiltin/cp.c \
    7883        kmkbuiltin/cp_utils.c \
    79         kmkbuiltin/ln.c \
    80         kmkbuiltin/install.c \
    8184        \
    82         kmkbuiltin/strmode.c \
    8385        kmkbuiltin/strlcpy.c
    8486endif
     87kmk_SOURCES.win32 += kmkbuiltin/mscfakes.c
    8588
    8689
     
    8891# Standalone kmkbuiltin commands.
    8992#
    90 ifneq ($(BUILD_TARGET),win32)
    91 PROGRAMS += kmk_append kmk_cp kmk_echo kmk_mkdir kmk_rm kmk_install kmk_ln
    92 else
    93 PROGRAMS += kmk_append kmk_echo kmk_mkdir
    94 endif
     93PROGRAMS += kmk_append kmk_cp kmk_echo kmk_mkdir kmk_install kmk_ln kmk_rm
    9594
    9695kmk_append_TEMPLATE = BIN
     
    9998kmk_append_SOURCES = \
    10099        kmkbuiltin/append.c \
     100        kmkbuiltin/err.c
    101101
    102102kmk_cp_TEMPLATE = BIN
     
    105105        kmkbuiltin/cp.c \
    106106        kmkbuiltin/cp_utils.c \
    107     kmkbuiltin/strlcpy.c
     107    kmkbuiltin/strlcpy.c \
     108        kmkbuiltin/err.c
     109kmk_cp_INCS.win32 = $(PATH_TARGET) .
     110kmk_cp_DEFS.win32 = HAVE_CONFIG_H
     111kmk_cp_SOURCES.win32 = \
     112        kmkbuiltin/mscfakes.c \
     113        kmkbuiltin/fts.c \
     114        w32/compat/dirent.c \
     115        getopt.c \
     116        getopt1.c
    108117
    109118kmk_echo_TEMPLATE = BIN
     
    111120kmk_echo_SOURCES = \
    112121        kmkbuiltin/echo.c
     122kmk_echo_SOURCES.win32 = \
     123        kmkbuiltin/mscfakes.c \
     124        kmkbuiltin/err.c
    113125
    114126kmk_install_TEMPLATE = BIN
    115127kmk_install_DEFS = kmk_builtin_install=main
    116128kmk_install_SOURCES = \
    117         kmkbuiltin/install.c
     129        kmkbuiltin/install.c \
     130        kmkbuiltin/err.c \
     131        kmkbuiltin/strmode.c \
     132        kmkbuiltin/setmode.c
     133kmk_install_INCS.win32 = $(PATH_TARGET) .
     134kmk_install_DEFS.win32 = HAVE_CONFIG_H
     135kmk_install_SOURCES.win32 = \
     136        kmkbuiltin/mscfakes.c \
     137        getopt.c \
     138        getopt1.c
    118139
    119140kmk_ln_TEMPLATE = BIN
    120141kmk_ln_DEFS = kmk_builtin_ln=main
    121142kmk_ln_SOURCES = \
    122         kmkbuiltin/ln.c
     143        kmkbuiltin/ln.c \
     144        kmkbuiltin/err.c
     145kmk_ln_INCS.win32 = $(PATH_TARGET) .
     146kmk_ln_DEFS.win32 = HAVE_CONFIG_H
     147kmk_ln_SOURCES.win32 = \
     148        kmkbuiltin/mscfakes.c\
     149        getopt.c \
     150        getopt1.c
    123151
    124152kmk_mkdir_TEMPLATE = BIN
     
    126154kmk_mkdir_SOURCES = \
    127155        kmkbuiltin/mkdir.c \
     156        kmkbuiltin/err.c \
    128157        kmkbuiltin/setmode.c
    129158kmk_mkdir_INCS.win32 = $(PATH_TARGET) .
    130159kmk_mkdir_DEFS.win32 = HAVE_CONFIG_H
    131160kmk_mkdir_SOURCES.win32 = \
    132         getopt.c \
    133         getopt1.c \
     161        kmkbuiltin/mscfakes.c \
     162        getopt.c \
     163        getopt1.c
    134164
    135165kmk_rm_TEMPLATE = BIN
     
    137167kmk_rm_SOURCES = \
    138168        kmkbuiltin/rm.c \
    139         kmkbuiltin/setmode.c \
     169        kmkbuiltin/err.c \
    140170        kmkbuiltin/strmode.c
     171kmk_rm_INCS.win32 = $(PATH_TARGET) .
     172kmk_rm_DEFS.win32 = HAVE_CONFIG_H
     173kmk_rm_SOURCES.win32 = \
     174        kmkbuiltin/mscfakes.c\
     175        getopt.c \
     176        getopt1.c
    141177
    142178
  • trunk/src/gmake/kmkbuiltin.c

    r368 r370  
    2929#include <stdio.h>
    3030#include <ctype.h>
     31#include "kmkbuiltin/err.h"
    3132#include "kmkbuiltin.h"
    3233
     
    178179    if (!strcmp(pszCmd, "append"))
    179180        rc = kmk_builtin_append(argc, argv, environ);
    180 #ifndef _MSC_VER
    181     else if (!strcmp(pszCmd, "cp"))
    182         rc = kmk_builtin_cp(argc, argv, environ);
    183     //else if (!strcmp(pszCmd, "chmod"))
    184     //    rc = kmk_builtin_chmod(argc, argv, environ);
    185 #endif
    186181    else if (!strcmp(pszCmd, "echo"))
    187182        rc = kmk_builtin_echo(argc, argv, environ);
     
    192187    else if (!strcmp(pszCmd, "mkdir"))
    193188        rc = kmk_builtin_mkdir(argc, argv, environ);
    194 #ifndef _MSC_VER
    195189    //else if (!strcmp(pszCmd, "mv"))
    196190    //    rc = kmk_builtin_mv(argc, argv, environ);
     
    199193    //else if (!strcmp(pszCmd, "rmdir"))
    200194    //    rc = kmk_builtin_rmdir(argc, argv, environ);
     195    /* obsolete */
     196#ifndef _MSC_VER
     197    else if (!strcmp(pszCmd, "cp"))
     198        rc = kmk_builtin_cp(argc, argv, environ);
    201199#endif
    202200    else
     
    205203        return 1;
    206204    }
     205    g_progname = "kmk";                 /* paranoia, make sure it's not pointing at a freed argv[0]. */
    207206    return rc;
    208207}
  • trunk/src/gmake/kmkbuiltin.h

    r368 r370  
    3030extern int kmk_builtin_append(int argc, char **argv, char **envp);
    3131extern int kmk_builtin_cp(int argc, char **argv, char **envp);
    32 extern int kmk_builtin_chmod(int argc, char **argv, char **envp);
    3332extern int kmk_builtin_echo(int argc, char **argv, char **envp);
    3433extern int kmk_builtin_install(int argc, char **argv, char **envp);
  • trunk/src/gmake/kmkbuiltin/append.c

    r348 r370  
    2727#include <string.h>
    2828#include <stdio.h>
    29 #include <errno.h>
     29#include "err.h"
    3030#include "kmkbuiltin.h"
    3131
     
    3838    FILE *pFile;
    3939
     40    g_progname = argv[0];
     41
    4042    /*
    4143     * Open the output file.
     
    4345    if (argc <= 1)
    4446    {
    45         fprintf(stderr, "append: missing filename!\n");
     47        errx(1, "missing filename!");
    4648        fprintf(stderr, "usage: append file [string ...]\n");
    4749        return 1;
     
    4951    pFile = fopen(argv[1], "a");
    5052    if (!pFile)
    51     {
    52         fprintf(stderr, "append: failed to open '%s': %s\n", argv[1], strerror(errno));
    53         return 1;
    54     }
     53        return err(1, "failed to open '%s'.", argv[1]);
    5554
    5655    /*
     
    7271        ||  ferror(pFile))
    7372    {
    74         fprintf(stderr, "append: error writing to '%s'!\n", argv[1]);
    7573        fclose(pFile);
    76         return 1;
     74        return errx(1, "error writing to '%s'!", argv[1]);
    7775    }
    7876    if (fclose(pFile))
    79     {
    80         fprintf(stderr, "append: failed to fclose '%s': %s\n", argv[1], strerror(errno));
    81         return 1;
    82     }
     77        return err(1, "failed to fclose '%s'!", argv[1]);
    8378    return 0;
    8479}
  • trunk/src/gmake/kmkbuiltin/cp.c

    r299 r370  
    4141static char sccsid[] = "@(#)cp.c        8.2 (Berkeley) 4/1/94";
    4242#endif /* not lint */
    43 #endif
    4443#include <sys/cdefs.h>
    45 //__FBSDID("$FreeBSD: src/bin/cp/cp.c,v 1.50 2004/04/06 20:06:44 markm Exp $");
     44__FBSDID("$FreeBSD: src/bin/cp/cp.c,v 1.50 2004/04/06 20:06:44 markm Exp $");
     45#endif
    4646
    4747/*
     
    6363#include <sys/stat.h>
    6464
    65 #include <err.h>
     65#include "err.h"
    6666#include <errno.h>
     67#ifdef DO_CP_TREE
    6768#include <fts.h>
     69#endif
    6870#include <limits.h>
    6971#include <signal.h>
     
    7173#include <stdlib.h>
    7274#include <string.h>
     75#ifndef _MSC_VER
    7376#include <unistd.h>
     77#else
     78#include "mscfakes.h"
     79#include "ftsfake.h"
     80#endif
    7481
    7582#include "cp_extern.h"
     
    103110
    104111int fflag, iflag, nflag, pflag, vflag;
    105 int Rflag, rflag;
     112static int Rflag, rflag;
    106113volatile sig_atomic_t info;
    107114
     
    122129        char *target;
    123130
    124         argv0 = argv[0];
     131        /* init globals */
     132        cp_argv0 = argv[0];
    125133        to.p_end = to.p_path;
    126134        to.target_end = emptystring;
     
    128136        fflag = iflag = nflag = pflag = vflag = Rflag = rflag = 0;
    129137        info = 0;
     138        /* reset getopt and set progname. */
     139        g_progname = argv[0];
    130140        opterr = 1;
    131141        optarg = NULL;
     
    154164                        break;
    155165                case 'R':
     166#ifdef DO_CP_TREE
    156167                        Rflag = 1;
    157168                        break;
     169#else
     170                        return errx(1, "recursive copy is not implemented!");
     171#endif
    158172                case 'f':
    159173                        fflag = 1;
     
    172186                        break;
    173187                case 'r':
     188#ifdef DO_CP_TREE
    174189                        rflag = 1;
    175190                        break;
     191#else
     192                        return errx(1, "recursive copy is not implemented!");
     193#endif
    176194                case 'v':
    177195                        vflag = 1;
    178196                        break;
    179197                default:
    180                         usage();
    181                         break;
     198                        return usage();
    182199                }
    183200        argc -= optind;
     
    185202
    186203        if (argc < 2)
    187                 usage();
     204                return usage();
    188205
    189206        fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
    190207        if (rflag) {
    191                 if (Rflag) {
    192                         fprintf(stderr,
    193                             "%s: the -R and -r options may not be specified together.\n", argv0);
    194                         return 1;
    195                 }
    196                 if (Hflag || Lflag || Pflag) {
    197                         fprintf(stderr, "%s: the -H, -L, and -P options may not be specified with the -r option.\n", argv0);
    198                         return 1;
    199                 }
     208                if (Rflag)
     209                        return errx(1,
     210                    "the -R and -r options may not be specified together.");
     211                if (Hflag || Lflag || Pflag)
     212                        errx(1,
     213        "the -H, -L, and -P options may not be specified with the -r option.");
     214#ifdef DO_CP_TREE
    200215                fts_options &= ~FTS_PHYSICAL;
    201216                fts_options |= FTS_LOGICAL;
     217#endif
    202218        }
     219#ifdef DO_CP_TREE
    203220        if (Rflag) {
    204221                if (Hflag)
     
    208225                        fts_options |= FTS_LOGICAL;
    209226                }
    210         } else {
     227        } else
     228#endif
     229        {
    211230                fts_options &= ~FTS_PHYSICAL;
    212231                fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
     
    218237        /* Save the target base in "to". */
    219238        target = argv[--argc];
    220         if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path)) {
    221                 fprintf(stderr, "%s: %s: name too long\n", argv0, target);
    222                 return 1;
    223         }
     239        if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
     240                return errx(1, "%s: name too long", target);
    224241        to.p_end = to.p_path + strlen(to.p_path);
    225242        if (to.p_path == to.p_end) {
     
    250267         */
    251268        r = stat(to.p_path, &to_stat);
    252         if (r == -1 && errno != ENOENT) {
    253                 fprintf(stderr, "%s: %s: %s\n", argv0, to.p_path, strerror(errno));
    254         }
     269        if (r == -1 && errno != ENOENT)
     270                return err(1, "%s", to.p_path);
    255271        if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
    256272                /*
    257273                 * Case (1).  Target is not a directory.
    258274                 */
    259                 if (argc > 1) {
    260                         usage();
    261                         return 1;
    262                 }
     275                if (argc > 1)
     276                        return usage();
    263277                /*
    264278                 * Need to detect the case:
     
    283297                if (have_trailing_slash && type == FILE_TO_FILE) {
    284298                        if (r == -1)
    285                                 fprintf(stderr, "%s: directory %s does not exist\n", argv0,
    286                                      to.p_path);
     299                                return errx(1, "directory %s does not exist",
     300                                            to.p_path);
    287301                        else
    288                                 fprintf(stderr, "%s: %s is not a directory\n", argv0, to.p_path);
    289                         return 1;
     302                                return errx(1, "%s is not a directory", to.p_path);
    290303                }
    291304        } else
     
    316329        umask(~mask);
    317330
    318         if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL) {
    319                 fprintf(stderr, "%s: fts_open: %s\n", argv0, strerror(errno));
    320                 return 1;
    321         }
     331        if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
     332                return err(1, "fts_open");
    322333        for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) {
    323334                switch (curr->fts_info) {
     
    325336                case FTS_DNR:
    326337                case FTS_ERR:
    327                         fprintf(stderr, "%s: %s: %s\n",
    328                             argv0, curr->fts_path, strerror(curr->fts_errno));
     338                        warnx("%s: %s",
     339                            curr->fts_path, strerror(curr->fts_errno));
    329340                        badcp = rval = 1;
    330341                        continue;
    331342                case FTS_DC:                    /* Warn, continue. */
    332                         fprintf(stderr, "%s: %s: directory causes a cycle\n", argv0, curr->fts_path);
     343                        warnx("%s: directory causes a cycle", curr->fts_path);
    333344                        badcp = rval = 1;
    334345                        continue;
     
    381392                        *target_mid = 0;
    382393                        if (target_mid - to.p_path + nlen >= PATH_MAX) {
    383                                 fprintf(stderr, "%s: %s%s: name too long (not copied)\n", argv0,
     394                                warnx("%s%s: name too long (not copied)",
    384395                                    to.p_path, p);
    385396                                badcp = rval = 1;
     
    417428                                    ((mode | S_IRWXU) & mask) != (mode & mask))
    418429                                        if (chmod(to.p_path, mode & mask) != 0){
    419                                                 fprintf(stderr, "%s: chmod: %s: %s\n", argv0, to.p_path, strerror(errno));
     430                                                warn("chmod: %s", to.p_path);
    420431                                                rval = 1;
    421432                                        }
     
    429440                else {
    430441                        if (to_stat.st_dev == curr->fts_statp->st_dev &&
    431                             to_stat.st_ino == curr->fts_statp->st_ino) {
    432                                 fprintf(stderr, "%s: %s and %s are identical (not copied).\n",
    433                                     argv0, to.p_path, curr->fts_path);
     442                            to_stat.st_dev != 0 &&
     443                            to_stat.st_ino == curr->fts_statp->st_ino &&
     444                            to_stat.st_ino != 0) {
     445                                warnx("%s and %s are identical (not copied).",
     446                                    to.p_path, curr->fts_path);
    434447                                badcp = rval = 1;
    435448                                if (S_ISDIR(curr->fts_statp->st_mode))
     
    439452                        if (!S_ISDIR(curr->fts_statp->st_mode) &&
    440453                            S_ISDIR(to_stat.st_mode)) {
    441                                 fprintf(stderr,
    442                                     "%s: cannot overwrite directory %s with "
    443                                     "non-directory %s\n",
    444                                     argv0, to.p_path, curr->fts_path);
     454                                warnx("cannot overwrite directory %s with "
     455                                    "non-directory %s",
     456                                    to.p_path, curr->fts_path);
    445457                                badcp = rval = 1;
    446458                                continue;
     
    450462
    451463                switch (curr->fts_statp->st_mode & S_IFMT) {
     464#ifdef S_IFLNK
    452465                case S_IFLNK:
    453466                        /* Catch special case of a non-dangling symlink */
     
    462475                        }
    463476                        break;
     477#endif
    464478                case S_IFDIR:
    465479                        if (!Rflag && !rflag) {
    466                                 fprintf(stderr, "%s: %s is a directory (not copied).\n",
    467                                     argv0, curr->fts_path);
     480                                warnx("%s is a directory (not copied).",
     481                                    curr->fts_path);
    468482                                (void)fts_set(ftsp, curr, FTS_SKIP);
    469483                                badcp = rval = 1;
     
    480494                        if (dne) {
    481495                                if (mkdir(to.p_path,
    482                                     curr->fts_statp->st_mode | S_IRWXU) < 0) {
    483                                         fprintf(stderr, "%s: %s: %s\n", argv0, to.p_path, strerror(errno));
    484                                         return 1;
    485                                 }
     496                                    curr->fts_statp->st_mode | S_IRWXU) < 0)
     497                                        return err(1, "%s", to.p_path);
    486498                        } else if (!S_ISDIR(to_stat.st_mode)) {
    487499                                errno = ENOTDIR;
    488                                 fprintf(stderr, "%s: %s: %s\n", argv0, to.p_path, strerror(errno));
     500                                return err(1, "%s", to.p_path);
    489501                        }
    490502                        /*
     
    495507                        curr->fts_number = pflag || dne;
    496508                        break;
     509#ifdef S_IFBLK
    497510                case S_IFBLK:
     511#endif
    498512                case S_IFCHR:
    499513                        if (Rflag) {
     
    505519                        }
    506520                        break;
     521#ifdef S_IFIFO
    507522                case S_IFIFO:
     523#endif
    508524                        if (Rflag) {
    509525                                if (copy_fifo(curr->fts_statp, !dne))
     
    522538                        (void)printf("%s -> %s\n", curr->fts_path, to.p_path);
    523539        }
    524         if (errno) {
    525                 fprintf(stderr, "%s: fts_read: %s\n", argv0, strerror(errno));
    526         }
     540        if (errno)
     541                return err(1, "fts_read");
    527542        return (rval);
    528543}
  • trunk/src/gmake/kmkbuiltin/cp_extern.h

    r229 r370  
    5353extern volatile sig_atomic_t info;
    5454
    55 __BEGIN_DECLS
    5655int     copy_fifo(struct stat *, int);
    5756int     copy_file(const FTSENT *, int);
     
    5958int     copy_special(struct stat *, int);
    6059int     setfile(struct stat *, int);
    61 void    usage(void);
    62 __END_DECLS
     60int     usage(void);
  • trunk/src/gmake/kmkbuiltin/cp_utils.c

    r347 r370  
    3131#if 0
    3232static char sccsid[] = "@(#)utils.c     8.3 (Berkeley) 4/1/94";
     33#include <sys/cdefs.h>
     34__FBSDID("$FreeBSD: src/bin/cp/utils.c,v 1.43 2004/04/06 20:06:44 markm Exp $");
    3335#endif
    3436#endif /* not lint */
    35 #include <sys/cdefs.h>
    36 //__FBSDID("$FreeBSD: src/bin/cp/utils.c,v 1.43 2004/04/06 20:06:44 markm Exp $");
    37 
     37
     38#ifndef _MSC_VER
    3839#include <sys/param.h>
     40#endif
    3941#include <sys/stat.h>
    4042#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
     
    4244#endif
    4345
    44 #include <err.h>
     46#include "err.h"
    4547#include <errno.h>
    4648#include <fcntl.h>
     49#ifndef _MSC_VER
    4750#include <fts.h>
     51#endif
    4852#include <limits.h>
    4953#include <stdio.h>
    5054#include <stdlib.h>
     55#include <signal.h>
     56#ifndef _MSC_VER
    5157#include <sysexits.h>
    5258#include <unistd.h>
    53 #include <signal.h>
     59#else
     60#include "mscfakes.h"
     61#include "ftsfake.h"
     62#endif
    5463
    5564#include "cp_extern.h"
     
    5766
    5867#ifndef MAXBSIZE
    59 #define MAXBSIZE (128*1024)
     68# define MAXBSIZE 16384
    6069#endif
    6170#ifndef O_BINARY
    6271# define O_BINARY 0
    6372#endif
     73
     74#ifndef S_ISVTX
     75# define S_ISVTX 0
     76#endif
    6477
    6578int
     
    7891
    7992        if ((from_fd = open(entp->fts_path, O_RDONLY | O_BINARY, 0)) == -1) {
    80                 fprintf(stderr, "%s: %s: %s\n", argv0, entp->fts_path, strerror(errno));
     93                warn("%s", entp->fts_path);
    8194                return (1);
    8295        }
     
    125138
    126139        if (to_fd == -1) {
    127                 fprintf(stderr, "%s: %s: %s\n", argv0, to.p_path, strerror(errno));
     140                warn("%s", to.p_path);
    128141                (void)close(from_fd);
    129142                return (1);
     
    142155                if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
    143156                    MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
    144                         fprintf(stderr, "%s: %s: %s\n", argv0, entp->fts_path, strerror(errno));
     157                        warn("%s", entp->fts_path);
    145158                        rval = 1;
    146159                } else {
     
    162175                        }
    163176                        if (wcount != (ssize_t)wresid) {
    164                                 fprintf(stderr, "%s: %s: %s\n", argv0, to.p_path, strerror(errno));
     177                                warn("%s", to.p_path);
    165178                                rval = 1;
    166179                        }
    167180                        /* Some systems don't unmap on close(2). */
    168181                        if (munmap(p, fs->st_size) < 0) {
    169                                 fprintf(stderr, "%s: %s: %s\n", entp->fts_path, strerror(errno));
     182                                warn("%s", entp->fts_path);
    170183                                rval = 1;
    171184                        }
     
    192205                        }
    193206                        if (wcount != (ssize_t)wresid) {
    194                                 fprintf(stderr, "%s: %s: %s\n", argv0, to.p_path, strerror(errno));
     207                                warn("%s", to.p_path);
    195208                                rval = 1;
    196209                                break;
     
    198211                }
    199212                if (rcount < 0) {
    200                         fprintf(stderr, "%s: %s: %s\n", argv0, entp->fts_path, strerror(errno));
     213                        warn("%s", entp->fts_path);
    201214                        rval = 1;
    202215                }
     
    214227        (void)close(from_fd);
    215228        if (close(to_fd)) {
    216                 fprintf(stderr, "%s: %s: %s\n", argv0, to.p_path, strerror(errno));
     229                warn("%s", to.p_path);
    217230                rval = 1;
    218231        }
     
    227240
    228241        if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
    229                 fprintf(stderr, "%s: readlink: %s: %s\n", argv0, p->fts_path, strerror(errno));
     242                warn("readlink: %s", p->fts_path);
    230243                return (1);
    231244        }
    232245        llink[len] = '\0';
    233246        if (exists && unlink(to.p_path)) {
    234                 fprintf(stderr, "%s: unlink: %s: %s\n", argv0, to.p_path, strerror(errno));
     247                warn("unlink: %s", to.p_path);
    235248                return (1);
    236249        }
    237250        if (symlink(llink, to.p_path)) {
    238                 fprintf(stderr, "%s: symlink: %s: %s\n", argv0, llink, strerror(errno));
     251                warn("symlink: %s", llink);
    239252                return (1);
    240253        }
     
    246259{
    247260        if (exists && unlink(to.p_path)) {
    248                 fprintf(stderr, "%s: unlink: %s: %s\n", argv0, to.p_path, strerror(errno));
     261                warn("unlink: %s", to.p_path);
    249262                return (1);
    250263        }
    251264        if (mkfifo(to.p_path, from_stat->st_mode)) {
    252                 fprintf(stderr, "%s: mkfifo: %s\n", argv0, to.p_path, strerror(errno));
     265                warn("mkfifo: %s", to.p_path);
    253266                return (1);
    254267        }
     
    260273{
    261274        if (exists && unlink(to.p_path)) {
    262                 fprintf(stderr, "%s: unlink: %s: %s\n", argv0, to.p_path, strerror(errno));
     275                warn("unlink: %s", to.p_path);
    263276                return (1);
    264277        }
    265278        if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
    266                 fprintf(stderr, "%s: mknod: %s: %s\n", argv0, to.p_path, strerror(errno));
     279                warn("mknod: %s", to.p_path);
    267280                return (1);
    268281        }
     
    292305#endif
    293306        if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
    294                 fprintf(stderr, "%s: %sutimes: %s: %s\n", argv0, islink ? "l" : "", to.p_path, strerror(errno));
     307                warn("%sutimes: %s", islink ? "l" : "", to.p_path);
    295308                rval = 1;
    296309        }
     
    314327                    chown(to.p_path, fs->st_uid, fs->st_gid))) {
    315328                        if (errno != EPERM) {
    316                                 fprintf(stderr, "%s: chown: %s: %s\n", argv0, to.p_path, strerror(errno));
     329                                warn("chown: %s", to.p_path);
    317330                                rval = 1;
    318331                        }
     
    324337                    (islink ? lchmod(to.p_path, fs->st_mode) :
    325338                    chmod(to.p_path, fs->st_mode))) {
    326                         fprintf(stderr, "%s: chmod: %s: %s\n", to.p_path, strerror(errno));
     339                        warn("chmod: %s", to.p_path);
    327340                        rval = 1;
    328341                }
     
    334347                    (islink ? (errno = ENOSYS) :
    335348                    chflags(to.p_path, fs->st_flags))) {
    336                         fprintf(stderr, "%s: chflags: %s: %s", argv0, to.p_path, strerror(errno));
     349                        warn("chflags: %s", to.p_path);
    337350                        rval = 1;
    338351                }
     
    342355}
    343356
    344 void
     357int
    345358usage(void)
    346359{
     
    349362"usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] src target",
    350363"       cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] src1 ... srcN directory");
    351         exit(EX_USAGE);
    352 }
     364        return EX_USAGE;
     365}
  • trunk/src/gmake/kmkbuiltin/echo.c

    r360 r370  
    3838static char sccsid[] = "@(#)echo.c      8.1 (Berkeley) 5/31/93";
    3939#endif /* not lint */
     40#include <sys/cdefs.h>
     41//__FBSDID("$FreeBSD: src/bin/echo/echo.c,v 1.17 2004/04/06 20:06:46 markm Exp $");
    4042#endif
    41 #ifndef _MSC_VER
    42 #include <sys/cdefs.h>
    43 #endif
    44 //__FBSDID("$FreeBSD: src/bin/echo/echo.c,v 1.17 2004/04/06 20:06:46 markm Exp $");
    4543
    4644#include <sys/types.h>
     
    5755#ifndef _MSC_VER
    5856#include <unistd.h>
     57#else
     58#include "mscfakes.h"
    5959#endif
    6060
     
    6363#endif
    6464
    65 #ifdef _MSC_VER
    66 #include <io.h>
    67 
    68 struct iovec {
    69     char *iov_base;
    70     size_t iov_len;
    71 };
    72 
    73 int writev(int fd, const struct iovec *vector, int count)
    74 {
    75     int size = 0;
    76     int i;
    77     for (i = 0; i < count; i++)
    78     {
    79         int cb = write(fd, vector[i].iov_base, vector[i].iov_len);
    80         if (cb < 0)
    81             return -1;
    82         size += cb;
    83     }
    84     return size;
    85 }
    86 
    87 #define STDERR_FILENO 2
    88 #define STDOUT_FILENO 1
    89 #endif /* _MSC_VER */
    9065
    9166/*
     
    127102        if ((iovfree = vp = iov = malloc((veclen + 1) * sizeof(struct iovec))) == NULL) {
    128103                errexit(progname, "malloc");
    129                 return 1;
     104                exit(1);
    130105        }
    131106
  • trunk/src/gmake/kmkbuiltin/install.c

    r368 r370  
    4242static char sccsid[] = "@(#)xinstall.c  8.1 (Berkeley) 7/21/93";
    4343#endif /* not lint */
    44 #endif
    4544
    4645#include <sys/cdefs.h>
    4746__FBSDID("$FreeBSD: src/usr.bin/xinstall/xinstall.c,v 1.66 2005/01/25 14:34:57 ssouhlal Exp $");
    48 
     47#endif
     48
     49#ifndef _MSC_VER
    4950#include <sys/param.h>
     51#ifdef USE_MMAP
    5052#include <sys/mman.h>
     53#endif
    5154#include <sys/mount.h>
     55#include <sys/wait.h>
     56#include <sys/time.h>
     57#else
     58#include <process.h>
     59#endif
    5260#include <sys/stat.h>
    53 #include <sys/time.h>
    54 #include <sys/wait.h>
    5561
    5662#include <ctype.h>
    57 #include <err.h>
     63#include "err.h"
    5864#include <errno.h>
    5965#include <fcntl.h>
     66#ifndef _MSC_VER
    6067#include <grp.h>
    6168#include <paths.h>
    6269#include <pwd.h>
     70#endif
    6371#include <stdio.h>
    6472#include <stdlib.h>
    6573#include <string.h>
     74#ifndef _MSC_VER
    6675#include <sysexits.h>
    6776#include <unistd.h>
     77#else
     78#include "mscfakes.h"
     79#endif
     80
     81extern void * setmode(const char *p);
     82extern mode_t getmode(const void *bbox, mode_t omode);
     83
     84#ifndef __unused
     85# define __unused
     86#endif
     87
     88#ifndef MAXBSIZE
     89# define MAXBSIZE 16384
     90#endif
    6891
    6992/* Bootstrap aid - this doesn't exist in most older releases */
     
    83106#endif
    84107
    85 static struct passwd *pp;
    86 static struct group *gp;
    87108static gid_t gid;
    88109static uid_t uid;
     
    91112static const char *suffix = BACKUP_SUFFIX;
    92113
    93 static void     copy(int, const char *, int, const char *, off_t);
     114static int      copy(int, const char *, int, const char *, off_t);
    94115static int      compare(int, const char *, size_t, int, const char *, size_t);
    95116static int      create_newfile(const char *, int, struct stat *);
    96117static int      create_tempfile(const char *, char *, size_t);
    97 static void     install(const char *, const char *, u_long, u_int);
    98 static void     install_dir(char *);
     118static int      install(const char *, const char *, u_long, u_int);
     119static int      install_dir(char *);
    99120static u_long   numeric_id(const char *, const char *);
    100 static void     strip(const char *);
     121static int      strip(const char *);
     122#ifdef USE_MMAP
    101123static int      trymmap(int);
    102 static void     usage(void);
     124#endif
     125static int      usage(void);
    103126
    104127int
     
    107130        struct stat from_sb, to_sb;
    108131        mode_t *set;
    109         u_long fset;
     132        u_long fset = 0;
    110133        int ch, no_target;
    111134        u_int iflags;
     
    132155                        dodir = 1;
    133156                        break;
    134                     case 'f':
     157                case 'f':
    135158#ifdef UF_IMMUTABLE
    136159                        flags = optarg;
    137160                        if (strtofflags(&flags, &fset, NULL))
    138                                 errx(EX_USAGE, "%s: invalid flag", flags);
     161                                return errx(EX_USAGE, "%s: invalid flag", flags);
    139162                        iflags |= SETFLAGS;
     163#else   
     164                        (void)flags;
    140165#endif
    141166                        break;
     
    152177                        if (!(set = setmode(optarg)))
    153178#endif
    154                                 errx(EX_USAGE, "invalid file mode: %s",
    155                                      optarg);
     179                                return errx(EX_USAGE, "invalid file mode: %s",
     180                                            optarg);
    156181                        mode = getmode(set, 0);
    157182                        free(set);
     
    174199                case '?':
    175200                default:
    176                         usage();
     201                        return usage();
    177202                }
    178203        argc -= optind;
     
    182207        if (dostrip && dodir) {
    183208                warnx("-d and -s may not be specified together");
    184                 usage();
     209                return usage();
    185210        }
    186211
    187212        /* must have at least two arguments, except when creating directories */
    188213        if (argc == 0 || (argc == 1 && !dodir))
    189                 usage();
     214                return usage();
    190215
    191216        /* need to make a temp copy so we can compare stripped version */
     
    195220        /* get group and owner id's */
    196221        if (group != NULL) {
     222#ifndef _MSC_VER
     223                struct group *gp;
    197224                if ((gp = getgrnam(group)) != NULL)
    198225                        gid = gp->gr_gid;
    199226                else
     227#endif
     228                {
    200229                        gid = (gid_t)numeric_id(group, "group");
     230                        if (gid == (gid_t)-1)
     231                                return 1;
     232                }
    201233        } else
    202234                gid = (gid_t)-1;
    203235
    204236        if (owner != NULL) {
     237#ifndef _MSC_VER
     238                struct passwd *pp;
    205239                if ((pp = getpwnam(owner)) != NULL)
    206240                        uid = pp->pw_uid;
    207241                else
     242#endif
     243                {
    208244                        uid = (uid_t)numeric_id(owner, "user");
     245                        if (uid == (uid_t)-1)
     246                                return 1;
     247                }
    209248        } else
    210249                uid = (uid_t)-1;
    211250
    212251        if (dodir) {
    213                 for (; *argv != NULL; ++argv)
    214                         install_dir(*argv);
    215                 exit(EX_OK);
     252                for (; *argv != NULL; ++argv) {
     253                        int rc = install_dir(*argv);
     254                        if (rc)
     255                                return rc;
     256                }
     257                return EX_OK;
    216258                /* NOTREACHED */
    217259        }
     
    219261        no_target = stat(to_name = argv[argc - 1], &to_sb);
    220262        if (!no_target && S_ISDIR(to_sb.st_mode)) {
    221                 for (; *argv != to_name; ++argv)
    222                         install(*argv, to_name, fset, iflags | DIRECTORY);
    223                 exit(EX_OK);
    224                 /* NOTREACHED */
     263                for (; *argv != to_name; ++argv) {
     264                        int rc = install(*argv, to_name, fset, iflags | DIRECTORY);
     265                        if (rc)
     266                                return rc;
     267                }
     268                return EX_OK;
    225269        }
    226270
     
    228272        if (argc != 2) {
    229273                warnx("wrong number or types of arguments");
    230                 usage();
     274                return usage();
    231275        }
    232276
    233277        if (!no_target) {
    234278                if (stat(*argv, &from_sb))
    235                         err(EX_OSERR, "%s", *argv);
     279                        return err(EX_OSERR, "%s", *argv);
    236280                if (!S_ISREG(to_sb.st_mode)) {
    237281                        errno = EFTYPE;
    238                         err(EX_OSERR, "%s", to_name);
     282                        return err(EX_OSERR, "%s", to_name);
    239283                }
    240284                if (to_sb.st_dev == from_sb.st_dev &&
    241                     to_sb.st_ino == from_sb.st_ino)
    242                         errx(EX_USAGE,
    243                             "%s and %s are the same file", *argv, to_name);
    244         }
    245         install(*argv, to_name, fset, iflags);
    246         exit(EX_OK);
    247         /* NOTREACHED */
    248 }
    249 
    250 u_long
     285                    to_sb.st_dev != 0 &&
     286                    to_sb.st_ino == from_sb.st_ino &&
     287                    to_sb.st_ino != 0)
     288                        return errx(EX_USAGE,
     289                                    "%s and %s are the same file", *argv, to_name);
     290        }
     291        return install(*argv, to_name, fset, iflags);
     292}
     293
     294static u_long
    251295numeric_id(const char *name, const char *type)
    252296{
     
    261305        val = strtoul(name, &ep, 10);
    262306        if (errno)
    263                 err(EX_NOUSER, "%s", name);
     307                return err(-1, "%s", name);
    264308        if (*ep != '\0')
    265                 errx(EX_NOUSER, "unknown %s %s", type, name);
     309                return errx(-1, "unknown %s %s", type, name);
    266310        return (val);
    267311}
     
    271315 *      build a path name and install the file
    272316 */
    273 void
     317static int
    274318install(const char *from_name, const char *to_name, u_long fset, u_int flags)
    275319{
     
    279323        int tempcopy, temp_fd, to_fd;
    280324        char backup[MAXPATHLEN], *p, pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN];
     325        int rc = EX_OK;
    281326
    282327        files_match = 0;
    283328        from_fd = -1;
    284329        to_fd = -1;
     330        temp_fd = -1;
    285331
    286332        /* If try to install NULL file to a directory, fails. */
    287         if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
     333        if (flags & DIRECTORY
     334#if defined(__EMX__) || defined(_MSC_VER)
     335            || (   stricmp(from_name, _PATH_DEVNULL)
     336                && stricmp(from_name, "nul")
     337#ifdef __EMX__
     338                && stricmp(from_name, "/dev/nul")
     339#endif
     340               )
     341#else
     342            || strcmp(from_name, _PATH_DEVNULL)
     343#endif
     344            ) {
    288345                if (stat(from_name, &from_sb))
    289                         err(EX_OSERR, "%s", from_name);
     346                        return err(EX_OSERR, "%s", from_name);
    290347                if (!S_ISREG(from_sb.st_mode)) {
    291348                        errno = EFTYPE;
    292                         err(EX_OSERR, "%s", from_name);
     349                        return err(EX_OSERR, "%s", from_name);
    293350                }
    294351                /* Build the target path. */
     
    310367                errno = EFTYPE;
    311368                warn("%s", to_name);
    312                 return;
     369                return EX_OK;
    313370        }
    314371
     
    317374
    318375        if (!devnull && (from_fd = open(from_name, O_RDONLY | O_BINARY, 0)) < 0)
    319                 err(EX_OSERR, "%s", from_name);
     376                return err(EX_OSERR, "%s", from_name);
    320377
    321378        /* If we don't strip, we can compare first. */
    322379        if (docompare && !dostrip && target) {
    323                 if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0)
    324                         err(EX_OSERR, "%s", to_name);
     380                if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0) {
     381                        rc = err(EX_OSERR, "%s", to_name);
     382                        goto l_done;
     383                }
    325384                if (devnull)
    326385                        files_match = to_sb.st_size == 0;
     
    331390
    332391                /* Close "to" file unless we match. */
    333                 if (!files_match)
     392                if (!files_match) {
    334393                        (void)close(to_fd);
     394                        to_fd = -1;
     395                }
    335396        }
    336397
     
    339400                        to_fd = create_tempfile(to_name, tempfile,
    340401                            sizeof(tempfile));
    341                         if (to_fd < 0)
    342                                 err(EX_OSERR, "%s", tempfile);
     402                        if (to_fd < 0) {
     403                                rc = err(EX_OSERR, "%s", tempfile);
     404                                goto l_done;
     405                        }
    343406                } else {
    344407                        if ((to_fd = create_newfile(to_name, target,
    345                             &to_sb)) < 0)
    346                                 err(EX_OSERR, "%s", to_name);
     408                            &to_sb)) < 0) {
     409                                rc = err(EX_OSERR, "%s", to_name);
     410                                goto l_done;
     411                        }
    347412                        if (verbose)
    348413                                (void)printf("install: %s -> %s\n",
    349414                                    from_name, to_name);
    350415                }
    351                 if (!devnull)
    352                         copy(from_fd, from_name, to_fd,
    353                              tempcopy ? tempfile : to_name, from_sb.st_size);
     416                if (!devnull) {
     417                        rc = copy(from_fd, from_name, to_fd,
     418                                  tempcopy ? tempfile : to_name, from_sb.st_size);
     419                        if (rc)
     420                                goto l_done;
     421                }
    354422        }
    355423
    356424        if (dostrip) {
    357                 strip(tempcopy ? tempfile : to_name);
     425#if defined(__EMX__) || defined(_MSC_VER)
     426                /* close before we strip. */
     427                close(to_fd);
     428                to_fd = -1;
     429#endif
     430                rc = strip(tempcopy ? tempfile : to_name);
     431                if (rc)
     432                        goto l_done;
    358433
    359434                /*
     
    361436                 * that does not work in-place -- like GNU binutils strip.
    362437                 */
     438#if !defined(__EMX__) && !defined(_MSC_VER)
    363439                close(to_fd);
     440#endif
    364441                to_fd = open(tempcopy ? tempfile : to_name, O_RDONLY | O_BINARY, 0);
    365                 if (to_fd < 0)
    366                         err(EX_OSERR, "stripping %s", to_name);
     442                if (to_fd < 0) {
     443                        rc = err(EX_OSERR, "stripping %s", to_name);
     444                        goto l_done;
     445                }
    367446        }
    368447
     
    374453
    375454                /* Re-open to_fd using the real target name. */
    376                 if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0)
    377                         err(EX_OSERR, "%s", to_name);
     455                if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0) {
     456                        rc = err(EX_OSERR, "%s", to_name);
     457                        goto l_done;
     458                }
    378459
    379460                if (fstat(temp_fd, &temp_sb)) {
     
    381462                        (void)unlink(tempfile);
    382463                        errno = serrno;
    383                         err(EX_OSERR, "%s", tempfile);
     464                        rc = err(EX_OSERR, "%s", tempfile);
     465                        goto l_done;
    384466                }
    385467
     
    391473                         * Need to preserve target file times, though.
    392474                         */
     475#if !defined(_MSC_VER) && !defined(__EMX__)
    393476                        if (to_sb.st_nlink != 1) {
    394477                                tvb[0].tv_sec = to_sb.st_atime;
     
    397480                                tvb[1].tv_usec = 0;
    398481                                (void)utimes(tempfile, tvb);
    399                         } else {
     482                        } else
     483#endif
     484                        {
     485
    400486                                files_match = 1;
    401487                                (void)unlink(tempfile);
    402488                        }
    403489                        (void) close(temp_fd);
     490                        temp_fd = -1;
    404491                }
    405492        }
     
    419506                            suffix) != strlen(to_name) + strlen(suffix)) {
    420507                                unlink(tempfile);
    421                                 errx(EX_OSERR, "%s: backup filename too long",
    422                                     to_name);
     508                                rc = errx(EX_OSERR, "%s: backup filename too long",
     509                                          to_name);
     510                                goto l_done;
    423511                        }
    424512                        if (verbose)
     
    428516                                unlink(tempfile);
    429517                                errno = serrno;
    430                                 err(EX_OSERR, "rename: %s to %s", to_name,
    431                                      backup);
     518                                rc = err(EX_OSERR, "rename: %s to %s", to_name,
     519                                         backup);
     520                                goto l_done;
    432521                        }
    433522                }
     
    438527                        unlink(tempfile);
    439528                        errno = serrno;
    440                         err(EX_OSERR, "rename: %s to %s",
    441                             tempfile, to_name);
     529                        rc = err(EX_OSERR, "rename: %s to %s",
     530                                 tempfile, to_name);
     531                        goto l_done;
    442532                }
    443533
    444534                /* Re-open to_fd so we aren't hosed by the rename(2). */
    445535                (void) close(to_fd);
    446                 if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0)
    447                         err(EX_OSERR, "%s", to_name);
     536                if ((to_fd = open(to_name, O_RDONLY | O_BINARY, 0)) < 0) {
     537                        rc = err(EX_OSERR, "%s", to_name);
     538                        goto l_done;
     539                }
    448540        }
    449541
     
    463555                (void)unlink(to_name);
    464556                errno = serrno;
    465                 err(EX_OSERR, "%s", to_name);
     557                rc = err(EX_OSERR, "%s", to_name);
     558                goto l_done;
    466559        }
    467560
     
    486579                        (void)unlink(to_name);
    487580                        errno = serrno;
    488                         err(EX_OSERR,"%s: chown/chgrp", to_name);
     581                        rc = err(EX_OSERR,"%s: chown/chgrp", to_name);
     582                        goto l_done;
    489583                }
    490584
     
    494588                        (void)unlink(to_name);
    495589                        errno = serrno;
    496                         err(EX_OSERR, "%s: chmod", to_name);
     590                        rc = err(EX_OSERR, "%s: chmod", to_name);
     591                        goto l_done;
    497592                }
    498593
     
    516611                                (void)unlink(to_name);
    517612                                errno = serrno;
    518                                 err(EX_OSERR, "%s: chflags", to_name);
    519                         }
    520                 }
    521         }
    522 #endif
    523 
    524         (void)close(to_fd);
     613                                rc = err(EX_OSERR, "%s: chflags", to_name);
     614                                goto l_done;
     615                        }
     616                }
     617        }
     618#endif
     619
     620l_done:
     621        if (to_fd >= 0)
     622                (void)close(to_fd);
     623        if (temp_fd >= 0)
     624                (void)close(temp_fd);
    525625        if (!devnull)
    526626                (void)close(from_fd);
     627        return rc;
    527628}
    528629
     
    531632 *      compare two files; non-zero means files differ
    532633 */
    533 int
     634static int
    534635compare(int from_fd, const char *from_name __unused, size_t from_len,
    535636        int to_fd, const char *to_name __unused, size_t to_len)
     
    544645
    545646        if (from_len <= MAX_CMP_SIZE) {
    546 #if !defined(__EMX__) && !defined(_MSC_VER)
     647#ifdef USE_MMAP
    547648                done_compare = 0;
    548649                if (trymmap(from_fd) && trymmap(to_fd)) {
     
    561662                        done_compare = 1;
    562663                }
    563 #endif
    564664        out:
     665#else
     666        (void)p; (void)q;
     667        done_compare = 0;
     668#endif
    565669                if (!done_compare) {
    566670                        char buf1[MAXBSIZE];
     
    637741                if (dobackup) {
    638742                        if ((size_t)snprintf(backup, MAXPATHLEN, "%s%s",
    639                             path, suffix) != strlen(path) + strlen(suffix))
     743                            path, suffix) != strlen(path) + strlen(suffix)) {
    640744                                errx(EX_OSERR, "%s: backup filename too long",
    641                                     path);
     745                                     path);
     746                                errno = ENAMETOOLONG;
     747                                return -1;
     748                        }
    642749                        (void)snprintf(backup, MAXPATHLEN, "%s%s",
    643750                            path, suffix);
     
    645752                                (void)printf("install: %s -> %s\n",
    646753                                    path, backup);
    647                         if (rename(path, backup) < 0)
     754                        if (rename(path, backup) < 0) {
    648755                                err(EX_OSERR, "rename: %s to %s", path, backup);
     756                                return -1;
     757                        }
    649758                } else
    650759                        if (unlink(path) < 0)
     
    662771 *      copy from one file to another
    663772 */
    664 void
     773static int
    665774copy(int from_fd, const char *from_name, int to_fd, const char *to_name,
    666775    off_t size)
     
    673782        /* Rewind file descriptors. */
    674783        if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1)
    675                 err(EX_OSERR, "lseek: %s", from_name);
     784                return err(EX_OSERR, "lseek: %s", from_name);
    676785        if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1)
    677                 err(EX_OSERR, "lseek: %s", to_name);
     786                return err(EX_OSERR, "lseek: %s", to_name);
    678787
    679788        /*
     
    683792         */
    684793        done_copy = 0;
    685 #if !defined(__EMX__) && !defined(_MSC_VER)
     794#ifdef USE_MMAP
    686795        if (size <= 8 * 1048576 && trymmap(from_fd) &&
    687796            (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED,
     
    695804                done_copy = 1;
    696805        }
     806#else
     807        (void)p;
    697808#endif
    698809        if (!done_copy) {
     
    702813                                (void)unlink(to_name);
    703814                                errno = nw > 0 ? EIO : serrno;
    704                                 err(EX_OSERR, "%s", to_name);
     815                                return err(EX_OSERR, "%s", to_name);
    705816                        }
    706817                if (nr != 0) {
     
    708819                        (void)unlink(to_name);
    709820                        errno = serrno;
    710                         err(EX_OSERR, "%s", from_name);
    711                 }
    712         }
     821                        return err(EX_OSERR, "%s", from_name);
     822                }
     823        }
     824        return EX_OK;
    713825}
    714826
     
    717829 *      use strip(1) to strip the target file
    718830 */
    719 void
     831static int
    720832strip(const char *to_name)
    721833{
     834#if defined(__EMX__) || defined(_MSC_VER)
     835        const char *stripbin = getenv("STRIPBIN");
     836        if (stripbin == NULL)
     837                stripbin = "strip";
     838        return spawnlp(P_WAIT, stripbin, stripbin, to_name, NULL);
     839#else
    722840        const char *stripbin;
    723841        int serrno, status;
     
    743861                }
    744862        }
     863#endif
    745864}
    746865
     
    749868 *      build directory heirarchy
    750869 */
    751 void
     870static int
    752871install_dir(char *path)
    753872{
     
    762881                        if (stat(path, &sb)) {
    763882                                if (errno != ENOENT || mkdir(path, 0755) < 0) {
    764                                         err(EX_OSERR, "mkdir %s", path);
     883                                        return err(EX_OSERR, "mkdir %s", path);
    765884                                        /* NOTREACHED */
    766885                                } else if (verbose)
     
    768887                                                     path);
    769888                        } else if (!S_ISDIR(sb.st_mode))
    770                                 errx(EX_OSERR, "%s exists but is not a directory", path);
     889                                return errx(EX_OSERR, "%s exists but is not a directory", path);
    771890                        if (!(*p = ch))
    772891                                break;
     
    777896        if (chmod(path, mode))
    778897                warn("chmod %o %s", mode, path);
     898        return EX_OK;
    779899}
    780900
     
    783903 *      print a usage message and die
    784904 */
    785 void
     905static int
    786906usage()
    787907{
     
    792912"               [-o owner] file1 ... fileN directory\n"
    793913"       install -d [-v] [-g group] [-m mode] [-o owner] directory ...\n");
    794         exit(EX_USAGE);
    795         /* NOTREACHED */
    796 }
    797 
     914        return EX_USAGE;
     915}
     916
     917#ifdef USE_MMAP
    798918/*
    799919 * trymmap --
    800920 *      return true (1) if mmap should be tried, false (0) if not.
    801921 */
    802 int
     922static int
    803923trymmap(int fd)
    804924{
     
    818938        return (0);
    819939}
     940#endif
  • trunk/src/gmake/kmkbuiltin/ln.c

    r368 r370  
    3838static char sccsid[] = "@(#)ln.c        8.2 (Berkeley) 3/31/94";
    3939#endif /* not lint */
    40 #endif
    4140#include <sys/cdefs.h>
    4241__FBSDID("$FreeBSD: src/bin/ln/ln.c,v 1.33 2005/02/09 17:37:37 ru Exp $");
    43 
     42#endif /* no $id */
     43
     44#ifndef _MSC_VER
    4445#include <sys/param.h>
     46#endif
    4547#include <sys/stat.h>
    4648
    47 #include <err.h>
     49#include "err.h"
    4850#include <errno.h>
    4951#include <limits.h>
     
    5153#include <stdlib.h>
    5254#include <string.h>
     55#ifndef _MSC_VER
    5356#include <unistd.h>
     57#else
     58#include "mscfakes.h"
     59#endif
    5460
    5561static int      fflag;                          /* Unlink existing files. */
     
    6369
    6470static int      linkit(const char *, const char *, int);
    65 static void     usage(void);
     71static int      usage(void);
     72
    6673
    6774int
     
    7279        int ch, exitval;
    7380
     81        /* kmk: reset getopt() and set program name. */
     82        g_progname = argv[0];
     83        opterr = 1;
     84        optarg = NULL;
     85        optopt = 0;
     86#if defined(__FreeBSD__) || defined(__EMX__)
     87        optreset = 1;
     88        optind = 1;
     89#else
     90        optind = 0; /* init */
     91#endif
     92
     93#if 0 /* kmk: we don't need this. */
    7494        /*
    7595         * Test for the special case where the utility is called as
     
    83103        if (strcmp(p, "link") == 0) {
    84104                while (getopt(argc, argv, "") != -1)
    85                         usage();
     105                        return usage();
    86106                argc -= optind;
    87107                argv += optind;
    88108                if (argc != 2)
    89                         usage();
     109                        return usage();
    90110                linkf = link;
    91                 exit(linkit(argv[0], argv[1], 0));
    92         }
     111                return linkit(argv[0], argv[1], 0);
     112        }
     113#else
     114        (void)p;
     115#endif
     116       
    93117
    94118        while ((ch = getopt(argc, argv, "fhinsv")) != -1)
     
    114138                case '?':
    115139                default:
    116                         usage();
     140                        return usage();
    117141                }
    118142
     
    125149        switch(argc) {
    126150        case 0:
    127                 usage();
     151                return usage();
    128152                /* NOTREACHED */
    129153        case 1:                         /* ln target */
    130                 exit(linkit(argv[0], ".", 1));
     154                return linkit(argv[0], ".", 1);
    131155        case 2:                         /* ln target source */
    132                 exit(linkit(argv[0], argv[1], 0));
     156                return linkit(argv[0], argv[1], 0);
    133157        default:
    134158                ;
     
    142166                 */
    143167                errno = ENOTDIR;
    144                 err(1, "%s", sourcedir);
     168                return err(1, "%s", sourcedir);
    145169        }
    146170        if (stat(sourcedir, &sb))
    147                 err(1, "%s", sourcedir);
     171                return err(1, "%s", sourcedir);
    148172        if (!S_ISDIR(sb.st_mode))
    149                 usage();
     173                return usage();
    150174        for (exitval = 0; *argv != sourcedir; ++argv)
    151175                exitval |= linkit(*argv, sourcedir, 1);
    152         exit(exitval);
     176        return exitval;
    153177}
    154178
    155 int
     179static int
    156180linkit(const char *target, const char *source, int isdir)
    157181{
     
    233257}
    234258
    235 void
     259static int
    236260usage(void)
    237261{
     
    240264            "       ln [-fhinsv] source_file ... target_dir",
    241265            "       link source_file target_file");
    242         exit(1);
     266        return 1;
    243267}
  • trunk/src/gmake/kmkbuiltin/mkdir.c

    r363 r370  
    3838static char sccsid[] = "@(#)mkdir.c     8.2 (Berkeley) 1/25/94";
    3939#endif /* not lint */
     40#include <sys/cdefs.h>
     41__FBSDID("$FreeBSD: src/bin/mkdir/mkdir.c,v 1.28 2004/04/06 20:06:48 markm Exp $");
    4042#endif
    41 #ifndef _MSC_VER
    42 #include <sys/cdefs.h>
    43 #endif
    44 //__FBSDID("$FreeBSD: src/bin/mkdir/mkdir.c,v 1.28 2004/04/06 20:06:48 markm Exp $");
    4543
    4644#include <sys/types.h>
    4745#include <sys/stat.h>
    4846
    49 //#include <err.h>
     47#include "err.h"
    5048#include <errno.h>
    5149#ifndef _MSC_VER
     
    5856#include <sysexits.h>
    5957#include <unistd.h>
    60 #endif
    61 
    62 #ifdef _MSC_VER
    63 #define setmode setmode_msc
    64 #include <stdarg.h>
    65 #include <io.h>
    66 #include <direct.h>
    67 #undef setmode
    68 #include "getopt.h"
    69 
    70 typedef int mode_t;
    71 #define EX_USAGE 1
    72 void warn(const char *fmt, ...)
    73 {
    74     int err = errno;
    75     va_list args;
    76     va_start(args, fmt);
    77     fprintf(stderr, "mkdir: ");
    78     vfprintf(stderr, fmt, args);
    79     fprintf(stderr, ": %s\n", strerror(err));
    80     va_end(args);
    81 }
    82 
    83 int mkdir_msc(const char *path, mode_t mode)
    84 {
    85     int rc = mkdir(path);
    86     if (rc)
    87     {
    88         int len = strlen(path);
    89         if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
    90         {
    91             char *str = strdup(path);
    92             while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
    93                 str[--len] = '\0';
    94             rc = mkdir(str);
    95             free(str);
    96         }
    97     }
    98     return rc;
    99 }
    100 #define mkdir(a,b) mkdir_msc(a,b)
    101 
    102 char *dirname(char *path)
    103 {         
    104     return path;
    105 }
    106 
    107 #define S_ISDIR(m)  (((m) & _S_IFMT) == _S_IFDIR)
    108 #define S_IRWXG 0000070
    109 #define S_IRWXO 0000007
    110 #define S_IRWXU (_S_IREAD | _S_IWRITE | _S_IEXEC)
    111 #define S_IXUSR _S_IEXEC
    112 #define S_IWUSR _S_IWRITE
    113 #define S_IRUSR _S_IREAD
    114 
     58#else
     59#include "mscfakes.h"
    11560#endif
    11661
     
    13378        mode = NULL;
    13479        vflag = 0;
     80        /* kmk: reset getopt and set progname */
     81        g_progname = argv[0];
    13582        opterr = 1;
    13683        optarg = NULL;
     
    166113                omode = S_IRWXU | S_IRWXG | S_IRWXO;
    167114        } else {
    168                 if ((set = setmode(mode)) == NULL) {
    169                         fprintf(stderr, "%s: invalid file mode: %s\n", mode, argv[0]);
    170                         return 1;
    171                 }
     115                if ((set = setmode(mode)) == NULL)
     116                        return errx(1, "invalid file mode: %s", mode);
    172117                omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
    173118                free(set);
     
    181126                } else if (mkdir(*argv, omode) < 0) {
    182127                        if (errno == ENOTDIR || errno == ENOENT)
    183                                 fprintf(stderr, "%s: %s: %s\n", argv[0], dirname(*argv), strerror(errno));
     128                                warn("%s", dirname(*argv));
    184129                        else
    185                                 fprintf(stderr, "%s: %s: %s\n", argv[0], *argv, strerror(errno));
     130                                warn("%s", *argv);
    186131                        success = 0;
    187132                } else if (vflag)
     
    198143                 */
    199144                if (success && mode != NULL && chmod(*argv, omode) == -1) {
    200                         fprintf(stderr, "%s: %s: %s\n", argv[0], *argv, strerror(errno));
     145                        warn("%s", *argv);
    201146                        exitval = 1;
    202147                }
     
    240185                        continue;
    241186                *p = '\0';
    242                 if (p[1] == '\0')
     187                if (!last && p[1] == '\0')
    243188                        last = 1;
    244189                if (first) {
  • trunk/src/gmake/kmkbuiltin/rm.c

    r341 r370  
    3838static char sccsid[] = "@(#)rm.c        8.5 (Berkeley) 4/18/94";
    3939#endif /* not lint */
    40 #endif
    4140#include <sys/cdefs.h>
    4241//__FBSDID("$FreeBSD: src/bin/rm/rm.c,v 1.47 2004/04/06 20:06:50 markm Exp $");
     42#endif
    4343
    4444#include <sys/stat.h>
     45#ifndef _MSC_VER
    4546#include <sys/param.h>
    4647#include <sys/mount.h>
    47 
    48 #include <err.h>
     48#endif
     49
     50#include "err.h"
    4951#include <errno.h>
    5052#include <fcntl.h>
     53#ifdef DO_RMTREE
    5154#include <fts.h>
     55#endif
     56#ifndef _MSC_VER
    5257#include <grp.h>
    5358#include <pwd.h>
     59#endif
    5460#include <stdio.h>
    5561#include <stdlib.h>
    5662#include <string.h>
     63#ifndef _MSC_VER
    5764#include <sysexits.h>
    5865#include <unistd.h>
     66#else
     67#include "mscfakes.h"
     68#endif
    5969
    6070#ifdef __EMX__
     
    8191static void     rm_file(char **);
    8292static int      rm_overwrite(char *, struct stat *);
     93#ifdef DO_RMTREE
    8394static void     rm_tree(char **);
     95#endif
    8496static int      usage(void);
    8597
     
    100112        dflag = eval = fflag = iflag = Pflag = vflag = Wflag = stdin_ok = 0;
    101113        uid = 0;
     114        /* kmk: reset getopt and set program name. */
     115        g_progname = argv[0];
    102116        opterr = 1;
    103117        optarg = NULL;
     
    110124#endif
    111125
     126#if 0 /* kmk: we don't need this */
    112127        /*
    113128         * Test for the special case where the utility is called as
     
    129144                return eval;
    130145        }
    131 
     146#else   
     147        (void)p;
     148#endif
    132149        Pflag = rflag = 0;
    133150        while ((ch = getopt(argc, argv, "dfiPRrvW")) != -1)
     
    149166                case 'R':
    150167                case 'r':                       /* Compatibility. */
     168#ifdef DO_RMTREE
    151169                        rflag = 1;
    152170                        break;
     171#else
     172                        errno = EINVAL;
     173                        return err(1, "Recursion is not supported!");
     174#endif
    153175                case 'v':
    154176                        vflag = 1;
     
    176198        if (*argv) {
    177199                stdin_ok = isatty(STDIN_FILENO);
    178 
     200#ifdef DO_RMTREE
    179201                if (rflag)
    180202                        rm_tree(argv);
    181203                else
     204#endif
    182205                        rm_file(argv);
    183206        }
     
    186209}
    187210
     211#ifdef DO_RMTREE
    188212static void
    189213rm_tree(char **argv)
     
    214238                flags |= FTS_WHITEOUT;
    215239#endif
    216         if (!(fts = fts_open(argv, flags, NULL)))
    217                 err(1, "fts_open");
     240        if (!(fts = fts_open(argv, flags, NULL))) {
     241                eval = err(1, "fts_open");
     242                return;
     243        }
    218244        while ((p = fts_read(fts)) != NULL) {
    219245                switch (p->fts_info) {
     
    226252                        continue;
    227253                case FTS_ERR:
    228                         errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
     254                        eval = errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
     255                        return;
    229256                case FTS_NS:
    230257                        /*
     
    318345                                                continue;
    319346                                rval = unlink(p->fts_accpath);
     347#ifdef _MSC_VER
     348                                if (rval != 0) {
     349                                        chmod(p->fts_accpath, 0777);
     350                                        rval = unlink(p->fts_accpath);
     351                                }
     352#endif
     353
    320354                                if (rval == 0 || (fflag && errno == ENOENT)) {
    321355                                        if (rval == 0 && vflag)
     
    335369        }
    336370}
     371#endif /* DO_RMTREE */
    337372
    338373static void
     
    395430                                                continue;
    396431                                rval = unlink(f);
     432#ifdef _MSC_VER
     433                                if (rval != 0) {
     434                                        chmod(f, 0777);
     435                                        rval = unlink(f);
     436                                }
     437#endif
    397438                        }
    398439                }
     
    448489#endif
    449490        if ((buf = malloc(bsize)) == NULL)
    450                 err(1, "%s: malloc", file);
     491                exit(err(1, "%s: malloc", file));
    451492
    452493#define PASS(byte) {                                                    \
     
    512553#ifdef SF_APPEND
    513554                if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
    514                         err(1, "fflagstostr");
     555                        exit(err(1, "fflagstostr"));
    515556                (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
    516557                    modep + 1, modep[9] == ' ' ? "" : " ",
     
    521562                free(flagsp);
    522563#else
     564                (void)flagsp;
    523565                (void)fprintf(stderr, "override %s%s %d/%d for %s? ",
    524566                    modep + 1, modep[9] == ' ' ? "" : " ",
  • trunk/src/gmake/kmkbuiltin/setmode.c

    r361 r370  
    3737#if defined(LIBC_SCCS) && !defined(lint)
    3838static char sccsid[] = "@(#)setmode.c   8.2 (Berkeley) 3/25/94";
     39#include <sys/cdefs.h>
     40//__FBSDID("$FreeBSD: src/lib/libc/gen/setmode.c,v 1.9 2003/02/23 00:24:03 mikeh Exp $");
    3941#endif /* LIBC_SCCS and not lint */
    40 #ifndef _MSC_VER
    41 #include <sys/cdefs.h>
    42 #endif
    43 //__FBSDID("$FreeBSD: src/lib/libc/gen/setmode.c,v 1.9 2003/02/23 00:24:03 mikeh Exp $");
    4442
    4543//#include "namespace.h"
     
    5351#ifndef _MSC_VER
    5452#include <unistd.h>
     53#else
     54#include "mscfakes.h"
    5555#endif
    5656
     
    5959#endif
    6060//#include "un-namespace.h"
    61 
    62 #ifdef _MSC_VER
    63 #define setmode setmode_msc
    64 #include <io.h>
    65 #undef setmode
    66 typedef unsigned int u_int;
    67 typedef int mode_t;
    68 #define S_ISDIR(m)  (((m) & _S_IFMT) == _S_IFDIR)
    69 #define S_IRWXU (_S_IREAD | _S_IWRITE | _S_IEXEC)
    70 #define S_IXUSR _S_IEXEC
    71 #define S_IWUSR _S_IWRITE
    72 #define S_IRUSR _S_IREAD
    73 #define S_IRWXG 0000070
    74 #define S_IRGRP 0000040
    75 #define S_IWGRP 0000020
    76 #define S_IXGRP 0000010
    77 #define S_IRWXO 0000007
    78 #define S_IROTH 0000004
    79 #define S_IWOTH 0000002
    80 #define S_IXOTH 0000001
    81 #define S_ISUID 0004000
    82 #define S_ISGID 0002000
    83 #endif /* _MSC_VER */
    84 
    8561
    8662#define SET_LEN 6               /* initial # of bitcmd struct to malloc */
  • trunk/src/gmake/kmkbuiltin/strlcpy.c

    r229 r370  
    3030#if defined(LIBC_SCCS) && !defined(lint)
    3131static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $");
    32 #endif /* LIBC_SCCS and not lint */
    3332#include <sys/cdefs.h>
    3433//__FBSDID("$FreeBSD: src/lib/libc/string/strlcpy.c,v 1.7 2003/05/01 19:03:14 nectar Exp $");
     34#endif /* LIBC_SCCS and not lint */
    3535
    3636#include <sys/types.h>
  • trunk/src/gmake/kmkbuiltin/strmode.c

    r228 r370  
    3434#if defined(LIBC_SCCS) && !defined(lint)
    3535static char sccsid[] = "@(#)strmode.c   8.3 (Berkeley) 8/15/94";
    36 #endif /* LIBC_SCCS and not lint */
    3736#include <sys/cdefs.h>
    3837//__FBSDID("$FreeBSD: src/lib/libc/string/strmode.c,v 1.4 2002/03/21 18:44:54 obrien Exp $");
     38#endif /* LIBC_SCCS and not lint */
     39
    3940
    4041#include <sys/types.h>
    4142#include <sys/stat.h>
    4243#include <string.h>
     44#ifdef _MSC_VER
     45#include "mscfakes.h"
     46#endif
    4347
    4448void
     
    5559                *p++ = 'c';
    5660                break;
     61#ifdef S_IFBLK
    5762        case S_IFBLK:                   /* block special */
    5863                *p++ = 'b';
    5964                break;
     65#endif
    6066        case S_IFREG:                   /* regular */
    6167                *p++ = '-';
    6268                break;
     69#ifndef _MSC_VER
    6370        case S_IFLNK:                   /* symbolic link */
    6471                *p++ = 'l';
    6572                break;
     73#endif
     74#ifdef S_IFSOCK
    6675        case S_IFSOCK:                  /* socket */
    6776                *p++ = 's';
    6877                break;
     78#endif
    6979#ifdef S_IFIFO
    7080        case S_IFIFO:                   /* fifo */
     
    136146        else
    137147                *p++ = '-';
     148#ifdef S_ISVTX
    138149        switch (mode & (S_IXOTH | S_ISVTX)) {
     150#else
     151        switch (mode & (S_IXOTH)) {
     152#endif
    139153        case 0:
    140154                *p++ = '-';
     
    143157                *p++ = 'x';
    144158                break;
     159#ifdef S_ISVTX
    145160        case S_ISVTX:
    146161                *p++ = 'T';
     
    149164                *p++ = 't';
    150165                break;
     166#endif
    151167        }
    152168        *p++ = ' ';             /* will be a '+' if ACL's implemented */
  • trunk/src/gmake/variable.c

    r368 r370  
    685685#ifdef CONFIG_WITH_KMK_BUILTIN
    686686  /* The supported kMk Builtin commands. */
    687 #ifdef _MSC_VER
    688   (void) define_variable ("KMK_BUILTIN", 11, "append echo mkdir", o_default, 0);
    689 #else
    690687  (void) define_variable ("KMK_BUILTIN", 11, "append cp echo install ln mkdir rm", o_default, 0);
    691 #endif
    692688#endif
    693689
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette