sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

cp.c (950B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <sys/stat.h>
      3 
      4 #include "fs.h"
      5 #include "util.h"
      6 
      7 static void
      8 usage(void)
      9 {
     10 	eprintf("usage: %s [-afpv] [-R [-H | -L | -P]] source ... dest\n", argv0);
     11 }
     12 
     13 int
     14 main(int argc, char *argv[])
     15 {
     16 	struct stat st;
     17 
     18 	ARGBEGIN {
     19 	case 'a':
     20 		cp_follow = 'P';
     21 		cp_aflag = cp_pflag = cp_rflag = 1;
     22 		break;
     23 	case 'f':
     24 		cp_fflag = 1;
     25 		break;
     26 	case 'p':
     27 		cp_pflag = 1;
     28 		break;
     29 	case 'r':
     30 	case 'R':
     31 		cp_rflag = 1;
     32 		break;
     33 	case 'v':
     34 		cp_vflag = 1;
     35 		break;
     36 	case 'H':
     37 	case 'L':
     38 	case 'P':
     39 		cp_follow = ARGC();
     40 		break;
     41 	default:
     42 		usage();
     43 	} ARGEND
     44 
     45 	if (argc < 2)
     46 		usage();
     47 
     48 	if (!cp_follow)
     49 		cp_follow = cp_rflag ? 'P' : 'L';
     50 
     51 	if (argc > 2) {
     52 		if (stat(argv[argc - 1], &st) < 0)
     53 			eprintf("stat %s:", argv[argc - 1]);
     54 		if (!S_ISDIR(st.st_mode))
     55 			eprintf("%s: not a directory\n", argv[argc - 1]);
     56 	}
     57 	enmasse(argc, argv, cp);
     58 
     59 	return fshut(stdout, "<stdout>") || cp_status;
     60 }