Ignore:
Timestamp:
Mar 26, 2018, 10:25:56 PM (7 years ago)
Author:
bird
Message:

kmkbuiltin: funnel output thru output.c (usually via err.c).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/kmkbuiltin/mv.c

    r3109 r3192  
    4747#endif
    4848
     49
     50/*********************************************************************************************************************************
     51*   Header Files                                                                                                                 *
     52*********************************************************************************************************************************/
    4953#include "config.h"
    5054#include <sys/types.h>
     
    8993
    9094
    91 static int fflg, iflg, nflg, vflg;
     95/*********************************************************************************************************************************
     96*   Structures and Typedefs                                                                                                      *
     97*********************************************************************************************************************************/
     98typedef struct MVINSTANCE
     99{
     100    PKMKBUILTINCTX pCtx;
     101    int fflg, iflg, nflg, vflg;
     102} MVINSTANCE;
     103typedef MVINSTANCE *PMVINSTANCE;
     104
     105
     106/*********************************************************************************************************************************
     107*   Global Variables                                                                                                             *
     108*********************************************************************************************************************************/
    92109static struct option long_options[] =
    93110{
     
    98115
    99116
    100 static int      do_move(char *, char *);
     117/*********************************************************************************************************************************
     118*   Internal Functions                                                                                                           *
     119*********************************************************************************************************************************/
     120extern void     bsd_strmode(mode_t mode, char *p); /* strmode.c */
     121
     122static int      do_move(PMVINSTANCE, char *, char *);
    101123#ifdef CROSS_DEVICE_MOVE
    102124static int      fastcopy(char *, char *, struct stat *);
    103125static int      copy(char *, char *);
    104126#endif
    105 static int      usage(FILE *);
    106 
    107 extern void bsd_strmode(mode_t mode, char *p);
     127static int      usage(PKMKBUILTINCTX, int);
     128
    108129
    109130#if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(__DragonFly__) && !defined(__OpenBSD__)
     
    132153
    133154int
    134 kmk_builtin_mv(int argc, char *argv[], char **envp)
    135 {
     155kmk_builtin_mv(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
     156{
     157        MVINSTANCE This;
    136158        size_t baselen, len;
    137159        int rval;
     
    141163        char path[PATH_MAX];
    142164
    143         /* kmk: reinitialize globals */
    144         fflg = iflg = nflg = vflg = 0;
     165        /* Initialize instance. */
     166        This.pCtx = pCtx;
     167        This.fflg = 0;
     168        This.iflg = 0;
     169        This.nflg = 0;
     170        This.vflg = 0;
    145171
    146172        /* kmk: reset getopt and set progname */
    147         g_progname = argv[0];
    148173        opterr = 1;
    149174        optarg = NULL;
     
    154179                switch (ch) {
    155180                case 'i':
    156                         iflg = 1;
    157                         fflg = nflg = 0;
     181                        This.iflg = 1;
     182                        This.fflg = This.nflg = 0;
    158183                        break;
    159184                case 'f':
    160                         fflg = 1;
    161                         iflg = nflg = 0;
     185                        This.fflg = 1;
     186                        This.iflg = This.nflg = 0;
    162187                        break;
    163188                case 'n':
    164                         nflg = 1;
    165                         fflg = iflg = 0;
     189                        This.nflg = 1;
     190                        This.fflg = This.iflg = 0;
    166191                        break;
    167192                case 'v':
    168                         vflg = 1;
     193                        This.vflg = 1;
    169194                        break;
    170195                case 261:
    171                         usage(stdout);
     196                        usage(pCtx, 0);
    172197                        return 0;
    173198                case 262:
    174199                        return kbuild_version(argv[0]);
    175200                default:
    176                         return usage(stderr);
     201                        return usage(pCtx, 1);
    177202                }
    178203        argc -= optind;
     
    180205
    181206        if (argc < 2)
    182                 return usage(stderr);
     207                return usage(pCtx, 1);
    183208
    184209        /*
     
    188213        if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
    189214                if (argc > 2)
    190                         return usage(stderr);
    191                 return do_move(argv[0], argv[1]);
     215                        return usage(pCtx, 1);
     216                return do_move(&This, argv[0], argv[1]);
    192217        }
    193218
    194219        /* It's a directory, move each file into it. */
    195         if (strlen(argv[argc - 1]) > sizeof(path) - 1)
    196                 return errx(1, "%s: destination pathname too long", *argv);
    197         (void)strcpy(path, argv[argc - 1]);
    198         baselen = strlen(path);
     220        baselen = strlen(argv[argc - 1]);
     221        if (baselen > sizeof(path) - 1)
     222                return errx(pCtx, 1, "%s: destination pathname too long", *argv);
     223        memcpy(path, argv[argc - 1], baselen);
    199224        endp = &path[baselen];
     225        *endp = '\0';
    200226#if defined(_MSC_VER) || defined(__EMX__)
    201227        if (!baselen || (*(endp - 1) != '/' && *(endp - 1) != '\\' && *(endp - 1) != ':')) {
     
    225251
    226252                if ((baselen + (len = strlen(p))) >= PATH_MAX) {
    227                         warnx("%s: destination pathname too long", *argv);
     253                        warnx(pCtx, "%s: destination pathname too long", *argv);
    228254                        rval = 1;
    229255                } else {
    230256                        memmove(endp, p, (size_t)len + 1);
    231                         if (do_move(*argv, path))
     257                        if (do_move(&This, *argv, path))
    232258                                rval = 1;
    233259                }
     
    236262}
    237263
     264#ifdef KMK_BUILTIN_STANDALONE
     265int main(int argc, char **argv, char **envp)
     266{
     267    KMKBUILTINCTX Ctx = { "kmk_mv", NULL };
     268    return kmk_builtin_mv(argc, argv, envp, &Ctx);
     269}
     270#endif
     271
    238272static int
    239 do_move(char *from, char *to)
     273do_move(PMVINSTANCE pThis, char *from, char *to)
    240274{
    241275        struct stat sb;
     
    248282         * make sure the user wants to clobber it.
    249283         */
    250         if (!fflg && !access(to, F_OK)) {
     284        if (!pThis->fflg && !access(to, F_OK)) {
    251285
    252286                /* prompt only if source exist */
    253287                if (lstat(from, &sb) == -1) {
    254                         warn("%s", from);
     288                        warn(pThis->pCtx, "%s", from);
    255289                        return (1);
    256290                }
     
    258292#define YESNO "(y/n [n]) "
    259293                ask = 0;
    260                 if (nflg) {
    261                         if (vflg)
    262                                 printf("%s not overwritten\n", to);
     294                if (pThis->nflg) {
     295                        if (pThis->vflg)
     296                                kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s not overwritten\n", to);
    263297                        return (0);
    264                 } else if (iflg) {
     298                } else if (pThis->iflg) {
    265299                        (void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
    266300                        ask = 1;
     
    274308                }
    275309                if (ask) {
     310                        fflush(stderr);
    276311                        first = ch = getchar();
    277312                        while (ch != '\n' && ch != EOF)
    278313                                ch = getchar();
    279314                        if (first != 'y' && first != 'Y') {
    280                                 (void)fprintf(stderr, "not overwritten\n");
     315                                kmk_builtin_ctx_printf(pThis->pCtx, 1, "not overwritten\n");
    281316                                return (0);
    282317                        }
     
    284319        }
    285320        if (!rename(from, to)) {
    286                 if (vflg)
    287                         printf("%s -> %s\n", from, to);
     321                if (pThis->vflg)
     322                        kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s -> %s\n", from, to);
    288323                return (0);
    289324        }
     
    292327                remove(to);
    293328                if (!rename(from, to)) {
    294                         if (vflg)
    295                                 printf("%s -> %s\n", from, to);
     329                        if (pThis->vflg)
     330                                kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s -> %s\n", from, to);
    296331                        return (0);
    297332                }
     
    301336        if (errno == EXDEV) {
    302337#ifndef CROSS_DEVICE_MOVE
    303                 warnx("cannot move `%s' to a different device: `%s'", from, to);
     338                warnx(pThis->pCtx, "cannot move `%s' to a different device: `%s'", from, to);
    304339                return (1);
    305340#else
     
    312347                 */
    313348                if (lstat(from, &sb) == -1) {
    314                         warn("%s", from);
     349                        warn(pThis->pCtx, "%s", from);
    315350                        return (1);
    316351                }
     
    318353                        /* Can't mv(1) a mount point. */
    319354                        if (realpath(from, path) == NULL) {
    320                                 warnx("cannot resolve %s: %s", from, path);
     355                                warnx(pThis->pCtx, "cannot resolve %s: %s", from, path);
    321356                                return (1);
    322357                        }
    323358                        if (!statfs(path, &sfs) &&
    324359                            !strcmp(path, sfs.f_mntonname)) {
    325                                 warnx("cannot rename a mount point");
     360                                warnx(pThis->pCtx, "cannot rename a mount point");
    326361                                return (1);
    327362                        }
     
    329364#endif
    330365        } else {
    331                 warn("rename %s to %s", from, to);
     366                warn(pThis->pCtx, "rename %s to %s", from, to);
    332367                return (1);
    333368        }
     
    340375         */
    341376        if (lstat(from, &sb)) {
    342                 warn("%s", from);
     377                warn(pThis->pCtx, "%s", from);
    343378                return (1);
    344379        }
    345380        return (S_ISREG(sb.st_mode) ?
    346             fastcopy(from, to, &sb) : copy(from, to));
     381            fastcopy(pThis, from, to, &sb) : copy(pThis, from, to));
    347382#endif
    348383}
     
    451486        }
    452487        if (vflg)
    453                 printf("%s -> %s\n", from, to);
     488                kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s -> %s\n", from, to);
    454489        return (0);
    455490}
     
    503538
    504539static int
    505 usage(FILE *pf)
    506 {
    507         fprintf(pf, "usage: %s [-f | -i | -n] [-v] source target\n"
    508                     "   or: %s [-f | -i | -n] [-v] source ... directory\n"
    509                     "   or: %s --help\n"
    510                     "   or: %s --version\n",
    511                 g_progname, g_progname, g_progname, g_progname);
     540usage(PKMKBUILTINCTX pCtx, int fIsErr)
     541{
     542        kmk_builtin_ctx_printf(pCtx, fIsErr,
     543                               "usage: %s [-f | -i | -n] [-v] source target\n"
     544                               "   or: %s [-f | -i | -n] [-v] source ... directory\n"
     545                               "   or: %s --help\n"
     546                               "   or: %s --version\n",
     547                               pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
    512548        return EX_USAGE;
    513549}
Note: See TracChangeset for help on using the changeset viewer.