aboutsummaryrefslogtreecommitdiff
path: root/libbb/find_root_device.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/find_root_device.c')
-rw-r--r--libbb/find_root_device.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/libbb/find_root_device.c b/libbb/find_root_device.c
new file mode 100644
index 000000000..71b79b8d0
--- /dev/null
+++ b/libbb/find_root_device.c
@@ -0,0 +1,33 @@
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
10#include "libbb.h"
11
12char *find_block_device(char *path)
13{
14 DIR *dir;
15 struct dirent *entry;
16 struct stat st;
17 dev_t dev;
18 char *retpath=NULL;
19
20 if(stat(path, &st) || !(dir = opendir("/dev"))) return NULL;
21 dev = (st.st_mode & S_IFMT) == S_IFBLK ? st.st_rdev : st.st_dev;
22 while((entry = readdir(dir)) != NULL) {
23 char devpath[PATH_MAX];
24 sprintf(devpath,"/dev/%s", entry->d_name);
25 if(!stat(devpath, &st) && S_ISBLK(st.st_mode) && st.st_rdev == dev) {
26 retpath = xstrdup(devpath);
27 break;
28 }
29 }
30 closedir(dir);
31
32 return retpath;
33}