Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||||
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3 | * Utility routines. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
"Robert P. J. Day" | 5d8843e | 2006-07-10 11:41:19 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
9 | |||||
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
11 | |||||
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 12 | char *find_block_device(char *path) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 13 | { |
14 | DIR *dir; | ||||
15 | struct dirent *entry; | ||||
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 16 | struct stat st; |
Matt Kraai | 774d135 | 2001-05-23 14:45:09 +0000 | [diff] [blame] | 17 | dev_t dev; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 18 | char *retpath=NULL; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 19 | |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame^] | 20 | if (stat(path, &st) || !(dir = opendir("/dev"))) |
21 | return NULL; | ||||
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 22 | dev = (st.st_mode & S_IFMT) == S_IFBLK ? st.st_rdev : st.st_dev; |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame^] | 23 | while ((entry = readdir(dir)) != NULL) { |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 24 | char devpath[PATH_MAX]; |
25 | sprintf(devpath,"/dev/%s", entry->d_name); | ||||
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame^] | 26 | if (!stat(devpath, &st) && S_ISBLK(st.st_mode) && st.st_rdev == dev) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 27 | retpath = xstrdup(devpath); |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 28 | break; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 29 | } |
30 | } | ||||
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 31 | closedir(dir); |
32 | |||||
33 | return retpath; | ||||
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 34 | } |