11
11
* This file is part of AutoOpts, a companion to AutoGen.
12
12
* AutoOpts is free software.
13
* AutoOpts is Copyright (c) 1992-2010 by Bruce Korb - all rights reserved
13
* AutoOpts is Copyright (c) 1992-2011 by Bruce Korb - all rights reserved
15
15
* AutoOpts is available under any one of two licenses. The license
16
16
* in use must be one of these two and the choice is under the control
37
37
findDirName(tOptions* pOpts, int* p_free);
40
findFileName(tOptions* pOpts, int* p_free_name);
40
findFileName(tOptions * pOpts, int * p_free_name);
117
117
char z[ AO_NAME_SIZE ];
118
118
if ((pzEndDir - pzDir) > AO_NAME_LIMIT )
120
strncpy(z, pzDir, (size_t)(pzEndDir - pzDir));
121
z[ (pzEndDir - pzDir) ] = NUL;
120
memcpy(z, pzDir, (size_t)(pzEndDir - pzDir));
121
z[pzEndDir - pzDir] = NUL;
122
122
pzEnv = getenv(z);
161
findFileName(tOptions* pOpts, int* p_free_name)
161
findFileName(tOptions * pOpts, int * p_free_name)
164
163
struct stat stBuf;
165
164
int free_dir_name = 0;
167
pzDir = findDirName(pOpts, &free_dir_name);
166
char const * pzDir = findDirName(pOpts, &free_dir_name);
168
167
if (pzDir == NULL)
173
172
* structure so we can bail out early.
175
174
if (stat(pzDir, &stBuf) != 0) do {
178
179
* IF we could not, check to see if we got a full
179
180
* path to a file name that has not been created yet.
181
if (errno == ENOENT) {
185
* Strip off the last component, stat the remaining string and
186
* that string must name a directory
188
char* pzDirCh = strrchr(pzDir, DIRCH);
189
if (pzDirCh == NULL) {
190
stBuf.st_mode = S_IFREG;
191
continue; /* bail out of error condition */
194
strncpy(z, pzDir, (size_t)(pzDirCh - pzDir));
195
z[ pzDirCh - pzDir ] = NUL;
197
if ( (stat(z, &stBuf) == 0)
198
&& S_ISDIR(stBuf.st_mode)) {
201
* We found the directory. Restore the file name and
202
* mark the full name as a regular file
204
stBuf.st_mode = S_IFREG;
205
continue; /* bail out of error condition */
182
if (errno != ENOENT) {
184
fprintf(stderr, zWarn, pOpts->pzProgName);
185
fprintf(stderr, zNoStat, errno, strerror(errno), pzDir);
187
AGFREE((void*)pzDir);
210
* We got a bogus name.
192
* Strip off the last component, stat the remaining string and
193
* that string must name a directory
212
fprintf(stderr, zWarn, pOpts->pzProgName);
213
fprintf(stderr, zNoStat, errno, strerror(errno), pzDir);
215
AGFREE((void*)pzDir);
195
dirchp = strrchr(pzDir, DIRCH);
196
if (dirchp == NULL) {
197
stBuf.st_mode = S_IFREG;
198
break; /* found directory -- viz., "." */
201
if ((dirchp - pzDir) >= sizeof(z))
204
memcpy(z, pzDir, (size_t)(dirchp - pzDir));
205
z[dirchp - pzDir] = NUL;
207
if ((stat(z, &stBuf) != 0) || ! S_ISDIR(stBuf.st_mode))
209
stBuf.st_mode = S_IFREG; /* file within this directory */