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

    r2912 r3192  
    4444#endif
    4545
     46
     47/*********************************************************************************************************************************
     48*   Header Files                                                                                                                 *
     49*********************************************************************************************************************************/
    4650#include "config.h"
    4751#include "err.h"
     
    6064# include "mscfakes.h"
    6165#endif
    62 #if defined(KMK) && defined(KBUILD_OS_WINDOWS)
    63 extern int dir_cache_deleted_directory(const char *pszDir);
    64 #endif
    65 
    66 static int rm_path(char *);
    67 static int usage(FILE *);
    68 
    69 static int pflag;
    70 static int vflag;
    71 static int ignore_fail_on_non_empty;
    72 static int ignore_fail_on_not_exist;
    73 
     66
     67
     68/*********************************************************************************************************************************
     69*   Structures and Typedefs                                                                                                      *
     70*********************************************************************************************************************************/
     71typedef struct RMDIRINSTANCE
     72{
     73    PKMKBUILTINCTX pCtx;
     74    int pflag;
     75    int vflag;
     76    int ignore_fail_on_non_empty;
     77    int ignore_fail_on_not_exist;
     78} RMDIRINSTANCE;
     79typedef RMDIRINSTANCE *PRMDIRINSTANCE;
     80
     81
     82/*********************************************************************************************************************************
     83*   Global Variables                                                                                                             *
     84*********************************************************************************************************************************/
    7485static struct option long_options[] =
    7586{
     
    8495
    8596
     97
     98/*********************************************************************************************************************************
     99*   Internal Functions                                                                                                           *
     100*********************************************************************************************************************************/
     101#if !defined(KMK_BUILTIN_STANDALONE) && defined(KBUILD_OS_WINDOWS)
     102extern int dir_cache_deleted_directory(const char *pszDir);
     103#endif
     104static int rm_path(PRMDIRINSTANCE, char *);
     105static int usage(PKMKBUILTINCTX, int);
     106
     107
    86108int
    87 kmk_builtin_rmdir(int argc, char *argv[], char **envp)
    88 {
     109kmk_builtin_rmdir(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
     110{
     111        RMDIRINSTANCE This;
    89112        int ch, errors;
    90113
    91         /* reinitialize globals */
    92         ignore_fail_on_not_exist = ignore_fail_on_non_empty = vflag = pflag = 0;
     114        /* Initialize global instance. */
     115        This.pCtx = pCtx;
     116        This.ignore_fail_on_not_exist = 0;
     117        This.ignore_fail_on_non_empty = 0;
     118        This.vflag = 0;
     119        This.pflag = 0;
    93120
    94121        /* kmk: reset getopt and set progname */
    95         g_progname = argv[0];
    96122        opterr = 1;
    97123        optarg = NULL;
     
    101127                switch(ch) {
    102128                case 'p':
    103                         pflag = 1;
     129                        This.pflag = 1;
    104130                        break;
    105131                case 'v':
    106                         vflag = 1;
     132                        This.vflag = 1;
    107133                        break;
    108134                case 260:
    109                         ignore_fail_on_non_empty = 1;
     135                        This.ignore_fail_on_non_empty = 1;
    110136                        break;
    111137                case 261:
    112                         ignore_fail_on_not_exist = 1;
     138                        This.ignore_fail_on_not_exist = 1;
    113139                        break;
    114140                case 262:
    115                         usage(stdout);
     141                        usage(pCtx, 0);
    116142                        return 0;
    117143                case 263:
     
    119145                case '?':
    120146                default:
    121                         return usage(stderr);
     147                        return usage(pCtx, 1);
    122148                }
    123149        argc -= optind;
     
    129155        for (errors = 0; *argv; argv++) {
    130156                if (rmdir(*argv) < 0) {
    131                         if (    (!ignore_fail_on_non_empty || (errno != ENOTEMPTY && errno != EPERM && errno != EACCES && errno != EINVAL && errno != EEXIST))
    132                             &&  (!ignore_fail_on_not_exist || errno != ENOENT)) {
    133                                 warn("rmdir: %s", *argv);
     157                        if (    (   !This.ignore_fail_on_non_empty
     158                                 || (errno != ENOTEMPTY && errno != EPERM && errno != EACCES && errno != EINVAL && errno != EEXIST))
     159                            &&  (   !This.ignore_fail_on_not_exist
     160                                 || errno != ENOENT)) {
     161                                warn(pCtx, "rmdir: %s", *argv);
    134162                                errors = 1;
    135163                                continue;
    136164                        }
    137                         if (!ignore_fail_on_not_exist || errno != ENOENT)
     165                        if (!This.ignore_fail_on_not_exist || errno != ENOENT)
    138166                                continue;
    139167                        /* (only ignored doesn't exist errors fall thru) */
    140168                } else {
    141 #if defined(KMK) && defined(KBUILD_OS_WINDOWS)
     169#if !defined(KMK_BUILTIN_STANDALONE) && defined(KBUILD_OS_WINDOWS)
    142170                        dir_cache_deleted_directory(*argv);
    143171#endif
    144                         if (vflag) {
    145                                 printf("%s\n", *argv);
    146                         }
    147                 }
    148                 if (pflag)
    149                         errors |= rm_path(*argv);
     172                        if (This.vflag)
     173                                kmk_builtin_ctx_printf(pCtx, 0, "%s\n", *argv);
     174                }
     175                if (This.pflag)
     176                        errors |= rm_path(&This, *argv);
    150177        }
    151178
     
    153180}
    154181
     182#ifdef KMK_BUILTIN_STANDALONE
     183int main(int argc, char **argv, char **envp)
     184{
     185    KMKBUILTINCTX Ctx = { "kmk_rmdir", NULL };
     186    return kmk_builtin_rmdir(argc, argv, envp, &Ctx);
     187}
     188#endif
     189
    155190static int
    156 rm_path(char *path)
     191rm_path(PRMDIRINSTANCE pThis, char *path)
    157192{
    158193        char *p;
     
    186221
    187222                if (rmdir(path) < 0) {
    188                         if (   ignore_fail_on_non_empty
     223                        if (   pThis->ignore_fail_on_non_empty
    189224                            && (   errno == ENOTEMPTY || errno == EPERM || errno == EACCES || errno == EINVAL || errno == EEXIST))
    190225                                break;
    191                         if (!ignore_fail_on_not_exist || errno != ENOENT) {
    192                                 warn("rmdir: %s", path);
     226                        if (!pThis->ignore_fail_on_not_exist || errno != ENOENT) {
     227                                warn(pThis->pCtx, "rmdir: %s", path);
    193228                                return (1);
    194229                        }
     
    199234                }
    200235#endif
    201                 if (vflag)
    202                         printf("%s\n", path);
     236                if (pThis->vflag)
     237                        kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s\n", path);
    203238        }
    204239
     
    207242
    208243static int
    209 usage(FILE *pf)
    210 {
    211         (void)fprintf(pf, "usage: %s [-pv --ignore-fail-on-non-empty --ignore-fail-on-not-exist] directory ...\n"
    212                           "   or: %s --help\n"
    213                           "   or: %s --version\n",
    214                       g_progname, g_progname, g_progname);
     244usage(PKMKBUILTINCTX pCtx, int fIsErr)
     245{
     246        kmk_builtin_ctx_printf(pCtx, fIsErr,
     247                               "usage: %s [-pv --ignore-fail-on-non-empty --ignore-fail-on-not-exist] directory ...\n"
     248                               "   or: %s --help\n"
     249                               "   or: %s --version\n",
     250                               pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
    215251        return 1;
    216252}
     253
Note: See TracChangeset for help on using the changeset viewer.