5 * Handle options that have file names for arguments.
11 * This file is part of AutoOpts, a companion to AutoGen.
12 * AutoOpts is free software.
13 * AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
15 * AutoOpts is available under any one of two licenses. The license
16 * in use must be one of these two and the choice is under the control
17 * of the user of the license.
19 * The GNU Lesser General Public License, version 3 or later
20 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
22 * The Modified Berkeley Software Distribution License
23 * See the file "COPYING.mbsd"
25 * These files have the following sha256 sums:
27 * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
28 * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
29 * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
33 * Make sure the directory containing the subject file exists and that
34 * the file exists or does not exist, per the option requirements.
36 * @param ftype file existence type flags
37 * @param pOpts program option descriptor
38 * @param pOD the option descriptor
41 check_existence(teOptFileType ftype, tOptions * pOpts, tOptDesc * pOD)
43 char const * fname = pOD->optArg.argString;
48 switch (ftype & FTYPE_MODE_EXIST_MASK) {
49 case FTYPE_MODE_MUST_NOT_EXIST:
50 if ((stat(fname, &sb) == 0) || (errno != ENOENT)) {
53 fserr_exit(pOpts->pzProgName, "stat", fname);
59 case FTYPE_MODE_MAY_EXIST:
61 char * p = strrchr(fname, DIRCH);
66 * The file may or may not exist and its directory is ".".
67 * Assume that "." exists.
71 l = (size_t)(p - fname);
72 p = AGALOC(l + 1, "fname");
76 if ((stat(p, &sb) != 0) || (errno = EINVAL, ! S_ISDIR(sb.st_mode)))
77 fserr_exit(pOpts->pzProgName, "stat", p);
84 case FTYPE_MODE_MUST_EXIST:
85 if ( (stat(fname, &sb) != 0)
86 || (errno = EINVAL, ! S_ISREG(sb.st_mode)) )
87 fserr_exit(pOpts->pzProgName, "stat", fname);
95 * Open the specified file with open(2) and save the FD.
97 * @param pOpts program option descriptor
98 * @param pOD the option descriptor
99 * @param mode the open mode (uses int flags value)
102 open_file_fd(tOptions * pOpts, tOptDesc * pOD, tuFileMode mode)
104 int fd = open(pOD->optArg.argString, mode.file_flags);
106 fserr_exit(pOpts->pzProgName, "open", pOD->optArg.argString);
109 if ((pOD->fOptState & OPTST_ALLOC_ARG) != 0)
110 pOD->optCookie = VOIDP(pOD->optArg.argString);
112 AGDUPSTR(pOD->optCookie, pOD->optArg.argString, "file name");
114 pOD->optArg.argFd = fd;
115 pOD->fOptState &= ~OPTST_ALLOC_ARG;
119 * Open the specified file with open(2) and save the FD.
121 * @param pOpts program option descriptor
122 * @param pOD the option descriptor
123 * @param mode the open mode (uses "char *" mode value)
126 fopen_file_fp(tOptions * pOpts, tOptDesc * pOD, tuFileMode mode)
128 FILE * fp = fopen(pOD->optArg.argString, mode.file_mode);
130 fserr_exit(pOpts->pzProgName, "fopen", pOD->optArg.argString);
133 if ((pOD->fOptState & OPTST_ALLOC_ARG) != 0)
134 pOD->optCookie = VOIDP(pOD->optArg.argString);
136 AGDUPSTR(pOD->optCookie, pOD->optArg.argString, "file name");
138 pOD->optArg.argFp = fp;
139 pOD->fOptState &= ~OPTST_ALLOC_ARG;
142 /*=export_func optionFileCheck
145 * what: Decipher a boolean value
146 * arg: + tOptions * + pOpts + program options descriptor +
147 * arg: + tOptDesc * + pOptDesc + the descriptor for this arg +
148 * arg: + teOptFileType + ftype + File handling type +
149 * arg: + tuFileMode + mode + file open mode (if needed) +
152 * Make sure the named file conforms with the file type mode.
153 * The mode specifies if the file must exist, must not exist or may
154 * (or may not) exist. The mode may also specify opening the
155 * file: don't, open just the descriptor (fd), or open as a stream
159 optionFileCheck(tOptions * pOpts, tOptDesc * pOD,
160 teOptFileType ftype, tuFileMode mode)
162 if (pOpts <= OPTPROC_EMIT_LIMIT) {
163 if (pOpts != OPTPROC_EMIT_USAGE)
166 switch (ftype & FTYPE_MODE_EXIST_MASK) {
167 case FTYPE_MODE_MUST_NOT_EXIST:
168 fputs(zFileCannotExist + tab_skip_ct, option_usage_fp);
171 case FTYPE_MODE_MUST_EXIST:
172 fputs(zFileMustExist + tab_skip_ct, option_usage_fp);
178 if ((pOD->fOptState & OPTST_RESET) != 0) {
179 if (pOD->optCookie != NULL)
180 AGFREE(pOD->optCookie);
184 check_existence(ftype, pOpts, pOD);
186 switch (ftype & FTYPE_MODE_OPEN_MASK) {
188 case FTYPE_MODE_NO_OPEN: break;
189 case FTYPE_MODE_OPEN_FD: open_file_fd( pOpts, pOD, mode); break;
190 case FTYPE_MODE_FOPEN_FP: fopen_file_fp(pOpts, pOD, mode); break;
198 * c-file-style: "stroustrup"
199 * indent-tabs-mode: nil
201 * end of autoopts/file.c */