1 2/* 3 * $Id: 1410aaa5f08a562e0cd6c28ffae5a49dc7a3164f $ 4 * Time-stamp: "2009-07-23 17:23:46 bkorb" 5 * 6 * This file is part of AutoOpts, a companion to AutoGen. 7 * AutoOpts is free software. 8 * AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved 9 * 10 * AutoOpts is available under any one of two licenses. The license 11 * in use must be one of these two and the choice is under the control 12 * of the user of the license. 13 * 14 * The GNU Lesser General Public License, version 3 or later 15 * See the files "COPYING.lgplv3" and "COPYING.gplv3" 16 * 17 * The Modified Berkeley Software Distribution License 18 * See the file "COPYING.mbsd" 19 * 20 * These files have the following md5sums: 21 * 22 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3 23 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3 24 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd 25 */ 26 27/*=export_func optionFileCheck 28 * private: 29 * 30 * what: Decipher a boolean value 31 * arg: + tOptions* + pOpts + program options descriptor + 32 * arg: + tOptDesc* + pOptDesc + the descriptor for this arg + 33 * arg: + teOptFileType + ftype + File handling type + 34 * arg: + tuFileMode + mode + file open mode (if needed) + 35 * 36 * doc: 37 * Make sure the named file conforms with the file type mode. 38 * The mode specifies if the file must exist, must not exist or may 39 * (or may not) exist. The mode may also specify opening the 40 * file: don't, open just the descriptor (fd), or open as a stream 41 * (FILE* pointer). 42=*/ 43void 44optionFileCheck(tOptions* pOpts, tOptDesc* pOD, 45 teOptFileType ftype, tuFileMode mode) 46{ 47 if (pOpts <= OPTPROC_EMIT_LIMIT) { 48 if (pOpts != OPTPROC_EMIT_USAGE) 49 return; 50 51 switch (ftype & FTYPE_MODE_EXIST_MASK) { 52 case FTYPE_MODE_MUST_NOT_EXIST: 53 fputs(zFileCannotExist, option_usage_fp); 54 break; 55 56 case FTYPE_MODE_MUST_EXIST: 57 fputs(zFileMustExist, option_usage_fp); 58 break; 59 } 60 return; 61 } 62 63 if ((pOD->fOptState & OPTST_RESET) != 0) { 64 if (pOD->optCookie != NULL) 65 AGFREE(pOD->optCookie); 66 return; 67 } 68 69 { 70 struct stat sb; 71 72 errno = 0; 73 74 switch (ftype & FTYPE_MODE_EXIST_MASK) { 75 case FTYPE_MODE_MUST_NOT_EXIST: 76 if ( (stat(pOD->optArg.argString, &sb) == 0) 77 || (errno != ENOENT) ){ 78 if (errno == 0) 79 errno = EINVAL; 80 fprintf(stderr, zFSOptError, errno, strerror(errno), 81 zFSOptErrNoExist, pOD->optArg.argString, pOD->pz_Name); 82 pOpts->pUsageProc(pOpts, EXIT_FAILURE); 83 /* NOTREACHED */ 84 } 85 /* FALLTHROUGH */ 86 87 default: 88 case FTYPE_MODE_MAY_EXIST: 89 { 90 char * p = strrchr(pOD->optArg.argString, DIRCH); 91 if (p != NULL) 92 *p = NUL; 93 if ( (stat(pOD->optArg.argString, &sb) != 0) 94 || (errno = EINVAL, ! S_ISDIR(sb.st_mode)) ){ 95 fprintf(stderr, zFSOptError, errno, strerror(errno), 96 zFSOptErrMayExist, pOD->optArg.argString, pOD->pz_Name); 97 pOpts->pUsageProc(pOpts, EXIT_FAILURE); 98 /* NOTREACHED */ 99 } 100 if (p != NULL) 101 *p = DIRCH; 102 break; 103 } 104 105 case FTYPE_MODE_MUST_EXIST: 106 if ( (stat(pOD->optArg.argString, &sb) != 0) 107 || (errno = EINVAL, ! S_ISREG(sb.st_mode)) ){ 108 fprintf(stderr, zFSOptError, errno, strerror(errno), 109 zFSOptErrMustExist, pOD->optArg.argString, 110 pOD->pz_Name); 111 pOpts->pUsageProc(pOpts, EXIT_FAILURE); 112 /* NOTREACHED */ 113 } 114 break; 115 } 116 } 117 118 switch (ftype & FTYPE_MODE_OPEN_MASK) { 119 default: 120 case FTYPE_MODE_NO_OPEN: 121 break; 122 123 case FTYPE_MODE_OPEN_FD: 124 { 125 int fd = open(pOD->optArg.argString, mode.file_flags); 126 if (fd < 0) { 127 fprintf(stderr, zFSOptError, errno, strerror(errno), 128 zFSOptErrOpen, pOD->optArg.argString, pOD->pz_Name); 129 pOpts->pUsageProc(pOpts, EXIT_FAILURE); 130 /* NOTREACHED */ 131 } 132 133 if ((pOD->fOptState & OPTST_ALLOC_ARG) != 0) 134 pOD->optCookie = (void *)pOD->optArg.argString; 135 else 136 AGDUPSTR(pOD->optCookie, pOD->optArg.argString, "file name"); 137 138 pOD->optArg.argFd = fd; 139 pOD->fOptState &= ~OPTST_ALLOC_ARG; 140 break; 141 } 142 143 case FTYPE_MODE_FOPEN_FP: 144 { 145 FILE* fp = fopen(pOD->optArg.argString, mode.file_mode); 146 if (fp == NULL) { 147 fprintf(stderr, zFSOptError, errno, strerror(errno), 148 zFSOptErrFopen, pOD->optArg.argString, pOD->pz_Name); 149 pOpts->pUsageProc(pOpts, EXIT_FAILURE); 150 /* NOTREACHED */ 151 } 152 153 if ((pOD->fOptState & OPTST_ALLOC_ARG) != 0) 154 pOD->optCookie = (void *)pOD->optArg.argString; 155 else 156 AGDUPSTR(pOD->optCookie, pOD->optArg.argString, "file name"); 157 158 pOD->optArg.argFp = fp; 159 pOD->fOptState &= ~OPTST_ALLOC_ARG; 160 break; 161 } 162 } 163} 164/* 165 * Local Variables: 166 * mode: C 167 * c-file-style: "stroustrup" 168 * indent-tabs-mode: nil 169 * End: 170 * end of autoopts/file.c */ 171