reindent-20030715
[openafs.git] / src / vfsck / dirutils.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <dirent.h>
19 #include <afs/partition.h>
20
21 /* ensure that we don't have a "/" instead of a "/dev/rxd0a" type of device.
22  * Overwrites abuffer with the corrected name.
23  */
24 EnsureDevice(abuffer)
25      char *abuffer;
26 {
27     struct dirent *dp;
28     char pbuffer[128];
29     struct stat tstat;
30     DIR *dirp;
31     char *dirName, dirn[100];
32     int code, i;
33     short dev;
34
35     code = stat(abuffer, &tstat);
36     if (code)
37         return code;
38     if (((tstat.st_mode & S_IFMT) == S_IFBLK)
39         || ((tstat.st_mode & S_IFMT) == S_IFCHR)) {
40         return 0;               /* already a block or char device */
41     }
42     /* otherwise, assume we've got a normal file, and we look up its device */
43     dev = tstat.st_dev;         /* remember device for this file */
44
45     /* now, look in /dev for the appropriate file */
46     dirp = opendir(dirName = AFS_DSKDEV);
47     while (dp = readdir(dirp)) {
48         strcpy(pbuffer, dirName);
49         strcat(pbuffer, "/");
50         strcat(pbuffer, dp->d_name);
51         if (stat(pbuffer, &tstat) != -1 && (tstat.st_mode & S_IFMT) == S_IFBLK
52             && (tstat.st_rdev == dev)) {
53             strcpy(abuffer, pbuffer);
54             closedir(dirp);
55             return 0;
56         }
57     }
58     closedir(dirp);
59
60     return 1;                   /* failed */
61 }