diff --git a/lib/isc/Makefile.in b/lib/isc/Makefile.in index 8b08334f78..4b6e2835e2 100644 --- a/lib/isc/Makefile.in +++ b/lib/isc/Makefile.in @@ -30,7 +30,7 @@ CWARNINGS = # Alphabetically UNIXOBJS = @ISC_ISCIPV6_O@ \ - unix/app.@O@ unix/dir.@O@ unix/file.@O@ \ + unix/app.@O@ unix/dir.@O@ unix/errno2result.@O@ unix/file.@O@ \ unix/interfaceiter.@O@ unix/net.@O@ unix/socket.@O@ \ unix/time.@O@ unix/stdtime.@O@ diff --git a/lib/isc/unix/Makefile.in b/lib/isc/unix/Makefile.in index ce3e61fa33..0a1f97db0f 100644 --- a/lib/isc/unix/Makefile.in +++ b/lib/isc/unix/Makefile.in @@ -28,13 +28,13 @@ CWARNINGS = # Alphabetically OBJS = @ISC_IPV6_O@ \ - app.@O@ dir.@O@ file.@O@ interfaceiter.@O@ net.@O@ \ - socket.@O@ stdtime.@O@ time.@O@ + app.@O@ dir.@O@ errno2result.@O@ file.@O@ interfaceiter.@O@ \ + net.@O@ socket.@O@ stdtime.@O@ time.@O@ # Alphabetically SRCS = @ISC_IPV6_C@ \ - app.c dir.c file.c interfaceiter.c net.c \ - socket.c stdtime.c time.c + app.c dir.c errno2result.c file.c interfaceiter.c \ + net.c socket.c stdtime.c time.c SUBDIRS = include TARGETS = ${OBJS} diff --git a/lib/isc/unix/dir.c b/lib/isc/unix/dir.c index 14dfd1cf77..33d5742e8b 100644 --- a/lib/isc/unix/dir.c +++ b/lib/isc/unix/dir.c @@ -15,7 +15,7 @@ * SOFTWARE. */ -/* $Id: dir.c,v 1.9 2000/05/08 14:37:54 tale Exp $ */ +/* $Id: dir.c,v 1.10 2000/05/11 15:09:27 tale Exp $ */ /* Principal Authors: DCL */ @@ -25,12 +25,14 @@ #include #include +#include #include #include +#include "errno2result.h" + #define ISC_DIR_MAGIC 0x4449522aU /* DIR*. */ -#define VALID_DIR(dir) ((dir) != NULL && \ - (dir)->magic == ISC_DIR_MAGIC) +#define VALID_DIR(dir) ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC) void isc_dir_init(isc_dir_t *dir) { @@ -60,20 +62,8 @@ isc_dir_open(isc_dir_t *dir, const char *dirname) { */ dir->handle = opendir(dirname); - if (dir->handle == NULL) { - if (errno == ENOMEM) - result = ISC_R_NOMEMORY; - else if (errno == EPERM) - result = ISC_R_NOPERM; - else if (errno == ENOENT) - result = ISC_R_NOTFOUND; - else { - UNEXPECTED_ERROR(__FILE__, __LINE__, - "opendir(%s) failed: %s", - dirname, strerror(errno)); - return (ISC_R_UNEXPECTED); - } - } + if (dir->handle == NULL) + return isc__errno2result(errno); return (result); } @@ -148,20 +138,8 @@ isc_dir_chdir(const char *dirname) { REQUIRE(dirname != NULL); - if (chdir(dirname) < 0) { - if (errno == ENOENT) - return (ISC_R_NOTFOUND); - else if (errno == EACCES) - return (ISC_R_NOPERM); - else if (errno == ENOMEM) - return (ISC_R_NOMEMORY); - else { - UNEXPECTED_ERROR(__FILE__, __LINE__, - "chdir(%s) failed: %s", - dirname, strerror(errno)); - return (ISC_R_UNEXPECTED); - } - } + if (chdir(dirname) < 0) + return (isc__errno2result(errno)); return (ISC_R_SUCCESS); } diff --git a/lib/isc/unix/errno2result.c b/lib/isc/unix/errno2result.c new file mode 100644 index 0000000000..aa3fee1d76 --- /dev/null +++ b/lib/isc/unix/errno2result.c @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2000 Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +#include + +#include + +#include "errno2result.h" + +/* + * Convert a POSIX errno value into an isc_result_t. The + * list of supported errno values is not complete; new users + * of this function should add any expected errors that are + * not already there. + */ +isc_result_t +isc__errno2result(int posixerrno) { + switch (posixerrno) { + case ENOTDIR: + case ELOOP: + case EINVAL: + case ENAMETOOLONG: + case EBADF: + return (ISC_R_INVALIDFILE); + case ENOENT: + return (ISC_R_FILENOTFOUND); + case EACCES: + return (ISC_R_NOPERM); + case EEXIST: + return (ISC_R_FILEEXISTS); + case EIO: + return (ISC_R_IOERROR); + case ENOMEM: + return (ISC_R_NOMEMORY); + default: + /* + * XXXDCL would be nice if perhaps this function could + * return the system's error string, so the caller + * might have something more descriptive than "unexpected + * error" to log with. + */ + return (ISC_R_UNEXPECTED); + } +} diff --git a/lib/isc/unix/errno2result.h b/lib/isc/unix/errno2result.h new file mode 100644 index 0000000000..6128ee84db --- /dev/null +++ b/lib/isc/unix/errno2result.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2000 Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +#include + +isc_result_t +isc__errno2result(int posixerrno); diff --git a/lib/isc/unix/file.c b/lib/isc/unix/file.c index 3f05739490..695e5faa0e 100644 --- a/lib/isc/unix/file.c +++ b/lib/isc/unix/file.c @@ -28,35 +28,7 @@ #include #include -/* - * Convert a POSIX errno value into an isc_result_t. The - * list of supported errno values is not complete; new users - * of this function should add any expected errors that are - * not already there. - */ -static isc_result_t -posix_result(int posixerrno) { - switch (posixerrno) { - case ENOTDIR: - case ELOOP: - case EINVAL: - case ENAMETOOLONG: - case EBADF: - return (ISC_R_INVALIDFILE); - case ENOENT: - return (ISC_R_FILENOTFOUND); - case EACCES: - return (ISC_R_NOPERM); - case EEXIST: - return (ISC_R_FILEEXISTS); - case EIO: - return (ISC_R_IOERROR); - case ENOMEM: - return (ISC_R_NOMEMORY); - default: - return (ISC_R_UNEXPECTED); - } -} +#include "errno2result.h" /* * XXXDCL As the API for accessing file statistics undoubtedly gets expanded, @@ -71,7 +43,7 @@ file_stats(const char *file, struct stat *stats) { isc_result_t result = ISC_R_SUCCESS; if (stat(file, stats) != 0) - result = posix_result(errno); + result = isc__errno2result(errno); return (result); } @@ -138,11 +110,11 @@ isc_file_openunique(char *templet, FILE **fp) { fd = mkstemp(templet); if (fd == -1) - result = posix_result(errno); + result = isc__errno2result(errno); if (result == ISC_R_SUCCESS) { f = fdopen(fd, "w+"); if (f == NULL) { - result = posix_result(errno); + result = isc__errno2result(errno); (void)remove(templet); (void)close(fd); @@ -159,7 +131,7 @@ isc_file_fopen(const char *filename, const char *mode, FILE **fp) { f = fopen(filename, mode); if (f == NULL) - return (posix_result(errno)); + return (isc__errno2result(errno)); *fp = f; return (ISC_R_SUCCESS); } @@ -172,7 +144,7 @@ isc_file_fclose(FILE *f) { if (r == 0) return (ISC_R_SUCCESS); else - return (posix_result(errno)); + return (isc__errno2result(errno)); } isc_result_t @@ -183,7 +155,7 @@ isc_file_fseek(FILE *f, long offset, int whence) { if (r == 0) return (ISC_R_SUCCESS); else - return (posix_result(errno)); + return (isc__errno2result(errno)); } isc_result_t @@ -197,7 +169,7 @@ isc_file_fread(void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nret) { if (feof(f)) result = ISC_R_EOF; else - result = posix_result(errno); + result = isc__errno2result(errno); } if (nret != NULL) *nret = r; @@ -212,7 +184,7 @@ isc_file_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *f, size_t *nre clearerr(f); r = fwrite(ptr, size, nmemb, f); if (r != nmemb) - result = posix_result(errno); + result = isc__errno2result(errno); if (nret != NULL) *nret = r; return (result); @@ -226,7 +198,7 @@ isc_file_fflush(FILE *f) { if (r == 0) return (ISC_R_SUCCESS); else - return (posix_result(errno)); + return (isc__errno2result(errno)); } isc_result_t @@ -237,7 +209,7 @@ isc_file_ffsync(FILE *f) { if (r == 0) return (ISC_R_SUCCESS); else - return (posix_result(errno)); + return (isc__errno2result(errno)); } isc_result_t @@ -248,5 +220,5 @@ isc_file_remove(const char *filename) { if (r == 0) return (ISC_R_SUCCESS); else - return (posix_result(errno)); + return (isc__errno2result(errno)); } diff --git a/util/copyrights b/util/copyrights index ef555e32ef..c8b3d034e6 100644 --- a/util/copyrights +++ b/util/copyrights @@ -705,6 +705,8 @@ ./lib/isc/unix/Makefile.in MAKE 1998,1999,2000 ./lib/isc/unix/app.c C 1999,2000 ./lib/isc/unix/dir.c C 1999,2000 +./lib/isc/unix/errno2isc.c C 2000 +./lib/isc/unix/errno2isc.h C 2000 ./lib/isc/unix/file.c C 2000 ./lib/isc/unix/ifiter_ioctl.c C 1999,2000 ./lib/isc/unix/ifiter_sysctl.c C 1999,2000