]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - misc/base_device.c
4 * Return the "base device" given a particular device; this is used to
5 * assure that we only fsck one partition on a particular drive at any
6 * one time. Otherwise, the disk heads will be seeking all over the
7 * place. If the base device can not be determined, return NULL.
9 * The base_device() function returns an allocated string which must
12 * Written by Theodore Ts'o, <tytso@mit.edu>
14 * Copyright (C) 2000 Theodore Ts'o.
17 * This file may be redistributed under the terms of the GNU Public
35 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
38 static const char *devfs_hier
[] = {
39 "host", "bus", "target", "lun", 0
42 char *base_device(const char *device
)
45 const char **hier
, *disk
;
48 str
= malloc(strlen(device
)+1);
54 /* Skip over /dev/; if it's not present, give up. */
55 if (strncmp(cp
, "/dev/", 5) != 0)
59 /* Skip over /dev/dsk/... */
60 if (strncmp(cp
, "dsk/", 4) == 0)
64 * For md devices, we treat them all as if they were all
65 * on one disk, since we don't know how to parallelize them.
67 if (cp
[0] == 'm' && cp
[1] == 'd') {
72 /* Handle DAC 960 devices */
73 if (strncmp(cp
, "rd/", 3) == 0) {
75 if (cp
[0] != 'c' || cp
[2] != 'd' ||
76 !isdigit(cp
[1]) || !isdigit(cp
[3]))
82 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
83 if ((cp
[0] == 'h' || cp
[0] == 's') && (cp
[1] == 'd')) {
85 /* If there's a single number after /dev/hd, skip it */
88 /* What follows must be an alpha char, or give up */
95 /* Now let's handle devfs (ugh) names */
97 if (strncmp(cp
, "ide/", 4) == 0)
99 if (strncmp(cp
, "scsi/", 5) == 0)
104 * Now we proceed down the expected devfs hierarchy.
105 * i.e., .../host1/bus2/target3/lun4/...
106 * If we don't find the expected token, followed by
107 * some number of digits at each level, abort.
109 for (hier
= devfs_hier
; *hier
; hier
++) {
111 if (strncmp(cp
, *hier
, len
) != 0)
114 while (*cp
!= '/' && *cp
!= 0) {
125 /* Now handle devfs /dev/disc or /dev/disk names */
127 if (strncmp(cp
, "discs/", 6) == 0)
129 else if (strncmp(cp
, "disks/", 6) == 0)
133 if (strncmp(cp
, disk
, 4) != 0)
136 while (*cp
!= '/' && *cp
!= 0) {
151 int main(int argc
, char** argv
)
157 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
159 cp
= strchr(buf
, '\n');
162 cp
= strchr(buf
, '\t');
165 base
= base_device(buf
);
166 printf("%s\t%s\n", buf
, base
? base
: "NONE");