make vfs & filesystems use failable copying
[minix3.git] / lib / libcompat_minix / mtab.c
blob88cd1869e2e6ac5786482e762265a76e1cdb1384
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, const char *str);
55 int load_mtab(char *prog_name)
57 /* Read in /etc/mtab and store it in /etc/mtab. */
59 int fd, n;
60 char *ptr;
62 /* Open the file. */
63 fd = open(etc_mtab, O_RDONLY);
64 if (fd < 0) {
65 err(prog_name, ": cannot open ");
66 return(-1);
69 /* File opened. Read it in. */
70 n = read(fd, mtab_in, BUF_SIZE);
71 if (n <= 0) {
72 /* Read failed. */
73 err(prog_name, ": cannot read ");
74 return(-1);
76 if (n == BUF_SIZE) {
77 /* Some nut has mounted 50 file systems or something like that. */
78 std_err(prog_name);
79 std_err(": file too large: ");
80 std_err(etc_mtab);
81 return(-1);
84 close(fd);
86 ptr = mtab_in;
88 return(0);
91 int get_mtab_entry(char dev[PATH_MAX], char mount_point[PATH_MAX],
92 char type[MNTNAMELEN], char flags[MNTFLAGLEN])
94 /* Return the next entry from mtab_in. */
96 int r;
98 if (iptr >= &mtab_in[BUF_SIZE]) {
99 dev[0] = '\0';
100 return(-1);
103 r = sscanf(iptr, "%s on %s type %s (%[^)]s\n",
104 dev, mount_point, type, flags);
105 if (r != 4) {
106 dev[0] = '\0';
107 return(-1);
110 iptr = strchr(iptr, '\n'); /* Find end of line */
111 if (iptr != NULL) iptr++; /* Move to next line */
112 return(0);
116 static void err(char *prog_name, const char *str)
118 std_err(prog_name);
119 std_err(str);
120 std_err(etc_mtab);
121 perror(" ");