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