Contents of /trunk/mkinitrd-magellan/klibc/usr/kinit/devname.c
Parent Directory | Revision Log
Revision 532 -
(show annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 2153 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 2153 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <unistd.h> |
4 | #include <dirent.h> |
5 | #include <string.h> |
6 | #include <sys/types.h> |
7 | #include <sys/sysmacros.h> |
8 | |
9 | /* |
10 | * Print the name of a block device. |
11 | */ |
12 | #define BUF_SIZE 512 |
13 | |
14 | static int scansysdir(char *namebuf, char *sysdir, dev_t dev) |
15 | { |
16 | char *dirtailptr = strchr(sysdir, '\0'); |
17 | DIR *dir; |
18 | int done = 0; |
19 | struct dirent *de; |
20 | char *systail; |
21 | FILE *sysdev; |
22 | unsigned long ma, mi; |
23 | char *ep; |
24 | ssize_t rd; |
25 | |
26 | if (!(dir = opendir(sysdir))) |
27 | return 0; |
28 | |
29 | *dirtailptr++ = '/'; |
30 | |
31 | while (!done && (de = readdir(dir))) { |
32 | /* Assume if we see a dot-name in sysfs it's special */ |
33 | if (de->d_name[0] == '.') |
34 | continue; |
35 | |
36 | if (de->d_type != DT_UNKNOWN && de->d_type != DT_DIR) |
37 | continue; |
38 | |
39 | if (strlen(de->d_name) >= |
40 | (BUF_SIZE - 64) - (dirtailptr - sysdir)) |
41 | continue; /* Badness... */ |
42 | |
43 | strcpy(dirtailptr, de->d_name); |
44 | systail = strchr(sysdir, '\0'); |
45 | |
46 | strcpy(systail, "/dev"); |
47 | if (!(sysdev = fopen(sysdir, "r"))) |
48 | continue; |
49 | |
50 | /* Abusing the namebuf as temporary storage here. */ |
51 | rd = fread(namebuf, 1, BUF_SIZE, sysdev); |
52 | namebuf[rd] = '\0'; /* Just in case... */ |
53 | |
54 | fclose(sysdev); |
55 | |
56 | ma = strtoul(namebuf, &ep, 10); |
57 | if (ma != major(dev) || *ep != ':') |
58 | continue; |
59 | |
60 | mi = strtoul(ep + 1, &ep, 10); |
61 | if (*ep != '\n') |
62 | continue; |
63 | |
64 | if (mi == minor(dev)) { |
65 | /* Found it! */ |
66 | strcpy(namebuf, de->d_name); |
67 | done = 1; |
68 | } else { |
69 | /* we have a major number match, scan for partitions */ |
70 | *systail = '\0'; |
71 | done = scansysdir(namebuf, sysdir, dev); |
72 | } |
73 | } |
74 | |
75 | closedir(dir); |
76 | return done; |
77 | } |
78 | |
79 | const char *bdevname(dev_t dev) |
80 | { |
81 | static char buf[BUF_SIZE]; |
82 | char sysdir[BUF_SIZE]; |
83 | char *p; |
84 | |
85 | strcpy(sysdir, "/sys/block"); |
86 | |
87 | if (!scansysdir(buf, sysdir, dev)) |
88 | strcpy(buf, "dev"); /* prints e.g. dev(3,5) */ |
89 | |
90 | p = strchr(buf, '\0'); |
91 | snprintf(p, sizeof buf - (p - buf), "(%d,%d)", major(dev), minor(dev)); |
92 | |
93 | return buf; |
94 | } |
95 | |
96 | #ifdef TEST_DEVNAME /* Standalone test */ |
97 | |
98 | int main(int argc, char *argv[]) |
99 | { |
100 | dev_t dev; |
101 | int i; |
102 | |
103 | for (i = 1; i < argc; i++) { |
104 | dev = strtoul(argv[i], NULL, 0); |
105 | |
106 | printf("0x%08x = %s\n", (unsigned int)dev, bdevname(dev)); |
107 | } |
108 | |
109 | return 0; |
110 | } |
111 | |
112 | #endif /* TEST */ |