]>
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
34 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
37 static const char *devfs_hier
[] = {
38 "host", "bus", "target", "lun", 0
41 char *base_device(const char *device
)
44 const char **hier
, *disk
;
47 str
= malloc(strlen(device
)+1);
53 /* Skip over /dev/; if it's not present, give up. */
54 if (strncmp(cp
, "/dev/", 5) != 0)
58 /* Skip over /dev/dsk/... */
59 if (strncmp(cp
, "dsk/", 4) == 0)
63 * For md devices, we treat them all as if they were all
64 * on one disk, since we don't know how to parallelize them.
66 if (cp
[0] == 'm' && cp
[1] == 'd') {
71 /* Handle DAC 960 devices */
72 if (strncmp(cp
, "rd/", 3) == 0) {
74 if (cp
[0] != 'c' || cp
[2] != 'd' ||
75 !isdigit(cp
[1]) || !isdigit(cp
[3]))
81 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
82 if ((cp
[0] == 'h' || cp
[0] == 's') && (cp
[1] == 'd')) {
84 /* If there's a single number after /dev/hd, skip it */
87 /* What follows must be an alpha char, or give up */
94 /* Now let's handle devfs (ugh) names */
96 if (strncmp(cp
, "ide/", 4) == 0)
98 if (strncmp(cp
, "scsi/", 5) == 0)
103 * Now we proceed down the expected devfs hierarchy.
104 * i.e., .../host1/bus2/target3/lun4/...
105 * If we don't find the expected token, followed by
106 * some number of digits at each level, abort.
108 for (hier
= devfs_hier
; *hier
; hier
++) {
110 if (strncmp(cp
, *hier
, len
) != 0)
113 while (*cp
!= '/' && *cp
!= 0) {
124 /* Now handle devfs /dev/disc or /dev/disk names */
126 if (strncmp(cp
, "discs/", 6) == 0)
128 else if (strncmp(cp
, "disks/", 6) == 0)
132 if (strncmp(cp
, disk
, 4) != 0)
135 while (*cp
!= '/' && *cp
!= 0) {
150 int main(int argc
, char** argv
)
156 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
158 cp
= strchr(buf
, '\n');
161 cp
= strchr(buf
, '\t');
164 base
= base_device(buf
);
165 printf("%s\t%s\n", buf
, base
? base
: "NONE");