Changeset 613 in kBuild for trunk/src/gmake/kmkbuiltin/mv.c
- Timestamp:
- Nov 26, 2006 6:19:04 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/gmake/kmkbuiltin/mv.c
r612 r613 42 42 #endif /* not lint */ 43 43 #endif 44 #if 0 44 45 #include <sys/cdefs.h> 45 46 __FBSDID("$FreeBSD: src/bin/mv/mv.c,v 1.46 2005/09/05 04:36:08 csjp Exp $"); 47 #endif 46 48 47 49 #include <sys/types.h> 50 #ifndef _MSC_VER 48 51 #include <sys/acl.h> 49 52 #include <sys/param.h> 50 53 #include <sys/time.h> 51 54 #include <sys/wait.h> 55 #include <sys/mount.h> 56 #endif 52 57 #include <sys/stat.h> 53 #include <sys/mount.h> 54 55 #include <err.h> 58 59 #include "err.h" 56 60 #include <errno.h> 57 61 #include <fcntl.h> 62 #ifndef _MSC_VER 58 63 #include <grp.h> 64 #endif 59 65 #include <limits.h> 66 #ifndef _MSC_VER 60 67 #include <paths.h> 61 68 #include <pwd.h> 69 #endif 62 70 #include <stdio.h> 63 71 #include <stdlib.h> 64 72 #include <string.h> 73 #ifndef _MSC_VER 65 74 #include <sysexits.h> 66 75 #include <unistd.h> 67 68 int fflg, iflg, nflg, vflg; 69 70 int copy(char *, char *); 71 int do_move(char *, char *); 72 int fastcopy(char *, char *, struct stat *); 73 void usage(void); 76 #else 77 #include "mscfakes.h" 78 #endif 79 80 #if !defined(__FreeBSD__) && !defined(__APPLE__) 81 extern void strmode(mode_t mode, char *p); 82 #endif 83 84 static int fflg, iflg, nflg, vflg; 85 86 static int do_move(char *, char *); 87 #ifdef CROSS_DEVICE_MOVE 88 static int fastcopy(char *, char *, struct stat *); 89 static int copy(char *, char *); 90 #endif 91 static int usage(void); 92 93 #if defined(_MSC_VER) || defined(__EMX__) || 0 94 static const char *user_from_uid(unsigned long id, int x) 95 { 96 static char s_buf[64]; 97 sprintf(s_buf, "%ld", id); 98 return s_buf; 99 } 100 static const char *group_from_gid(unsigned long id, int x) 101 { 102 static char s_buf[64]; 103 sprintf(s_buf, "%ld", id); 104 return s_buf; 105 } 106 #endif 107 74 108 75 109 int 76 main(int argc, char *argv[])110 kmk_builtin_mv(int argc, char *argv[]) 77 111 { 78 112 size_t baselen, len; … … 82 116 int ch; 83 117 char path[PATH_MAX]; 118 119 /* kmk: reinitialize globals */ 120 fflg = iflg = nflg = vflg = 0; 121 122 /* kmk: reset getopt and set progname */ 123 g_progname = argv[0]; 124 opterr = 1; 125 optarg = NULL; 126 optopt = 0; 127 #if defined(__FreeBSD__) || defined(__EMX__) || defined(__APPLE__) 128 optreset = 1; 129 optind = 1; 130 #else 131 optind = 0; /* init */ 132 #endif 84 133 85 134 while ((ch = getopt(argc, argv, "finv")) != -1) … … 101 150 break; 102 151 default: 103 usage();152 return usage(); 104 153 } 105 154 argc -= optind; … … 107 156 108 157 if (argc < 2) 109 usage();158 return usage(); 110 159 111 160 /* … … 115 164 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) { 116 165 if (argc > 2) 117 usage();118 exit(do_move(argv[0], argv[1]));166 return usage(); 167 return do_move(argv[0], argv[1]); 119 168 } 120 169 121 170 /* It's a directory, move each file into it. */ 122 171 if (strlen(argv[argc - 1]) > sizeof(path) - 1) 123 errx(1, "%s: destination pathname too long", *argv);172 return errx(1, "%s: destination pathname too long", *argv); 124 173 (void)strcpy(path, argv[argc - 1]); 125 174 baselen = strlen(path); 126 175 endp = &path[baselen]; 176 #if defined(_MSC_VER) || defined(__EMX__) 177 if (!baselen || (*(endp - 1) != '/' && *(endp - 1) != '\\' && *(endp - 1) != ':')) { 178 #else 127 179 if (!baselen || *(endp - 1) != '/') { 180 #endif 128 181 *endp++ = '/'; 129 182 ++baselen; … … 135 188 */ 136 189 p = *argv + strlen(*argv); 190 #if defined(_MSC_VER) || defined(__EMX__) 191 while (p != *argv && (p[-1] == '/' || p[-1] == '\\')) 192 --p; 193 while (p != *argv && p[-1] != '/' && p[-1] != '/' && p[-1] != ':') 194 --p; 195 #else 137 196 while (p != *argv && p[-1] == '/') 138 197 --p; 139 198 while (p != *argv && p[-1] != '/') 140 199 --p; 200 #endif 141 201 142 202 if ((baselen + (len = strlen(p))) >= PATH_MAX) { … … 149 209 } 150 210 } 151 exit(rval);152 } 153 154 int211 return rval; 212 } 213 214 static int 155 215 do_move(char *from, char *to) 156 216 { … … 167 227 168 228 /* prompt only if source exist */ 169 229 if (lstat(from, &sb) == -1) { 170 230 warn("%s", from); 171 231 return (1); … … 206 266 207 267 if (errno == EXDEV) { 268 #ifndef CROSS_DEVICE_MOVE 269 warnx("cannot move `%s' to a different device: `%s'", from, to); 270 return (1); 271 #else 208 272 struct statfs sfs; 209 273 char path[PATH_MAX]; … … 229 293 } 230 294 } 295 #endif 231 296 } else { 232 297 warn("rename %s to %s", from, to); … … 234 299 } 235 300 301 #ifdef CROSS_DEVICE_MOVE 236 302 /* 237 303 * If rename fails because we're trying to cross devices, and … … 245 311 return (S_ISREG(sb.st_mode) ? 246 312 fastcopy(from, to, &sb) : copy(from, to)); 247 } 248 313 #endif 314 } 315 316 #ifdef CROSS_DEVICE_MOVE 249 317 int 250 fastcopy(char *from, char *to, struct stat *sbp)318 static fastcopy(char *from, char *to, struct stat *sbp) 251 319 { 252 320 struct timeval tval[2]; … … 397 465 return (0); 398 466 } 399 400 void 467 #endif /* CROSS_DEVICE_MOVE */ 468 469 470 static int 401 471 usage(void) 402 472 { … … 405 475 "usage: mv [-f | -i | -n] [-v] source target", 406 476 " mv [-f | -i | -n] [-v] source ... directory"); 407 exit(EX_USAGE);408 } 477 return EX_USAGE; 478 }
Note:
See TracChangeset
for help on using the changeset viewer.