2 * finddev.c -- this routine attempts to find a particular device in
5 * Copyright (C) 2000 Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
22 #include <sys/types.h>
32 #include <sys/mkdev.h>
34 #ifdef HAVE_SYS_SYSMACROS_H
35 #include <sys/sysmacros.h>
44 struct dir_list *next;
48 * This function adds an entry to the directory list
50 static void add_to_dirlist(const char *name, struct dir_list **list)
54 dp = malloc(sizeof(struct dir_list));
57 dp->name = malloc(strlen(name)+1);
62 strcpy(dp->name, name);
68 * This function frees a directory list
70 static void free_dirlist(struct dir_list **list)
72 struct dir_list *dp, *next;
74 for (dp = *list; dp; dp = next) {
82 static int scan_dir(char *dirname, dev_t device, struct dir_list **list,
91 dirlen = strlen(dirname);
92 if ((dir = opendir(dirname)) == NULL)
96 if (dirlen + strlen(dp->d_name) + 2 >= sizeof(path))
98 if (dp->d_name[0] == '.' &&
99 ((dp->d_name[1] == 0) ||
100 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
102 sprintf(path, "%s/%s", dirname, dp->d_name);
103 if (stat(path, &st) < 0)
105 if (S_ISDIR(st.st_mode))
106 add_to_dirlist(path, list);
107 if (ext2fsP_is_disk_device(st.st_mode) &&
108 st.st_rdev == device) {
109 cp = malloc(strlen(path)+1);
127 * This function finds the pathname to a block device with a given
128 * device number. It returns a pointer to allocated memory to the
129 * pathname on success, and NULL on failure.
131 char *ext2fs_find_block_device(dev_t device)
133 struct dir_list *list = 0, *new_list = 0;
134 struct dir_list *current;
139 * Add the starting directories to search...
141 add_to_dirlist("/devices", &list);
142 add_to_dirlist("/devfs", &list);
143 add_to_dirlist("/dev", &list);
149 printf("Scanning directory %s\n", current->name);
151 scan_dir(current->name, device, &new_list, &ret_path);
157 * If we're done checking at this level, descend to
158 * the next level of subdirectories. (breadth-first)
163 /* Avoid infinite loop */
164 if (++level >= EXT2FS_MAX_NESTED_LINKS)
169 free_dirlist(&new_list);
175 int main(int argc, char** argv)
180 const char *errmsg = "Couldn't parse %s: %s\n";
182 if ((argc != 2) && (argc != 3)) {
183 fprintf(stderr, "Usage: %s device_number\n", argv[0]);
184 fprintf(stderr, "\t: %s major minor\n", argv[0]);
188 device = strtoul(argv[1], &tmp, 0);
190 fprintf(stderr, errmsg, "device number", argv[1]);
194 major = strtoul(argv[1], &tmp, 0);
196 fprintf(stderr, errmsg, "major number", argv[1]);
199 minor = strtoul(argv[2], &tmp, 0);
201 fprintf(stderr, errmsg, "minor number", argv[2]);
204 device = makedev(major, minor);
205 printf("Looking for device 0x%04x (%d:%d)\n", device,
208 devname = ext2fs_find_block_device(device);
210 printf("Found device! %s\n", devname);
213 printf("Couldn't find device.\n");