Initial import from FreeBSD RELENG_4:
[dragonfly.git] / release / sysinstall / dev2c.sh
1 :
2 #
3 # ----------------------------------------------------------------------------
4 # "THE BEER-WARE LICENSE" (Revision 42):
5 # <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
6 # can do whatever you want with this stuff. If we meet some day, and you think
7 # this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
8 # ----------------------------------------------------------------------------
9 #
10 # $FreeBSD: src/release/sysinstall/dev2c.sh,v 1.4.2.1 2002/07/02 21:27:28 jhb Exp $
11 #
12 # During installation, we suffer badly of we have to run MAKEDEV.  MAKEDEV
13 # need sh, ln, chown, mknod, awk, rm, test and probably emacs too when
14 # we come down to it.  So instead this script will make a C-procedure which
15 # makes all the B & C nodes of a specified directory.
16 #
17 # Poul-Henning
18
19 (cd $1; ls -li ) | sed 's/,//' | awk '
20 BEGIN   {
21         while (getline < "/etc/passwd") {
22                 split($0,a,":")
23                 uid[a[1]] = a[3]
24         }
25         while (getline < "/etc/group") {
26                 split($0,a,":")
27                 gid[a[1]] = a[3]
28         }
29         printf("/*\n");
30         printf(" * This file is generated from the contents of /dev\n");
31         printf(" */\n");
32         printf("#define CHK(foo) {i = foo;}\n");
33         printf("#include <unistd.h>\n");
34         printf("#include <sys/types.h>\n");
35         printf("#include <sys/stat.h>\n");
36         printf("int makedevs()\n{\n\tint i=0;\n");
37         }
38         {
39         printf ("/* %s */\n",$0)
40         $4 = uid[$4]
41         $5 = gid[$5]
42         if (substr($2,1,1) == "b") {
43                 k="S_IFBLK"
44         } else if (substr($2,1,1) == "c") {
45                 k="S_IFCHR"
46         } else if (substr($2,1,1) == "d") {
47                 next
48         } else if (substr($2,1,1) == "-") {
49                 next
50         } else {
51                 next
52         }
53         m = 0;
54         if (substr($2,2,1)  == "r") m += 400;
55         if (substr($2,3,1)  == "w") m += 200;
56         if (substr($2,4,1)  == "x") m += 100;
57         if (substr($2,5,1)  == "r") m += 40;
58         if (substr($2,6,1)  == "w") m += 20;
59         if (substr($2,7,1)  == "x") m += 10;
60         if (substr($2,8,1)  == "r") m += 4;
61         if (substr($2,9,1)  == "w") m += 2;
62         if (substr($2,10,1) == "x") m += 1;
63         
64         if (a[$1] != 0) {
65                 printf ("\tCHK(link(\"%s\",\"%s\"));\n", \
66                         a[$1],$11)
67         } else {
68                 printf ("\tCHK(mknod(\"%s\",%s,makedev(%d,%d)));\n", \
69                         $11, k, $6, $7)
70                 printf ("\tCHK(chmod(\"%s\",0%d));\n", \
71                         $11, m)
72                 printf ("\tCHK(chown(\"%s\",%d,%d));\n", \
73                         $11, $4,$5)
74                 a[$1] = $11
75         }
76         }
77 END     {
78         printf("\treturn i;\n}\n");
79         }
80 '