blob: c595321dfe832f0197e2363047a387f997664edf [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 Andersencb81e642003-07-14 21:21:08 +00005 * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
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 Andersenaad1a882001-03-16 22:47:14 +000020 */
21
22#include <stdio.h>
23#include <string.h>
24#include <dirent.h>
Eric Andersenc911a432001-05-15 17:42:16 +000025#include <stdlib.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000026#include "libbb.h"
27
28
29
Eric Andersenc911a432001-05-15 17:42:16 +000030extern char *find_real_root_device_name(const char* name)
Eric Andersenaad1a882001-03-16 22:47:14 +000031{
32 DIR *dir;
33 struct dirent *entry;
34 struct stat statBuf, rootStat;
Eric Andersen8b113f92001-06-01 21:47:15 +000035 char *fileName = NULL;
Matt Kraai774d1352001-05-23 14:45:09 +000036 dev_t dev;
Eric Andersenaad1a882001-03-16 22:47:14 +000037
Eric Andersen8b113f92001-06-01 21:47:15 +000038 if (stat("/", &rootStat) != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 bb_perror_msg("could not stat '/'");
Eric Andersen8b113f92001-06-01 21:47:15 +000040 else {
41 if ((dev = rootStat.st_rdev)==0)
42 dev=rootStat.st_dev;
Eric Andersenaad1a882001-03-16 22:47:14 +000043
Eric Andersen8b113f92001-06-01 21:47:15 +000044 dir = opendir("/dev");
45 if (!dir)
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 bb_perror_msg("could not open '/dev'");
Eric Andersen8b113f92001-06-01 21:47:15 +000047 else {
48 while((entry = readdir(dir)) != NULL) {
Eric Andersendd92c772003-06-20 09:25:34 +000049 const char *myname = entry->d_name;
Glenn L McGrath24833432003-06-10 17:22:49 +000050 /* Must skip ".." since that is "/", and so we
51 * would get a false positive on ".." */
Eric Andersendd92c772003-06-20 09:25:34 +000052 if (myname[0] == '.' && myname[1] == '.' && !myname[2])
Eric Andersen8b113f92001-06-01 21:47:15 +000053 continue;
Eric Andersenaad1a882001-03-16 22:47:14 +000054
Eric Andersendd92c772003-06-20 09:25:34 +000055 fileName = concat_path_file("/dev", myname);
Glenn L McGrath24833432003-06-10 17:22:49 +000056
Eric Andersen8b113f92001-06-01 21:47:15 +000057 /* Some char devices have the same dev_t as block
58 * devices, so make sure this is a block device */
59 if (stat(fileName, &statBuf) == 0 &&
60 S_ISBLK(statBuf.st_mode)!=0 &&
61 statBuf.st_rdev == dev)
62 break;
63 free(fileName);
64 fileName=NULL;
65 }
66 closedir(dir);
Eric Andersenaad1a882001-03-16 22:47:14 +000067 }
68 }
Eric Andersen8b113f92001-06-01 21:47:15 +000069 if(fileName==NULL)
Glenn L McGrath24833432003-06-10 17:22:49 +000070 fileName = bb_xstrdup("/dev/root");
Matt Kraai774d1352001-05-23 14:45:09 +000071 return fileName;
Eric Andersenaad1a882001-03-16 22:47:14 +000072}
73
74
75/* END CODE */
76/*
77Local Variables:
78c-file-style: "linux"
79c-basic-offset: 4
80tab-width: 4
81End:
82*/