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
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:
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
26 * put_mtab_entry(&s1, &s2, &s3, &s4) - append a line to mtab_out
27 * rewrite_mtab(&prog_name) - write mtab_out to /etc/mtab
29 * If load_mtab and rewrite_mtab work, they return 0. If they fail, they
30 * print their own error messages on stderr and return -1. When get_mtab_entry
31 * runs out of entries to return, it sets the first pointer to NULL and returns
32 * -1 instead of 0. Also, rewrite_mtab returns -1 if it fails.
35 #include <sys/types.h>
37 #include <minix/minlib.h>
45 #define BUF_SIZE 512 /* size of the /etc/mtab buffer */
47 char *etc_mtab
= "/etc/mtab"; /* name of the /etc/mtab file */
48 static char mtab_in
[BUF_SIZE
+1]; /* holds /etc/mtab when it is read in */
49 static char mtab_out
[BUF_SIZE
+1]; /* buf to build /etc/mtab for output later */
50 static char *iptr
= mtab_in
; /* pointer to next line to feed out. */
51 static char *optr
= mtab_out
; /* pointer to place where next line goes */
53 int load_mtab(char *prog_name
);
54 int rewrite_mtab(char *prog_name
);
55 int get_mtab_entry(char *special
, char *mounted_on
, char *version
, char
57 int put_mtab_entry(char *special
, char *mounted_on
, char *version
, char
59 static void err(char *prog_name
, char *str
);
62 int load_mtab(prog_name
)
65 /* Read in /etc/mtab and store it in /etc/mtab. */
71 fd
= open(etc_mtab
, O_RDONLY
);
73 err(prog_name
, ": cannot open ");
77 /* File opened. Read it in. */
78 n
= read(fd
, mtab_in
, BUF_SIZE
);
81 err(prog_name
, ": cannot read ");
85 /* Some nut has mounted 50 file systems or something like that. */
87 std_err(": file too large: ");
94 /* Replace all the whitespace by '\0'. */
96 while (*ptr
!= '\0') {
97 if (isspace(*ptr
)) *ptr
= '\0';
104 int rewrite_mtab(prog_name
)
107 /* Write mtab_out to /etc/mtab. */
111 /* Do a creat to truncate the file. */
112 fd
= creat(etc_mtab
, 0777);
114 err(prog_name
, ": cannot overwrite ");
118 /* File created. Write it. */
119 n
= write(fd
, mtab_out
, (unsigned int)(optr
- mtab_out
));
122 err(prog_name
, " could not write ");
131 int get_mtab_entry(special
, mounted_on
, version
, rw_flag
)
137 /* Return the next entry from mtab_in. */
139 if (iptr
>= &mtab_in
[BUF_SIZE
]) {
144 strcpy(special
, iptr
);
145 while (isprint(*iptr
)) iptr
++;
146 while (*iptr
== '\0'&& iptr
< &mtab_in
[BUF_SIZE
]) iptr
++;
148 strcpy(mounted_on
, iptr
);
149 while (isprint(*iptr
)) iptr
++;
150 while (*iptr
== '\0'&& iptr
< &mtab_in
[BUF_SIZE
]) iptr
++;
152 strcpy(version
, iptr
);
153 while (isprint(*iptr
)) iptr
++;
154 while (*iptr
== '\0'&& iptr
< &mtab_in
[BUF_SIZE
]) iptr
++;
156 strcpy(rw_flag
, iptr
);
157 while (isprint(*iptr
)) iptr
++;
158 while (*iptr
== '\0'&& iptr
< &mtab_in
[BUF_SIZE
]) iptr
++;
163 int put_mtab_entry(special
, mounted_on
, version
, rw_flag
)
169 /* Append an entry to the mtab_out buffer. */
173 n1
= strlen(special
);
174 n2
= strlen(mounted_on
);
175 n3
= strlen(version
);
176 n4
= strlen(rw_flag
);
178 if (optr
+ n1
+ n2
+ n3
+ n4
+ 5 >= &mtab_out
[BUF_SIZE
]) return(-1);
179 strcpy(optr
, special
);
183 strcpy(optr
, mounted_on
);
187 strcpy(optr
, version
);
191 strcpy(optr
, rw_flag
);
200 char *prog_name
, *str
;