]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/dir.c
Update usage info.
[thirdparty/cups.git] / cups / dir.c
index f74498b745d4318b43f4f94d3a9fc2739e9e6060..184aa7c1962108b610887896255c96752aba1093 100644 (file)
@@ -1,57 +1,28 @@
 /*
- * "$Id: dir.c 177 2006-06-21 00:20:03Z jlovell $"
+ * 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-2017 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.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /*
  * Include necessary headers...
  */
 
+#include "string-private.h"
+#include "debug-internal.h"
 #include "dir.h"
-#include "string.h"
-#include "debug.h"
-#include <stdlib.h>
-#include <errno.h>
 
 
 /*
  * Windows implementation...
  */
 
-#ifdef WIN32
+#ifdef _WIN32
 #  include <windows.h>
 
 /*
@@ -82,17 +53,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 +91,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,12 +134,14 @@ 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 */
+  WIN32_FIND_DATAA     entry;          /* Directory entry data */
 
 
  /*
@@ -184,11 +161,11 @@ cupsDirRead(cups_dir_t *dp)               /* I - Directory */
     * No, find the first file...
     */
 
-    dp->dir = FindFirstFile(dp->directory, &entry);
+    dp->dir = FindFirstFileA(dp->directory, &entry);
     if (dp->dir == INVALID_HANDLE_VALUE)
       return (NULL);
   }
-  else if (!FindNextFile(dp->dir, &entry))
+  else if (!FindNextFileA(dp->dir, &entry))
     return (NULL);
 
  /*
@@ -205,7 +182,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 +194,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 +244,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 +271,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 +325,18 @@ 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 */
 
 
-  DEBUG_printf(("cupsDirRead(dp=%p)\n", dp));
+  DEBUG_printf(("2cupsDirRead(dp=%p)", (void *)dp));
 
  /*
   * Range check input...
@@ -366,54 +349,61 @@ cupsDirRead(cups_dir_t *dp)               /* I - Directory */
   * Try reading an entry that is not "." or ".."...
   */
 
-  do
+  for (;;)
   {
-    if (readdir_r(dp->dir, (struct dirent *)buffer, &entry))
-    {
-      DEBUG_printf(("    readdir_r() failed - %s\n", strerror(errno)));
-      return (NULL);
-    }
+   /*
+    * Read the next entry...
+    */
 
-    if (!entry)
+    if ((entry = readdir(dp->dir)) == NULL)
     {
-      DEBUG_puts("    readdir_r() returned a NULL pointer!");
+      DEBUG_puts("3cupsDirRead: readdir() 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() returned \"%s\"...", entry->d_name));
 
- /*
-  * Copy the name over and get the file information...
-  */
  /*
+    * Skip "." and ".."...
+    */
 
-  strlcpy(dp->entry.filename, entry->d_name, sizeof(dp->entry.filename));
+    if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
+      continue;
 
-  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);
-  }
+   /*
+    * Copy the name over and get the file information...
+    */
 
- /*
-  * Return the entry...
-  */
+    strlcpy(dp->entry.filename, entry->d_name, sizeof(dp->entry.filename));
 
-  return (&(dp->entry));
+    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 +418,4 @@ cupsDirRewind(cups_dir_t *dp)              /* I - Directory */
 
   rewinddir(dp->dir);
 }
-
-
-#endif /* WIN32 */
-
-/*
- * End of "$Id: dir.c 177 2006-06-21 00:20:03Z jlovell $".
- */
+#endif /* _WIN32 */