blob: 81824a216e6dec0d975607145120bd0241f11163 [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 *
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 Andersenc7bda1c2004-03-15 08:29:22 +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 {
Eric Andersen1cda7152004-01-13 11:39:22 +000041 /* This check is here in case they pass in /dev name */
42 if ((rootStat.st_mode & S_IFMT) == S_IFBLK)
43 dev = rootStat.st_rdev;
44 else
45 dev = rootStat.st_dev;
Eric Andersenaad1a882001-03-16 22:47:14 +000046
Eric Andersen8b113f92001-06-01 21:47:15 +000047 dir = opendir("/dev");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000048 if (!dir)
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 bb_perror_msg("could not open '/dev'");
Eric Andersen8b113f92001-06-01 21:47:15 +000050 else {
51 while((entry = readdir(dir)) != NULL) {
Eric Andersendd92c772003-06-20 09:25:34 +000052 const char *myname = entry->d_name;
Glenn L McGrath24833432003-06-10 17:22:49 +000053 /* Must skip ".." since that is "/", and so we
54 * would get a false positive on ".." */
Eric Andersendd92c772003-06-20 09:25:34 +000055 if (myname[0] == '.' && myname[1] == '.' && !myname[2])
Eric Andersen8b113f92001-06-01 21:47:15 +000056 continue;
Eric Andersenaad1a882001-03-16 22:47:14 +000057
Eric Andersendd92c772003-06-20 09:25:34 +000058 fileName = concat_path_file("/dev", myname);
Glenn L McGrath24833432003-06-10 17:22:49 +000059
Eric Andersen8b113f92001-06-01 21:47:15 +000060 /* Some char devices have the same dev_t as block
61 * devices, so make sure this is a block device */
Eric Andersenc7bda1c2004-03-15 08:29:22 +000062 if (stat(fileName, &statBuf) == 0 &&
Eric Andersen8b113f92001-06-01 21:47:15 +000063 S_ISBLK(statBuf.st_mode)!=0 &&
Eric Andersenc7bda1c2004-03-15 08:29:22 +000064 statBuf.st_rdev == dev)
Eric Andersen8b113f92001-06-01 21:47:15 +000065 break;
66 free(fileName);
67 fileName=NULL;
68 }
69 closedir(dir);
Eric Andersenaad1a882001-03-16 22:47:14 +000070 }
71 }
Eric Andersen8b113f92001-06-01 21:47:15 +000072 if(fileName==NULL)
Glenn L McGrath24833432003-06-10 17:22:49 +000073 fileName = bb_xstrdup("/dev/root");
Matt Kraai774d1352001-05-23 14:45:09 +000074 return fileName;
Eric Andersenaad1a882001-03-16 22:47:14 +000075}
76
77
78/* END CODE */
79/*
80Local Variables:
81c-file-style: "linux"
82c-basic-offset: 4
83tab-width: 4
84End:
85*/