annotate toys/mknod.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents 9e64ca0e3b79
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
488
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
2 *
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
3 * mknod.c - make block or character special file
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
4 *
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
5 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
6 *
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
7 * Not in SUSv3.
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
8
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
9 USE_MKNOD(NEWTOY(mknod, "<2>4", TOYFLAG_BIN))
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
10
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
11 config MKNOD
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
12 bool "mknod"
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
13 default y
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
14 help
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
15 usage: mknod NAME TYPE [MAJOR MINOR]
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
16
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
17 Create a special file NAME with a given type, possible types are
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
18 b create a block device with the given MAJOR/MINOR
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
19 c or u create a character device with the given MAJOR/MINOR
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
20 p create a named pipe ignoring MAJOR/MINOR
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
21 */
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
22
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
23 #include "toys.h"
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
24
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
25 static const char modes_char[] = {'p', 'c', 'u', 'b'};
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
26 static const mode_t modes[] = {S_IFIFO, S_IFCHR, S_IFCHR, S_IFBLK};
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
27
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
28 void mknod_main(void)
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
29 {
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
30 int major=0, minor=0, type;
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
31 char * tmp;
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
32 int mode = 0660;
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
33
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
34 tmp = strchr(modes_char, toys.optargs[1][0]);
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
35 if (!tmp)
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
36 perror_exit("unknown special device type %c", toys.optargs[1][0]);
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
37
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
38 type = modes[tmp-modes_char];
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
39
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
40 if (type == S_IFCHR || type == S_IFBLK) {
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
41 if (toys.optc != 4)
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
42 perror_exit("creating a block/char device requires major/minor");
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
43
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
44 major = atoi(toys.optargs[2]);
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
45 minor = atoi(toys.optargs[3]);
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
46 }
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
47
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
48 if (mknod(toys.optargs[0], mode | type, makedev(major, minor)))
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
49 perror_exit("mknod %s failed", toys.optargs[0]);
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
50
9e64ca0e3b79 Adding mknod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
51 }