/** * blkid_devno_to_devname: * @devno: device number * * This function finds the pathname to a block device with a given * device number. * * Returns: a pointer to allocated memory to the pathname on success, * and NULL on failure. */ char *blkid_devno_to_devname(dev_t devno) { struct dir_list *list = NULL, *new_list = NULL; char *devname = NULL; const char **dir; /* * Add the starting directories to search in reverse order of * importance, since we are using a stack... */ for (dir = devdirs; *dir; dir++) add_to_dirlist(*dir, NULL, &list); while (list) { struct dir_list *current = list; list = list->next; DBG(DEBUG_DEVNO, printf("directory %s\n", current->name)); blkid__scan_dir(current->name, devno, &new_list, &devname); free(current->name); free(current); if (devname) break; /* * If we're done checking at this level, descend to * the next level of subdirectories. (breadth-first) */ if (list == NULL) { list = new_list; new_list = NULL; } } free_dirlist(&list); free_dirlist(&new_list); if (!devname) { DBG(DEBUG_DEVNO, printf("blkid: couldn't find devno 0x%04lx\n", (unsigned long) devno)); } else { DBG(DEBUG_DEVNO, printf("found devno 0x%04llx as %s\n", (long long)devno, devname)); } return devname; }
static char *scandev_devno_to_devpath(dev_t devno) { struct dir_list *list = NULL, *new_list = NULL; char *devname = NULL; const char **dir; /* * Add the starting directories to search in reverse order of * importance, since we are using a stack... */ for (dir = devdirs; *dir; dir++) add_to_dirlist(*dir, NULL, &list); while (list) { struct dir_list *current = list; list = list->next; DBG(DEVNO, ul_debug("directory %s", current->name)); blkid__scan_dir(current->name, devno, &new_list, &devname); free(current->name); free(current); if (devname) break; /* * If we're done checking at this level, descend to * the next level of subdirectories. (breadth-first) */ if (list == NULL) { list = new_list; new_list = NULL; } } free_dirlist(&list); free_dirlist(&new_list); return devname; }
/* * This function finds the pathname to a block device with a given * device number. It returns a pointer to allocated memory to the * pathname on success, and NULL on failure. */ char *ext2fs_find_block_device(dev_t device) { struct dir_list *list = NULL, *new_list = NULL; struct dir_list *current; char *ret_path = NULL; /* * Add the starting directories to search... */ add_to_dirlist("/devices", &list); add_to_dirlist("/devfs", &list); add_to_dirlist("/dev", &list); while (list) { current = list; list = list->next; #ifdef DEBUG printf("Scanning directory %s\n", current->name); #endif scan_dir(current->name, device, &new_list, &ret_path); free(current->name); free(current); if (ret_path) break; /* * If we're done checking at this level, descend to * the next level of subdirectories. (breadth-first) */ if (list == 0) { list = new_list; new_list = 0; } } free_dirlist(&list); free_dirlist(&new_list); return ret_path; }
int main(int argc, char **argv) { struct wh_dirlist *ls = NULL; int want = ALL_DIRS; int i, want_resetable = 0, opt_f_missing = 0; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); if (argc == 1) usage(stderr); whereis_init_debug(); construct_dirlist(&ls, BIN_DIR, bindirs); construct_dirlist_from_env("PATH", &ls, BIN_DIR); construct_dirlist(&ls, MAN_DIR, mandirs); construct_dirlist_from_env("MANPATH", &ls, MAN_DIR); construct_dirlist(&ls, SRC_DIR, srcdirs); for (i = 1; i < argc; i++) { const char *arg = argv[i]; int arg_i = i; DBG(ARGV, ul_debug("argv[%d]: %s", i, arg)); if (*arg != '-') { lookup(arg, ls, want); /* * The lookup mask ("want") is cumulative and it's * resetable only when it has been already used. * * whereis -b -m foo :'foo' mask=BIN|MAN * whereis -b foo bar :'foo' and 'bar' mask=BIN|MAN * whereis -b foo -m bar :'foo' mask=BIN; 'bar' mask=MAN */ want_resetable = 1; continue; } for (++arg; arg && *arg; arg++) { DBG(ARGV, ul_debug(" arg: %s", arg)); switch (*arg) { case 'f': opt_f_missing = 0; break; case 'u': uflag = 1; opt_f_missing = 0; break; case 'B': if (*(arg + 1)) usage(stderr); i++; free_dirlist(&ls, BIN_DIR); construct_dirlist_from_argv( &ls, &i, argc, argv, BIN_DIR); opt_f_missing = 1; break; case 'M': if (*(arg + 1)) usage(stderr); i++; free_dirlist(&ls, MAN_DIR); construct_dirlist_from_argv( &ls, &i, argc, argv, MAN_DIR); opt_f_missing = 1; break; case 'S': if (*(arg + 1)) usage(stderr); i++; free_dirlist(&ls, SRC_DIR); construct_dirlist_from_argv( &ls, &i, argc, argv, SRC_DIR); opt_f_missing = 1; break; case 'b': if (want_resetable) { want = ALL_DIRS; want_resetable = 0; } want = want == ALL_DIRS ? BIN_DIR : want | BIN_DIR; opt_f_missing = 0; break; case 'm': if (want_resetable) { want = ALL_DIRS; want_resetable = 0; } want = want == ALL_DIRS ? MAN_DIR : want | MAN_DIR; opt_f_missing = 0; break; case 's': if (want_resetable) { want = ALL_DIRS; want_resetable = 0; } want = want == ALL_DIRS ? SRC_DIR : want | SRC_DIR; opt_f_missing = 0; break; case 'l': list_dirlist(ls); break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'h': usage(stdout); default: usage(stderr); } if (arg_i < i) /* moved to the next argv[] item */ break; } } free_dirlist(&ls, ALL_DIRS); if (opt_f_missing) errx(EXIT_FAILURE, _("option -f is missing")); return EXIT_SUCCESS; }