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

kmk_ln, kmk_mkdir, kmk_mv, kmk_printf: changed to use getopt_r and got rid of remaining static buffers.

Location:
trunk/src/kmk/kmkbuiltin
Files:
5 edited

Legend:

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

    r3214 r3215  
    122122# define IS_SLASH(ch)   ((ch) == '/')
    123123#endif
     124
    124125
    125126/*********************************************************************************************************************************
  • trunk/src/kmk/kmkbuiltin/ln.c

    r3192 r3215  
    4242#endif /* no $id */
    4343
     44#define FAKES_NO_GETOPT_H /* bird */
    4445#include "config.h"
    4546#ifndef _MSC_VER
     
    5556#include <string.h>
    5657#include <unistd.h>
    57 #include "getopt.h"
     58#include "getopt_r.h"
    5859#ifdef _MSC_VER
    5960# include "mscfakes.h"
     
    9394{
    9495        LNINSTANCE This;
     96        struct getopt_state_r gos;
    9597        struct stat sb;
    9698        char *sourcedir;
     
    107109        This.linkf = NULL;
    108110
    109         /* kmk: reset getopt() and set program name. */
    110         opterr = 1;
    111         optarg = NULL;
    112         optopt = 0;
    113         optind = 0; /* init */
    114 
    115         while ((ch = getopt_long(argc, argv, "fhinsv", long_options, NULL)) != -1)
     111        getopt_initialize_r(&gos, argc, argv, "fhinsv", long_options, envp, pCtx);
     112        while ((ch = getopt_long_r(&gos, NULL)) != -1)
    116113                switch (ch) {
    117114                case 'f':
     
    143140                }
    144141
    145         argv += optind;
    146         argc -= optind;
     142        argv += gos.optind;
     143        argc -= gos.optind;
    147144
    148145        This.linkf = This.sflag ? symlink : link;
  • trunk/src/kmk/kmkbuiltin/mkdir.c

    r3192 r3215  
    4242#endif
    4343
     44#define FAKES_NO_GETOPT_H /* bird */
    4445#include "config.h"
    4546#include <sys/types.h>
     
    6162# include <alloca.h>
    6263#endif
    63 #include "getopt.h"
     64#include "getopt_r.h"
    6465#ifdef __HAIKU__
    6566# include "haikufakes.h"
     
    9091kmk_builtin_mkdir(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
    9192{
     93        struct getopt_state_r gos;
    9294        int ch, exitval, success, pflag, vflag;
    9395        mode_t omode, *set = (mode_t *)NULL;
     
    9799        mode = NULL;
    98100
    99         /* kmk: reset getopt and set progname */
    100         opterr = 1;
    101         optarg = NULL;
    102         optopt = 0;
    103         optind = 0; /* init */
    104         while ((ch = getopt_long(argc, argv, "m:pv", long_options, NULL)) != -1)
     101        getopt_initialize_r(&gos, argc, argv, "m:pv", long_options, envp, pCtx);
     102        while ((ch = getopt_long_r(&gos, NULL)) != -1)
    105103                switch(ch) {
    106104                case 'm':
    107                         mode = optarg;
     105                        mode = gos.optarg;
    108106                        break;
    109107                case 'p':
     
    123121                }
    124122
    125         argc -= optind;
    126         argv += optind;
     123        argc -= gos.optind;
     124        argv += gos.optind;
    127125        if (argv[0] == NULL)
    128126                return usage(pCtx, 1);
  • trunk/src/kmk/kmkbuiltin/mv.c

    r3192 r3215  
    5151*   Header Files                                                                                                                 *
    5252*********************************************************************************************************************************/
     53#define FAKES_NO_GETOPT_H /* bird */
    5354#include "config.h"
    5455#include <sys/types.h>
     
    8081#endif
    8182#include <unistd.h>
    82 #include "getopt.h"
     83#include "getopt_r.h"
    8384#ifdef __sun__
    8485# include "solfakes.h"
     
    121122
    122123static int      do_move(PMVINSTANCE, char *, char *);
    123 #ifdef CROSS_DEVICE_MOVE
     124#if 0 // def CROSS_DEVICE_MOVE
    124125static int      fastcopy(char *, char *, struct stat *);
    125126static int      copy(char *, char *);
     
    128129
    129130
    130 #if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(__DragonFly__) && !defined(__OpenBSD__)
    131 # ifdef __OS2__
    132 static
    133 # endif
    134 const char *user_from_uid(uid_t id, int x)
    135 {
    136         static char s_buf[64];
    137         sprintf(s_buf, "%ld", (long int)id);
    138         (void)x;
    139         return s_buf;
    140 }
    141 # ifdef __OS2__
    142 static
    143 # endif
    144 const char *group_from_gid(gid_t id, int x)
    145 {
    146         static char s_buf[64];
    147         sprintf(s_buf, "%ld", (long int)id);
    148         (void)x;
    149         return s_buf;
    150 }
    151 #endif /* 'not in libc' */
    152 
    153 
    154131int
    155132kmk_builtin_mv(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
    156133{
    157134        MVINSTANCE This;
     135        struct getopt_state_r gos;
    158136        size_t baselen, len;
    159137        int rval;
     
    170148        This.vflg = 0;
    171149
    172         /* kmk: reset getopt and set progname */
    173         opterr = 1;
    174         optarg = NULL;
    175         optopt = 0;
    176         optind = 0; /* init */
    177 
    178         while ((ch = getopt_long(argc, argv, "finv", long_options, NULL)) != -1)
     150        getopt_initialize_r(&gos, argc, argv, "finv", long_options, envp, pCtx);
     151        while ((ch = getopt_long_r(&gos, NULL)) != -1)
    179152                switch (ch) {
    180153                case 'i':
     
    201174                        return usage(pCtx, 1);
    202175                }
    203         argc -= optind;
    204         argv += optind;
     176        argc -= gos.optind;
     177        argv += gos.optind;
    205178
    206179        if (argc < 2)
     
    301274                } else if (access(to, W_OK) && !stat(to, &sb)) {
    302275                        bsd_strmode(sb.st_mode, modep);
     276#if 0 /* probably not thread safe, also BSDism. */
    303277                        (void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
    304278                            modep + 1, modep[9] == ' ' ? "" : " ",
    305279                            user_from_uid((unsigned long)sb.st_uid, 0),
    306280                            group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO);
     281#else
     282                        (void)fprintf(stderr, "override %s%s%ul/%ul for %s? %s",
     283                                      modep + 1, modep[9] == ' ' ? "" : " ",
     284                                      (unsigned long)sb.st_uid, (unsigned long)sb.st_gid,
     285                                      to, YESNO);
     286#endif
    307287                        ask = 1;
    308288                }
     
    335315
    336316        if (errno == EXDEV) {
    337 #ifndef CROSS_DEVICE_MOVE
     317#if 1 //ndef CROSS_DEVICE_MOVE
    338318                warnx(pThis->pCtx, "cannot move `%s' to a different device: `%s'", from, to);
    339319                return (1);
     
    368348        }
    369349
    370 #ifdef CROSS_DEVICE_MOVE
     350#if 0//def CROSS_DEVICE_MOVE
    371351        /*
    372352         * If rename fails because we're trying to cross devices, and
     
    383363}
    384364
    385 #ifdef CROSS_DEVICE_MOVE
     365#if 0 //def CROSS_DEVICE_MOVE - using static buffers and fork.
    386366int
    387367static fastcopy(char *from, char *to, struct stat *sbp)
  • trunk/src/kmk/kmkbuiltin/printf.c

    r3192 r3215  
    5050*   Header Files                                                                                                                 *
    5151*********************************************************************************************************************************/
     52#define FAKES_NO_GETOPT_H /* bird */
    5253#if !defined(KMK_BUILTIN_STANDALONE) && !defined(BUILTIN) && !defined(SHELL)
    5354# include "../makeint.h"
     
    7071#include <string.h>
    7172#include <unistd.h>
    72 #include "getopt.h"
     73#include "getopt_r.h"
    7374#ifdef __sun__
    7475# include "solfakes.h"
     
    139140{
    140141    PKMKBUILTINCTX pCtx;
     142    /* former globals */
    141143    size_t b_length;
    142144    char *b_fmt;
     
    146148    char *g_o;
    147149#endif
     150    /* former function level statics in common_printf(); both need freeing. */
     151    char *a, *t;
     152
     153    /* former function level statics in conv_expand(); needs freeing. */
     154    char *conv_str;
     155
    148156    /* Buffer the output because windows doesn't do line buffering of stdout. */
    149157    size_t g_cchBuf;
     
    167175*   Internal Functions                                                                                                           *
    168176*********************************************************************************************************************************/
    169 static int       common_printf(PPRINTFINSTANCE pThis, int argc, char *argv[]);
     177static int       common_printf(PPRINTFINSTANCE pThis, char *argv[], PKMKBUILTINCTX pCtx);
     178static int       common_printf_inner(PPRINTFINSTANCE pThis, char *argv[]);
    170179static void      conv_escape_str(PPRINTFINSTANCE, char *, void (*)(PPRINTFINSTANCE, int));
    171180static char     *conv_escape(PPRINTFINSTANCE, char *, char *);
    172 static char     *conv_expand(const char *);
     181static const char *conv_expand(PPRINTFINSTANCE, const char *);
    173182static int       getchr(PPRINTFINSTANCE);
    174183static double    getdouble(PPRINTFINSTANCE);
     
    191200int kmk_builtin_printf(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
    192201{
     202        PRINTFINSTANCE This;
     203        struct getopt_state_r gos;
    193204        int ch;
    194         PRINTFINSTANCE This;
    195         This.pCtx = pCtx;
    196         This.b_length = 0;
    197         This.b_fmt = NULL;
    198         This.rval = 0;
    199         This.gargv = NULL;
    200 #ifndef KMK_BUILTIN_STANDALONE
    201         This.g_o = NULL;
    202 #endif
    203         This.g_cchBuf = 0;
    204 
    205         /* kmk: reset getopt, set progname and reset buffer. */
    206         opterr = 1;
    207         optarg = NULL;
    208         optopt = 0;
    209         optind = 0; /* init */
    210 
    211         while ((ch = getopt_long(argc, argv, "", long_options, NULL)) != -1) {
     205
     206        getopt_initialize_r(&gos, argc, argv, "", long_options, envp, pCtx);
     207        while ((ch = getopt_long_r(&gos, NULL)) != -1) {
    212208                switch (ch) {
    213209                case 261:
     
    221217                }
    222218        }
    223         argc -= optind;
    224         argv += optind;
     219        argc -= gos.optind;
     220        argv += gos.optind;
    225221
    226222        if (argc < 1)
    227223                return usage(pCtx, 1);
    228         return common_printf(&This, argc, argv);
     224
     225#ifndef KMK_BUILTIN_STANDALONE
     226        This.g_o = NULL;
     227#endif
     228        return common_printf(&This, argv, pCtx);
    229229}
    230230
     
    249249            fatal(NILF, strlen(funcname) + INTSTR_LENGTH, _("$(%s): no format string\n"), funcname);
    250250
    251         This.pCtx = NULL;
    252         This.b_length = 0;
    253         This.b_fmt = NULL;
    254         This.rval = 0;
    255         This.gargv = NULL;
    256         This.g_cchBuf = 0;
    257251        This.g_o = o;
    258 
    259         rc = common_printf(&This, argc, argv);
     252        rc = common_printf(&This, argv, NULL);
    260253        o = This.g_o;
    261254
     
    266259#endif /* KMK_BUILTIN_STANDALONE */
    267260
    268 static int common_printf(PPRINTFINSTANCE pThis, int argc, char *argv[])
     261static int common_printf(PPRINTFINSTANCE pThis, char *argv[], PKMKBUILTINCTX pCtx)
     262{
     263        int rc;
     264
     265        /* Init all but g_o. */
     266        pThis->pCtx = pCtx;
     267        pThis->b_length = 0;
     268        pThis->b_fmt = NULL;
     269        pThis->rval = 0;
     270        pThis->gargv = NULL;
     271        pThis->g_cchBuf = 0;
     272        pThis->a = NULL;
     273        pThis->t = NULL;
     274        pThis->conv_str = NULL;
     275
     276        rc = common_printf_inner(pThis, argv);
     277
     278        /* Cleanup allocations. */
     279        if (pThis->a) {
     280                free(pThis->a);
     281                pThis->a = NULL;
     282        }
     283        if (pThis->t) {
     284                free(pThis->t);
     285                pThis->t = NULL;
     286        }
     287        if (pThis->conv_str) {
     288                free(pThis->conv_str);
     289                pThis->conv_str = NULL;
     290        }
     291        return rc;
     292}
     293
     294static int common_printf_inner(PPRINTFINSTANCE pThis, char *argv[])
    269295{
    270296        char *fmt, *start;
     
    275301        char longbuf[64];
    276302
    277         /* kmk: reinitialize globals */
    278         pThis->b_length = 0;
    279         pThis->b_fmt = NULL;
    280         pThis->rval = 0;
    281         pThis->gargv = NULL;
    282         pThis->g_cchBuf = 0;
    283303        format = *argv;
    284304        pThis->gargv = ++argv;
     
    338358
    339359                        case 'B': {
    340                                 const char *p = conv_expand(getstr(pThis));
     360                                const char *p = conv_expand(pThis, getstr(pThis));
    341361                                *fmt = 's';
    342362                                PF(start, p);
     
    347367                                 * but the string we generate might have
    348368                                 * embedded nulls. */
    349                                 static char *a, *t;
    350369                                char *cp = getstr(pThis);
    351370                                /* Free on entry in case shell longjumped out */
    352                                 if (a != NULL)
    353                                         free(a);
    354                                 a = NULL;
    355                                 if (t != NULL)
    356                                         free(t);
    357                                 t = NULL;
     371                                if (pThis->a != NULL) {
     372                                        free(pThis->a);
     373                                        pThis->a = NULL;
     374                                }
     375                                if (pThis->t != NULL) {
     376                                        free(pThis->t);
     377                                        pThis->t = NULL;
     378                                }
    358379                                /* Count number of bytes we want to output */
    359380                                pThis->b_length = 0;
    360381                                conv_escape_str(pThis, cp, b_count);
    361                                 t = malloc(pThis->b_length + 1);
    362                                 if (t == NULL)
     382                                pThis->t = malloc(pThis->b_length + 1);
     383                                if (pThis->t == NULL)
    363384                                        break;
    364                                 memset(t, 'x', pThis->b_length);
    365                                 t[pThis->b_length] = 0;
     385                                memset(pThis->t, 'x', pThis->b_length);
     386                                pThis->t[pThis->b_length] = 0;
    366387                                /* Get printf to calculate the lengths */
    367388                                *fmt = 's';
    368                                 APF(&a, start, t);
    369                                 pThis->b_fmt = a;
     389                                APF(&pThis->a, start, pThis->t);
     390                                pThis->b_fmt = pThis->a;
    370391                                /* Output leading spaces and data bytes */
    371392                                conv_escape_str(pThis, cp, b_output);
     
    710731/* expand a string so that everything is printable */
    711732
    712 static char *
    713 conv_expand(const char *str)
    714 {
    715         static char *conv_str;
    716         static char no_memory[] = "<no memory>";
     733static const char *
     734conv_expand(PPRINTFINSTANCE pThis, const char *str)
     735{
     736        static const char no_memory[] = "<no memory>";
    717737        char *cp;
    718738        int ch;
    719739
    720         if (conv_str)
    721                 free(conv_str);
     740        if (pThis->conv_str)
     741                free(pThis->conv_str);
    722742        /* get a buffer that is definitely large enough.... */
    723         conv_str = malloc(4 * strlen(str) + 1);
    724         if (!conv_str)
     743        pThis->conv_str = cp = malloc(4 * strlen(str) + 1);
     744        if (!cp)
    725745                return no_memory;
    726         cp = conv_str;
    727746
    728747        while ((ch = *(const unsigned char *)str++) != '\0') {
     
    771790
    772791        *cp = 0;
    773         return conv_str;
     792        return pThis->conv_str;
    774793}
    775794
Note: See TracChangeset for help on using the changeset viewer.