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>
36 #include <minix/minlib.h>
44 #define BUF_SIZE 512 /* size of the /etc/mtab buffer */
46 char *etc_mtab
= "/etc/mtab"; /* name of the /etc/mtab file */
47 static char mtab_in
[BUF_SIZE
+1]; /* holds /etc/mtab when it is read in */
48 static char mtab_out
[BUF_SIZE
+1]; /* buf to build /etc/mtab for output later */
49 static char *iptr
= mtab_in
; /* pointer to next line to feed out. */
50 static char *optr
= mtab_out
; /* pointer to place where next line goes */
52 _PROTOTYPE(int load_mtab
, (char *prog_name
));
53 _PROTOTYPE(int rewrite_mtab
, (char *prog_name
));
54 _PROTOTYPE(int get_mtab_entry
, (char *special
, char *mounted_on
,
55 char *version
, char *rw_flag
));
56 _PROTOTYPE(int put_mtab_entry
, (char *special
, char *mounted_on
,
57 char *version
, char *rw_flag
));
58 _PROTOTYPE(void err
, (char *prog_name
, char *str
));
61 int load_mtab(prog_name
)
64 /* Read in /etc/mtab and store it in /etc/mtab. */
70 fd
= open(etc_mtab
, O_RDONLY
);
72 err(prog_name
, ": cannot open ");
76 /* File opened. Read it in. */
77 n
= read(fd
, mtab_in
, BUF_SIZE
);
80 err(prog_name
, ": cannot read ");
84 /* Some nut has mounted 50 file systems or something like that. */
86 std_err(": file too large: ");
93 /* Replace all the whitespace by '\0'. */
95 while (*ptr
!= '\0') {
96 if (isspace(*ptr
)) *ptr
= '\0';
103 int rewrite_mtab(prog_name
)
106 /* Write mtab_out to /etc/mtab. */
110 /* Do a creat to truncate the file. */
111 fd
= creat(etc_mtab
, 0777);
113 err(prog_name
, ": cannot overwrite ");
117 /* File created. Write it. */
118 n
= write(fd
, mtab_out
, (unsigned int)(optr
- mtab_out
));
121 err(prog_name
, " could not write ");
130 int get_mtab_entry(special
, mounted_on
, version
, rw_flag
)
136 /* Return the next entry from mtab_in. */
138 if (iptr
>= &mtab_in
[BUF_SIZE
]) {
143 strcpy(special
, iptr
);
144 while (isprint(*iptr
)) iptr
++;
145 while (*iptr
== '\0'&& iptr
< &mtab_in
[BUF_SIZE
]) iptr
++;
147 strcpy(mounted_on
, iptr
);
148 while (isprint(*iptr
)) iptr
++;
149 while (*iptr
== '\0'&& iptr
< &mtab_in
[BUF_SIZE
]) iptr
++;
151 strcpy(version
, iptr
);
152 while (isprint(*iptr
)) iptr
++;
153 while (*iptr
== '\0'&& iptr
< &mtab_in
[BUF_SIZE
]) iptr
++;
155 strcpy(rw_flag
, iptr
);
156 while (isprint(*iptr
)) iptr
++;
157 while (*iptr
== '\0'&& iptr
< &mtab_in
[BUF_SIZE
]) iptr
++;
162 int put_mtab_entry(special
, mounted_on
, version
, rw_flag
)
168 /* Append an entry to the mtab_out buffer. */
172 n1
= strlen(special
);
173 n2
= strlen(mounted_on
);
174 n3
= strlen(version
);
175 n4
= strlen(rw_flag
);
177 if (optr
+ n1
+ n2
+ n3
+ n4
+ 5 >= &mtab_out
[BUF_SIZE
]) return(-1);
178 strcpy(optr
, special
);
182 strcpy(optr
, mounted_on
);
186 strcpy(optr
, version
);
190 strcpy(optr
, rw_flag
);
199 char *prog_name
, *str
;