VM: munmap used by VM for itself is no longer used
[minix.git] / lib / libcompat_minix / mtab.c
blobc40a597bded7457fe9b02e604751e2200c91af6f
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
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 <lib.h>
37 #include <minix/minlib.h>
38 #include <ctype.h>
39 #include <fcntl.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <stdio.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
56 *rw_flag);
57 int put_mtab_entry(char *special, char *mounted_on, char *version, char
58 *rw_flag);
59 static void err(char *prog_name, char *str );
62 int load_mtab(prog_name)
63 char *prog_name;
65 /* Read in /etc/mtab and store it in /etc/mtab. */
67 int fd, n;
68 char *ptr;
70 /* Open the file. */
71 fd = open(etc_mtab, O_RDONLY);
72 if (fd < 0) {
73 err(prog_name, ": cannot open ");
74 return(-1);
77 /* File opened. Read it in. */
78 n = read(fd, mtab_in, BUF_SIZE);
79 if (n <= 0) {
80 /* Read failed. */
81 err(prog_name, ": cannot read ");
82 return(-1);
84 if (n == BUF_SIZE) {
85 /* Some nut has mounted 50 file systems or something like that. */
86 std_err(prog_name);
87 std_err(": file too large: ");
88 std_err(etc_mtab);
89 return(-1);
92 close(fd);
94 /* Replace all the whitespace by '\0'. */
95 ptr = mtab_in;
96 while (*ptr != '\0') {
97 if (isspace(*ptr)) *ptr = '\0';
98 ptr++;
100 return(0);
104 int rewrite_mtab(prog_name)
105 char *prog_name;
107 /* Write mtab_out to /etc/mtab. */
109 int fd, n;
111 /* Do a creat to truncate the file. */
112 fd = creat(etc_mtab, 0777);
113 if (fd < 0) {
114 err(prog_name, ": cannot overwrite ");
115 return(-1);
118 /* File created. Write it. */
119 n = write(fd, mtab_out, (unsigned int)(optr - mtab_out));
120 if (n <= 0) {
121 /* Write failed. */
122 err(prog_name, " could not write ");
123 return(-1);
126 close(fd);
127 return(0);
131 int get_mtab_entry(special, mounted_on, version, rw_flag)
132 char *special;
133 char *mounted_on;
134 char *version;
135 char *rw_flag;
137 /* Return the next entry from mtab_in. */
139 if (iptr >= &mtab_in[BUF_SIZE]) {
140 special[0] = '\0';
141 return(-1);
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++;
159 return(0);
163 int put_mtab_entry(special, mounted_on, version, rw_flag)
164 char *special;
165 char *mounted_on;
166 char *version;
167 char *rw_flag;
169 /* Append an entry to the mtab_out buffer. */
171 int n1, n2, n3, n4;
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);
180 optr += n1;
181 *optr++ = ' ';
183 strcpy(optr, mounted_on);
184 optr += n2;
185 *optr++ = ' ';
187 strcpy(optr, version);
188 optr += n3;
189 *optr++ = ' ';
191 strcpy(optr, rw_flag);
192 optr += n4;
193 *optr++ = '\n';
194 return(0);
198 static void
199 err(prog_name, str)
200 char *prog_name, *str;
202 std_err(prog_name);
203 std_err(str);
204 std_err(etc_mtab);
205 perror(" ");