sbase

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

mknod.c (1171B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <sys/stat.h>
      3 #include <sys/types.h>
      4 #ifndef makedev
      5 #include <sys/sysmacros.h>
      6 #endif
      7 
      8 #include <fcntl.h>
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <string.h>
     12 #include <unistd.h>
     13 
     14 #include "util.h"
     15 
     16 static void
     17 usage(void)
     18 {
     19 	eprintf("usage: %s [-m mode] name b|c|u major minor\n"
     20 	        "       %s [-m mode] name p\n",
     21 	        argv0, argv0);
     22 }
     23 
     24 int
     25 main(int argc, char *argv[])
     26 {
     27 	mode_t mode = 0666;
     28 	dev_t dev;
     29 
     30 	ARGBEGIN {
     31 	case 'm':
     32 		mode = parsemode(EARGF(usage()), mode, umask(0));
     33 		break;
     34 	default:
     35 		usage();
     36 	} ARGEND;
     37 
     38 	if (argc < 2)
     39 		usage();
     40 
     41 	if (strlen(argv[1]) != 1)
     42 		goto invalid;
     43 	switch (argv[1][0]) {
     44 	case 'b':
     45 		mode |= S_IFBLK;
     46 		break;
     47 	case 'u':
     48 	case 'c':
     49 		mode |= S_IFCHR;
     50 		break;
     51 	case 'p':
     52 		mode |= S_IFIFO;
     53 		break;
     54 	default:
     55 	invalid:
     56 		eprintf("invalid type '%s'\n", argv[1]);
     57 	}
     58 
     59 	if (S_ISFIFO(mode)) {
     60 		if (argc != 2)
     61 			usage();
     62 		dev = 0;
     63 	} else {
     64 		if (argc != 4)
     65 			usage();
     66 		dev = makedev(estrtonum(argv[2], 0, LLONG_MAX), estrtonum(argv[3], 0, LLONG_MAX));
     67 	}
     68 
     69 	if (mknod(argv[0], mode, dev) == -1)
     70 		eprintf("mknod %s:", argv[0]);
     71 	return 0;
     72 }