1 /*
2  * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
3  *
4  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5  */
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <mntent.h>
11 #include <bits/uClibc_mutex.h>
12 
13 __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
14 
15 
16 
17 /* Reentrant version of getmntent.  */
getmntent_r(FILE * filep,struct mntent * mnt,char * buff,int bufsize)18 struct mntent *getmntent_r (FILE *filep,
19 	struct mntent *mnt, char *buff, int bufsize)
20 {
21 	static const char sep[] = " \t\n";
22 
23 	char *cp, *ptrptr;
24 
25 	if (!filep || !mnt || !buff)
26 	    return NULL;
27 
28 	/* Loop on the file, skipping comment lines. - FvK 03/07/93 */
29 	while ((cp = fgets(buff, bufsize, filep)) != NULL) {
30 		if (buff[0] == '#' || buff[0] == '\n')
31 			continue;
32 		break;
33 	}
34 
35 	/* At the EOF, the buffer should be unchanged. We should
36 	 * check the return value from fgets ().
37 	 */
38 	if (cp == NULL)
39 		return NULL;
40 
41 	ptrptr = 0;
42 	mnt->mnt_fsname = strtok_r(buff, sep, &ptrptr);
43 	if (mnt->mnt_fsname == NULL)
44 		return NULL;
45 
46 	mnt->mnt_dir = strtok_r(NULL, sep, &ptrptr);
47 	if (mnt->mnt_dir == NULL)
48 		return NULL;
49 
50 	mnt->mnt_type = strtok_r(NULL, sep, &ptrptr);
51 	if (mnt->mnt_type == NULL)
52 		return NULL;
53 
54 	mnt->mnt_opts = strtok_r(NULL, sep, &ptrptr);
55 	if (mnt->mnt_opts == NULL)
56 		mnt->mnt_opts = "";
57 
58 	cp = strtok_r(NULL, sep, &ptrptr);
59 	mnt->mnt_freq = (cp != NULL) ? atoi(cp) : 0;
60 
61 	cp = strtok_r(NULL, sep, &ptrptr);
62 	mnt->mnt_passno = (cp != NULL) ? atoi(cp) : 0;
63 
64 	return mnt;
65 }
libc_hidden_def(getmntent_r)66 libc_hidden_def(getmntent_r)
67 
68 struct mntent *getmntent(FILE * filep)
69 {
70     struct mntent *tmp;
71     static char *buff = NULL;
72     static struct mntent mnt;
73     __UCLIBC_MUTEX_LOCK(mylock);
74 
75     if (!buff) {
76             buff = malloc(BUFSIZ);
77 		if (!buff)
78 		    abort();
79     }
80 
81     tmp = getmntent_r(filep, &mnt, buff, BUFSIZ);
82     __UCLIBC_MUTEX_UNLOCK(mylock);
83     return(tmp);
84 }
85 
addmntent(FILE * filep,const struct mntent * mnt)86 int addmntent(FILE * filep, const struct mntent *mnt)
87 {
88 	if (fseek(filep, 0, SEEK_END) < 0)
89 		return 1;
90 
91 	return (fprintf (filep, "%s %s %s %s %d %d\n", mnt->mnt_fsname, mnt->mnt_dir,
92 		 mnt->mnt_type, mnt->mnt_opts, mnt->mnt_freq, mnt->mnt_passno) < 0 ? 1 : 0);
93 }
94 
hasmntopt(const struct mntent * mnt,const char * opt)95 char *hasmntopt(const struct mntent *mnt, const char *opt)
96 {
97 	return strstr(mnt->mnt_opts, opt);
98 }
99 
setmntent(const char * name,const char * mode)100 FILE *setmntent(const char *name, const char *mode)
101 {
102 	return fopen(name, mode);
103 }
libc_hidden_def(setmntent)104 libc_hidden_def(setmntent)
105 
106 int endmntent(FILE * filep)
107 {
108 	if (filep != NULL)
109 		fclose(filep);
110 	return 1;
111 }
112 libc_hidden_def(endmntent)
113