*
* This set of APIs abstracts enumeration of directory entries.
*
+ * Copyright 2007 by Apple Inc.
* Copyright 1997-2005 by Easy Software Products, all rights reserved.
*
* These coded instructions, statements, and computer programs are the
- * property of Easy Software Products and are protected by Federal
- * copyright law. Distribution and use rights are outlined in the file
- * "LICENSE.txt" which should have been included with this file. If this
- * file is missing or damaged please contact Easy Software Products
- * at:
- *
- * Attn: CUPS Licensing Information
- * Easy Software Products
- * 44141 Airport View Drive, Suite 204
- * Hollywood, Maryland 20636 USA
- *
- * Voice: (301) 373-9600
- * EMail: cups-info@cups.org
- * WWW: http://www.cups.org
+ * property of Apple Inc. and are protected by Federal copyright
+ * law. Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file. If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
*
* Contents:
*
cups_dentry_t * /* O - Directory entry */
cupsDirRead(cups_dir_t *dp) /* I - Directory */
{
- char buffer[sizeof(struct dirent) + 1024];
- /* Directory entry buffer */
struct dirent *entry; /* Pointer to entry */
char filename[1024]; /* Full filename */
+# ifdef HAVE_PTHREAD_H
+ char buffer[sizeof(struct dirent) + 1024];
+ /* Directory entry buffer */
+# endif /* HAVE_PTHREAD_H */
DEBUG_printf(("cupsDirRead(dp=%p)\n", dp));
* Try reading an entry that is not "." or ".."...
*/
- do
+ for (;;)
{
+# ifdef HAVE_PTHREAD_H
+ /*
+ * Read the next entry using the reentrant version of readdir...
+ */
+
if (readdir_r(dp->dir, (struct dirent *)buffer, &entry))
{
DEBUG_printf((" readdir_r() failed - %s\n", strerror(errno)));
}
DEBUG_printf((" readdir_r() returned \"%s\"...\n", entry->d_name));
- }
- while (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."));
- /*
- * Copy the name over and get the file information...
- */
+# else
+ /*
+ * Read the next entry using the original version of readdir...
+ */
- strlcpy(dp->entry.filename, entry->d_name, sizeof(dp->entry.filename));
+ if ((entry = readdir(dp->dir)) == NULL)
+ {
+ DEBUG_puts(" readdir() returned a NULL pointer!");
+ return (NULL);
+ }
- snprintf(filename, sizeof(filename), "%s/%s", dp->directory, entry->d_name);
- if (stat(filename, &(dp->entry.fileinfo)))
- {
- DEBUG_printf((" stat() failed for \"%s\" - %s...\n", filename,
- strerror(errno)));
- return (NULL);
- }
+ DEBUG_printf((" readdir() returned \"%s\"...\n", entry->d_name));
- /*
- * Return the entry...
- */
+# endif /* HAVE_PTHREAD_H */
- return (&(dp->entry));
+ /*
+ * Skip "." and ".."...
+ */
+
+ if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
+ continue;
+
+ /*
+ * Copy the name over and get the file information...
+ */
+
+ strlcpy(dp->entry.filename, entry->d_name, sizeof(dp->entry.filename));
+
+ snprintf(filename, sizeof(filename), "%s/%s", dp->directory, entry->d_name);
+
+ if (stat(filename, &(dp->entry.fileinfo)))
+ {
+ DEBUG_printf((" stat() failed for \"%s\" - %s...\n", filename,
+ strerror(errno)));
+ continue;
+ }
+
+ /*
+ * Return the entry...
+ */
+
+ return (&(dp->entry));
+ }
}