Magellan Linux

Annotation of /trunk/mkinitrd-magellan/klibc/usr/kinit/devname.c

Parent Directory Parent Directory | Revision Log Revision Log


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