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: 5 * 6 * special mounted_on version rw_flag 7 * 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 13 * 14 * An example /etc/mtab: 15 * 16 * /dev/ram / 2 rw 17 * /dev/hd1 /usr 2 rw 18 * /dev/fd0 /user 1 ro 19 * 20 * 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. 23 * 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 * 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. 31 */ 32 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> 42 43 #define BUF_SIZE 512 /* size of the /etc/mtab buffer */ 44 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. */ 48 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); 53 54 55 int load_mtab(char *prog_name) 56 { 57 /* Read in /etc/mtab and store it in /etc/mtab. */ 58 59 int fd, n; 60 char *ptr; 61 62 /* Open the file. */ 63 fd = open(etc_mtab, O_RDONLY); 64 if (fd < 0) { 65 err(prog_name, ": cannot open "); 66 return(-1); 67 } 68 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); 75 } 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); 82 } 83 84 close(fd); 85 86 ptr = mtab_in; 87 88 return(0); 89 } 90 91 int get_mtab_entry(char dev[PATH_MAX], char mount_point[PATH_MAX], 92 char type[MNTNAMELEN], char flags[MNTFLAGLEN]) 93 { 94 /* Return the next entry from mtab_in. */ 95 96 int r; 97 98 if (iptr >= &mtab_in[BUF_SIZE]) { 99 dev[0] = '\0'; 100 return(-1); 101 } 102 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); 108 } 109 110 iptr = strchr(iptr, '\n'); /* Find end of line */ 111 if (iptr != NULL) iptr++; /* Move to next line */ 112 return(0); 113 114 } 115 116 static void err(char *prog_name, const char *str) 117 { 118 std_err(prog_name); 119 std_err(str); 120 std_err(etc_mtab); 121 perror(" "); 122 } 123