]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/dir.c
Fix source file header text duplication text duplication.
[thirdparty/cups.git] / cups / dir.c
index e06edea91ccc34c20a6874c579b446fab0960ada..65b8c4f9c3ee313f47604eddc938cb0cb8f897a1 100644 (file)
@@ -1,50 +1,25 @@
 /*
- * "$Id$"
+ * Directory routines for CUPS.
  *
- *   Public directory routines for the Common UNIX Printing System (CUPS).
+ * This set of APIs abstracts enumeration of directory entries.
  *
- *   This set of APIs abstracts enumeration of directory entries.
+ * Copyright 2007-2012 by Apple Inc.
+ * Copyright 1997-2005 by Easy Software Products, all rights reserved.
  *
- *   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
- *
- * Contents:
- *
- *   _cups_dir_time() - Convert a FILETIME value to a UNIX time value.
- *   cupsDirClose()   - Close a directory.
- *   cupsDirOpen()    - Open a directory.
- *   cupsDirRead()    - Read the next directory entry.
- *   cupsDirRewind()  - Rewind to the start of the directory.
- *   cupsDirClose()   - Close a directory.
- *   cupsDirOpen()    - Open a directory.
- *   cupsDirRead()    - Read the next directory entry.
- *   cupsDirRewind()  - Rewind to the start of the directory.
+ * These coded instructions, statements, and computer programs are the
+ * 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
+ * missing or damaged, see the license at "http://www.cups.org/".
  */
 
 /*
  * Include necessary headers...
  */
 
+#include "string-private.h"
+#include "debug-private.h"
 #include "dir.h"
-#include "string.h"
-#include "debug.h"
-#include <stdlib.h>
-#include <errno.h>
 
 
 /*
@@ -82,17 +57,19 @@ _cups_dir_time(FILETIME ft)         /* I - File time */
   * between them...
   */
 
