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/cp.c

    r3148 r3192  
    157157
    158158
    159 static int copy(char *[], enum op, int);
     159static int copy(PKMKBUILTINCTX pCtx, char *[], enum op, int);
    160160static int mastercmp(const FTSENT **, const FTSENT **);
    161161#ifdef SIGINFO
    162162static void siginfo(int __unused);
    163163#endif
    164 static int usage(FILE *);
     164static int usage(PKMKBUILTINCTX, int);
    165165
    166166int
    167 kmk_builtin_cp(int argc, char *argv[], char **envp)
     167kmk_builtin_cp(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
    168168{
    169169        struct stat to_stat, tmp_stat;
     
    180180        info = 0;
    181181        cp_ignore_non_existing = cp_changed_only = 0;
    182         kBuildProtectionInit(&g_ProtData);
     182        kBuildProtectionInit(&g_ProtData, pCtx);
    183183
    184184        /* reset getopt and set progname. */
    185         g_progname = argv[0];
    186185        opterr = 1;
    187186        optarg = NULL;
     
    229228                        break;
    230229                case CP_OPT_HELP:
    231                         usage(stdout);
     230                        usage(pCtx, 0);
    232231                        kBuildProtectionTerm(&g_ProtData);
    233232                        return 0;
     
    261260                default:
    262261                        kBuildProtectionTerm(&g_ProtData);
    263                         return usage(stderr);
     262                        return usage(pCtx, 1);
    264263                }
    265264        argc -= optind;
     
    268267        if (argc < 2) {
    269268                kBuildProtectionTerm(&g_ProtData);
    270                 return usage(stderr);
     269                return usage(pCtx, 1);
    271270        }
    272271
     
    275274                if (Rflag) {
    276275                        kBuildProtectionTerm(&g_ProtData);
    277                         return errx(1,
     276                        return errx(pCtx, 1,
    278277                    "the -R and -r options may not be specified together.");
    279278                }
    280279                if (Hflag || Lflag || Pflag)
    281                         errx(1,
     280                        errx(pCtx, 1,
    282281        "the -H, -L, and -P options may not be specified with the -r option.");
    283282                fts_options &= ~FTS_PHYSICAL;
     
    303302        if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path)) {
    304303                kBuildProtectionTerm(&g_ProtData);
    305                 return errx(1, "%s: name too long", target);
     304                return errx(pCtx, 1, "%s: name too long", target);
    306305        }
    307306        to.p_end = to.p_path + strlen(to.p_path);
     
    335334        if (r == -1 && errno != ENOENT) {
    336335                kBuildProtectionTerm(&g_ProtData);
    337                 return err(1, "stat: %s", to.p_path);
     336                return err(pCtx, 1, "stat: %s", to.p_path);
    338337        }
    339338        if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
     
    343342                if (argc > 1) {
    344343                        kBuildProtectionTerm(&g_ProtData);
    345                         return usage(stderr);
     344                        return usage(pCtx, 1);
    346345                }
    347346                /*
     
    368367                        kBuildProtectionTerm(&g_ProtData);
    369368                        if (r == -1)
    370                                 return errx(1, "directory %s does not exist",
     369                                return errx(pCtx, 1, "directory %s does not exist",
    371370                                            to.p_path);
    372371                        else
    373                                 return errx(1, "%s is not a directory", to.p_path);
     372                                return errx(pCtx, 1, "%s is not a directory", to.p_path);
    374373                }
    375374        } else
     
    387386                                     : KBUILDPROTECTIONTYPE_FULL,
    388387                                     to.p_path)) {
    389             rc = copy(argv, type, fts_options);
     388            rc = copy(pCtx, argv, type, fts_options);
    390389        }
    391390
     
    394393}
    395394
     395#ifdef KMK_BUILTIN_STANDALONE
     396int main(int argc, char **argv, char **envp)
     397{
     398    KMKBUILTINCTX Ctx = { "kmk_cp", NULL };
     399    return kmk_builtin_cp(argc, argv, envp, &Ctx);
     400}
     401#endif
     402
    396403static int
    397 copy(char *argv[], enum op type, int fts_options)
     404copy(PKMKBUILTINCTX pCtx, char *argv[], enum op type, int fts_options)
    398405{
    399406        struct stat to_stat;
     
    413420
    414421        if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
    415                 return err(1, "fts_open");
     422                return err(pCtx, 1, "fts_open");
    416423        for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) {
    417424                int copied = 0;
     
    422429                            && curr->fts_errno == ENOENT) {
    423430                                if (vflag) {
    424                                         warnx("fts: %s: %s", curr->fts_path,
     431                                        warnx(pCtx, "fts: %s: %s", curr->fts_path,
    425432                                              strerror(curr->fts_errno));
    426433                                }
     
    430437                case FTS_DNR:
    431438                case FTS_ERR:
    432                         warnx("fts: %s: %s",
     439                        warnx(pCtx, "fts: %s: %s",
    433440                            curr->fts_path, strerror(curr->fts_errno));
    434441                        badcp = rval = 1;
    435442                        continue;
    436443                case FTS_DC:                    /* Warn, continue. */
    437                         warnx("%s: directory causes a cycle", curr->fts_path);
     444                        warnx(pCtx, "%s: directory causes a cycle", curr->fts_path);
    438445                        badcp = rval = 1;
    439446                        continue;
     
    490497                        *target_mid = 0;
    491498                        if (target_mid - to.p_path + nlen >= PATH_MAX) {
    492                                 warnx("%s%s: name too long (not copied)",
     499                                warnx(pCtx, "%s%s: name too long (not copied)",
    493500                                    to.p_path, p);
    494501                                badcp = rval = 1;
     
    519526                         */
    520527                        if (pflag) {
    521                                 if (setfile(curr->fts_statp, -1))
     528                                if (setfile(pCtx, curr->fts_statp, -1))
    522529                                    rval = 1;
    523530                        } else {
     
    526533                                    ((mode | S_IRWXU) & mask) != (mode & mask))
    527534                                        if (chmod(to.p_path, mode & mask) != 0){
    528                                                 warn("chmod: %s", to.p_path);
     535                                                warn(pCtx, "chmod: %s", to.p_path);
    529536                                                rval = 1;
    530537                                        }
     
    541548                            to_stat.st_ino == curr->fts_statp->st_ino &&
    542549                            to_stat.st_ino != 0) {
    543                                 warnx("%s and %s are identical (not copied).",
     550                                warnx(pCtx, "%s and %s are identical (not copied).",
    544551                                    to.p_path, curr->fts_path);
    545552                                badcp = rval = 1;
     
    550557                        if (!S_ISDIR(curr->fts_statp->st_mode) &&
    551558                            S_ISDIR(to_stat.st_mode)) {
    552                                 warnx("cannot overwrite directory %s with "
     559                                warnx(pCtx, "cannot overwrite directory %s with "
    553560                                    "non-directory %s",
    554561                                    to.p_path, curr->fts_path);
     
    566573                            ((fts_options & FTS_COMFOLLOW) &&
    567574                            curr->fts_level == 0)) {
    568                                 if (copy_file(curr, dne, cp_changed_only, &copied))
     575                                if (copy_file(pCtx, curr, dne, cp_changed_only, &copied))
    569576                                        badcp = rval = 1;
    570577                        } else {
    571                                 if (copy_link(curr, !dne))
     578                                if (copy_link(pCtx, curr, !dne))
    572579                                        badcp = rval = 1;
    573580                        }
     
    576583                case S_IFDIR:
    577584                        if (!Rflag && !rflag) {
    578                                 warnx("%s is a directory (not copied).",
     585                                warnx(pCtx, "%s is a directory (not copied).",
    579586                                    curr->fts_path);
    580587                                (void)fts_set(ftsp, curr, FTS_SKIP);
     
    593600                                if (mkdir(to.p_path,
    594601                                    curr->fts_statp->st_mode | S_IRWXU) < 0)
    595                                         return err(1, "mkdir: %s", to.p_path);
     602                                        return err(pCtx, 1, "mkdir: %s", to.p_path);
    596603                        } else if (!S_ISDIR(to_stat.st_mode)) {
    597604                                errno = ENOTDIR;
    598                                 return err(1, "to-mode: %s", to.p_path);
     605                                return err(pCtx, 1, "to-mode: %s", to.p_path);
    599606                        }
    600607                        /*
     
    610617                case S_IFCHR:
    611618                        if (Rflag) {
    612                                 if (copy_special(curr->fts_statp, !dne))
     619                                if (copy_special(pCtx, curr->fts_statp, !dne))
    613620                                        badcp = rval = 1;
    614621                        } else {
    615                                 if (copy_file(curr, dne, cp_changed_only, &copied))
     622                                if (copy_file(pCtx, curr, dne, cp_changed_only, &copied))
    616623                                        badcp = rval = 1;
    617624                        }
     
    621628#endif
    622629                        if (Rflag) {
    623                                 if (copy_fifo(curr->fts_statp, !dne))
     630                                if (copy_fifo(pCtx, curr->fts_statp, !dne))
    624631                                        badcp = rval = 1;
    625632                        } else {
    626                                 if (copy_file(curr, dne, cp_changed_only, &copied))
     633                                if (copy_file(pCtx, curr, dne, cp_changed_only, &copied))
    627634                                        badcp = rval = 1;
    628635                        }
    629636                        break;
    630637                default:
    631                         if (copy_file(curr, dne, cp_changed_only, &copied))
     638                        if (copy_file(pCtx, curr, dne, cp_changed_only, &copied))
    632639                                badcp = rval = 1;
    633640                        break;
    634641                }
    635642                if (vflag && !badcp)
    636                         (void)printf(copied ? "%s -> %s\n" : "%s matches %s - not copied\n",
    637                                      curr->fts_path, to.p_path);
     643                        kmk_builtin_ctx_printf(pCtx, 0, copied ? "%s -> %s\n" : "%s matches %s - not copied\n",
     644                                               curr->fts_path, to.p_path);
    638645        }
    639646        if (errno)
    640                 return err(1, "fts_read");
     647                return err(pCtx, 1, "fts_read");
    641648        return (rval);
    642649}
     
    679686
    680687static int
    681 usage(FILE *fp)
     688usage(PKMKBUILTINCTX pCtx, int fIsErr)
    682689{
    683         fprintf(fp,
     690        kmk_builtin_ctx_printf(pCtx, fIsErr,
    684691"usage: %s [options] src target\n"
    685692"   or: %s [options] src1 ... srcN directory\n"
     
    728735"yourself in the foot.\n"
    729736                ,
    730                 g_progname, g_progname, g_progname, g_progname,
     737                pCtx->pszProgName, pCtx->pszProgName,
     738                pCtx->pszProgName, pCtx->pszProgName,
    731739                kBuildProtectionDefaultDepth(), kBuildProtectionDefaultDepth());
    732740        return 1;
Note: See TracChangeset for help on using the changeset viewer.