Annotation of embedaddon/ntp/sntp/libopts/file.c, revision 1.1.1.1
1.1 misho 1:
2: /**
3: * \file file.c
4: *
5: * Time-stamp: "2010-07-10 11:00:59 bkorb"
6: *
7: * This file is part of AutoOpts, a companion to AutoGen.
8: * AutoOpts is free software.
9: * AutoOpts is Copyright (c) 1992-2011 by Bruce Korb - all rights reserved
10: *
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.
14: *
15: * The GNU Lesser General Public License, version 3 or later
16: * See the files "COPYING.lgplv3" and "COPYING.gplv3"
17: *
18: * The Modified Berkeley Software Distribution License
19: * See the file "COPYING.mbsd"
20: *
21: * These files have the following md5sums:
22: *
23: * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
24: * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
25: * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
26: */
27:
28: /*=export_func optionFileCheck
29: * private:
30: *
31: * what: Decipher a boolean value
32: * arg: + tOptions* + pOpts + program options descriptor +
33: * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
34: * arg: + teOptFileType + ftype + File handling type +
35: * arg: + tuFileMode + mode + file open mode (if needed) +
36: *
37: * doc:
38: * Make sure the named file conforms with the file type mode.
39: * The mode specifies if the file must exist, must not exist or may
40: * (or may not) exist. The mode may also specify opening the
41: * file: don't, open just the descriptor (fd), or open as a stream
42: * (FILE* pointer).
43: =*/
44: void
45: optionFileCheck(tOptions* pOpts, tOptDesc* pOD,
46: teOptFileType ftype, tuFileMode mode)
47: {
48: if (pOpts <= OPTPROC_EMIT_LIMIT) {
49: if (pOpts != OPTPROC_EMIT_USAGE)
50: return;
51:
52: switch (ftype & FTYPE_MODE_EXIST_MASK) {
53: case FTYPE_MODE_MUST_NOT_EXIST:
54: fputs(zFileCannotExist, option_usage_fp);
55: break;
56:
57: case FTYPE_MODE_MUST_EXIST:
58: fputs(zFileMustExist, option_usage_fp);
59: break;
60: }
61: return;
62: }
63:
64: if ((pOD->fOptState & OPTST_RESET) != 0) {
65: if (pOD->optCookie != NULL)
66: AGFREE(pOD->optCookie);
67: return;
68: }
69:
70: {
71: struct stat sb;
72:
73: errno = 0;
74:
75: switch (ftype & FTYPE_MODE_EXIST_MASK) {
76: case FTYPE_MODE_MUST_NOT_EXIST:
77: if ( (stat(pOD->optArg.argString, &sb) == 0)
78: || (errno != ENOENT) ){
79: if (errno == 0)
80: errno = EINVAL;
81: fprintf(stderr, zFSOptError, errno, strerror(errno),
82: zFSOptErrNoExist, pOD->optArg.argString, pOD->pz_Name);
83: pOpts->pUsageProc(pOpts, EXIT_FAILURE);
84: /* NOTREACHED */
85: }
86: /* FALLTHROUGH */
87:
88: default:
89: case FTYPE_MODE_MAY_EXIST:
90: {
91: char * p = strrchr(pOD->optArg.argString, DIRCH);
92: if (p == NULL)
93: break; /* assume "." always exists. */
94:
95: *p = NUL;
96: if ( (stat(pOD->optArg.argString, &sb) != 0)
97: || (errno = EINVAL, ! S_ISDIR(sb.st_mode)) ){
98: fprintf(stderr, zFSOptError, errno, strerror(errno),
99: zFSOptErrMayExist, pOD->optArg.argString, pOD->pz_Name);
100: pOpts->pUsageProc(pOpts, EXIT_FAILURE);
101: /* NOTREACHED */
102: }
103: if (p != NULL)
104: *p = DIRCH;
105: break;
106: }
107:
108: case FTYPE_MODE_MUST_EXIST:
109: if ( (stat(pOD->optArg.argString, &sb) != 0)
110: || (errno = EINVAL, ! S_ISREG(sb.st_mode)) ){
111: fprintf(stderr, zFSOptError, errno, strerror(errno),
112: zFSOptErrMustExist, pOD->optArg.argString,
113: pOD->pz_Name);
114: pOpts->pUsageProc(pOpts, EXIT_FAILURE);
115: /* NOTREACHED */
116: }
117: break;
118: }
119: }
120:
121: switch (ftype & FTYPE_MODE_OPEN_MASK) {
122: default:
123: case FTYPE_MODE_NO_OPEN:
124: break;
125:
126: case FTYPE_MODE_OPEN_FD:
127: {
128: int fd = open(pOD->optArg.argString, mode.file_flags);
129: if (fd < 0) {
130: fprintf(stderr, zFSOptError, errno, strerror(errno),
131: zFSOptErrOpen, pOD->optArg.argString, pOD->pz_Name);
132: pOpts->pUsageProc(pOpts, EXIT_FAILURE);
133: /* NOTREACHED */
134: }
135:
136: if ((pOD->fOptState & OPTST_ALLOC_ARG) != 0)
137: pOD->optCookie = (void *)pOD->optArg.argString;
138: else
139: AGDUPSTR(pOD->optCookie, pOD->optArg.argString, "file name");
140:
141: pOD->optArg.argFd = fd;
142: pOD->fOptState &= ~OPTST_ALLOC_ARG;
143: break;
144: }
145:
146: case FTYPE_MODE_FOPEN_FP:
147: {
148: FILE* fp = fopen(pOD->optArg.argString, mode.file_mode);
149: if (fp == NULL) {
150: fprintf(stderr, zFSOptError, errno, strerror(errno),
151: zFSOptErrFopen, pOD->optArg.argString, pOD->pz_Name);
152: pOpts->pUsageProc(pOpts, EXIT_FAILURE);
153: /* NOTREACHED */
154: }
155:
156: if ((pOD->fOptState & OPTST_ALLOC_ARG) != 0)
157: pOD->optCookie = (void *)pOD->optArg.argString;
158: else
159: AGDUPSTR(pOD->optCookie, pOD->optArg.argString, "file name");
160:
161: pOD->optArg.argFp = fp;
162: pOD->fOptState &= ~OPTST_ALLOC_ARG;
163: break;
164: }
165: }
166: }
167: /*
168: * Local Variables:
169: * mode: C
170: * c-file-style: "stroustrup"
171: * indent-tabs-mode: nil
172: * End:
173: * end of autoopts/file.c */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>