github.com/afumu/libc@v0.0.6/musl/src/misc/mntent.c (about)

     1  #include <stdio.h>
     2  #include <string.h>
     3  #include <mntent.h>
     4  #include <errno.h>
     5  
     6  static char *internal_buf;
     7  static size_t internal_bufsize;
     8  
     9  #define SENTINEL (char *)&internal_buf
    10  
    11  FILE *setmntent(const char *name, const char *mode)
    12  {
    13  	return fopen(name, mode);
    14  }
    15  
    16  int endmntent(FILE *f)
    17  {
    18  	if (f) fclose(f);
    19  	return 1;
    20  }
    21  
    22  struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
    23  {
    24  	int cnt, n[8], use_internal = (linebuf == SENTINEL);
    25  
    26  	mnt->mnt_freq = 0;
    27  	mnt->mnt_passno = 0;
    28  
    29  	do {
    30  		if (use_internal) {
    31  			getline(&internal_buf, &internal_bufsize, f);
    32  			linebuf = internal_buf;
    33  		} else {
    34  			fgets(linebuf, buflen, f);
    35  		}
    36  		if (feof(f) || ferror(f)) return 0;
    37  		if (!strchr(linebuf, '\n')) {
    38  			fscanf(f, "%*[^\n]%*[\n]");
    39  			errno = ERANGE;
    40  			return 0;
    41  		}
    42  		cnt = sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d",
    43  			n, n+1, n+2, n+3, n+4, n+5, n+6, n+7,
    44  			&mnt->mnt_freq, &mnt->mnt_passno);
    45  	} while (cnt < 2 || linebuf[n[0]] == '#');
    46  
    47  	linebuf[n[1]] = 0;
    48  	linebuf[n[3]] = 0;
    49  	linebuf[n[5]] = 0;
    50  	linebuf[n[7]] = 0;
    51  
    52  	mnt->mnt_fsname = linebuf+n[0];
    53  	mnt->mnt_dir = linebuf+n[2];
    54  	mnt->mnt_type = linebuf+n[4];
    55  	mnt->mnt_opts = linebuf+n[6];
    56  
    57  	return mnt;
    58  }
    59  
    60  struct mntent *getmntent(FILE *f)
    61  {
    62  	static struct mntent mnt;
    63  	return getmntent_r(f, &mnt, SENTINEL, 0);
    64  }
    65  
    66  int addmntent(FILE *f, const struct mntent *mnt)
    67  {
    68  	if (fseek(f, 0, SEEK_END)) return 1;
    69  	return fprintf(f, "%s\t%s\t%s\t%s\t%d\t%d\n",
    70  		mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts,
    71  		mnt->mnt_freq, mnt->mnt_passno) < 0;
    72  }
    73  
    74  char *hasmntopt(const struct mntent *mnt, const char *opt)
    75  {
    76  	return strstr(mnt->mnt_opts, opt);
    77  }