LCOV - code coverage report
Current view: top level - lib - savedir.c (source / functions) Hit Total Coverage
Test: coreutils.info Lines: 28 37 75.7 %
Date: 2018-01-30 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* savedir.c -- save the list of files in a directory in a string
       2             : 
       3             :    Copyright (C) 1990, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005,
       4             :    2006 Free Software Foundation, Inc.
       5             : 
       6             :    This program is free software: you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             : 
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      18             : 
      19             : /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
      20             : 
      21             : #include <config.h>
      22             : 
      23             : #include "savedir.h"
      24             : 
      25             : #include <sys/types.h>
      26             : 
      27             : #include <errno.h>
      28             : 
      29             : #include <dirent.h>
      30             : #ifndef _D_EXACT_NAMLEN
      31             : # define _D_EXACT_NAMLEN(dp)    strlen ((dp)->d_name)
      32             : #endif
      33             : 
      34             : #include <stddef.h>
      35             : #include <stdlib.h>
      36             : #include <string.h>
      37             : 
      38             : #include "openat.h"
      39             : #include "xalloc.h"
      40             : 
      41             : #ifndef NAME_SIZE_DEFAULT
      42             : # define NAME_SIZE_DEFAULT 512
      43             : #endif
      44             : 
      45             : /* The results of opendir() in this file are not used with dirfd and fchdir,
      46             :    therefore save some unnecessary work in fchdir.c.  */
      47             : #undef opendir
      48             : #undef closedir
      49             : 
      50             : /* Return a freshly allocated string containing the file names
      51             :    in directory DIRP, separated by '\0' characters;
      52             :    the end is marked by two '\0' characters in a row.
      53             :    Return NULL (setting errno) if DIRP cannot be read or closed.
      54             :    If DIRP is NULL, return NULL without affecting errno.  */
      55             : 
      56             : static char *
      57           3 : savedirstream (DIR *dirp)
      58             : {
      59             :   char *name_space;
      60           3 :   size_t allocated = NAME_SIZE_DEFAULT;
      61           3 :   size_t used = 0;
      62             :   int save_errno;
      63             : 
      64           3 :   if (dirp == NULL)
      65           0 :     return NULL;
      66             : 
      67           3 :   name_space = xmalloc (allocated);
      68             : 
      69             :   for (;;)
      70        1016 :     {
      71             :       struct dirent const *dp;
      72             :       char const *entry;
      73             : 
      74        1019 :       errno = 0;
      75        1019 :       dp = readdir (dirp);
      76        1019 :       if (! dp)
      77           3 :         break;
      78             : 
      79             :       /* Skip "", ".", and "..".  "" is returned by at least one buggy
      80             :          implementation: Solaris 2.4 readdir on NFS file systems.  */
      81        1016 :       entry = dp->d_name;
      82        1016 :       if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
      83             :         {
      84        1010 :           size_t entry_size = _D_EXACT_NAMLEN (dp) + 1;
      85        1010 :           if (used + entry_size < used)
      86           0 :             xalloc_die ();
      87        1010 :           if (allocated <= used + entry_size)
      88             :             {
      89             :               do
      90             :                 {
      91           9 :                   if (2 * allocated < allocated)
      92           0 :                     xalloc_die ();
      93           9 :                   allocated *= 2;
      94             :                 }
      95           9 :               while (allocated <= used + entry_size);
      96             : 
      97           9 :               name_space = xrealloc (name_space, allocated);
      98             :             }
      99        1010 :           memcpy (name_space + used, entry, entry_size);
     100        1010 :           used += entry_size;
     101             :         }
     102             :     }
     103           3 :   name_space[used] = '\0';
     104           3 :   save_errno = errno;
     105           3 :   if (closedir (dirp) != 0)
     106           0 :     save_errno = errno;
     107           3 :   if (save_errno != 0)
     108             :     {
     109           0 :       free (name_space);
     110           0 :       errno = save_errno;
     111           0 :       return NULL;
     112             :     }
     113           3 :   return name_space;
     114             : }
     115             : 
     116             : /* Return a freshly allocated string containing the file names
     117             :    in directory DIR, separated by '\0' characters;
     118             :    the end is marked by two '\0' characters in a row.
     119             :    Return NULL (setting errno) if DIR cannot be opened, read, or closed.  */
     120             : 
     121             : char *
     122           3 : savedir (char const *dir)
     123             : {
     124           3 :   return savedirstream (opendir (dir));
     125             : }
     126             : 
     127             : /* Return a freshly allocated string containing the file names
     128             :    in directory FD, separated by '\0' characters;
     129             :    the end is marked by two '\0' characters in a row.
     130             :    Return NULL (setting errno) if FD cannot be read or closed.  */
     131             : 
     132             : char *
     133           0 : fdsavedir (int fd)
     134             : {
     135           0 :   return savedirstream (fdopendir (fd));
     136             : }

Generated by: LCOV version 1.10