Back to index

glibc  2.9
Defines | Functions
mntent_r.c File Reference
#include <alloca.h>
#include <mntent.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <string.h>
#include <sys/types.h>

Go to the source code of this file.

Defines

#define encode_name(name)

Functions

FILE__setmntent (const char *file, const char *mode)
 weak_alias (__setmntent, setmntent)
 weak_alias (__endmntent, endmntent)
struct mntent__getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
 weak_alias (__getmntent_r, getmntent_r)
 weak_alias (__addmntent, addmntent)

Define Documentation

#define encode_name (   name)

Function Documentation

struct mntent* __getmntent_r ( FILE stream,
struct mntent mp,
char *  buffer,
int  bufsiz 
) [read]

Definition at line 124 of file mntent_r.c.

{
  char *cp;
  char *head;

  flockfile (stream);
  do
    {
      char *end_ptr;

      if (fgets_unlocked (buffer, bufsiz, stream) == NULL)
       {
         funlockfile (stream);
         return NULL;
       }

      end_ptr = strchr (buffer, '\n');
      if (end_ptr != NULL)  /* chop newline */
       *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)
           if (strchr (tmp, '\n') != NULL)
             break;
       }

      head = buffer + strspn (buffer, " \t");
      /* skip empty lines and comment lines:  */
    }
  while (head[0] == '\0' || head[0] == '#');

  cp = __strsep (&head, " \t");
  mp->mnt_fsname = cp != NULL ? decode_name (cp) : (char *) "";
  if (head)
    head += strspn (head, " \t");
  cp = __strsep (&head, " \t");
  mp->mnt_dir = cp != NULL ? decode_name (cp) : (char *) "";
  if (head)
    head += strspn (head, " \t");
  cp = __strsep (&head, " \t");
  mp->mnt_type = cp != NULL ? decode_name (cp) : (char *) "";
  if (head)
    head += strspn (head, " \t");
  cp = __strsep (&head, " \t");
  mp->mnt_opts = cp != NULL ? decode_name (cp) : (char *) "";
  switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
    {
    case 0:
      mp->mnt_freq = 0;
    case 1:
      mp->mnt_passno = 0;
    case 2:
      break;
    }
  funlockfile (stream);

  return mp;
}

Here is the call graph for this function:

FILE* __setmntent ( const char *  file,
const char *  mode 
)

Definition at line 40 of file mntent_r.c.

{
  /* Extend the mode parameter with "c" to disable cancellation in the
     I/O functions.  */
  size_t modelen = strlen (mode);
  char newmode[modelen + 2];
  memcpy (mempcpy (newmode, mode, modelen), "c", 2);
  FILE *result = fopen (file, newmode);

  if (result != NULL)
    /* We do the locking ourselves.  */
    __fsetlocking (result, FSETLOCKING_BYCALLER);

  return result;
}

Here is the call graph for this function:

Definition at line 56 of file mntent_r.c.

{
  if (stream)        /* SunOS 4.x allows for NULL stream */
    fclose (stream);
  return 1;          /* SunOS 4.x says to always return 1 */
}

Here is the call graph for this function:

Definition at line 68 of file mntent_r.c.

{
  char *rp = buf;
  char *wp = buf;

  do
    if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '4' && rp[3] == '0')
      {
       /* \040 is a SPACE.  */
       *wp++ = ' ';
       rp += 3;
      }
    else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '1')
      {
       /* \011 is a TAB.  */
       *wp++ = '\t';
       rp += 3;
      }
    else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '2')
      {
       /* \012 is a NEWLINE.  */
       *wp++ = '\n';
       rp += 3;
      }
    else if (rp[0] == '\\' && rp[1] == '\\')
      {
       /* We have to escape \\ to be able to represent all characters.  */
       *wp++ = '\\';
       rp += 1;
      }
    else if (rp[0] == '\\' && rp[1] == '1' && rp[2] == '3' && rp[3] == '4')
      {
       /* \134 is also \\.  */
       *wp++ = '\\';
       rp += 3;
      }
    else
      *wp++ = *rp;
  while (*rp++ != '\0');

  return buf;
}
weak_alias ( __getmntent_r  ,
getmntent_r   
)

Definition at line 185 of file mntent_r.c.

     {                                                               \
    const char *rp = name;                                           \
                                                                     \
    while (*rp != '\0')                                                     \
      if (*rp == ' ' || *rp == '\t' || *rp == '\\')                         \
       break;                                                        \
      else                                                           \
       ++rp;                                                         \
                                                                     \
    if (*rp != '\0')                                                 \
      {                                                                     \
       /* In the worst case the length of the string can increase to        \
          founr times the current length.  */                               \
       char *wp;                                                     \
                                                                     \
       rp = name;                                                    \
	name = wp = (char *) alloca (strlen (name) * 4 + 1);                \
                                                                     \
       do                                                            \
         if (*rp == ' ')                                             \
           {                                                         \
             *wp++ = '\\';                                           \
             *wp++ = '0';                                            \
             *wp++ = '4';                                            \
             *wp++ = '0';                                            \
           }                                                         \
         else if (*rp == '\t')                                              \
           {                                                         \
             *wp++ = '\\';                                           \
             *wp++ = '0';                                            \
             *wp++ = '1';                                            \
             *wp++ = '1';                                            \
           }                                                         \
         else if (*rp == '\n')                                              \
           {                                                         \
             *wp++ = '\\';                                           \
             *wp++ = '0';                                            \
             *wp++ = '1';                                            \
             *wp++ = '2';                                            \
           }                                                         \
         else if (*rp == '\\')                                              \
           {                                                         \
             *wp++ = '\\';                                           \
             *wp++ = '\\';                                           \
           }                                                         \
         else                                                        \
           *wp++ = *rp;                                              \
       while (*rp++ != '\0');                                               \
      }                                                                     \
  } while (0)


/* Write the mount table entry described by MNT to STREAM.
   Return zero on success, nonzero on failure.  */
int
__addmntent (FILE *stream, const struct mntent *mnt)
{
  struct mntent mntcopy = *mnt;
  if (fseek (stream, 0, SEEK_END))
    return 1;

  /* Encode spaces and tabs in the names.  */
  encode_name (mntcopy.mnt_fsname);
  encode_name (mntcopy.mnt_dir);
  encode_name (mntcopy.mnt_type);
  encode_name (mntcopy.mnt_opts);

  return (fprintf (stream, "%s %s %s %s %d %d\n",
                 mntcopy.mnt_fsname,
                 mntcopy.mnt_dir,
                 mntcopy.mnt_type,
                 mntcopy.mnt_opts,
                 mntcopy.mnt_freq,
                 mntcopy.mnt_passno)
         < 0 ? 1 : 0);

Here is the call graph for this function:

Definition at line 268 of file mntent_r.c.

{
  const size_t optlen = strlen (opt);
  char *rest = mnt->mnt_opts, *p;

  while ((p = strstr (rest, opt)) != NULL)
    {
      if ((p == rest || p[-1] == ',')
         && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ','))
       return p;

      rest = strchr (p, ',');
      if (rest == NULL)
       break;
      ++rest;
    }

  return NULL;