bumped version
[gnutls.git] / src / libopts / file.c
blob5a5b3eb53f74624b07185edf6c81c9126e1b8341
2 /**
3 * \file file.c
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
28 /**
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
36 static void
37 check_existence(teOptFileType ftype, tOptions * pOpts, tOptDesc * pOD)
39 char const * fname = pOD->optArg.argString;
40 struct stat sb;
42 errno = 0;
44 switch (ftype & FTYPE_MODE_EXIST_MASK) {
45 case FTYPE_MODE_MUST_NOT_EXIST:
46 if ((stat(fname, &sb) == 0) || (errno != ENOENT)) {
47 if (errno == 0)
48 errno = EINVAL;
49 fprintf(stderr, zFSOptError, errno, strerror(errno),
50 zFSOptErrNoExist, fname, pOD->pz_Name);
51 pOpts->pUsageProc(pOpts, EXIT_FAILURE);
52 /* NOTREACHED */
54 /* FALLTHROUGH */
56 default:
57 case FTYPE_MODE_MAY_EXIST:
59 char * p = strrchr(fname, DIRCH);
60 size_t l;
62 if (p == NULL)
64 * The file may or may not exist and its directory is ".".
65 * Assume that "." exists.
67 break;
69 l = p - fname;
70 p = AGALOC(l + 1, "fname");
71 memcpy(p, fname, l);
72 p[l] = NUL;
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);
78 /* NOTREACHED */
80 AGFREE(p);
81 break;
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,
89 pOD->pz_Name);
90 pOpts->pUsageProc(pOpts, EXIT_FAILURE);
91 /* NOTREACHED */
93 break;
97 /**
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)
104 static void
105 open_file_fd(tOptions * pOpts, tOptDesc * pOD, tuFileMode mode)
107 int fd = open(pOD->optArg.argString, mode.file_flags);
108 if (fd < 0) {
109 fprintf(stderr, zFSOptError, errno, strerror(errno),
110 zFSOptErrOpen, pOD->optArg.argString, pOD->pz_Name);
111 pOpts->pUsageProc(pOpts, EXIT_FAILURE);
112 /* NOTREACHED */
115 if ((pOD->fOptState & OPTST_ALLOC_ARG) != 0)
116 pOD->optCookie = (void *)pOD->optArg.argString;
117 else
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)
131 static void
132 fopen_file_fp(tOptions * pOpts, tOptDesc * pOD, tuFileMode mode)
134 FILE* fp = fopen(pOD->optArg.argString, mode.file_mode);
135 if (fp == NULL) {
136 fprintf(stderr, zFSOptError, errno, strerror(errno),
137 zFSOptErrFopen, pOD->optArg.argString, pOD->pz_Name);
138 pOpts->pUsageProc(pOpts, EXIT_FAILURE);
139 /* NOTREACHED */
142 if ((pOD->fOptState & OPTST_ALLOC_ARG) != 0)
143 pOD->optCookie = (void *)pOD->optArg.argString;
144 else
145 AGDUPSTR(pOD->optCookie, pOD->optArg.argString, "file name");
147 pOD->optArg.argFp = fp;
148 pOD->fOptState &= ~OPTST_ALLOC_ARG;
151 /*=export_func optionFileCheck
152 * private:
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) +
160 * doc:
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
165 * (FILE* pointer).
167 void
168 optionFileCheck(tOptions * pOpts, tOptDesc * pOD,
169 teOptFileType ftype, tuFileMode mode)
171 if (pOpts <= OPTPROC_EMIT_LIMIT) {
172 if (pOpts != OPTPROC_EMIT_USAGE)
173 return;
175 switch (ftype & FTYPE_MODE_EXIST_MASK) {
176 case FTYPE_MODE_MUST_NOT_EXIST:
177 fputs(zFileCannotExist, option_usage_fp);
178 break;
180 case FTYPE_MODE_MUST_EXIST:
181 fputs(zFileMustExist, option_usage_fp);
182 break;
184 return;
187 if ((pOD->fOptState & OPTST_RESET) != 0) {
188 if (pOD->optCookie != NULL)
189 AGFREE(pOD->optCookie);
190 return;
193 check_existence(ftype, pOpts, pOD);
195 switch (ftype & FTYPE_MODE_OPEN_MASK) {
196 default:
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;
203 * Local Variables:
204 * mode: C
205 * c-file-style: "stroustrup"
206 * indent-tabs-mode: nil
207 * End:
208 * end of autoopts/file.c */