5 * Time-stamp: "2011-08-06 08:49:35 bkorb"
7 * This file is part of AutoOpts, a companion to AutoGen.
8 * AutoOpts is free software.
9 * AutoOpts is Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
11 * AutoOpts is available under any one of two licenses. The license
12 * in use must be one of these two and the choice is under the control
13 * of the user of the license.
15 * The GNU Lesser General Public License, version 3 or later
16 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
18 * The Modified Berkeley Software Distribution License
19 * See the file "COPYING.mbsd"
21 * These files have the following md5sums:
23 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
24 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
25 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
29 * Make sure the directory containing the subject file exists and that
30 * the file exists or does not exist, per the option requirements.
32 * @param ftype file existence type flags
33 * @param pOpts program option descriptor
34 * @param pOD the option descriptor
37 check_existence(teOptFileType ftype
, tOptions
* pOpts
, tOptDesc
* pOD
)
39 char const * fname
= pOD
->optArg
.argString
;
44 switch (ftype
& FTYPE_MODE_EXIST_MASK
) {
45 case FTYPE_MODE_MUST_NOT_EXIST
:
46 if ((stat(fname
, &sb
) == 0) || (errno
!= ENOENT
)) {
49 fprintf(stderr
, zFSOptError
, errno
, strerror(errno
),
50 zFSOptErrNoExist
, fname
, pOD
->pz_Name
);
51 pOpts
->pUsageProc(pOpts
, EXIT_FAILURE
);
57 case FTYPE_MODE_MAY_EXIST
:
59 char * p
= strrchr(fname
, DIRCH
);
64 * The file may or may not exist and its directory is ".".
65 * Assume that "." exists.
70 p
= AGALOC(l
+ 1, "fname");
74 if ((stat(p
, &sb
) != 0) || (errno
= EINVAL
, ! S_ISDIR(sb
.st_mode
))) {
75 fprintf(stderr
, zFSOptError
, errno
, strerror(errno
),
76 zFSOptErrMayExist
, fname
, pOD
->pz_Name
);
77 pOpts
->pUsageProc(pOpts
, EXIT_FAILURE
);
84 case FTYPE_MODE_MUST_EXIST
:
85 if ( (stat(fname
, &sb
) != 0)
86 || (errno
= EINVAL
, ! S_ISREG(sb
.st_mode
)) ) {
87 fprintf(stderr
, zFSOptError
, errno
, strerror(errno
),
88 zFSOptErrMustExist
, fname
,
90 pOpts
->pUsageProc(pOpts
, EXIT_FAILURE
);
98 * Open the specified file with open(2) and save the FD.
100 * @param pOpts program option descriptor
101 * @param pOD the option descriptor
102 * @param mode the open mode (uses int flags value)
105 open_file_fd(tOptions
* pOpts
, tOptDesc
* pOD
, tuFileMode mode
)
107 int fd
= open(pOD
->optArg
.argString
, mode
.file_flags
);
109 fprintf(stderr
, zFSOptError
, errno
, strerror(errno
),
110 zFSOptErrOpen
, pOD
->optArg
.argString
, pOD
->pz_Name
);
111 pOpts
->pUsageProc(pOpts
, EXIT_FAILURE
);
115 if ((pOD
->fOptState
& OPTST_ALLOC_ARG
) != 0)
116 pOD
->optCookie
= (void *)pOD
->optArg
.argString
;
118 AGDUPSTR(pOD
->optCookie
, pOD
->optArg
.argString
, "file name");
120 pOD
->optArg
.argFd
= fd
;
121 pOD
->fOptState
&= ~OPTST_ALLOC_ARG
;
125 * Open the specified file with open(2) and save the FD.
127 * @param pOpts program option descriptor
128 * @param pOD the option descriptor
129 * @param mode the open mode (uses "char *" mode value)
132 fopen_file_fp(tOptions
* pOpts
, tOptDesc
* pOD
, tuFileMode mode
)
134 FILE* fp
= fopen(pOD
->optArg
.argString
, mode
.file_mode
);
136 fprintf(stderr
, zFSOptError
, errno
, strerror(errno
),
137 zFSOptErrFopen
, pOD
->optArg
.argString
, pOD
->pz_Name
);
138 pOpts
->pUsageProc(pOpts
, EXIT_FAILURE
);
142 if ((pOD
->fOptState
& OPTST_ALLOC_ARG
) != 0)
143 pOD
->optCookie
= (void *)pOD
->optArg
.argString
;
145 AGDUPSTR(pOD
->optCookie
, pOD
->optArg
.argString
, "file name");
147 pOD
->optArg
.argFp
= fp
;
148 pOD
->fOptState
&= ~OPTST_ALLOC_ARG
;
151 /*=export_func optionFileCheck
154 * what: Decipher a boolean value
155 * arg: + tOptions* + pOpts + program options descriptor +
156 * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
157 * arg: + teOptFileType + ftype + File handling type +
158 * arg: + tuFileMode + mode + file open mode (if needed) +
161 * Make sure the named file conforms with the file type mode.
162 * The mode specifies if the file must exist, must not exist or may
163 * (or may not) exist. The mode may also specify opening the
164 * file: don't, open just the descriptor (fd), or open as a stream
168 optionFileCheck(tOptions
* pOpts
, tOptDesc
* pOD
,
169 teOptFileType ftype
, tuFileMode mode
)
171 if (pOpts
<= OPTPROC_EMIT_LIMIT
) {
172 if (pOpts
!= OPTPROC_EMIT_USAGE
)
175 switch (ftype
& FTYPE_MODE_EXIST_MASK
) {
176 case FTYPE_MODE_MUST_NOT_EXIST
:
177 fputs(zFileCannotExist
, option_usage_fp
);
180 case FTYPE_MODE_MUST_EXIST
:
181 fputs(zFileMustExist
, option_usage_fp
);
187 if ((pOD
->fOptState
& OPTST_RESET
) != 0) {
188 if (pOD
->optCookie
!= NULL
)
189 AGFREE(pOD
->optCookie
);
193 check_existence(ftype
, pOpts
, pOD
);
195 switch (ftype
& FTYPE_MODE_OPEN_MASK
) {
197 case FTYPE_MODE_NO_OPEN
: break;
198 case FTYPE_MODE_OPEN_FD
: open_file_fd( pOpts
, pOD
, mode
); break;
199 case FTYPE_MODE_FOPEN_FP
: fopen_file_fp(pOpts
, pOD
, mode
); break;
205 * c-file-style: "stroustrup"
206 * indent-tabs-mode: nil
208 * end of autoopts/file.c */