]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - misc/mntent_r.c
time/tst-strftime2.c: Make the file easier to maintain
[thirdparty/glibc.git] / misc / mntent_r.c
index 1476c86ee2da183006b2a947b8c319afee2fb89a..5d88c45c6f599b5a40f6b285c15096c4da614903 100644 (file)
@@ -1,5 +1,5 @@
 /* Utilities for reading/writing fstab, mtab, etc.
-   Copyright (C) 1995-2000, 2001, 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 1995-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -13,9 +13,8 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
 #include <alloca.h>
 #include <mntent.h>
 #include <string.h>
 #include <sys/types.h>
 
-#ifdef USE_IN_LIBIO
-# define flockfile(s) _IO_flockfile (s)
-# define funlockfile(s) _IO_funlockfile (s)
-#endif
+#define flockfile(s) _IO_flockfile (s)
+#define funlockfile(s) _IO_funlockfile (s)
 
 #undef __setmntent
 #undef __endmntent
@@ -39,10 +36,10 @@ FILE *
 __setmntent (const char *file, const char *mode)
 {
   /* Extend the mode parameter with "c" to disable cancellation in the
-     I/O functions.  */
+     I/O functions and "e" to set FD_CLOEXEC.  */
   size_t modelen = strlen (mode);
-  char newmode[modelen + 2];
-  memcpy (mempcpy (newmode, mode, modelen), "c", 2);
+  char newmode[modelen + 3];
+  memcpy (mempcpy (newmode, mode, modelen), "ce", 3);
   FILE *result = fopen (file, newmode);
 
   if (result != NULL)
@@ -51,7 +48,7 @@ __setmntent (const char *file, const char *mode)
 
   return result;
 }
-INTDEF(__setmntent)
+libc_hidden_def (__setmntent)
 weak_alias (__setmntent, setmntent)
 
 
@@ -63,7 +60,7 @@ __endmntent (FILE *stream)
     fclose (stream);
   return 1;            /* SunOS 4.x says to always return 1 */
 }
-INTDEF(__endmntent)
+libc_hidden_def (__endmntent)
 weak_alias (__endmntent, endmntent)
 
 
@@ -130,7 +127,7 @@ __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
     {
       char *end_ptr;
 
-      if (fgets_unlocked (buffer, bufsiz, stream) == NULL)
+      if (__fgets_unlocked (buffer, bufsiz, stream) == NULL)
        {
          funlockfile (stream);
          return NULL;
@@ -138,12 +135,18 @@ __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
 
       end_ptr = strchr (buffer, '\n');
       if (end_ptr != NULL)     /* chop newline */
-       *end_ptr = '\0';
+       {
+         /* Do not walk past the start of buffer if it's all whitespace.  */
+         while (end_ptr != buffer
+                && (end_ptr[-1] == ' ' || end_ptr[-1] == '\t'))
+            end_ptr--;
+         *end_ptr = '\0';
+       }
       else
        {
          /* Not the whole line was read.  Do it now but forget it.  */
          char tmp[1024];
-         while (fgets_unlocked (tmp, sizeof tmp, stream) != NULL)
+         while (__fgets_unlocked (tmp, sizeof tmp, stream) != NULL)
            if (strchr (tmp, '\n') != NULL)
              break;
        }
@@ -171,8 +174,10 @@ __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
     {
     case 0:
       mp->mnt_freq = 0;
+      /* Fall through.  */
     case 1:
       mp->mnt_passno = 0;
+      /* Fall through.  */
     case 2:
       break;
     }
@@ -180,7 +185,7 @@ __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
 
   return mp;
 }
-INTDEF(__getmntent_r)
+libc_hidden_def (__getmntent_r)
 weak_alias (__getmntent_r, getmntent_r)
 
 
@@ -193,7 +198,7 @@ weak_alias (__getmntent_r, getmntent_r)
     const char *rp = name;                                                   \
                                                                              \
     while (*rp != '\0')                                                              \
-      if (*rp == ' ' || *rp == '\t' || *rp == '\\')                          \
+      if (*rp == ' ' || *rp == '\t' || *rp == '\n' || *rp == '\\')           \
        break;                                                                \
       else                                                                   \
        ++rp;                                                                 \
@@ -201,7 +206,7 @@ weak_alias (__getmntent_r, getmntent_r)
     if (*rp != '\0')                                                         \
       {                                                                              \
        /* In the worst case the length of the string can increase to         \
-          founr times the current length.  */                                \
+          four times the current length.  */                                 \
        char *wp;                                                             \
                                                                              \
        rp = name;                                                            \
@@ -262,8 +267,8 @@ __addmntent (FILE *stream, const struct mntent *mnt)
                   mntcopy.mnt_type,
                   mntcopy.mnt_opts,
                   mntcopy.mnt_freq,
-                  mntcopy.mnt_passno)
-         < 0 ? 1 : 0);
+                  mntcopy.mnt_passno) < 0
+         || fflush (stream) != 0);
 }
 weak_alias (__addmntent, addmntent)
 
@@ -278,14 +283,11 @@ __hasmntopt (const struct mntent *mnt, const char *opt)
 
   while ((p = strstr (rest, opt)) != NULL)
     {
-      if (p == rest
-         || (p[-1] == ','
-             && (p[optlen] == '\0' ||
-                 p[optlen] == '='  ||
-                 p[optlen] == ',')))
+      if ((p == rest || p[-1] == ',')
+         && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ','))
        return p;
 
-      rest = strchr (rest, ',');
+      rest = strchr (p, ',');
       if (rest == NULL)
        break;
       ++rest;
@@ -293,4 +295,5 @@ __hasmntopt (const struct mntent *mnt, const char *opt)
 
   return NULL;
 }
+libc_hidden_def (__hasmntopt)
 weak_alias (__hasmntopt, hasmntopt)