comparison toys/posix/cp.c @ 1521:a8d9bf6ca8bd draft

Implement mv -f and -i.
author Rob Landley <rob@landley.net>
date Tue, 14 Oct 2014 00:01:22 -0500
parents 4bfbd8b96f66
children fc2200f927af
comparison
equal deleted inserted replaced
1520:dfd6b3404c16 1521:a8d9bf6ca8bd
146 } else if ((flags & FLAG_F) && unlinkat(cfd, catch, 0)) { 146 } else if ((flags & FLAG_F) && unlinkat(cfd, catch, 0)) {
147 error_msg("unlink '%s'", catch); 147 error_msg("unlink '%s'", catch);
148 return 0; 148 return 0;
149 } else if (flags & FLAG_n) return 0; 149 } else if (flags & FLAG_n) return 0;
150 else if (flags & FLAG_i) { 150 else if (flags & FLAG_i) {
151 fprintf(stderr, "cp: overwrite '%s'", s = dirtree_path(try, 0)); 151 fprintf(stderr, "%s: overwrite '%s'", toys.which->name,
152 s = dirtree_path(try, 0));
152 free(s); 153 free(s);
153 if (!yesno("", 1)) return 0; 154 if (!yesno("", 1)) return 0;
154 } 155 }
155 } 156 }
156 157
310 311
311 if (destdir) TT.destname = xmprintf("%s/%s", destname, basename(src)); 312 if (destdir) TT.destname = xmprintf("%s/%s", destname, basename(src));
312 else TT.destname = destname; 313 else TT.destname = destname;
313 314
314 errno = EXDEV; 315 errno = EXDEV;
315 if (CFG_CP_MV && toys.which->name[0] == 'm') rc = rename(src, TT.destname); 316 if (CFG_CP_MV && toys.which->name[0] == 'm') {
317 if (!(toys.optflags & FLAG_f)) {
318 struct stat st;
319
320 // Technically "is writeable" is more complicated (022 is not writeable
321 // by the owner, just everybody _else_) but I don't care.
322 if (!stat(src, &st)
323 && ((toys.optflags & FLAG_i) || !(st.st_mode & 0222)))
324 {
325 fprintf(stderr, "%s: overwrite '%s'", toys.which->name, src);
326 if (!yesno("", 1)) rc = 0;
327 else unlink(src);
328 }
329 }
330
331 if (rc) rc = rename(src, TT.destname);
332 }
316 333
317 // Skip nonexistent sources 334 // Skip nonexistent sources
318 if (rc) { 335 if (rc) {
319 int symfollow = toys.optflags & (FLAG_H|FLAG_L); 336 int symfollow = toys.optflags & (FLAG_H|FLAG_L);
320 337