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

    r3148 r3192  
    4242#endif
    4343
     44
     45/*********************************************************************************************************************************
     46*   Header Files                                                                                                                 *
     47*********************************************************************************************************************************/
    4448#include "config.h"
    4549#include <sys/stat.h>
     
    9094#include "k/kDefs.h"    /* for K_OS */
    9195
     96
     97/*********************************************************************************************************************************
     98*   Defined Constants And Macros                                                                                                 *
     99*********************************************************************************************************************************/
    92100#if defined(__EMX__) || defined(KBUILD_OS_WINDOWS)
    93101# define IS_SLASH(ch)   ( (ch) == '/' || (ch) == '\\' )
     
    110118#endif
    111119
    112 extern void bsd_strmode(mode_t mode, char *p);
    113 
    114 static int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok;
     120#if 1
     121#define CUR_LINE_H2(x)  "[line " #x "]"
     122#define CUR_LINE_H1(x)  CUR_LINE_H2(x)
     123#define CUR_LINE()      CUR_LINE_H1(__LINE__)
     124#else
     125# define CUR_LINE()
     126#endif
     127
     128
     129/*********************************************************************************************************************************
     130*   Structures and Typedefs                                                                                                      *
     131*********************************************************************************************************************************/
     132typedef struct RMINSTANCE
     133{
     134    PKMKBUILTINCTX pCtx;
     135    int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok;
    115136#ifdef KBUILD_OS_WINDOWS
    116 static int fUseNtDeleteFile;
    117 #endif
    118 static uid_t uid;
    119 
    120 static char *argv0;
    121 static KBUILDPROTECTION g_ProtData;
    122 
     137    int fUseNtDeleteFile;
     138#endif
     139    uid_t uid;
     140    KBUILDPROTECTION g_ProtData;
     141} RMINSTANCE;
     142typedef RMINSTANCE *PRMINSTANCE;
     143
     144
     145/*********************************************************************************************************************************
     146*   Global Variables                                                                                                             *
     147*********************************************************************************************************************************/
    123148static struct option long_options[] =
    124149{
     
    137162
    138163
    139 static int      check(char *, char *, struct stat *);
    140 static void     checkdot(char **);
    141 static int      rm_file(char **);
    142 static int      rm_overwrite(char *, struct stat *);
    143 static int      rm_tree(char **);
    144 static int      usage(FILE *);
    145 
    146 #if 1
    147 #define CUR_LINE_H2(x)  "[line " #x "]"
    148 #define CUR_LINE_H1(x)  CUR_LINE_H2(x)
    149 #define CUR_LINE()      CUR_LINE_H1(__LINE__)
    150 #else
    151 # define CUR_LINE()
    152 #endif
     164/*********************************************************************************************************************************
     165*   Internal Functions                                                                                                           *
     166*********************************************************************************************************************************/
     167extern void bsd_strmode(mode_t mode, char *p); /* strmode.c */
     168
     169static int      check(PRMINSTANCE, char *, char *, struct stat *);
     170static void     checkdot(PRMINSTANCE, char **);
     171static int      rm_file(PRMINSTANCE, char **);
     172static int      rm_overwrite(PRMINSTANCE, char *, struct stat *);
     173static int      rm_tree(PRMINSTANCE, char **);
     174static int      usage(PKMKBUILTINCTX, int);
     175
    153176
    154177
     
    161184 */
    162185int
    163 kmk_builtin_rm(int argc, char *argv[], char **envp)
    164 {
     186kmk_builtin_rm(int argc, char *argv[], char **envp, PKMKBUILTINCTX pCtx)
     187{
     188        RMINSTANCE This;
    165189        int ch, rflag;
    166190
    167         /* reinitialize globals */
    168         argv0 = argv[0];
    169         dflag = eval = fflag = iflag = Pflag = vflag = Wflag = stdin_ok = 0;
     191        /* Init global instance data */
     192        This.pCtx = pCtx;
     193        This.dflag = 0;
     194        This.eval = 0;
     195        This.fflag = 0;
     196        This.iflag = 0;
     197        This.Pflag = 0;
     198        This.vflag = 0;
     199        This.Wflag = 0;
     200        This.stdin_ok = 0;
    170201#ifdef KBUILD_OS_WINDOWS
    171         fUseNtDeleteFile = 0;
    172 #endif
    173         uid = 0;
    174         kBuildProtectionInit(&g_ProtData);
     202        This.fUseNtDeleteFile = 0;
     203#endif
     204        This.uid = 0;
     205        kBuildProtectionInit(&This.g_ProtData, pCtx);
    175206
    176207        /* kmk: reset getopt and set program name. */
    177         g_progname = argv[0];
    178208        opterr = 1;
    179209        optarg = NULL;
     
    181211        optind = 0; /* init */
    182212
    183         Pflag = rflag = 0;
     213        rflag = 0;
    184214        while ((ch = getopt_long(argc, argv, "dfiPRvW", long_options, NULL)) != -1)
    185                 switch(ch) {
     215                switch (ch) {
    186216                case 'd':
    187                         dflag = 1;
     217                        This.dflag = 1;
    188218                        break;
    189219                case 'f':
    190                         fflag = 1;
    191                         iflag = 0;
     220                        This.fflag = 1;
     221                        This.iflag = 0;
    192222                        break;
    193223                case 'i':
    194                         fflag = 0;
    195                         iflag = 1;
     224                        This.fflag = 0;
     225                        This.iflag = 1;
    196226                        break;
    197227                case 'P':
    198                         Pflag = 1;
     228                        This.Pflag = 1;
    199229                        break;
    200230                case 'R':
     
    205235                        break;
    206236                case 'v':
    207                         vflag = 1;
     237                        This.vflag = 1;
    208238                        break;
    209239#ifdef FTS_WHITEOUT
    210240                case 'W':
    211                         Wflag = 1;
     241                        This.Wflag = 1;
    212242                        break;
    213243#endif
    214244                case 261:
    215                         kBuildProtectionTerm(&g_ProtData);
    216                         usage(stdout);
     245                        kBuildProtectionTerm(&This.g_ProtData);
     246                        usage(pCtx, 0);
    217247                        return 0;
    218248                case 262:
    219                         kBuildProtectionTerm(&g_ProtData);
     249                        kBuildProtectionTerm(&This.g_ProtData);
    220250                        return kbuild_version(argv[0]);
    221251                case 263:
    222                         kBuildProtectionDisable(&g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE);
     252                        kBuildProtectionDisable(&This.g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE);
    223253                        break;
    224254                case 264:
    225                         kBuildProtectionEnable(&g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE);
     255                        kBuildProtectionEnable(&This.g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE);
    226256                        break;
    227257                case 265:
    228                         kBuildProtectionEnable(&g_ProtData, KBUILDPROTECTIONTYPE_FULL);
     258                        kBuildProtectionEnable(&This.g_ProtData, KBUILDPROTECTIONTYPE_FULL);
    229259                        break;
    230260                case 266:
    231                         kBuildProtectionDisable(&g_ProtData, KBUILDPROTECTIONTYPE_FULL);
     261                        kBuildProtectionDisable(&This.g_ProtData, KBUILDPROTECTIONTYPE_FULL);
    232262                        break;
    233263                case 267:
    234                         if (kBuildProtectionSetDepth(&g_ProtData, optarg)) {
    235                             kBuildProtectionTerm(&g_ProtData);
     264                        if (kBuildProtectionSetDepth(&This.g_ProtData, optarg)) {
     265                            kBuildProtectionTerm(&This.g_ProtData);
    236266                            return 1;
    237267                        }
     
    239269#ifdef KBUILD_OS_WINDOWS
    240270                case 268:
    241                         fUseNtDeleteFile = 1;
     271                        This.fUseNtDeleteFile = 1;
    242272                        break;
    243273#endif
    244274                case '?':
    245275                default:
    246                         kBuildProtectionTerm(&g_ProtData);
    247                         return usage(stderr);
     276                        kBuildProtectionTerm(&This.g_ProtData);
     277                        return usage(pCtx, 1);
    248278                }
    249279        argc -= optind;
     
    251281
    252282        if (argc < 1) {
    253                 kBuildProtectionTerm(&g_ProtData);
    254                 if (fflag)
     283                kBuildProtectionTerm(&This.g_ProtData);
     284                if (This.fflag)
    255285                        return (0);
    256                 return usage(stderr);
    257         }
    258 
    259         if (!kBuildProtectionScanEnv(&g_ProtData, envp, "KMK_RM_")) {
    260                 checkdot(argv);
    261                 uid = geteuid();
     286                return usage(pCtx, 1);
     287        }
     288
     289        if (!kBuildProtectionScanEnv(&This.g_ProtData, envp, "KMK_RM_")) {
     290                checkdot(&This, argv);
     291                This.uid = geteuid();
    262292
    263293                if (*argv) {
    264                         stdin_ok = isatty(STDIN_FILENO);
     294                        This.stdin_ok = isatty(STDIN_FILENO);
    265295                        if (rflag)
    266                                 eval |= rm_tree(argv);
     296                                This.eval |= rm_tree(&This, argv);
    267297                        else
    268                                 eval |= rm_file(argv);
     298                                This.eval |= rm_file(&This, argv);
    269299                }
    270300        } else {
    271                 eval = 1;
    272         }
    273 
    274         kBuildProtectionTerm(&g_ProtData);
    275         return eval;
     301                This.eval = 1;
     302        }
     303
     304        kBuildProtectionTerm(&This.g_ProtData);
     305        return This.eval;
    276306}
    277307
     308#ifdef KMK_BUILTIN_STANDALONE
     309int main(int argc, char **argv, char **envp)
     310{
     311        KMKBUILTINCTX Ctx = { "kmk_rm", NULL };
     312        return kmk_builtin_rm(argc, argv, envp, &Ctx);
     313}
     314#endif
     315
    278316static int
    279 rm_tree(char **argv)
     317rm_tree(PRMINSTANCE pThis, char **argv)
    280318{
    281319        FTS *fts;
     
    291329        int i;
    292330        for (i = 0; argv[i]; i++) {
    293                 if (kBuildProtectionEnforce(&g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE, argv[i])) {
     331                if (kBuildProtectionEnforce(&pThis->g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE, argv[i])) {
    294332                        return 1;
    295333                }
     
    300338         * (-i) or can't ask anyway (stdin_ok), don't stat the file.
    301339         */
    302         needstat = !uid || (!fflag && !iflag && stdin_ok);
     340        needstat = !pThis->uid || (!pThis->fflag && !pThis->iflag && pThis->stdin_ok);
    303341
    304342        /*
     
    312350                flags |= FTS_NOSTAT;
    313351#ifdef FTS_WHITEOUT
    314         if (Wflag)
     352        if (pThis->Wflag)
    315353                flags |= FTS_WHITEOUT;
    316354#endif
    317355        if (!(fts = fts_open(argv, flags, NULL))) {
    318                 return err(1, "fts_open");
     356                return err(pThis->pCtx, 1, "fts_open");
    319357        }
    320358        while ((p = fts_read(fts)) != NULL) {
     
    322360                switch (p->fts_info) {
    323361                case FTS_DNR:
    324                         if (!fflag || p->fts_errno != ENOENT) {
    325                                 fprintf(stderr, "fts: %s: %s: %s" CUR_LINE() "\n",
    326                                         argv0, p->fts_path, strerror(p->fts_errno));
    327                                 eval = 1;
    328                         }
     362                        if (!pThis->fflag || p->fts_errno != ENOENT)
     363                                pThis->eval = errx(pThis->pCtx, 1, "fts: %s: %s" CUR_LINE() "\n",
     364                                                   p->fts_path, strerror(p->fts_errno));
    329365                        continue;
    330366                case FTS_ERR:
    331367                        fts_close(fts);
    332                         return errx(1, "fts: %s: %s " CUR_LINE(), p->fts_path, strerror(p->fts_errno));
     368                        return errx(pThis->pCtx, 1, "fts: %s: %s " CUR_LINE(), p->fts_path, strerror(p->fts_errno));
    333369                case FTS_NS:
    334370                        /*
     
    338374                        if (!needstat)
    339375                                break;
    340                         if (!fflag || p->fts_errno != ENOENT) {
    341                                 fprintf(stderr, "fts: %s: %s: %s " CUR_LINE() "\n",
    342                                         argv0, p->fts_path, strerror(p->fts_errno));
    343                                 eval = 1;
    344                         }
     376                        if (!pThis->fflag || p->fts_errno != ENOENT)
     377                                pThis->eval = errx(pThis->pCtx, 1, "fts: %s: %s " CUR_LINE() "\n",
     378                                                   p->fts_path, strerror(p->fts_errno));
    345379                        continue;
    346380                case FTS_D:
    347381                        /* Pre-order: give user chance to skip. */
    348                         if (!fflag && !check(p->fts_path, p->fts_accpath,
    349                             p->fts_statp)) {
     382                        if (!pThis->fflag && !check(pThis, p->fts_path, p->fts_accpath, p->fts_statp)) {
    350383                                (void)fts_set(fts, p, FTS_SKIP);
    351384                                p->fts_number = SKIPPED;
    352385                        }
    353386#ifdef UF_APPEND
    354                         else if (!uid &&
     387                        else if (!pThis->uid &&
    355388                                 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
    356389                                 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
     
    366399                        break;
    367400                default:
    368                         if (!fflag &&
    369                             !check(p->fts_path, p->fts_accpath, p->fts_statp))
     401                        if (!pThis->fflag && !check(pThis, p->fts_path, p->fts_accpath, p->fts_statp))
    370402                                continue;
    371403                }
     
    374406                 * Protect against deleting root files and directories.
    375407                 */
    376                 if (kBuildProtectionEnforce(&g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE, p->fts_accpath)) {
     408                if (kBuildProtectionEnforce(&pThis->g_ProtData, KBUILDPROTECTIONTYPE_RECURSIVE, p->fts_accpath)) {
    377409                        fts_close(fts);
    378410                        return 1;
     
    381413                rval = 0;
    382414#ifdef UF_APPEND
    383                 if (!uid &&
     415                if (!pThis->uid &&
    384416                    (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
    385417                    !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
     
    405437                                rval = rmdir(p->fts_accpath);
    406438#endif
    407                                 if (rval == 0 || (fflag && errno == ENOENT)) {
    408                                         if (rval == 0 && vflag)
    409                                                 (void)printf("%s\n",
    410                                                     p->fts_path);
     439                                if (rval == 0 || (pThis->fflag && errno == ENOENT)) {
     440                                        if (rval == 0 && pThis->vflag)
     441                                                kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s\n", p->fts_path);
    411442#if defined(KMK) && defined(KBUILD_OS_WINDOWS)
    412443                                        if (rval == 0) {
     
    423454                        case FTS_W:
    424455                                rval = undelete(p->fts_accpath);
    425                                 if (rval == 0 && (fflag && errno == ENOENT)) {
     456                                if (rval == 0 && (pThis->fflag && errno == ENOENT)) {
    426457                                        if (vflag)
    427                                                 (void)printf("%s\n",
    428                                                     p->fts_path);
     458                                                kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s\n", p->fts_path);
    429459                                        continue;
    430460                                }
     
    438468                                 * the file, it can't be unlinked.
    439469                                 */
    440                                 if (fflag)
     470                                if (pThis->fflag)
    441471                                        continue;
    442472                                /* FALLTHROUGH */
    443473                        default:
    444                                 if (Pflag)
    445                                         if (!rm_overwrite(p->fts_accpath, NULL))
     474                                if (pThis->Pflag)
     475                                        if (!rm_overwrite(pThis, p->fts_accpath, NULL))
    446476                                                continue;
    447477#ifdef KBUILD_OS_WINDOWS
     
    455485#endif
    456486
    457                                 if (rval == 0 || (fflag && errno == ENOENT)) {
    458                                         if (rval == 0 && vflag)
    459                                                 (void)printf("%s\n",
    460                                                     p->fts_path);
     487                                if (rval == 0 || (pThis->fflag && errno == ENOENT)) {
     488                                        if (rval == 0 && pThis->vflag)
     489                                                kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s\n", p->fts_path);
    461490                                        continue;
    462491                                }
     
    468497err:
    469498#endif
    470                 fprintf(stderr, "%s: %s: %s: %s " CUR_LINE() "\n", operation, argv0, p->fts_path, strerror(errno));
    471                 eval = 1;
     499                pThis->eval = errx(pThis->pCtx, 1, "%s: %s failed: %s " CUR_LINE() "\n", p->fts_path, operation, strerror(errno));
    472500        }
    473501        if (errno) {
    474                 fprintf(stderr, "%s: fts_read: %s " CUR_LINE() "\n", argv0, strerror(errno));
    475                 eval = 1;
     502                pThis->eval = errx(pThis->pCtx, 1, "fts_read: %s " CUR_LINE() "\n", strerror(errno));
    476503        }
    477504        fts_close(fts);
    478         return eval;
     505        return pThis->eval;
    479506}
    480507
    481508static int
    482 rm_file(char **argv)
     509rm_file(PRMINSTANCE pThis, char **argv)
    483510{
    484511        struct stat sb;
     
    491518        int i;
    492519        for (i = 0; argv[i]; i++) {
    493                 if (kBuildProtectionEnforce(&g_ProtData, KBUILDPROTECTIONTYPE_FULL, argv[i]))
     520                if (kBuildProtectionEnforce(&pThis->g_ProtData, KBUILDPROTECTIONTYPE_FULL, argv[i]))
    494521                        return 1;
    495522        }
     
    504531                if (lstat(f, &sb)) {
    505532#ifdef FTS_WHITEOUT
    506                         if (Wflag) {
     533                        if (pThis->Wflag) {
    507534                                sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
    508535                        } else {
     
    510537                        {
    511538#endif
    512                                 if (!fflag || errno != ENOENT) {
    513                                         fprintf(stderr, "lstat: %s: %s: %s " CUR_LINE() "\n", argv0, f, strerror(errno));
    514                                         eval = 1;
    515                                 }
     539                                if (!pThis->fflag || errno != ENOENT)
     540                                        pThis->eval = errx(pThis->pCtx, 1, "%s: lstat failed: %s " CUR_LINE() "\n",
     541                                                           f, strerror(errno));
    516542                                continue;
    517543                        }
    518544#ifdef FTS_WHITEOUT
    519                 } else if (Wflag) {
    520                         fprintf(stderr, "%s: %s: %s\n", argv0, f, strerror(EEXIST));
    521                         eval = 1;
     545                } else if (pThis->Wflag) {
     546                        errx(pThis->pCtx, "%s: %s\n", f, strerror(EEXIST));
     547                        pThis->eval = 1;
    522548                        continue;
    523549#endif
    524550                }
    525551
    526                 if (S_ISDIR(sb.st_mode) && !dflag) {
    527                         fprintf(stderr, "%s: %s: is a directory\n", argv0, f);
    528                         eval = 1;
     552                if (S_ISDIR(sb.st_mode) && !pThis->dflag) {
     553                        pThis->eval = errx(pThis->pCtx, 1, "%s: is a directory\n", f);
    529554                        continue;
    530555                }
    531                 if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
     556                if (!pThis->fflag && !S_ISWHT(sb.st_mode) && !check(pThis, f, f, &sb))
    532557                        continue;
    533558                rval = 0;
     
    546571                                operation = "rmdir";
    547572                        } else {
    548                                 if (Pflag)
    549                                         if (!rm_overwrite(f, &sb))
     573                                if (pThis->Pflag)
     574                                        if (!rm_overwrite(pThis, f, &sb))
    550575                                                continue;
    551576#ifndef KBUILD_OS_WINDOWS
     
    553578                                operation = "unlink";
    554579#else
    555                                 if (fUseNtDeleteFile) {
     580                                if (pThis->fUseNtDeleteFile) {
    556581                                        rval = birdUnlinkForcedFast(f);
    557582                                        operation = "NtDeleteFile";
     
    563588                        }
    564589                }
    565                 if (rval && (!fflag || errno != ENOENT)) {
    566                         fprintf(stderr, "%s: %s: %s: %s" CUR_LINE() "\n", operation, argv0, f, strerror(errno));
    567                         eval = 1;
    568                 }
    569                 if (vflag && rval == 0)
    570                         (void)printf("%s\n", f);
    571         }
    572         return eval;
     590                if (rval && (!pThis->fflag || errno != ENOENT))
     591                        pThis->eval = errx(pThis->pCtx, 1, "%s: %s failed: %s" CUR_LINE() "\n", f, operation, strerror(errno));
     592                if (pThis->vflag && rval == 0)
     593                        kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s\n", f);
     594        }
     595        return pThis->eval;
    573596}
    574597
     
    585608 */
    586609static int
    587 rm_overwrite(char *file, struct stat *sbp)
     610rm_overwrite(PRMINSTANCE pThis, char *file, struct stat *sbp)
    588611{
    589612        struct stat sb;
     
    595618        char *buf = NULL;
    596619        const char *operation = "lstat";
     620        int error;
    597621
    598622        fd = -1;
     
    605629                return (1);
    606630        operation = "open";
    607         if ((fd = open(file, O_WRONLY, 0)) == -1)
     631        if ((fd = open(file, O_WRONLY | KMK_OPEN_NO_INHERIT, 0)) == -1)
    608632                goto err;
    609633#ifdef HAVE_FSTATFS
     
    616640        bsize = 1024;
    617641#endif
    618         if ((buf = malloc(bsize)) == NULL)
    619                 exit(err(1, "%s: malloc", file));
     642        if ((buf = malloc(bsize)) == NULL) {
     643                err(pThis->pCtx, 1, "%s: malloc", file);
     644                close(fd);
     645                return 1;
     646        }
    620647
    621648#define PASS(byte) {                                                    \
     
    643670        operation = "fsync/close";
    644671
    645 err:    eval = 1;
     672err:    pThis->eval = 1;
     673        error = errno;
    646674        if (buf)
    647675                free(buf);
    648676        if (fd != -1)
    649677                close(fd);
    650         fprintf(stderr, "%s: %s: %s: %s" CUR_LINE() "\n", operation, argv0, file, strerror(errno));
     678        errx(pThis->pCtx, 1, "%s: %s: %s: %s" CUR_LINE() "\n", operation, pThis->pCtx->pszProgName, file, strerror(error));
    651679        return (0);
    652680}
     
    654682
    655683static int
    656 check(char *path, char *name, struct stat *sp)
     684check(PRMINSTANCE pThis, char *path, char *name, struct stat *sp)
    657685{
    658686        int ch, first;
     
    660688
    661689        /* Check -i first. */
    662         if (iflag)
    663                 (void)fprintf(stderr, "remove %s? ", path);
     690        if (pThis->iflag)
     691                (void)fprintf(stderr, "%s: remove %s? ", pThis->pCtx->pszProgName, path);
    664692        else {
    665693                /*
     
    672700                 * barf later.
    673701                 */
    674                 if (!stdin_ok || S_ISLNK(sp->st_mode) || Pflag ||
     702                if (!pThis->stdin_ok || S_ISLNK(sp->st_mode) || pThis->Pflag ||
    675703                    (!access(name, W_OK) &&
    676704#ifdef SF_APPEND
    677705                    !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
    678                     (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid))
     706                    (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !pThis->uid))
    679707#else
    680708                    1)
     
    684712                bsd_strmode(sp->st_mode, modep);
    685713#if defined(SF_APPEND) && K_OS != K_OS_GNU_KFBSD && K_OS != K_OS_GNU_HURD
    686                 if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
    687                         exit(err(1, "fflagstostr"));
     714                if ((flagsp = fflagstostr(sp->st_flags)) == NULL) {
     715                        err(pThis->pCtx, 1, "fflagstostr");
     716                        flagsp = "<bad-fflagstostr>";
     717                }
    688718                (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
    689719                              modep + 1, modep[9] == ' ' ? "" : " ",
     
    710740#define ISDOT(a)        ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
    711741static void
    712 checkdot(char **argv)
     742checkdot(PRMINSTANCE pThis, char **argv)
    713743{
    714744        char *p, **save, **t;
     
    737767                if (ISDOT(p)) {
    738768                        if (!complained++)
    739                                 fprintf(stderr, "%s: \".\" and \"..\" may not be removed\n", argv0);
    740                         eval = 1;
     769                                warnx(pThis->pCtx, "\".\" and \"..\" may not be removed\n");
     770                        pThis->eval = 1;
    741771                        for (save = t; (t[0] = t[1]) != NULL; ++t)
    742772                                continue;
     
    748778
    749779static int
    750 usage(FILE *pf)
    751 {
    752         fprintf(pf,
    753                 "usage: %s [options] file ...\n"
    754                 "   or: %s --help\n"
    755                 "   or: %s --version\n"
    756                 "\n"
    757                 "Options:\n"
    758                 "   -f\n"
    759                 "       Attempt to remove files without prompting, regardless of the file\n"
    760                 "       permission. Ignore non-existing files. Overrides previous -i's.\n"
    761                 "   -i\n"
    762                 "       Prompt for each file. Always.\n"
    763                 "   -d\n"
    764                 "       Attempt to remove directories as well as other kinds of files.\n"
    765                 "   -P\n"
    766                 "       Overwrite regular files before deleting; three passes: ff,0,ff\n"
    767                 "   -R\n"
    768                 "       Attempt to remove the file hierachy rooted in each file argument.\n"
    769                 "       This option implies -d and file protection.\n"
    770                 "   -v\n"
    771                 "       Be verbose, show files as they are removed.\n"
    772                 "   -W\n"
    773                 "       Undelete without files.\n"
    774                 "   --disable-protection\n"
    775                 "       Will disable the protection file protection applied with -R.\n"
    776                 "   --enable-protection\n"
    777                 "       Will enable the protection file protection applied with -R.\n"
    778                 "   --enable-full-protection\n"
    779                 "       Will enable the protection file protection for all operations.\n"
    780                 "   --disable-full-protection\n"
    781                 "       Will disable the protection file protection for all operations.\n"
    782                 "   --protection-depth\n"
    783                 "       Number or path indicating the file protection depth. Default: %d\n"
    784                 "\n"
    785                 "Environment:\n"
    786                 "    KMK_RM_DISABLE_PROTECTION\n"
    787                 "       Same as --disable-protection. Overrides command line.\n"
    788                 "    KMK_RM_ENABLE_PROTECTION\n"
    789                 "       Same as --enable-protection. Overrides everyone else.\n"
    790                 "    KMK_RM_ENABLE_FULL_PROTECTION\n"
    791                 "       Same as --enable-full-protection. Overrides everyone else.\n"
    792                 "    KMK_RM_DISABLE_FULL_PROTECTION\n"
    793                 "       Same as --disable-full-protection. Overrides command line.\n"
    794                 "    KMK_RM_PROTECTION_DEPTH\n"
    795                 "       Same as --protection-depth. Overrides command line.\n"
    796                 "\n"
    797                 "The file protection of the top %d layers of the file hierarchy is there\n"
    798                 "to try prevent makefiles from doing bad things to your system. This\n"
    799                 "protection is not bulletproof, but should help prevent you from shooting\n"
    800                 "yourself in the foot.\n"
    801                 ,
    802                 g_progname, g_progname, g_progname,
    803                 kBuildProtectionDefaultDepth(), kBuildProtectionDefaultDepth());
     780usage(PKMKBUILTINCTX pCtx, int fIsErr)
     781{
     782        kmk_builtin_ctx_printf(pCtx, fIsErr,
     783                               "usage: %s [options] file ...\n"
     784                               "   or: %s --help\n"
     785                               "   or: %s --version\n"
     786                               "\n"
     787                               "Options:\n"
     788                               "   -f\n"
     789                               "       Attempt to remove files without prompting, regardless of the file\n"
     790                               "       permission. Ignore non-existing files. Overrides previous -i's.\n"
     791                               "   -i\n"
     792                               "       Prompt for each file. Always.\n"
     793                               "   -d\n"
     794                               "       Attempt to remove directories as well as other kinds of files.\n"
     795                               "   -P\n"
     796                               "       Overwrite regular files before deleting; three passes: ff,0,ff\n"
     797                               "   -R\n"
     798                               "       Attempt to remove the file hierachy rooted in each file argument.\n"
     799                               "       This option implies -d and file protection.\n"
     800                               "   -v\n"
     801                               "       Be verbose, show files as they are removed.\n"
     802                               "   -W\n"
     803                               "       Undelete without files.\n"
     804                               "   --disable-protection\n"
     805                               "       Will disable the protection file protection applied with -R.\n"
     806                               "   --enable-protection\n"
     807                               "       Will enable the protection file protection applied with -R.\n"
     808                               "   --enable-full-protection\n"
     809                               "       Will enable the protection file protection for all operations.\n"
     810                               "   --disable-full-protection\n"
     811                               "       Will disable the protection file protection for all operations.\n"
     812                               "   --protection-depth\n"
     813                               "       Number or path indicating the file protection depth. Default: %d\n"
     814                               "\n"
     815                               "Environment:\n"
     816                               "    KMK_RM_DISABLE_PROTECTION\n"
     817                               "       Same as --disable-protection. Overrides command line.\n"
     818                               "    KMK_RM_ENABLE_PROTECTION\n"
     819                               "       Same as --enable-protection. Overrides everyone else.\n"
     820                               "    KMK_RM_ENABLE_FULL_PROTECTION\n"
     821                               "       Same as --enable-full-protection. Overrides everyone else.\n"
     822                               "    KMK_RM_DISABLE_FULL_PROTECTION\n"
     823                               "       Same as --disable-full-protection. Overrides command line.\n"
     824                               "    KMK_RM_PROTECTION_DEPTH\n"
     825                               "       Same as --protection-depth. Overrides command line.\n"
     826                               "\n"
     827                               "The file protection of the top %d layers of the file hierarchy is there\n"
     828                               "to try prevent makefiles from doing bad things to your system. This\n"
     829                               "protection is not bulletproof, but should help prevent you from shooting\n"
     830                               "yourself in the foot.\n"
     831                               ,
     832                               pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName,
     833                               kBuildProtectionDefaultDepth(), kBuildProtectionDefaultDepth());
    804834        return EX_USAGE;
    805835}
     836
Note: See TracChangeset for help on using the changeset viewer.