Changeset 3215 for trunk/src/kmk/kmkbuiltin
- Timestamp:
- Mar 31, 2018, 12:01:55 AM (7 years ago)
- Location:
- trunk/src/kmk/kmkbuiltin
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/kmkbuiltin/install.c
r3214 r3215 122 122 # define IS_SLASH(ch) ((ch) == '/') 123 123 #endif 124 124 125 125 126 /********************************************************************************************************************************* -
trunk/src/kmk/kmkbuiltin/ln.c
r3192 r3215 42 42 #endif /* no $id */ 43 43 44 #define FAKES_NO_GETOPT_H /* bird */ 44 45 #include "config.h" 45 46 #ifndef _MSC_VER … … 55 56 #include <string.h> 56 57 #include <unistd.h> 57 #include "getopt .h"58 #include "getopt_r.h" 58 59 #ifdef _MSC_VER 59 60 # include "mscfakes.h" … … 93 94 { 94 95 LNINSTANCE This; 96 struct getopt_state_r gos; 95 97 struct stat sb; 96 98 char *sourcedir; … … 107 109 This.linkf = NULL; 108 110 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) 116 113 switch (ch) { 117 114 case 'f': … … 143 140 } 144 141 145 argv += optind;146 argc -= optind;142 argv += gos.optind; 143 argc -= gos.optind; 147 144 148 145 This.linkf = This.sflag ? symlink : link; -
trunk/src/kmk/kmkbuiltin/mkdir.c
r3192 r3215 42 42 #endif 43 43 44 #define FAKES_NO_GETOPT_H /* bird */ 44 45 #include "config.h" 45 46 #include <sys/types.h> … … 61 62 # include <alloca.h> 62 63 #endif 63 #include "getopt .h"64 #include "getopt_r.h" 64 65 #ifdef __HAIKU__ 65 66 # include "haikufakes.h" … … 90 91 kmk_builtin_mkdir(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx) 91 92 { 93 struct getopt_state_r gos; 92 94 int ch, exitval, success, pflag, vflag; 93 95 mode_t omode, *set = (mode_t *)NULL; … … 97 99 mode = NULL; 98 100 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) 105 103 switch(ch) { 106 104 case 'm': 107 mode = optarg;105 mode = gos.optarg; 108 106 break; 109 107 case 'p': … … 123 121 } 124 122 125 argc -= optind;126 argv += optind;123 argc -= gos.optind; 124 argv += gos.optind; 127 125 if (argv[0] == NULL) 128 126 return usage(pCtx, 1); -
trunk/src/kmk/kmkbuiltin/mv.c
r3192 r3215 51 51 * Header Files * 52 52 *********************************************************************************************************************************/ 53 #define FAKES_NO_GETOPT_H /* bird */ 53 54 #include "config.h" 54 55 #include <sys/types.h> … … 80 81 #endif 81 82 #include <unistd.h> 82 #include "getopt .h"83 #include "getopt_r.h" 83 84 #ifdef __sun__ 84 85 # include "solfakes.h" … … 121 122 122 123 static int do_move(PMVINSTANCE, char *, char *); 123 #if def CROSS_DEVICE_MOVE124 #if 0 // def CROSS_DEVICE_MOVE 124 125 static int fastcopy(char *, char *, struct stat *); 125 126 static int copy(char *, char *); … … 128 129 129 130 130 #if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(__DragonFly__) && !defined(__OpenBSD__)131 # ifdef __OS2__132 static133 # endif134 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 static143 # endif144 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 154 131 int 155 132 kmk_builtin_mv(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx) 156 133 { 157 134 MVINSTANCE This; 135 struct getopt_state_r gos; 158 136 size_t baselen, len; 159 137 int rval; … … 170 148 This.vflg = 0; 171 149 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) 179 152 switch (ch) { 180 153 case 'i': … … 201 174 return usage(pCtx, 1); 202 175 } 203 argc -= optind;204 argv += optind;176 argc -= gos.optind; 177 argv += gos.optind; 205 178 206 179 if (argc < 2) … … 301 274 } else if (access(to, W_OK) && !stat(to, &sb)) { 302 275 bsd_strmode(sb.st_mode, modep); 276 #if 0 /* probably not thread safe, also BSDism. */ 303 277 (void)fprintf(stderr, "override %s%s%s/%s for %s? %s", 304 278 modep + 1, modep[9] == ' ' ? "" : " ", 305 279 user_from_uid((unsigned long)sb.st_uid, 0), 306 280 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 307 287 ask = 1; 308 288 } … … 335 315 336 316 if (errno == EXDEV) { 337 #if ndef CROSS_DEVICE_MOVE317 #if 1 //ndef CROSS_DEVICE_MOVE 338 318 warnx(pThis->pCtx, "cannot move `%s' to a different device: `%s'", from, to); 339 319 return (1); … … 368 348 } 369 349 370 #if def CROSS_DEVICE_MOVE350 #if 0//def CROSS_DEVICE_MOVE 371 351 /* 372 352 * If rename fails because we're trying to cross devices, and … … 383 363 } 384 364 385 #if def CROSS_DEVICE_MOVE365 #if 0 //def CROSS_DEVICE_MOVE - using static buffers and fork. 386 366 int 387 367 static fastcopy(char *from, char *to, struct stat *sbp) -
trunk/src/kmk/kmkbuiltin/printf.c
r3192 r3215 50 50 * Header Files * 51 51 *********************************************************************************************************************************/ 52 #define FAKES_NO_GETOPT_H /* bird */ 52 53 #if !defined(KMK_BUILTIN_STANDALONE) && !defined(BUILTIN) && !defined(SHELL) 53 54 # include "../makeint.h" … … 70 71 #include <string.h> 71 72 #include <unistd.h> 72 #include "getopt .h"73 #include "getopt_r.h" 73 74 #ifdef __sun__ 74 75 # include "solfakes.h" … … 139 140 { 140 141 PKMKBUILTINCTX pCtx; 142 /* former globals */ 141 143 size_t b_length; 142 144 char *b_fmt; … … 146 148 char *g_o; 147 149 #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 148 156 /* Buffer the output because windows doesn't do line buffering of stdout. */ 149 157 size_t g_cchBuf; … … 167 175 * Internal Functions * 168 176 *********************************************************************************************************************************/ 169 static int common_printf(PPRINTFINSTANCE pThis, int argc, char *argv[]); 177 static int common_printf(PPRINTFINSTANCE pThis, char *argv[], PKMKBUILTINCTX pCtx); 178 static int common_printf_inner(PPRINTFINSTANCE pThis, char *argv[]); 170 179 static void conv_escape_str(PPRINTFINSTANCE, char *, void (*)(PPRINTFINSTANCE, int)); 171 180 static char *conv_escape(PPRINTFINSTANCE, char *, char *); 172 static c har *conv_expand(const char *);181 static const char *conv_expand(PPRINTFINSTANCE, const char *); 173 182 static int getchr(PPRINTFINSTANCE); 174 183 static double getdouble(PPRINTFINSTANCE); … … 191 200 int kmk_builtin_printf(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx) 192 201 { 202 PRINTFINSTANCE This; 203 struct getopt_state_r gos; 193 204 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) { 212 208 switch (ch) { 213 209 case 261: … … 221 217 } 222 218 } 223 argc -= optind;224 argv += optind;219 argc -= gos.optind; 220 argv += gos.optind; 225 221 226 222 if (argc < 1) 227 223 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); 229 229 } 230 230 … … 249 249 fatal(NILF, strlen(funcname) + INTSTR_LENGTH, _("$(%s): no format string\n"), funcname); 250 250 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;257 251 This.g_o = o; 258 259 rc = common_printf(&This, argc, argv); 252 rc = common_printf(&This, argv, NULL); 260 253 o = This.g_o; 261 254 … … 266 259 #endif /* KMK_BUILTIN_STANDALONE */ 267 260 268 static int common_printf(PPRINTFINSTANCE pThis, int argc, char *argv[]) 261 static 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 294 static int common_printf_inner(PPRINTFINSTANCE pThis, char *argv[]) 269 295 { 270 296 char *fmt, *start; … … 275 301 char longbuf[64]; 276 302 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;283 303 format = *argv; 284 304 pThis->gargv = ++argv; … … 338 358 339 359 case 'B': { 340 const char *p = conv_expand( getstr(pThis));360 const char *p = conv_expand(pThis, getstr(pThis)); 341 361 *fmt = 's'; 342 362 PF(start, p); … … 347 367 * but the string we generate might have 348 368 * embedded nulls. */ 349 static char *a, *t;350 369 char *cp = getstr(pThis); 351 370 /* 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 } 358 379 /* Count number of bytes we want to output */ 359 380 pThis->b_length = 0; 360 381 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) 363 384 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; 366 387 /* Get printf to calculate the lengths */ 367 388 *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; 370 391 /* Output leading spaces and data bytes */ 371 392 conv_escape_str(pThis, cp, b_output); … … 710 731 /* expand a string so that everything is printable */ 711 732 712 static char * 713 conv_expand(const char *str) 714 { 715 static char *conv_str; 716 static char no_memory[] = "<no memory>"; 733 static const char * 734 conv_expand(PPRINTFINSTANCE pThis, const char *str) 735 { 736 static const char no_memory[] = "<no memory>"; 717 737 char *cp; 718 738 int ch; 719 739 720 if ( conv_str)721 free( conv_str);740 if (pThis->conv_str) 741 free(pThis->conv_str); 722 742 /* get a buffer that is definitely large enough.... */ 723 conv_str= malloc(4 * strlen(str) + 1);724 if (!c onv_str)743 pThis->conv_str = cp = malloc(4 * strlen(str) + 1); 744 if (!cp) 725 745 return no_memory; 726 cp = conv_str;727 746 728 747 while ((ch = *(const unsigned char *)str++) != '\0') { … … 771 790 772 791 *cp = 0; 773 return conv_str;792 return pThis->conv_str; 774 793 } 775 794
Note:
See TracChangeset
for help on using the changeset viewer.