Changeset 3220 for trunk/src/kmk


Ignore:
Timestamp:
Mar 31, 2018, 12:34:57 AM (7 years ago)
Author:
bird
Message:

kmk_chmod: converted to getopt_r.

File:
1 edited

Legend:

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

    r3192 r3220  
    4242/*__FBSDID("$FreeBSD: src/bin/chmod/chmod.c,v 1.33 2005/01/10 08:39:20 imp Exp $");*/
    4343
     44/*********************************************************************************************************************************
     45*   Header Files                                                                                                                 *
     46*********************************************************************************************************************************/
     47#define FAKES_NO_GETOPT_H /* bird */
    4448#include "config.h"
    4549#include <sys/types.h>
     
    6468# include "haikufakes.h"
    6569#endif
    66 #include "getopt.h"
     70#include "getopt_r.h"
    6771#include "kmkbuiltin.h"
    6872
    69 extern void * bsd_setmode(const char *p);
    70 extern mode_t bsd_getmode(const void *bbox, mode_t omode);
    71 extern void bsd_strmode(mode_t mode, char *p);
    72 
    73 #if (defined(__APPLE__) && !defined(_DARWIN_FEATURE_UNIX_CONFORMANCE)) || defined(__OpenBSD__)
    74 extern int lchmod(const char *, mode_t);
    75 #endif
    76 
    77 static int usage(PKMKBUILTINCTX pCtx, int is_err);
    78 
     73
     74/*********************************************************************************************************************************
     75*   Global Variables                                                                                                             *
     76*********************************************************************************************************************************/
    7977static struct option long_options[] =
    8078{
     
    8583
    8684
     85/*********************************************************************************************************************************
     86*   Internal Functions                                                                                                           *
     87*********************************************************************************************************************************/
     88extern void * bsd_setmode(const char *p);
     89extern mode_t bsd_getmode(const void *bbox, mode_t omode);
     90extern void bsd_strmode(mode_t mode, char *p);
     91
     92#if (defined(__APPLE__) && !defined(_DARWIN_FEATURE_UNIX_CONFORMANCE)) || defined(__OpenBSD__)
     93extern int lchmod(const char *, mode_t);
     94#endif
     95
     96static int usage(PKMKBUILTINCTX pCtx, int is_err);
     97
     98
    8799int
    88100kmk_builtin_chmod(int argc, char *argv[], char **envp, PKMKBUILTINCTX pCtx)
    89101{
     102        struct getopt_state_r gos;
    90103        FTS *ftsp;
    91104        FTSENT *p;
     
    97110        int (*change_mode)(const char *, mode_t);
    98111
    99         /* kmk: reset getopt and set progname */
    100         opterr = 1;
    101         optarg = NULL;
    102         optopt = 0;
    103         optind = 0; /* init */
    104 
    105112        set = NULL;
    106113        Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
    107         while ((ch = getopt_long(argc, argv, "HLPRXfghorstuvwx", long_options, NULL)) != -1)
     114
     115        getopt_initialize_r(&gos, argc, argv, "HLPRXfghorstuvwx", long_options, envp, pCtx);
     116        while ((ch = getopt_long_r(&gos, NULL)) != -1)
    108117                switch (ch) {
    109118                case 'H':
     
    143152                case 'g': case 'o': case 'r': case 's':
    144153                case 't': case 'u': case 'w': case 'X': case 'x':
    145                         if (argv[optind - 1][0] == '-' &&
    146                             argv[optind - 1][1] == ch &&
    147                             argv[optind - 1][2] == '\0')
    148                                 --optind;
     154                        if (argv[gos.optind - 1][0] == '-' &&
     155                            argv[gos.optind - 1][1] == ch &&
     156                            argv[gos.optind - 1][2] == '\0')
     157                                --gos.optind;
    149158                        goto done;
    150159                case 'v':
     
    160169                        return usage(pCtx, 1);
    161170                }
    162 done:   argv += optind;
    163         argc -= optind;
     171done:   argv += gos.optind;
     172        argc -= gos.optind;
    164173
    165174        if (argc < 2)
     
    179188        } else
    180189                fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
     190#ifndef KMK_BUILTIN_STANDALONE
     191        fts_options |= FTS_NOCHDIR; /* Don't change the CWD while inside kmk. */
     192#endif
    181193
    182194        if (hflag)
Note: See TracChangeset for help on using the changeset viewer.