dir.c revision f8f8a5b3a33f581c6e209c7ded8168e7acc73dfb
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Copyright (C) 1999-2001 Internet Software Consortium.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Permission to use, copy, modify, and/or distribute this software for any
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * purpose with or without fee is hereby granted, provided that the above
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * copyright notice and this permission notice appear in all copies.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * PERFORMANCE OF THIS SOFTWARE.
110d1702731f42dd620879c1d765ebe91f3920ceMichael Graff/* $Id: dir.c,v 1.27 2008/12/01 23:47:45 tbox Exp $ */
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley * \author Principal Authors: DCL */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define ISC_DIR_MAGIC ISC_MAGIC('D', 'I', 'R', '*')
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley#define VALID_DIR(dir) ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * \brief Allocate workspace and open directory stream. If either one fails,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * NULL will be returned.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleyisc_dir_open(isc_dir_t *dir, const char *dirname) {
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley * Copy directory name. Need to have enough space for the name,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * a possible path separator, the wildcard, and the final NUL.
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Graff /* XXXDCL ? */
e496615043400500492fa7b891c515c8e7cb7d08Bob Halley * Append path separator, if needed, and "*".
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley *p++ = '\0';
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Open stream.
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Graff * \brief Return previously retrieved file or get next one.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Unix's dirent has
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * separate open and read functions, but the Win32 and DOS interfaces open
e496615043400500492fa7b891c515c8e7cb7d08Bob Halley * the dir stream and reads the first file in one operation.
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * Fetch next file in directory.
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * Make sure that the space for the name is long enough.
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley if (sizeof(dir->entry.name) <= strlen(entry->d_name))
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * Some dirents have d_namlen, but it is not portable.
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * \brief Close directory stream.
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * \brief Reposition directory stream at start.
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley * \brief Change the current directory to 'dirname'.
8d6024e7cffbd84fa8d06ce50c60307d7b3b49c2Michael Graff * \brief mkdtemp is not portable, so this emulates it.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * Replace trailing Xs with the process-id, zero-filled.
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff for (x = templet + strlen(templet) - 1; *x == 'X' && x >= templet;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley x++; /* Set x to start of ex-Xs. */
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * The BSD algorithm.
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley while (*p != '\0') {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley else if (*p != 'z')
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff * Reset character and move to next.
c1e7aff941dbf40090fec49300e728ad017d4f0cMark Andrews if (*p == '\0') {
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * Tried all combinations. errno should already
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff * be EEXIST, but ensure it is anyway for
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * isc__errno2result().
c1e7aff941dbf40090fec49300e728ad017d4f0cMark Andrews if (i == -1)