OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / misc / mntent / mntent.c
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
12 libc_hidden_proto(strstr)
13 libc_hidden_proto(strtok_r)
14 libc_hidden_proto(atoi)
15 libc_hidden_proto(fopen)
16 libc_hidden_proto(fclose)
17 libc_hidden_proto(fseek)
18 libc_hidden_proto(fgets)
19 libc_hidden_proto(abort)
20 libc_hidden_proto(fprintf)
21
22 #ifdef __UCLIBC_HAS_THREADS__
23 # include <pthread.h>
24 static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
25 #endif
26 #define LOCK    __pthread_mutex_lock(&mylock)
27 #define UNLOCK  __pthread_mutex_unlock(&mylock)
28
29 /* Reentrant version of getmntent.  */
30 struct mntent *getmntent_r (FILE *filep, 
31         struct mntent *mnt, char *buff, int bufsize)
32 {
33         char *cp, *ptrptr;
34         const char *sep = " \t\n";
35
36         if (!filep || !mnt || !buff)
37             return NULL;
38
39         /* Loop on the file, skipping comment lines. - FvK 03/07/93 */
40         while ((cp = fgets(buff, bufsize, filep)) != NULL) {
41                 if (buff[0] == '#' || buff[0] == '\n')
42                         continue;
43                 break;
44         }
45
46         /* At the EOF, the buffer should be unchanged. We should
47          * check the return value from fgets ().
48          */
49         if (cp == NULL)
50                 return NULL;
51
52         ptrptr = 0;
53         mnt->mnt_fsname = strtok_r(buff, sep, &ptrptr);
54         if (mnt->mnt_fsname == NULL)
55                 return NULL;
56
57         mnt->mnt_dir = strtok_r(NULL, sep, &ptrptr);
58         if (mnt->mnt_dir == NULL)
59                 return NULL;
60
61         mnt->mnt_type = strtok_r(NULL, sep, &ptrptr);
62         if (mnt->mnt_type == NULL)
63                 return NULL;
64
65         mnt->mnt_opts = strtok_r(NULL, sep, &ptrptr);
66         if (mnt->mnt_opts == NULL)
67                 mnt->mnt_opts = "";
68
69         cp = strtok_r(NULL, sep, &ptrptr);
70         mnt->mnt_freq = (cp != NULL) ? atoi(cp) : 0;
71
72         cp = strtok_r(NULL, sep, &ptrptr);
73         mnt->mnt_passno = (cp != NULL) ? atoi(cp) : 0;
74
75         return mnt;
76 }
77 libc_hidden_proto(getmntent_r)
78 libc_hidden_def(getmntent_r)
79
80 struct mntent *getmntent(FILE * filep)
81 {
82     struct mntent *tmp;
83     static char *buff = NULL;
84     static struct mntent mnt;
85     LOCK;
86     
87     if (!buff) {
88             buff = malloc(BUFSIZ);
89                 if (!buff)
90                     abort();
91     }
92     
93     tmp = getmntent_r(filep, &mnt, buff, BUFSIZ);
94     UNLOCK;
95     return(tmp);
96 }
97
98 int addmntent(FILE * filep, const struct mntent *mnt)
99 {
100         if (fseek(filep, 0, SEEK_END) < 0)
101                 return 1;
102
103         if (fprintf (filep, "%s %s %s %s %d %d\n", mnt->mnt_fsname, mnt->mnt_dir,
104                  mnt->mnt_type, mnt->mnt_opts, mnt->mnt_freq, mnt->mnt_passno) < 1)
105                 return 1;
106
107         return 0;
108 }
109
110 char *hasmntopt(const struct mntent *mnt, const char *opt)
111 {
112         return strstr(mnt->mnt_opts, opt);
113 }
114
115 FILE *setmntent(const char *name, const char *mode)
116 {
117         return fopen(name, mode);
118 }
119 libc_hidden_proto(setmntent)
120 libc_hidden_def(setmntent)
121
122 int endmntent(FILE * filep)
123 {
124         if (filep != NULL)
125                 fclose(filep);
126         return 1;
127 }
128 libc_hidden_proto(endmntent)
129 libc_hidden_def(endmntent)