/[inetutils]/inetutils/lib/getcwd.c
ViewVC logotype

Diff of /inetutils/lib/getcwd.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by gray, Sun Jul 31 20:11:00 2005 UTC revision 1.4 by ams, Fri Dec 2 14:00:00 2005 UTC
# Line 16  Line 16 
16     with this program; if not, write to the Free Software Foundation,     with this program; if not, write to the Free Software Foundation,
17     Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */     Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18    
19  #ifdef  HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
20  # include "config.h"  # include <config.h>
21  #endif  #endif
22    
23  #if !_LIBC  #if !_LIBC
# Line 60  Line 60 
60  # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)  # define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
61  #endif  #endif
62    
63  #if HAVE_UNISTD_H || _LIBC  #include <unistd.h>
 # include <unistd.h>  
 #endif  
   
64  #include <stdlib.h>  #include <stdlib.h>
65  #include <string.h>  #include <string.h>
66    
# Line 204  __getcwd (char *buf, size_t size) Line 201  __getcwd (char *buf, size_t size)
201        ino_t dotino;        ino_t dotino;
202        bool mount_point;        bool mount_point;
203        int parent_status;        int parent_status;
204          size_t dirroom;
205          size_t namlen;
206    
207        /* Look at the parent directory.  */        /* Look at the parent directory.  */
208  #ifdef AT_FDCWD  #ifdef AT_FDCWD
# Line 244  __getcwd (char *buf, size_t size) Line 243  __getcwd (char *buf, size_t size)
243          goto lose;          goto lose;
244        dotlist[dotlen++] = '/';        dotlist[dotlen++] = '/';
245  #endif  #endif
246        /* Clear errno to distinguish EOF from error if readdir returns        for (;;)
          NULL.  */  
       __set_errno (0);  
       while ((d = __readdir (dirstream)) != NULL)  
247          {          {
248              /* Clear errno to distinguish EOF from error if readdir returns
249                 NULL.  */
250              __set_errno (0);
251              d = __readdir (dirstream);
252              if (d == NULL)
253                {
254                  if (errno == 0)
255                    /* EOF on dirstream, which means that the current directory
256                       has been removed.  */
257                    __set_errno (ENOENT);
258                  goto lose;
259                }
260            if (d->d_name[0] == '.' &&            if (d->d_name[0] == '.' &&
261                (d->d_name[1] == '\0' ||                (d->d_name[1] == '\0' ||
262                 (d->d_name[1] == '.' && d->d_name[2] == '\0')))                 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
# Line 306  __getcwd (char *buf, size_t size) Line 314  __getcwd (char *buf, size_t size)
314                  break;                  break;
315              }              }
316          }          }
       if (d == NULL)  
         {  
           if (errno == 0)  
             /* EOF on dirstream, which means that the current directory  
                has been removed.  */  
             __set_errno (ENOENT);  
           goto lose;  
         }  
       else  
         {  
           size_t dirroom = dirp - dir;  
           size_t namlen = _D_EXACT_NAMLEN (d);  
317    
318            if (dirroom <= namlen)        dirroom = dirp - dir;
319          namlen = _D_EXACT_NAMLEN (d);
320    
321          if (dirroom <= namlen)
322            {
323              if (size != 0)
324              {              {
325                if (size != 0)                __set_errno (ERANGE);
326                  {                goto lose;
327                    __set_errno (ERANGE);              }
328                    goto lose;            else
329                  }              {
330                else                char *tmp;
331                  {                size_t oldsize = allocated;
                   char *tmp;  
                   size_t oldsize = allocated;  
332    
333                    allocated += MAX (allocated, namlen);                allocated += MAX (allocated, namlen);
334                    if (allocated < oldsize                if (allocated < oldsize
335                        || ! (tmp = realloc (dir, allocated)))                    || ! (tmp = realloc (dir, allocated)))
336                      goto memory_exhausted;                  goto memory_exhausted;
337    
338                    /* Move current contents up to the end of the buffer.                /* Move current contents up to the end of the buffer.
339                       This is guaranteed to be non-overlapping.  */                   This is guaranteed to be non-overlapping.  */
340                    dirp = memcpy (tmp + allocated - (oldsize - dirroom),                dirp = memcpy (tmp + allocated - (oldsize - dirroom),
341                                   tmp + dirroom,                               tmp + dirroom,
342                                   oldsize - dirroom);                               oldsize - dirroom);
343                    dir = tmp;                dir = tmp;
                 }  
344              }              }
           dirp -= namlen;  
           memcpy (dirp, d->d_name, namlen);  
           *--dirp = '/';  
345          }          }
346          dirp -= namlen;
347          memcpy (dirp, d->d_name, namlen);
348          *--dirp = '/';
349    
350        thisdev = dotdev;        thisdev = dotdev;
351        thisino = dotino;        thisino = dotino;

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26