3
0

find_root_device.c 747 B

123456789101112131415161718192021222324252627282930313233
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. #include "libbb.h"
  10. char *find_block_device(char *path)
  11. {
  12. DIR *dir;
  13. struct dirent *entry;
  14. struct stat st;
  15. dev_t dev;
  16. char *retpath=NULL;
  17. if(stat(path, &st) || !(dir = opendir("/dev"))) return NULL;
  18. dev = (st.st_mode & S_IFMT) == S_IFBLK ? st.st_rdev : st.st_dev;
  19. while((entry = readdir(dir)) != NULL) {
  20. char devpath[PATH_MAX];
  21. sprintf(devpath,"/dev/%s", entry->d_name);
  22. if(!stat(devpath, &st) && S_ISBLK(st.st_mode) && st.st_rdev == dev) {
  23. retpath = xstrdup(devpath);
  24. break;
  25. }
  26. }
  27. closedir(dir);
  28. return retpath;
  29. }