Busybox 1.20.2 update
[tomato.git] / release / src / router / busybox / e2fsprogs / ext2fs / finddev.c
blobe9e83fd7ad88c769ba761311cd88f7014d359dfd
1 /* vi: set sw=4 ts=4: */
2 /*
3 * finddev.c -- this routine attempts to find a particular device in
4 * /dev
6 * Copyright (C) 2000 Theodore Ts'o.
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Public
10 * License.
11 * %End-Header%
14 #include <stdio.h>
15 #include <string.h>
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #include <stdlib.h>
20 #include <string.h>
21 #ifdef HAVE_SYS_TYPES_H
22 #include <sys/types.h>
23 #endif
24 #ifdef HAVE_SYS_STAT_H
25 #include <sys/stat.h>
26 #endif
27 #include <dirent.h>
28 #ifdef HAVE_ERRNO_H
29 #include <errno.h>
30 #endif
31 #ifdef HAVE_SYS_MKDEV_H
32 #include <sys/mkdev.h>
33 #endif
35 #include "ext2_fs.h"
36 #include "ext2fs.h"
38 struct dir_list {
39 char *name;
40 struct dir_list *next;
44 * This function adds an entry to the directory list
46 static void add_to_dirlist(const char *name, struct dir_list **list)
48 struct dir_list *dp;
50 dp = xmalloc(sizeof(struct dir_list));
51 dp->name = xmalloc(strlen(name)+1);
52 strcpy(dp->name, name);
53 dp->next = *list;
54 *list = dp;
58 * This function frees a directory list
60 static void free_dirlist(struct dir_list **list)
62 struct dir_list *dp, *next;
64 for (dp = *list; dp; dp = next) {
65 next = dp->next;
66 free(dp->name);
67 free(dp);
69 *list = 0;
72 static int scan_dir(char *dir_name, dev_t device, struct dir_list **list,
73 char **ret_path)
75 DIR *dir;
76 struct dirent *dp;
77 char path[1024], *cp;
78 int dirlen;
79 struct stat st;
81 dirlen = strlen(dir_name);
82 if ((dir = opendir(dir_name)) == NULL)
83 return errno;
84 dp = readdir(dir);
85 while (dp) {
86 if (dirlen + strlen(dp->d_name) + 2 >= sizeof(path))
87 goto skip_to_next;
88 if (dp->d_name[0] == '.' &&
89 ((dp->d_name[1] == 0) ||
90 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
91 goto skip_to_next;
92 sprintf(path, "%s/%s", dir_name, dp->d_name);
93 if (stat(path, &st) < 0)
94 goto skip_to_next;
95 if (S_ISDIR(st.st_mode))
96 add_to_dirlist(path, list);
97 if (S_ISBLK(st.st_mode) && st.st_rdev == device) {
98 cp = xmalloc(strlen(path)+1);
99 strcpy(cp, path);
100 *ret_path = cp;
101 goto success;
103 skip_to_next:
104 dp = readdir(dir);
106 success:
107 closedir(dir);
108 return 0;
112 * This function finds the pathname to a block device with a given
113 * device number. It returns a pointer to allocated memory to the
114 * pathname on success, and NULL on failure.
116 char *ext2fs_find_block_device(dev_t device)
118 struct dir_list *list = NULL, *new_list = NULL;
119 struct dir_list *current;
120 char *ret_path = NULL;
123 * Add the starting directories to search...
125 add_to_dirlist("/devices", &list);
126 add_to_dirlist("/devfs", &list);
127 add_to_dirlist("/dev", &list);
129 while (list) {
130 current = list;
131 list = list->next;
132 #ifdef DEBUG
133 printf("Scanning directory %s\n", current->name);
134 #endif
135 scan_dir(current->name, device, &new_list, &ret_path);
136 free(current->name);
137 free(current);
138 if (ret_path)
139 break;
141 * If we're done checking at this level, descend to
142 * the next level of subdirectories. (breadth-first)
144 if (list == 0) {
145 list = new_list;
146 new_list = 0;
149 free_dirlist(&list);
150 free_dirlist(&new_list);
151 return ret_path;
155 #ifdef DEBUG
156 int main(int argc, char** argv)
158 char *devname, *tmp;
159 int major, minor;
160 dev_t device;
161 const char *errmsg = "Cannot parse %s: %s\n";
163 if ((argc != 2) && (argc != 3)) {
164 fprintf(stderr, "Usage: %s device_number\n", argv[0]);
165 fprintf(stderr, "\t: %s major minor\n", argv[0]);
166 exit(1);
168 if (argc == 2) {
169 device = strtoul(argv[1], &tmp, 0);
170 if (*tmp) {
171 fprintf(stderr, errmsg, "device number", argv[1]);
172 exit(1);
174 } else {
175 major = strtoul(argv[1], &tmp, 0);
176 if (*tmp) {
177 fprintf(stderr, errmsg, "major number", argv[1]);
178 exit(1);
180 minor = strtoul(argv[2], &tmp, 0);
181 if (*tmp) {
182 fprintf(stderr, errmsg, "minor number", argv[2]);
183 exit(1);
185 device = makedev(major, minor);
186 printf("Looking for device 0x%04x (%d:%d)\n", device,
187 major, minor);
189 devname = ext2fs_find_block_device(device);
190 if (devname) {
191 printf("Found device! %s\n", devname);
192 free(devname);
193 } else {
194 printf("Cannot find device.\n");
196 return 0;
199 #endif