newlib/winsup/cygwin/scandir.cc
Christopher Faylor 9e2baf8dfa * cygerrno.h: New file. Use this throughout whenever errno manipulation is
required.
* errno.cc: Use DWORD to hold Windows errors.
(geterrno_from_win_error): New function.
(seterrno_from_win_error): Use geterrno_from_win_error to convert supplied
windows error (suggested by Corinna Vinschen).
* path.cc (symlink_info): Add error element.
* path.cc (path_conv::check): Remove errno setting.  Use new symlink_info errno
element to set path_conv error, where appropriate.
(symlink_info::check): Set error element rather than attempting to manipulate
errno.  Add more checks for trailing / and /..  even though they are currently
useless.  Avoid setting EINVAL.
* path.cc (normalize_posix_path): Correct check for trailing /.
2000-08-22 03:58:47 +00:00

102 lines
1.9 KiB
C++

/* scandir.cc
Copyright 1998 Cygnus Solutions.
Written by Corinna Vinschen <corinna.vinschen@cityweb.de>
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>
#include "cygerrno.h"
extern "C"
int
scandir (const char *dir,
struct dirent ***namelist,
int (*select) (const struct dirent *),
int (*compar) (const struct dirent **, const struct dirent **))
{
DIR *dirp;
struct dirent *ent, *etmp, **nl = NULL, **ntmp;
int count = 0;
int allocated = 0;
if (!(dirp = opendir (dir)))
return -1;
int prior_errno = get_errno ();
set_errno (0);
while ((ent = readdir (dirp)))
{
if (!select || select (ent))
{
/* Ignore error from readdir/select. See POSIX specs. */
set_errno (0);
if (count == allocated)
{
if (allocated == 0)
allocated = 10;
else
allocated *= 2;
ntmp = (struct dirent **) realloc (nl, allocated * sizeof *nl);
if (!ntmp)
{
set_errno (ENOMEM);
break;
}
nl = ntmp;
}
if (!(etmp = (struct dirent *) malloc (sizeof *ent)))
{
set_errno (ENOMEM);
break;
}
*etmp = *ent;
nl[count++] = etmp;
}
}
if ((prior_errno = get_errno ()) != 0)
{
closedir (dirp);
if (nl)
{
while (count > 0)
free (nl[--count]);
free (nl);
}
/* Ignore errors from closedir() and what not else. */
set_errno (prior_errno);
return -1;
}
closedir (dirp);
set_errno (prior_errno);
qsort (nl, count, sizeof *nl, (int (*)(const void *, const void *)) compar);
if (namelist)
*namelist = nl;
return count;
}
extern "C"
int
alphasort (const struct dirent **a, const struct dirent **b)
{
return strcoll ((*a)->d_name, (*b)->d_name);
}