blob: 675f8d2f501b3d539d403c70143b047791d4b4cf [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
Rob Landley3c6bfe32005-10-25 02:22:13 +000010#include <limits.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000011#include <stdio.h>
12#include <string.h>
13#include <dirent.h>
Eric Andersenc911a432001-05-15 17:42:16 +000014#include <stdlib.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000015#include "libbb.h"
16
Rob Landleydfba7412006-03-06 20:47:33 +000017char *find_block_device(char *path)
Eric Andersenaad1a882001-03-16 22:47:14 +000018{
19 DIR *dir;
20 struct dirent *entry;
Rob Landley6a6798b2005-08-10 20:35:54 +000021 struct stat st;
Matt Kraai774d1352001-05-23 14:45:09 +000022 dev_t dev;
Rob Landley6a6798b2005-08-10 20:35:54 +000023 char *retpath=NULL;
Eric Andersenaad1a882001-03-16 22:47:14 +000024
Rob Landley6a6798b2005-08-10 20:35:54 +000025 if(stat(path, &st) || !(dir = opendir("/dev"))) return NULL;
26 dev = (st.st_mode & S_IFMT) == S_IFBLK ? st.st_rdev : st.st_dev;
27 while((entry = readdir(dir)) != NULL) {
28 char devpath[PATH_MAX];
29 sprintf(devpath,"/dev/%s", entry->d_name);
30 if(!stat(devpath, &st) && S_ISBLK(st.st_mode) && st.st_rdev == dev) {
31 retpath = bb_xstrdup(devpath);
32 break;
Eric Andersenaad1a882001-03-16 22:47:14 +000033 }
34 }
Rob Landley6a6798b2005-08-10 20:35:54 +000035 closedir(dir);
36
37 return retpath;
Eric Andersenaad1a882001-03-16 22:47:14 +000038}