Changeset 3192 for trunk/src/kmk/kmkbuiltin/mv.c
- Timestamp:
- Mar 26, 2018, 10:25:56 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/kmkbuiltin/mv.c
r3109 r3192 47 47 #endif 48 48 49 50 /********************************************************************************************************************************* 51 * Header Files * 52 *********************************************************************************************************************************/ 49 53 #include "config.h" 50 54 #include <sys/types.h> … … 89 93 90 94 91 static int fflg, iflg, nflg, vflg; 95 /********************************************************************************************************************************* 96 * Structures and Typedefs * 97 *********************************************************************************************************************************/ 98 typedef struct MVINSTANCE 99 { 100 PKMKBUILTINCTX pCtx; 101 int fflg, iflg, nflg, vflg; 102 } MVINSTANCE; 103 typedef MVINSTANCE *PMVINSTANCE; 104 105 106 /********************************************************************************************************************************* 107 * Global Variables * 108 *********************************************************************************************************************************/ 92 109 static struct option long_options[] = 93 110 { … … 98 115 99 116 100 static int do_move(char *, char *); 117 /********************************************************************************************************************************* 118 * Internal Functions * 119 *********************************************************************************************************************************/ 120 extern void bsd_strmode(mode_t mode, char *p); /* strmode.c */ 121 122 static int do_move(PMVINSTANCE, char *, char *); 101 123 #ifdef CROSS_DEVICE_MOVE 102 124 static int fastcopy(char *, char *, struct stat *); 103 125 static int copy(char *, char *); 104 126 #endif 105 static int usage(FILE *); 106 107 extern void bsd_strmode(mode_t mode, char *p); 127 static int usage(PKMKBUILTINCTX, int); 128 108 129 109 130 #if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(__DragonFly__) && !defined(__OpenBSD__) … … 132 153 133 154 int 134 kmk_builtin_mv(int argc, char *argv[], char **envp) 135 { 155 kmk_builtin_mv(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx) 156 { 157 MVINSTANCE This; 136 158 size_t baselen, len; 137 159 int rval; … … 141 163 char path[PATH_MAX]; 142 164 143 /* kmk: reinitialize globals */ 144 fflg = iflg = nflg = vflg = 0; 165 /* Initialize instance. */ 166 This.pCtx = pCtx; 167 This.fflg = 0; 168 This.iflg = 0; 169 This.nflg = 0; 170 This.vflg = 0; 145 171 146 172 /* kmk: reset getopt and set progname */ 147 g_progname = argv[0];148 173 opterr = 1; 149 174 optarg = NULL; … … 154 179 switch (ch) { 155 180 case 'i': 156 iflg = 1;157 fflg =nflg = 0;181 This.iflg = 1; 182 This.fflg = This.nflg = 0; 158 183 break; 159 184 case 'f': 160 fflg = 1;161 iflg =nflg = 0;185 This.fflg = 1; 186 This.iflg = This.nflg = 0; 162 187 break; 163 188 case 'n': 164 nflg = 1;165 fflg =iflg = 0;189 This.nflg = 1; 190 This.fflg = This.iflg = 0; 166 191 break; 167 192 case 'v': 168 vflg = 1;193 This.vflg = 1; 169 194 break; 170 195 case 261: 171 usage( stdout);196 usage(pCtx, 0); 172 197 return 0; 173 198 case 262: 174 199 return kbuild_version(argv[0]); 175 200 default: 176 return usage( stderr);201 return usage(pCtx, 1); 177 202 } 178 203 argc -= optind; … … 180 205 181 206 if (argc < 2) 182 return usage( stderr);207 return usage(pCtx, 1); 183 208 184 209 /* … … 188 213 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) { 189 214 if (argc > 2) 190 return usage( stderr);191 return do_move( argv[0], argv[1]);215 return usage(pCtx, 1); 216 return do_move(&This, argv[0], argv[1]); 192 217 } 193 218 194 219 /* It's a directory, move each file into it. */ 195 if (strlen(argv[argc - 1]) > sizeof(path) - 1)196 return errx(1, "%s: destination pathname too long", *argv);197 (void)strcpy(path, argv[argc - 1]);198 baselen = strlen(path);220 baselen = strlen(argv[argc - 1]); 221 if (baselen > sizeof(path) - 1) 222 return errx(pCtx, 1, "%s: destination pathname too long", *argv); 223 memcpy(path, argv[argc - 1], baselen); 199 224 endp = &path[baselen]; 225 *endp = '\0'; 200 226 #if defined(_MSC_VER) || defined(__EMX__) 201 227 if (!baselen || (*(endp - 1) != '/' && *(endp - 1) != '\\' && *(endp - 1) != ':')) { … … 225 251 226 252 if ((baselen + (len = strlen(p))) >= PATH_MAX) { 227 warnx( "%s: destination pathname too long", *argv);253 warnx(pCtx, "%s: destination pathname too long", *argv); 228 254 rval = 1; 229 255 } else { 230 256 memmove(endp, p, (size_t)len + 1); 231 if (do_move( *argv, path))257 if (do_move(&This, *argv, path)) 232 258 rval = 1; 233 259 } … … 236 262 } 237 263 264 #ifdef KMK_BUILTIN_STANDALONE 265 int main(int argc, char **argv, char **envp) 266 { 267 KMKBUILTINCTX Ctx = { "kmk_mv", NULL }; 268 return kmk_builtin_mv(argc, argv, envp, &Ctx); 269 } 270 #endif 271 238 272 static int 239 do_move( char *from, char *to)273 do_move(PMVINSTANCE pThis, char *from, char *to) 240 274 { 241 275 struct stat sb; … … 248 282 * make sure the user wants to clobber it. 249 283 */ 250 if (! fflg && !access(to, F_OK)) {284 if (!pThis->fflg && !access(to, F_OK)) { 251 285 252 286 /* prompt only if source exist */ 253 287 if (lstat(from, &sb) == -1) { 254 warn( "%s", from);288 warn(pThis->pCtx, "%s", from); 255 289 return (1); 256 290 } … … 258 292 #define YESNO "(y/n [n]) " 259 293 ask = 0; 260 if ( nflg) {261 if ( vflg)262 printf("%s not overwritten\n", to);294 if (pThis->nflg) { 295 if (pThis->vflg) 296 kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s not overwritten\n", to); 263 297 return (0); 264 } else if ( iflg) {298 } else if (pThis->iflg) { 265 299 (void)fprintf(stderr, "overwrite %s? %s", to, YESNO); 266 300 ask = 1; … … 274 308 } 275 309 if (ask) { 310 fflush(stderr); 276 311 first = ch = getchar(); 277 312 while (ch != '\n' && ch != EOF) 278 313 ch = getchar(); 279 314 if (first != 'y' && first != 'Y') { 280 (void)fprintf(stderr, "not overwritten\n");315 kmk_builtin_ctx_printf(pThis->pCtx, 1, "not overwritten\n"); 281 316 return (0); 282 317 } … … 284 319 } 285 320 if (!rename(from, to)) { 286 if ( vflg)287 printf("%s -> %s\n", from, to);321 if (pThis->vflg) 322 kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s -> %s\n", from, to); 288 323 return (0); 289 324 } … … 292 327 remove(to); 293 328 if (!rename(from, to)) { 294 if ( vflg)295 printf("%s -> %s\n", from, to);329 if (pThis->vflg) 330 kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s -> %s\n", from, to); 296 331 return (0); 297 332 } … … 301 336 if (errno == EXDEV) { 302 337 #ifndef CROSS_DEVICE_MOVE 303 warnx( "cannot move `%s' to a different device: `%s'", from, to);338 warnx(pThis->pCtx, "cannot move `%s' to a different device: `%s'", from, to); 304 339 return (1); 305 340 #else … … 312 347 */ 313 348 if (lstat(from, &sb) == -1) { 314 warn( "%s", from);349 warn(pThis->pCtx, "%s", from); 315 350 return (1); 316 351 } … … 318 353 /* Can't mv(1) a mount point. */ 319 354 if (realpath(from, path) == NULL) { 320 warnx( "cannot resolve %s: %s", from, path);355 warnx(pThis->pCtx, "cannot resolve %s: %s", from, path); 321 356 return (1); 322 357 } 323 358 if (!statfs(path, &sfs) && 324 359 !strcmp(path, sfs.f_mntonname)) { 325 warnx( "cannot rename a mount point");360 warnx(pThis->pCtx, "cannot rename a mount point"); 326 361 return (1); 327 362 } … … 329 364 #endif 330 365 } else { 331 warn( "rename %s to %s", from, to);366 warn(pThis->pCtx, "rename %s to %s", from, to); 332 367 return (1); 333 368 } … … 340 375 */ 341 376 if (lstat(from, &sb)) { 342 warn( "%s", from);377 warn(pThis->pCtx, "%s", from); 343 378 return (1); 344 379 } 345 380 return (S_ISREG(sb.st_mode) ? 346 fastcopy( from, to, &sb) : copy(from, to));381 fastcopy(pThis, from, to, &sb) : copy(pThis, from, to)); 347 382 #endif 348 383 } … … 451 486 } 452 487 if (vflg) 453 printf("%s -> %s\n", from, to);488 kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s -> %s\n", from, to); 454 489 return (0); 455 490 } … … 503 538 504 539 static int 505 usage(FILE *pf) 506 { 507 fprintf(pf, "usage: %s [-f | -i | -n] [-v] source target\n" 508 " or: %s [-f | -i | -n] [-v] source ... directory\n" 509 " or: %s --help\n" 510 " or: %s --version\n", 511 g_progname, g_progname, g_progname, g_progname); 540 usage(PKMKBUILTINCTX pCtx, int fIsErr) 541 { 542 kmk_builtin_ctx_printf(pCtx, fIsErr, 543 "usage: %s [-f | -i | -n] [-v] source target\n" 544 " or: %s [-f | -i | -n] [-v] source ... directory\n" 545 " or: %s --help\n" 546 " or: %s --version\n", 547 pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName); 512 548 return EX_USAGE; 513 549 }
Note:
See TracChangeset
for help on using the changeset viewer.