blob: 1d74d1ea822c17741b33b04783528051be9ac147 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersenbdfd0d72001-10-24 05:00:29 +00003 * Utility routines.
Eric Andersenaad1a882001-03-16 22:47:14 +00004 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
"Robert P. J. Day"5d8843e2006-07-10 11:41:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
9
Eric Andersenaad1a882001-03-16 22:47:14 +000010#include "libbb.h"
11
Rob Landleydfba7412006-03-06 20:47:33 +000012char *find_block_device(char *path)
Eric Andersenaad1a882001-03-16 22:47:14 +000013{
14 DIR *dir;
15 struct dirent *entry;
Rob Landley6a6798b2005-08-10 20:35:54 +000016 struct stat st;
Matt Kraai774d1352001-05-23 14:45:09 +000017 dev_t dev;
Rob Landley6a6798b2005-08-10 20:35:54 +000018 char *retpath=NULL;
Eric Andersenaad1a882001-03-16 22:47:14 +000019
Denis Vlasenkobf0a2012006-12-26 10:42:51 +000020 if (stat(path, &st) || !(dir = opendir("/dev")))
21 return NULL;
Rob Landley6a6798b2005-08-10 20:35:54 +000022 dev = (st.st_mode & S_IFMT) == S_IFBLK ? st.st_rdev : st.st_dev;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +000023 while ((entry = readdir(dir)) != NULL) {
Rob Landley6a6798b2005-08-10 20:35:54 +000024 char devpath[PATH_MAX];
25 sprintf(devpath,"/dev/%s", entry->d_name);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +000026 if (!stat(devpath, &st) && S_ISBLK(st.st_mode) && st.st_rdev == dev) {
Rob Landleyd921b2e2006-08-03 15:41:12 +000027 retpath = xstrdup(devpath);
Rob Landley6a6798b2005-08-10 20:35:54 +000028 break;
Eric Andersenaad1a882001-03-16 22:47:14 +000029 }
30 }
Rob Landley6a6798b2005-08-10 20:35:54 +000031 closedir(dir);
32
33 return retpath;
Eric Andersenaad1a882001-03-16 22:47:14 +000034}