xref: /minix3/minix/commands/printroot/printroot.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1  /* printroot - print root device on stdout	Author: Bruce Evans */
2  
3  /* This program figures out what the root device is by doing a stat on it, and
4   * then searching /dev until it finds an entry with the same device number.
5   *
6   *  9 Dec 1989	- clean up for 1.5 - full prototypes (BDE)
7   * 15 Oct 1989	- avoid ACK cc bugs (BDE):
8   *		- sizeof "foo" is 2 (from wrong type char *) instead of 4
9   *		- char foo[10] = "bar"; allocates 4 bytes instead of 10
10   *  1 Oct 1989	- Minor changes by Andy Tanenbaum
11   *  5 Oct 1992	- Use readdir (kjb)
12   * 26 Nov 1994	- Flag -r: print just the root device (kjb)
13   */
14  
15  #include <sys/types.h>
16  #include <sys/stat.h>
17  #include <fcntl.h>
18  #include <limits.h>
19  #include <stdlib.h>
20  #include <string.h>
21  #include <unistd.h>
22  #include <minix/minlib.h>
23  #include <dirent.h>
24  
25  static char DEV_PATH[] = "/dev/";	/* #define would step on sizeof bug */
26  static char MESSAGE[] = " / ";	/* ditto */
27  #define UNKNOWN_DEV	"/dev/unknown"
28  #define ROOT		"/"
29  int rflag;
30  
31  int main(int argc, char **argv);
32  void done(char *name, int status);
33  
main(argc,argv)34  int main(argc, argv)
35  int argc;
36  char **argv;
37  {
38    DIR *dp;
39    struct dirent *entry;
40    struct stat filestat, rootstat;
41    static char namebuf[sizeof DEV_PATH + NAME_MAX];
42  
43    rflag = (argc > 1 && strcmp(argv[1], "-r") == 0);
44  
45    if (stat(ROOT, &rootstat) == 0 && (dp = opendir(DEV_PATH)) != (DIR *) NULL) {
46  	while ((entry = readdir(dp)) != (struct dirent *) NULL) {
47  		strcpy(namebuf, DEV_PATH);
48  		strcat(namebuf, entry->d_name);
49  		if (stat(namebuf, &filestat) != 0) continue;
50  		if ((filestat.st_mode & S_IFMT) != S_IFBLK) continue;
51  		if (filestat.st_rdev != rootstat.st_dev) continue;
52  		done(namebuf, 0);
53  	}
54    }
55    done(UNKNOWN_DEV, 1);
56    return(0);			/* not reached */
57  }
58  
done(name,status)59  void done(name, status)
60  char *name;
61  int status;
62  {
63    int v;
64  
65    write(1, name, strlen(name));
66    if (rflag) {
67  	write(1, "\n", 1);
68  	exit(status);
69    }
70    write(1, MESSAGE, sizeof MESSAGE - 1);
71    v = fsversion(name, "printroot");	/* determine file system version */
72    switch (v) {
73  	case FSVERSION_MFS1: write(1, "1 rw\n", 5);	break;
74  	case FSVERSION_MFS2: write(1, "2 rw\n", 5);	break;
75  	case FSVERSION_MFS3: write(1, "3 rw\n", 5);	break;
76  	case FSVERSION_EXT2: write(1, "ext2 rw\n", 8);	break;
77  	default: write(1, "0 rw\n", 5);			break;
78    }
79    exit(status);
80  }
81