blob: 71b79b8d09178950dca2aafb26067740531c552e [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
Rob Landley6a6798b2005-08-10 20:35:54 +000020 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) {
Rob Landleyd921b2e2006-08-03 15:41:12 +000026 retpath = xstrdup(devpath);
Rob Landley6a6798b2005-08-10 20:35:54 +000027 break;
Eric Andersenaad1a882001-03-16 22:47:14 +000028 }
29 }
Rob Landley6a6798b2005-08-10 20:35:54 +000030 closedir(dir);
31
32 return retpath;
Eric Andersenaad1a882001-03-16 22:47:14 +000033}