kernel: scheduling fix for ARM
[minix.git] / lib / libcompat_minix / mtab.c
bloba38dde4df835dcd651a3c41ae5f761dbe521649a
1 /* This package consists of 4 routines for handling the /etc/mtab file.
2 * The /etc/mtab file contains information about the root and mounted file
3 * systems as a series of lines, each one with exactly four fields separated
4 * by one space as follows:
6 * special mounted_on version rw_flag
8 * where
9 * special is the name of the block special file
10 * mounted_on is the directory on which it is mounted
11 * version is either 1 or 2 for MINIX V1 and V2 file systems
12 * rw_flag is rw or ro for read/write or read only
14 * An example /etc/mtab:
16 * /dev/ram / 2 rw
17 * /dev/hd1 /usr 2 rw
18 * /dev/fd0 /user 1 ro
21 * The four routines for handling /etc/mtab are as follows. They use two
22 * (hidden) internal buffers, mtab_in for input and mtab_out for output.
24 * load_mtab(&prog_name) - read /etc/mtab into mtab_in
25 * get_mtab_entry(&s1, &s2, &s3, &s4) - arrays that are filled in
27 * If load_mtab works, it returns 0. If it fails, it prints its own error
28 * message on stderr and returns -1. When get_mtab_entry
29 * runs out of entries to return, it sets the first pointer to NULL and returns
30 * -1 instead of 0.
33 #include <sys/types.h>
34 #include <lib.h>
35 #include <minix/minlib.h>
36 #include <ctype.h>
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <stdio.h>
43 #define BUF_SIZE 512 /* size of the /etc/mtab buffer */
45 char *etc_mtab = "/etc/mtab"; /* name of the /etc/mtab file */
46 static char mtab_in[BUF_SIZE+1]; /* holds /etc/mtab when it is read in */
47 static char *iptr = mtab_in; /* pointer to next line to feed out. */
49 int load_mtab(char *prog_name );
50 int get_mtab_entry(char dev[PATH_MAX], char mount_point[PATH_MAX],
51 char type[MNTNAMELEN], char flags[MNTFLAGLEN]);
52 static void err(char *prog_name, char *str );
55 int load_mtab(prog_name)
56 char *prog_name;
58 /* Read in /etc/mtab and store it in /etc/mtab. */
60 int fd, n;
61 char *ptr;
63 /* Open the file. */
64 fd = open(etc_mtab, O_RDONLY);
65 if (fd < 0) {
66 err(prog_name, ": cannot open ");
67 return(-1);
70 /* File opened. Read it in. */
71 n = read(fd, mtab_in, BUF_SIZE);
72 if (n <= 0) {
73 /* Read failed. */
74 err(prog_name, ": cannot read ");
75 return(-1);
77 if (n == BUF_SIZE) {
78 /* Some nut has mounted 50 file systems or something like that. */
79 std_err(prog_name);
80 std_err(": file too large: ");
81 std_err(etc_mtab);
82 return(-1);
85 close(fd);
87 ptr = mtab_in;
89 return(0);
92 int get_mtab_entry(char dev[PATH_MAX], char mount_point[PATH_MAX],
93 char type[MNTNAMELEN], char flags[MNTFLAGLEN])
95 /* Return the next entry from mtab_in. */
97 int r;
99 if (iptr >= &mtab_in[BUF_SIZE]) {
100 dev[0] = '\0';
101 return(-1);
104 r = sscanf(iptr, "%s on %s type %s (%[^)]s\n",
105 dev, mount_point, type, flags);
106 if (r != 4) {
107 dev[0] = '\0';
108 return(-1);
111 iptr = strchr(iptr, '\n'); /* Find end of line */
112 if (iptr != NULL) iptr++; /* Move to next line */
113 return(0);
117 static void
118 err(prog_name, str)
119 char *prog_name, *str;
121 std_err(prog_name);
122 std_err(str);
123 std_err(etc_mtab);
124 perror(" ");