12b15cb3dSCy Schubert
22b15cb3dSCy Schubert /**
32b15cb3dSCy Schubert * \file file.c
42b15cb3dSCy Schubert *
52b15cb3dSCy Schubert * Handle options that have file names for arguments.
62b15cb3dSCy Schubert *
72b15cb3dSCy Schubert * @addtogroup autoopts
82b15cb3dSCy Schubert * @{
92b15cb3dSCy Schubert */
102b15cb3dSCy Schubert /*
112b15cb3dSCy Schubert * This file is part of AutoOpts, a companion to AutoGen.
122b15cb3dSCy Schubert * AutoOpts is free software.
13*a466cc55SCy Schubert * AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
142b15cb3dSCy Schubert *
152b15cb3dSCy Schubert * AutoOpts is available under any one of two licenses. The license
162b15cb3dSCy Schubert * in use must be one of these two and the choice is under the control
172b15cb3dSCy Schubert * of the user of the license.
182b15cb3dSCy Schubert *
192b15cb3dSCy Schubert * The GNU Lesser General Public License, version 3 or later
202b15cb3dSCy Schubert * See the files "COPYING.lgplv3" and "COPYING.gplv3"
212b15cb3dSCy Schubert *
222b15cb3dSCy Schubert * The Modified Berkeley Software Distribution License
232b15cb3dSCy Schubert * See the file "COPYING.mbsd"
242b15cb3dSCy Schubert *
252b15cb3dSCy Schubert * These files have the following sha256 sums:
262b15cb3dSCy Schubert *
272b15cb3dSCy Schubert * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
282b15cb3dSCy Schubert * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
292b15cb3dSCy Schubert * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
302b15cb3dSCy Schubert */
312b15cb3dSCy Schubert
322b15cb3dSCy Schubert /**
332b15cb3dSCy Schubert * Make sure the directory containing the subject file exists and that
342b15cb3dSCy Schubert * the file exists or does not exist, per the option requirements.
352b15cb3dSCy Schubert *
362b15cb3dSCy Schubert * @param ftype file existence type flags
372b15cb3dSCy Schubert * @param pOpts program option descriptor
382b15cb3dSCy Schubert * @param pOD the option descriptor
392b15cb3dSCy Schubert */
402b15cb3dSCy Schubert static void
check_existence(teOptFileType ftype,tOptions * pOpts,tOptDesc * pOD)412b15cb3dSCy Schubert check_existence(teOptFileType ftype, tOptions * pOpts, tOptDesc * pOD)
422b15cb3dSCy Schubert {
432b15cb3dSCy Schubert char const * fname = pOD->optArg.argString;
442b15cb3dSCy Schubert struct stat sb;
452b15cb3dSCy Schubert
462b15cb3dSCy Schubert errno = 0;
472b15cb3dSCy Schubert
482b15cb3dSCy Schubert switch (ftype & FTYPE_MODE_EXIST_MASK) {
492b15cb3dSCy Schubert case FTYPE_MODE_MUST_NOT_EXIST:
502b15cb3dSCy Schubert if ((stat(fname, &sb) == 0) || (errno != ENOENT)) {
512b15cb3dSCy Schubert if (errno == 0)
522b15cb3dSCy Schubert errno = EINVAL;
532b15cb3dSCy Schubert fserr_exit(pOpts->pzProgName, "stat", fname);
542b15cb3dSCy Schubert /* NOTREACHED */
552b15cb3dSCy Schubert }
562b15cb3dSCy Schubert /* FALLTHROUGH */
572b15cb3dSCy Schubert
582b15cb3dSCy Schubert default:
592b15cb3dSCy Schubert case FTYPE_MODE_MAY_EXIST:
602b15cb3dSCy Schubert {
612b15cb3dSCy Schubert char * p = strrchr(fname, DIRCH);
622b15cb3dSCy Schubert size_t l;
632b15cb3dSCy Schubert
642b15cb3dSCy Schubert if (p == NULL)
652b15cb3dSCy Schubert /*
662b15cb3dSCy Schubert * The file may or may not exist and its directory is ".".
672b15cb3dSCy Schubert * Assume that "." exists.
682b15cb3dSCy Schubert */
692b15cb3dSCy Schubert break;
702b15cb3dSCy Schubert
712b15cb3dSCy Schubert l = (size_t)(p - fname);
722b15cb3dSCy Schubert p = AGALOC(l + 1, "fname");
732b15cb3dSCy Schubert memcpy(p, fname, l);
742b15cb3dSCy Schubert p[l] = NUL;
752b15cb3dSCy Schubert
762b15cb3dSCy Schubert if ((stat(p, &sb) != 0) || (errno = EINVAL, ! S_ISDIR(sb.st_mode)))
772b15cb3dSCy Schubert fserr_exit(pOpts->pzProgName, "stat", p);
782b15cb3dSCy Schubert /* NOTREACHED */
792b15cb3dSCy Schubert
802b15cb3dSCy Schubert AGFREE(p);
812b15cb3dSCy Schubert break;
822b15cb3dSCy Schubert }
832b15cb3dSCy Schubert
842b15cb3dSCy Schubert case FTYPE_MODE_MUST_EXIST:
852b15cb3dSCy Schubert if ( (stat(fname, &sb) != 0)
862b15cb3dSCy Schubert || (errno = EINVAL, ! S_ISREG(sb.st_mode)) )
872b15cb3dSCy Schubert fserr_exit(pOpts->pzProgName, "stat", fname);
882b15cb3dSCy Schubert /* NOTREACHED */
892b15cb3dSCy Schubert
902b15cb3dSCy Schubert break;
912b15cb3dSCy Schubert }
922b15cb3dSCy Schubert }
932b15cb3dSCy Schubert
942b15cb3dSCy Schubert /**
952b15cb3dSCy Schubert * Open the specified file with open(2) and save the FD.
962b15cb3dSCy Schubert *
972b15cb3dSCy Schubert * @param pOpts program option descriptor
982b15cb3dSCy Schubert * @param pOD the option descriptor
992b15cb3dSCy Schubert * @param mode the open mode (uses int flags value)
1002b15cb3dSCy Schubert */
1012b15cb3dSCy Schubert static void
open_file_fd(tOptions * pOpts,tOptDesc * pOD,tuFileMode mode)1022b15cb3dSCy Schubert open_file_fd(tOptions * pOpts, tOptDesc * pOD, tuFileMode mode)
1032b15cb3dSCy Schubert {
1042b15cb3dSCy Schubert int fd = open(pOD->optArg.argString, mode.file_flags);
1052b15cb3dSCy Schubert if (fd < 0)
1062b15cb3dSCy Schubert fserr_exit(pOpts->pzProgName, "open", pOD->optArg.argString);
1072b15cb3dSCy Schubert /* NOTREACHED */
1082b15cb3dSCy Schubert
1092b15cb3dSCy Schubert if ((pOD->fOptState & OPTST_ALLOC_ARG) != 0)
110276da39aSCy Schubert pOD->optCookie = VOIDP(pOD->optArg.argString);
1112b15cb3dSCy Schubert else
1122b15cb3dSCy Schubert AGDUPSTR(pOD->optCookie, pOD->optArg.argString, "file name");
1132b15cb3dSCy Schubert
1142b15cb3dSCy Schubert pOD->optArg.argFd = fd;
1152b15cb3dSCy Schubert pOD->fOptState &= ~OPTST_ALLOC_ARG;
1162b15cb3dSCy Schubert }
1172b15cb3dSCy Schubert
1182b15cb3dSCy Schubert /**
1192b15cb3dSCy Schubert * Open the specified file with open(2) and save the FD.
1202b15cb3dSCy Schubert *
1212b15cb3dSCy Schubert * @param pOpts program option descriptor
1222b15cb3dSCy Schubert * @param pOD the option descriptor
1232b15cb3dSCy Schubert * @param mode the open mode (uses "char *" mode value)
1242b15cb3dSCy Schubert */
1252b15cb3dSCy Schubert static void
fopen_file_fp(tOptions * pOpts,tOptDesc * pOD,tuFileMode mode)1262b15cb3dSCy Schubert fopen_file_fp(tOptions * pOpts, tOptDesc * pOD, tuFileMode mode)
1272b15cb3dSCy Schubert {
1282b15cb3dSCy Schubert FILE * fp = fopen(pOD->optArg.argString, mode.file_mode);
1292b15cb3dSCy Schubert if (fp == NULL)
1302b15cb3dSCy Schubert fserr_exit(pOpts->pzProgName, "fopen", pOD->optArg.argString);
1312b15cb3dSCy Schubert /* NOTREACHED */
1322b15cb3dSCy Schubert
1332b15cb3dSCy Schubert if ((pOD->fOptState & OPTST_ALLOC_ARG) != 0)
134276da39aSCy Schubert pOD->optCookie = VOIDP(pOD->optArg.argString);
1352b15cb3dSCy Schubert else
1362b15cb3dSCy Schubert AGDUPSTR(pOD->optCookie, pOD->optArg.argString, "file name");
1372b15cb3dSCy Schubert
1382b15cb3dSCy Schubert pOD->optArg.argFp = fp;
1392b15cb3dSCy Schubert pOD->fOptState &= ~OPTST_ALLOC_ARG;
1402b15cb3dSCy Schubert }
1412b15cb3dSCy Schubert
1422b15cb3dSCy Schubert /*=export_func optionFileCheck
1432b15cb3dSCy Schubert * private:
1442b15cb3dSCy Schubert *
1452b15cb3dSCy Schubert * what: Decipher a boolean value
1462b15cb3dSCy Schubert * arg: + tOptions * + pOpts + program options descriptor +
1472b15cb3dSCy Schubert * arg: + tOptDesc * + pOptDesc + the descriptor for this arg +
1482b15cb3dSCy Schubert * arg: + teOptFileType + ftype + File handling type +
1492b15cb3dSCy Schubert * arg: + tuFileMode + mode + file open mode (if needed) +
1502b15cb3dSCy Schubert *
1512b15cb3dSCy Schubert * doc:
1522b15cb3dSCy Schubert * Make sure the named file conforms with the file type mode.
1532b15cb3dSCy Schubert * The mode specifies if the file must exist, must not exist or may
1542b15cb3dSCy Schubert * (or may not) exist. The mode may also specify opening the
1552b15cb3dSCy Schubert * file: don't, open just the descriptor (fd), or open as a stream
1562b15cb3dSCy Schubert * (FILE * pointer).
1572b15cb3dSCy Schubert =*/
1582b15cb3dSCy Schubert void
optionFileCheck(tOptions * pOpts,tOptDesc * pOD,teOptFileType ftype,tuFileMode mode)1592b15cb3dSCy Schubert optionFileCheck(tOptions * pOpts, tOptDesc * pOD,
1602b15cb3dSCy Schubert teOptFileType ftype, tuFileMode mode)
1612b15cb3dSCy Schubert {
1622b15cb3dSCy Schubert if (pOpts <= OPTPROC_EMIT_LIMIT) {
1632b15cb3dSCy Schubert if (pOpts != OPTPROC_EMIT_USAGE)
1642b15cb3dSCy Schubert return;
1652b15cb3dSCy Schubert
1662b15cb3dSCy Schubert switch (ftype & FTYPE_MODE_EXIST_MASK) {
1672b15cb3dSCy Schubert case FTYPE_MODE_MUST_NOT_EXIST:
1682b15cb3dSCy Schubert fputs(zFileCannotExist + tab_skip_ct, option_usage_fp);
1692b15cb3dSCy Schubert break;
1702b15cb3dSCy Schubert
1712b15cb3dSCy Schubert case FTYPE_MODE_MUST_EXIST:
1722b15cb3dSCy Schubert fputs(zFileMustExist + tab_skip_ct, option_usage_fp);
1732b15cb3dSCy Schubert break;
1742b15cb3dSCy Schubert }
1752b15cb3dSCy Schubert return;
1762b15cb3dSCy Schubert }
1772b15cb3dSCy Schubert
1782b15cb3dSCy Schubert if ((pOD->fOptState & OPTST_RESET) != 0) {
1792b15cb3dSCy Schubert if (pOD->optCookie != NULL)
1802b15cb3dSCy Schubert AGFREE(pOD->optCookie);
1812b15cb3dSCy Schubert return;
1822b15cb3dSCy Schubert }
1832b15cb3dSCy Schubert
1842b15cb3dSCy Schubert check_existence(ftype, pOpts, pOD);
1852b15cb3dSCy Schubert
1862b15cb3dSCy Schubert switch (ftype & FTYPE_MODE_OPEN_MASK) {
1872b15cb3dSCy Schubert default:
1882b15cb3dSCy Schubert case FTYPE_MODE_NO_OPEN: break;
1892b15cb3dSCy Schubert case FTYPE_MODE_OPEN_FD: open_file_fd( pOpts, pOD, mode); break;
1902b15cb3dSCy Schubert case FTYPE_MODE_FOPEN_FP: fopen_file_fp(pOpts, pOD, mode); break;
1912b15cb3dSCy Schubert }
1922b15cb3dSCy Schubert }
1932b15cb3dSCy Schubert
1942b15cb3dSCy Schubert /** @}
1952b15cb3dSCy Schubert *
1962b15cb3dSCy Schubert * Local Variables:
1972b15cb3dSCy Schubert * mode: C
1982b15cb3dSCy Schubert * c-file-style: "stroustrup"
1992b15cb3dSCy Schubert * indent-tabs-mode: nil
2002b15cb3dSCy Schubert * End:
2012b15cb3dSCy Schubert * end of autoopts/file.c */
202