-  val = ft.dwLowDateTime + (ft.dwHighDateTime << 32);
+  val = ft.dwLowDateTime + ((ULONGLONG)ft.dwHighDateTime << 32);
   return ((time_t)(val / 10000000 - 11644732800));
 }
 
 
 /*
  * 'cupsDirClose()' - Close a directory.
+ *
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 void
-cupsDirClose(cups_dir_t *dp)           /* I - Directory */
+cupsDirClose(cups_dir_t *dp)           /* I - Directory pointer */
 {
  /*
   * Range check input...
@@ -118,9 +95,11 @@ cupsDirClose(cups_dir_t *dp)                /* I - Directory */
 
 /*
  * 'cupsDirOpen()' - Open a directory.
+ *
+ * @since CUPS 1.2/macOS 10.5@
  */
 
-cups_dir_t *                           /* O - Directory */
+cups_dir_t *                           /* O - Directory pointer or @code NULL@ if the directory could not be opened. */
 cupsDirOpen(const char *directory)     /* I - Directory name */
 {
   cups_dir_t   *dp;                    /* Directory */
@@ -159,10 +138,12 @@ cupsDirOpen(const char *directory)        /* I - Directory name */
 
 /*
  * 'cupsDirRead()' - Read the next directory entry.
+ *
+ * @since CUPS 1.2/macOS 10.5@
  */
 
-cups_dentry_t *                        /* O - Directory entry */
-cupsDirRead(cups_dir_t *dp)            /* I - Directory */
+cups_dentry_t *                                /* O - Directory entry or @code NULL@ if there are no more */
+cupsDirRead(cups_dir_t *dp)            /* I - Directory pointer */
 {
   WIN32_FIND_DATA      entry;          /* Directory entry data */
 
@@ -205,7 +186,7 @@ cupsDirRead(cups_dir_t *dp)         /* I - Directory */
   dp->entry.fileinfo.st_atime = _cups_dir_time(entry.ftLastAccessTime);
   dp->entry.fileinfo.st_ctime = _cups_dir_time(entry.ftCreationTime);
   dp->entry.fileinfo.st_mtime = _cups_dir_time(entry.ftLastWriteTime);
-  dp->entry.fileinfo.st_size  = entry.nFileSizeLow + (entry.nFileSizeHigh << 32);
+  dp->entry.fileinfo.st_size  = entry.nFileSizeLow + ((unsigned long long)entry.nFileSizeHigh << 32);
 
  /*
   * Return the entry...
@@ -217,10 +198,12 @@ cupsDirRead(cups_dir_t *dp)               /* I - Directory */
 
 /*
  * 'cupsDirRewind()' - Rewind to the start of the directory.
+ *
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 void
-cupsDirRewind(cups_dir_t *dp)          /* I - Directory */
+cupsDirRewind(cups_dir_t *dp)          /* I - Directory pointer */
 {
  /*
   * Range check input...
@@ -265,12 +248,14 @@ struct _cups_dir_s                        /**** Directory data structure ****/
 
 /*
  * 'cupsDirClose()' - Close a directory.
+ *
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 void
-cupsDirClose(cups_dir_t *dp)           /* I - Directory */
+cupsDirClose(cups_dir_t *dp)           /* I - Directory pointer */
 {
-  DEBUG_printf(("cupsDirClose(dp=%p)\n", dp));
+  DEBUG_printf(("cupsDirClose(dp=%p)", (void *)dp));
 
  /*
   * Range check input...
@@ -290,15 +275,17 @@ cupsDirClose(cups_dir_t *dp)              /* I - Directory */
 
 /*
  * 'cupsDirOpen()' - Open a directory.
+ *
+ * @since CUPS 1.2/macOS 10.5@
  */
 
-cups_dir_t *                           /* O - Directory */
+cups_dir_t *                           /* O - Directory pointer or @code NULL@ if the directory could not be opened. */
 cupsDirOpen(const char *directory)     /* I - Directory name */
 {
   cups_dir_t   *dp;                    /* Directory */
 
 
-  DEBUG_printf(("cupsDirOpen(directory=\"%s\")\n", directory));
+  DEBUG_printf(("cupsDirOpen(directory=\"%s\")", directory));
 
  /*
   * Range check input...
@@ -342,18 +329,22 @@ cupsDirOpen(const char *directory)        /* I - Directory name */
 
 /*
  * 'cupsDirRead()' - Read the next directory entry.
+ *
+ * @since CUPS 1.2/macOS 10.5@
  */
 
-cups_dentry_t *                                /* O - Directory entry */
-cupsDirRead(cups_dir_t *dp)            /* I - Directory */
+cups_dentry_t *                                /* O - Directory entry or @code NULL@ when there are no more */
+cupsDirRead(cups_dir_t *dp)            /* I - Directory pointer */
 {
-  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));
+  DEBUG_printf(("2cupsDirRead(dp=%p)", (void *)dp));
 
  /*
   * Range check input...
@@ -366,54 +357,84 @@ cupsDirRead(cups_dir_t *dp)               /* I - Directory */
   * 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(("3cupsDirRead: readdir_r() failed - %s\n", strerror(errno)));
       return (NULL);
     }
 
     if (!entry)
     {
-      DEBUG_puts("    readdir_r() returned a NULL pointer!");
+      DEBUG_puts("3cupsDirRead: readdir_r() returned a NULL pointer!");
       return (NULL);
     }
 
-    DEBUG_printf(("    readdir_r() returned \"%s\"...\n", entry->d_name));
-  }
-  while (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."));
+    DEBUG_printf(("4cupsDirRead: readdir_r() returned \"%s\"...",
+                  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("3cupsDirRead: 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(("4cupsDirRead: readdir() returned \"%s\"...", 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(("3cupsDirRead: stat() failed for \"%s\" - %s...", filename,
+                    strerror(errno)));
+      continue;
+    }
+
+   /*
+    * Return the entry...
+    */
+
+    return (&(dp->entry));
+  }
 }
 
 
 /*
  * 'cupsDirRewind()' - Rewind to the start of the directory.
+ *
+ * @since CUPS 1.2/macOS 10.5@
  */
 
 void
-cupsDirRewind(cups_dir_t *dp)          /* I - Directory */
+cupsDirRewind(cups_dir_t *dp)          /* I - Directory pointer */
 {
-  DEBUG_printf(("cupsDirRewind(dp=%p)\n", dp));
+  DEBUG_printf(("cupsDirRewind(dp=%p)", (void *)dp));
 
  /*
   * Range check input...
@@ -428,10 +449,4 @@ cupsDirRewind(cups_dir_t *dp)              /* I - Directory */
 
   rewinddir(dp->dir);
 }
-
-
 #endif /* WIN32 */
-
-/*
- * End of "$Id$".
- */