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 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <dirent.h> |
Eric Andersen | c911a43 | 2001-05-15 17:42:16 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 26 | #include "libbb.h" |
| 27 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame^] | 28 | extern char *find_block_device(char *path) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 29 | { |
| 30 | DIR *dir; |
| 31 | struct dirent *entry; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame^] | 32 | struct stat st; |
Matt Kraai | 774d135 | 2001-05-23 14:45:09 +0000 | [diff] [blame] | 33 | dev_t dev; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame^] | 34 | char *retpath=NULL; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 35 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame^] | 36 | if(stat(path, &st) || !(dir = opendir("/dev"))) return NULL; |
| 37 | dev = (st.st_mode & S_IFMT) == S_IFBLK ? st.st_rdev : st.st_dev; |
| 38 | while((entry = readdir(dir)) != NULL) { |
| 39 | char devpath[PATH_MAX]; |
| 40 | sprintf(devpath,"/dev/%s", entry->d_name); |
| 41 | if(!stat(devpath, &st) && S_ISBLK(st.st_mode) && st.st_rdev == dev) { |
| 42 | retpath = bb_xstrdup(devpath); |
| 43 | break; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 44 | } |
| 45 | } |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame^] | 46 | closedir(dir); |
| 47 | |
| 48 | return retpath; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 49 | } |