]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/dir.c
4 * Public directory routines for the Common UNIX Printing System (CUPS).
6 * This set of APIs abstracts enumeration of directory entries.
8 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
10 * These coded instructions, statements, and computer programs are the
11 * property of Easy Software Products and are protected by Federal
12 * copyright law. Distribution and use rights are outlined in the file
13 * "LICENSE.txt" which should have been included with this file. If this
14 * file is missing or damaged please contact Easy Software Products
17 * Attn: CUPS Licensing Information
18 * Easy Software Products
19 * 44141 Airport View Drive, Suite 204
20 * Hollywood, Maryland 20636 USA
22 * Voice: (301) 373-9600
23 * EMail: cups-info@cups.org
24 * WWW: http://www.cups.org
28 * _cups_dir_time() - Convert a FILETIME value to a UNIX time value.
29 * cupsDirClose() - Close a directory.
30 * cupsDirOpen() - Open a directory.
31 * cupsDirRead() - Read the next directory entry.
32 * cupsDirRewind() - Rewind to the start of the directory.
33 * cupsDirClose() - Close a directory.
34 * cupsDirOpen() - Open a directory.
35 * cupsDirRead() - Read the next directory entry.
36 * cupsDirRewind() - Rewind to the start of the directory.
40 * Include necessary headers...
51 * Windows implementation...
58 * Types and structures...
61 struct _cups_dir_s
/**** Directory data structure ****/
63 char directory
[1024]; /* Directory filename */
64 HANDLE dir
; /* Directory handle */
65 cups_dentry_t entry
; /* Directory entry */
70 * '_cups_dir_time()' - Convert a FILETIME value to a UNIX time value.
73 time_t /* O - UNIX time */
74 _cups_dir_time(FILETIME ft
) /* I - File time */
76 ULONGLONG val
; /* File time in 0.1 usecs */
80 * Convert file time (1/10 microseconds since Jan 1, 1601) to UNIX
81 * time (seconds since Jan 1, 1970). There are 11,644,732,800 seconds
85 val
= ft
.dwLowDateTime
+ (ft
.dwHighDateTime
<< 32);
86 return ((time_t)(val
/ 10000000 - 11644732800));
91 * 'cupsDirClose()' - Close a directory.
95 cupsDirClose(cups_dir_t
*dp
) /* I - Directory */
98 * Range check input...
105 * Close an open directory handle...
108 if (dp
->dir
!= INVALID_HANDLE_VALUE
)
112 * Free memory used...
120 * 'cupsDirOpen()' - Open a directory.
123 cups_dir_t
* /* O - Directory */
124 cupsDirOpen(const char *directory
) /* I - Directory name */
126 cups_dir_t
*dp
; /* Directory */
130 * Range check input...
137 * Allocate memory for the directory structure...
140 dp
= (cups_dir_t
*)calloc(1, sizeof(cups_dir_t
));
145 * Copy the directory name for later use...
148 dp
->dir
= INVALID_HANDLE_VALUE
;
150 strlcpy(dp
->directory
, directory
, sizeof(dp
->directory
));
153 * Return the new directory structure...
161 * 'cupsDirRead()' - Read the next directory entry.
164 cups_dentry_t
* /* O - Directory entry */
165 cupsDirRead(cups_dir_t
*dp
) /* I - Directory */
167 WIN32_FIND_DATA entry
; /* Directory entry data */
171 * Range check input...
178 * See if we have already started finding files...
181 if (dp
->dir
== INVALID_HANDLE_VALUE
)
184 * No, find the first file...
187 dp
->dir
= FindFirstFile(dp
->directory
, &entry
);
188 if (dp
->dir
== INVALID_HANDLE_VALUE
)
191 else if (!FindNextFile(dp
->dir
, &entry
))
195 * Copy the name over and convert the file information...
198 strlcpy(dp
->entry
.filename
, entry
.cFileName
, sizeof(dp
->entry
.filename
));
200 if (entry
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
201 dp
->entry
.fileinfo
.st_mode
= 0755 | S_IFDIR
;
203 dp
->entry
.fileinfo
.st_mode
= 0644;
205 dp
->entry
.fileinfo
.st_atime
= _cups_dir_time(entry
.ftLastAccessTime
);
206 dp
->entry
.fileinfo
.st_ctime
= _cups_dir_time(entry
.ftCreationTime
);
207 dp
->entry
.fileinfo
.st_mtime
= _cups_dir_time(entry
.ftLastWriteTime
);
208 dp
->entry
.fileinfo
.st_size
= entry
.nFileSizeLow
+ (entry
.nFileSizeHigh
<< 32);
211 * Return the entry...
214 return (&(dp
->entry
));
219 * 'cupsDirRewind()' - Rewind to the start of the directory.
223 cupsDirRewind(cups_dir_t
*dp
) /* I - Directory */
226 * Range check input...
233 * Close an open directory handle...
236 if (dp
->dir
!= INVALID_HANDLE_VALUE
)
239 dp
->dir
= INVALID_HANDLE_VALUE
;
247 * POSIX implementation...
250 # include <sys/types.h>
255 * Types and structures...
258 struct _cups_dir_s
/**** Directory data structure ****/
260 char directory
[1024]; /* Directory filename */
261 DIR *dir
; /* Directory file */
262 cups_dentry_t entry
; /* Directory entry */
267 * 'cupsDirClose()' - Close a directory.
271 cupsDirClose(cups_dir_t
*dp
) /* I - Directory */
273 DEBUG_printf(("cupsDirClose(dp=%p)\n", dp
));
276 * Range check input...
283 * Close the directory and free memory...
292 * 'cupsDirOpen()' - Open a directory.
295 cups_dir_t
* /* O - Directory */
296 cupsDirOpen(const char *directory
) /* I - Directory name */
298 cups_dir_t
*dp
; /* Directory */
301 DEBUG_printf(("cupsDirOpen(directory=\"%s\")\n", directory
));
304 * Range check input...
311 * Allocate memory for the directory structure...
314 dp
= (cups_dir_t
*)calloc(1, sizeof(cups_dir_t
));
319 * Open the directory...
322 dp
->dir
= opendir(directory
);
330 * Copy the directory name for later use...
333 strlcpy(dp
->directory
, directory
, sizeof(dp
->directory
));
336 * Return the new directory structure...
344 * 'cupsDirRead()' - Read the next directory entry.
347 cups_dentry_t
* /* O - Directory entry */
348 cupsDirRead(cups_dir_t
*dp
) /* I - Directory */
350 char buffer
[sizeof(struct dirent
) + 1024];
351 /* Directory entry buffer */
352 struct dirent
*entry
; /* Pointer to entry */
353 char filename
[1024]; /* Full filename */
356 DEBUG_printf(("cupsDirRead(dp=%p)\n", dp
));
359 * Range check input...
366 * Try reading an entry that is not "." or ".."...
371 if (readdir_r(dp
->dir
, (struct dirent
*)buffer
, &entry
))
373 DEBUG_printf((" readdir_r() failed - %s\n", strerror(errno
)));
379 DEBUG_puts(" readdir_r() returned a NULL pointer!");
383 DEBUG_printf((" readdir_r() returned \"%s\"...\n", entry
->d_name
));
385 while (!strcmp(entry
->d_name
, ".") || !strcmp(entry
->d_name
, ".."));
388 * Copy the name over and get the file information...
391 strlcpy(dp
->entry
.filename
, entry
->d_name
, sizeof(dp
->entry
.filename
));
393 snprintf(filename
, sizeof(filename
), "%s/%s", dp
->directory
, entry
->d_name
);
394 if (stat(filename
, &(dp
->entry
.fileinfo
)))
396 DEBUG_printf((" stat() failed for \"%s\" - %s...\n", filename
,
402 * Return the entry...
405 return (&(dp
->entry
));
410 * 'cupsDirRewind()' - Rewind to the start of the directory.
414 cupsDirRewind(cups_dir_t
*dp
) /* I - Directory */
416 DEBUG_printf(("cupsDirRewind(dp=%p)\n", dp
));
419 * Range check input...
426 * Rewind the directory...