2000-04-24 21:36:10 +00:00
|
|
|
/*
|
2001-07-17 20:29:36 +00:00
|
|
|
* Copyright (C) 2000, 2001 Internet Software Consortium.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2000-04-24 21:36:10 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2000-07-27 09:55:03 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
|
|
|
|
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
|
|
|
|
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
|
|
|
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2000-04-24 21:36:10 +00:00
|
|
|
*/
|
|
|
|
|
2001-07-17 20:29:36 +00:00
|
|
|
/* $Id: file.c,v 1.20 2001/07/17 20:29:26 gson Exp $ */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#undef rename
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <process.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/utime.h>
|
|
|
|
|
|
|
|
#include <isc/file.h>
|
|
|
|
#include <isc/result.h>
|
|
|
|
#include <isc/time.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
#include <isc/stat.h>
|
|
|
|
|
|
|
|
#include "errno2result.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emulate UNIX mkstemp, which returns an open FD to the new file
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
gettemp(char *path, int *doopen) {
|
|
|
|
char *start, *trv;
|
|
|
|
struct stat sbuf;
|
|
|
|
int pid;
|
|
|
|
|
|
|
|
trv = strrchr(path, 'X');
|
|
|
|
trv++;
|
|
|
|
pid = getpid();
|
|
|
|
/* extra X's get set to 0's */
|
|
|
|
while (*--trv == 'X') {
|
|
|
|
*trv = (pid % 10) + '0';
|
|
|
|
pid /= 10;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* check the target directory; if you have six X's and it
|
|
|
|
* doesn't exist this runs for a *very* long time.
|
|
|
|
*/
|
|
|
|
for (start = trv + 1;; --trv) {
|
|
|
|
if (trv <= path)
|
|
|
|
break;
|
|
|
|
if (*trv == '\\') {
|
|
|
|
*trv = '\0';
|
|
|
|
if (stat(path, &sbuf))
|
2001-07-08 05:09:35 +00:00
|
|
|
return (0);
|
2001-07-06 05:08:39 +00:00
|
|
|
if (!S_ISDIR(sbuf.st_mode)) {
|
|
|
|
errno = ENOTDIR;
|
2001-07-08 05:09:35 +00:00
|
|
|
return (0);
|
2001-07-06 05:08:39 +00:00
|
|
|
}
|
|
|
|
*trv = '\\';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (doopen) {
|
|
|
|
if ((*doopen =
|
2001-07-09 21:06:30 +00:00
|
|
|
open(path, O_CREAT|O_EXCL|O_RDWR,
|
|
|
|
_S_IREAD | _S_IWRITE)) >= 0)
|
2001-07-08 05:09:35 +00:00
|
|
|
return (1);
|
2001-07-06 05:08:39 +00:00
|
|
|
if (errno != EEXIST)
|
2001-07-08 05:09:35 +00:00
|
|
|
return (0);
|
2001-07-09 21:06:30 +00:00
|
|
|
} else if (stat(path, &sbuf))
|
2001-07-08 05:09:35 +00:00
|
|
|
return (errno == ENOENT ? 1 : 0);
|
2001-07-06 05:08:39 +00:00
|
|
|
|
|
|
|
/* tricky little algorithm for backward compatibility */
|
|
|
|
for (trv = start;;) {
|
|
|
|
if (!*trv)
|
2001-07-08 05:09:35 +00:00
|
|
|
return (0);
|
2001-07-06 05:08:39 +00:00
|
|
|
if (*trv == 'z')
|
|
|
|
*trv++ = 'a';
|
|
|
|
else {
|
|
|
|
if (isdigit(*trv))
|
|
|
|
*trv = 'a';
|
|
|
|
else
|
|
|
|
++*trv;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
mkstemp(char *path) {
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
return (gettemp(path, &fd) ? fd : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
|
|
|
|
* it might be good to provide a mechanism that allows for the results
|
|
|
|
* of a previous stat() to be used again without having to do another stat,
|
|
|
|
* such as perl's mechanism of using "_" in place of a file name to indicate
|
|
|
|
* that the results of the last stat should be used. But then you get into
|
|
|
|
* annoying MP issues. BTW, Win32 has stat().
|
|
|
|
*/
|
|
|
|
static isc_result_t
|
|
|
|
file_stats(const char *file, struct stat *stats) {
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
2001-07-10 06:24:15 +00:00
|
|
|
REQUIRE(file != NULL);
|
|
|
|
REQUIRE(stats != NULL);
|
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
if (stat(file, stats) != 0)
|
|
|
|
result = isc__errno2result(errno);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2001-07-09 21:06:30 +00:00
|
|
|
/*
|
|
|
|
* isc_file_safemovefile is needed to be defined here to ensure that
|
|
|
|
* any file with the new name is renamed to a backup name and then the
|
|
|
|
* rename is done. If all goes well then the backup can be deleted,
|
|
|
|
* otherwise it gets renamed back.
|
2001-07-06 05:08:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
isc_file_safemovefile(const char *oldname, const char *newname) {
|
|
|
|
BOOL filestatus;
|
|
|
|
char buf[512];
|
|
|
|
struct stat sbuf;
|
|
|
|
BOOL exists = FALSE;
|
|
|
|
int tmpfd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure we have something to do
|
|
|
|
*/
|
2001-07-06 21:57:22 +00:00
|
|
|
if (stat(oldname, &sbuf) != 0) {
|
2001-07-06 05:08:39 +00:00
|
|
|
errno = ENOENT;
|
2001-07-08 05:09:35 +00:00
|
|
|
return (-1);
|
2001-07-06 05:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rename to a backup the new file if it still exists
|
|
|
|
*/
|
2001-07-08 05:09:35 +00:00
|
|
|
if (stat(newname, &sbuf) == 0) {
|
2001-07-06 05:08:39 +00:00
|
|
|
exists = TRUE;
|
2001-07-08 05:09:35 +00:00
|
|
|
strcpy(buf, newname);
|
|
|
|
strcat(buf, ".XXXXX");
|
|
|
|
tmpfd = mkstemp(buf);
|
|
|
|
if (tmpfd > 0)
|
|
|
|
_close(tmpfd);
|
|
|
|
DeleteFile(buf);
|
2001-07-06 05:08:39 +00:00
|
|
|
_chmod(newname, _S_IREAD | _S_IWRITE);
|
|
|
|
|
2001-07-08 05:09:35 +00:00
|
|
|
filestatus = MoveFile(newname, buf);
|
2001-07-06 05:08:39 +00:00
|
|
|
}
|
2001-07-08 05:09:35 +00:00
|
|
|
/* Now rename the file to the new name
|
2001-07-06 05:08:39 +00:00
|
|
|
*/
|
|
|
|
_chmod(oldname, _S_IREAD | _S_IWRITE);
|
|
|
|
|
|
|
|
filestatus = MoveFile(oldname, newname);
|
2001-07-08 05:09:35 +00:00
|
|
|
if (filestatus == 0) {
|
2001-07-09 21:06:30 +00:00
|
|
|
/*
|
|
|
|
* Try to rename the backup back to the original name
|
|
|
|
* if the backup got created
|
2001-07-06 05:08:39 +00:00
|
|
|
*/
|
2001-07-06 21:57:22 +00:00
|
|
|
if (exists == TRUE) {
|
2001-07-06 05:08:39 +00:00
|
|
|
filestatus = MoveFile(buf, newname);
|
2001-07-06 21:57:22 +00:00
|
|
|
if (filestatus == 0) {
|
2001-07-06 05:08:39 +00:00
|
|
|
errno = EACCES;
|
|
|
|
}
|
|
|
|
}
|
2001-07-06 21:57:22 +00:00
|
|
|
return (-1);
|
2001-07-06 05:08:39 +00:00
|
|
|
}
|
|
|
|
|
2001-07-09 21:06:30 +00:00
|
|
|
/*
|
|
|
|
* Delete the backup file if it got created
|
2001-07-06 05:08:39 +00:00
|
|
|
*/
|
2001-07-09 21:06:30 +00:00
|
|
|
if (exists == TRUE)
|
2001-07-06 05:08:39 +00:00
|
|
|
filestatus = DeleteFile(buf);
|
2001-07-06 21:57:22 +00:00
|
|
|
return (0);
|
2001-07-06 05:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_file_getmodtime(const char *file, isc_time_t *time) {
|
|
|
|
isc_result_t result;
|
|
|
|
struct stat stats;
|
|
|
|
|
2001-07-10 06:24:15 +00:00
|
|
|
REQUIRE(file != NULL);
|
|
|
|
REQUIRE(time != NULL);
|
2001-07-06 05:08:39 +00:00
|
|
|
|
|
|
|
result = file_stats(file, &stats);
|
|
|
|
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
/*
|
|
|
|
* XXXDCL some operating systems provide nanoseconds, too,
|
|
|
|
* such as BSD/OS via st_mtimespec.
|
|
|
|
*/
|
|
|
|
isc_time_set(time, stats.st_mtime, 0);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_file_settime(const char *file, isc_time_t *time) {
|
|
|
|
struct utimbuf timem;
|
|
|
|
|
|
|
|
REQUIRE(file != NULL && time != NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* tv_sec is at least a 32 bit quantity on all platforms we're
|
|
|
|
* dealing with, but it is signed on most (all?) of them,
|
|
|
|
* so we need to make sure the high bit isn't set. This unfortunately
|
|
|
|
* loses when either:
|
|
|
|
* * tv_sec becomes a signed 64 bit integer but long is 32 bits
|
|
|
|
* and isc_time_seconds > LONG_MAX, or
|
|
|
|
* * isc_time_seconds is changed to be > 32 bits but long is 32 bits
|
|
|
|
* and isc_time_seconds has at least 33 significant bits.
|
|
|
|
*/
|
|
|
|
timem.actime = timem.modtime = (long)isc_time_seconds(time);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here is the real check for the high bit being set.
|
|
|
|
*/
|
|
|
|
if ((timem.actime &
|
|
|
|
(1UL << (sizeof(timem.actime) * CHAR_BIT - 1))) != 0)
|
|
|
|
return (ISC_R_RANGE);
|
|
|
|
|
|
|
|
if (utime(file, &timem) < 0)
|
|
|
|
return (isc__errno2result(errno));
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
}
|
2000-04-24 21:36:10 +00:00
|
|
|
|
|
|
|
#undef TEMPLATE
|
2001-07-06 05:08:39 +00:00
|
|
|
#define TEMPLATE "XXXXXXXXXX.tmp" /* 14 characters. */
|
2000-04-24 21:36:10 +00:00
|
|
|
|
|
|
|
isc_result_t
|
2001-07-06 05:08:39 +00:00
|
|
|
isc_file_mktemplate(const char *path, char *buf, size_t buflen) {
|
|
|
|
return (isc_file_template(path, TEMPLATE, buf, buflen));
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_file_template(const char *path, const char *templet, char *buf,
|
|
|
|
size_t buflen) {
|
2000-04-24 21:36:10 +00:00
|
|
|
char *s;
|
|
|
|
|
2001-07-10 06:24:15 +00:00
|
|
|
REQUIRE(path != NULL);
|
|
|
|
REQUIRE(templet != NULL);
|
2001-07-06 05:08:39 +00:00
|
|
|
REQUIRE(buf != NULL);
|
2000-04-24 21:36:10 +00:00
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
s = strrchr(templet, '\\');
|
|
|
|
if (s != NULL)
|
|
|
|
templet = s + 1;
|
2000-04-24 21:36:10 +00:00
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
s = strrchr(path, '\\');
|
2000-04-24 21:36:10 +00:00
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
if (s != NULL) {
|
|
|
|
if ((s - path + 1 + strlen(templet) + 1) > buflen)
|
|
|
|
return (ISC_R_NOSPACE);
|
2000-04-24 21:36:10 +00:00
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
strncpy(buf, path, s - path + 1);
|
|
|
|
buf[s - path + 1] = '\0';
|
|
|
|
strcat(buf, templet);
|
2000-04-24 21:36:10 +00:00
|
|
|
} else {
|
2001-07-06 05:08:39 +00:00
|
|
|
if ((strlen(templet) + 1) > buflen)
|
2000-04-24 21:36:10 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
strcpy(buf, templet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_file_renameunique(const char *file, char *templet) {
|
|
|
|
int fd = -1;
|
|
|
|
int res = 0;
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
2001-07-10 06:24:15 +00:00
|
|
|
REQUIRE(file != NULL);
|
|
|
|
REQUIRE(templet != NULL);
|
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
fd = mkstemp(templet);
|
2001-07-09 21:06:30 +00:00
|
|
|
if (fd == -1)
|
2001-07-06 05:08:39 +00:00
|
|
|
result = isc__errno2result(errno);
|
2001-07-09 21:06:30 +00:00
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
res = isc_file_safemovefile(file, templet);
|
|
|
|
if (res != 0) {
|
|
|
|
result = isc__errno2result(errno);
|
|
|
|
(void)unlink(templet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_file_openunique(char *templet, FILE **fp) {
|
|
|
|
int fd;
|
|
|
|
FILE *f;
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
REQUIRE(templet != NULL);
|
|
|
|
REQUIRE(fp != NULL && *fp == NULL);
|
|
|
|
|
|
|
|
/*
|
2001-07-08 05:09:35 +00:00
|
|
|
* Win32 does not have mkstemp. Using emulation above.
|
2001-07-06 05:08:39 +00:00
|
|
|
*/
|
|
|
|
fd = mkstemp(templet);
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
result = isc__errno2result(errno);
|
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
f = fdopen(fd, "w+");
|
|
|
|
if (f == NULL) {
|
|
|
|
result = isc__errno2result(errno);
|
|
|
|
(void)remove(templet);
|
|
|
|
(void)close(fd);
|
|
|
|
} else
|
|
|
|
*fp = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_file_remove(const char *filename) {
|
|
|
|
int r;
|
|
|
|
|
2001-07-10 06:24:15 +00:00
|
|
|
REQUIRE(filename != NULL);
|
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
r = unlink(filename);
|
|
|
|
if (r == 0)
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
else
|
|
|
|
return (isc__errno2result(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_file_rename(const char *oldname, const char *newname) {
|
|
|
|
int r;
|
|
|
|
|
2001-07-10 06:24:15 +00:00
|
|
|
REQUIRE(oldname != NULL);
|
|
|
|
REQUIRE(newname != NULL);
|
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
r = isc_file_safemovefile(oldname, newname);
|
|
|
|
if (r == 0)
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
else
|
|
|
|
return (isc__errno2result(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_boolean_t
|
|
|
|
isc_file_exists(const char *pathname) {
|
|
|
|
struct stat stats;
|
|
|
|
|
2001-07-10 06:24:15 +00:00
|
|
|
REQUIRE(pathname != NULL);
|
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
return (ISC_TF(file_stats(pathname, &stats) == ISC_R_SUCCESS));
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_boolean_t
|
|
|
|
isc_file_isabsolute(const char *filename) {
|
2001-07-10 06:24:15 +00:00
|
|
|
REQUIRE(filename != NULL);
|
2001-07-06 05:08:39 +00:00
|
|
|
/*
|
2001-07-12 05:58:28 +00:00
|
|
|
* Look for c:\path\... style, c:/path/... or \\computer\shar\path...
|
2001-07-11 04:32:15 +00:00
|
|
|
* the UNC style file specs
|
2001-07-06 05:08:39 +00:00
|
|
|
*/
|
2001-07-11 04:32:15 +00:00
|
|
|
if ((filename[0] == '\\') && (filename[1] == '\\'))
|
2001-07-10 06:24:15 +00:00
|
|
|
return (ISC_TRUE);
|
2001-07-16 04:09:45 +00:00
|
|
|
if (isalpha(filename[0]) && filename[1] == ':' && filename[2] == '\\')
|
2001-07-10 17:31:05 +00:00
|
|
|
return (ISC_TRUE);
|
2001-07-16 04:09:45 +00:00
|
|
|
if (isalpha(filename[0]) && filename[1] == ':' && filename[2] == '/')
|
2001-07-12 05:58:28 +00:00
|
|
|
return (ISC_TRUE);
|
2001-07-10 17:31:05 +00:00
|
|
|
return (ISC_FALSE);
|
2001-07-06 05:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_boolean_t
|
|
|
|
isc_file_iscurrentdir(const char *filename) {
|
2001-07-10 06:24:15 +00:00
|
|
|
REQUIRE(filename != NULL);
|
2001-07-06 05:08:39 +00:00
|
|
|
return (ISC_TF(filename[0] == '.' && filename[1] == '\0'));
|
|
|
|
}
|
|
|
|
|
2001-07-16 18:33:02 +00:00
|
|
|
isc_boolean_t
|
|
|
|
isc_file_ischdiridempotent(const char *filename) {
|
|
|
|
REQUIRE(filename != NULL);
|
|
|
|
|
|
|
|
if (isc_file_isabsolute(filename))
|
|
|
|
return (ISC_TRUE);
|
|
|
|
if (filename[0] == '\\')
|
|
|
|
return (ISC_TRUE);
|
|
|
|
if (filename[0] == '/')
|
|
|
|
return (ISC_TRUE);
|
|
|
|
if (isc_file_iscurrentdir(filename))
|
|
|
|
return (ISC_TRUE);
|
|
|
|
return (ISC_FALSE);
|
|
|
|
}
|
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
const char *
|
|
|
|
isc_file_basename(const char *filename) {
|
|
|
|
char *s;
|
|
|
|
|
2001-07-10 06:24:15 +00:00
|
|
|
REQUIRE(filename != NULL);
|
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
s = strrchr(filename, '\\');
|
2001-07-06 21:57:22 +00:00
|
|
|
if (s == NULL)
|
|
|
|
return (filename);
|
|
|
|
return (s + 1);
|
2001-07-06 05:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_file_progname(const char *filename, char *progname, size_t namelen) {
|
|
|
|
const char *s;
|
|
|
|
char *p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
REQUIRE(filename != NULL);
|
2001-07-10 06:24:15 +00:00
|
|
|
REQUIRE(progname != NULL);
|
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
/*
|
|
|
|
* Strip the path from the name
|
|
|
|
*/
|
|
|
|
s = isc_file_basename(filename);
|
2001-07-08 05:09:35 +00:00
|
|
|
if (s == NULL) {
|
|
|
|
return (ISC_R_NOSPACE);
|
|
|
|
}
|
2001-07-06 05:08:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Strip any and all suffixes
|
|
|
|
*/
|
|
|
|
p = strchr(s, '.');
|
2001-07-06 21:57:22 +00:00
|
|
|
if (p == NULL) {
|
|
|
|
if (namelen <= strlen(s))
|
|
|
|
return (ISC_R_NOSPACE);
|
2001-07-06 05:08:39 +00:00
|
|
|
|
|
|
|
strcpy(progname, s);
|
2001-07-06 21:57:22 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2000-04-24 21:36:10 +00:00
|
|
|
}
|
|
|
|
|
2001-07-06 05:08:39 +00:00
|
|
|
/*
|
|
|
|
* Copy the result to the buffer
|
|
|
|
*/
|
|
|
|
len = p - s;
|
2001-07-06 21:57:22 +00:00
|
|
|
if (len >= namelen)
|
|
|
|
return (ISC_R_NOSPACE);
|
2001-07-06 05:08:39 +00:00
|
|
|
|
|
|
|
strncpy(progname, s, len);
|
|
|
|
progname[len] = '\0';
|
2001-07-06 21:57:22 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2000-04-24 21:36:10 +00:00
|
|
|
}
|
2001-07-16 03:52:14 +00:00
|
|
|
|
|
|
|
isc_result_t
|
2001-07-16 17:22:17 +00:00
|
|
|
isc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
|
2001-07-16 03:52:14 +00:00
|
|
|
char *ptrname;
|
|
|
|
DWORD retval;
|
|
|
|
|
|
|
|
REQUIRE(filename != NULL);
|
|
|
|
REQUIRE(path != NULL);
|
|
|
|
|
|
|
|
retval = GetFullPathName(filename, pathlen, path, &ptrname);
|
|
|
|
|
|
|
|
/* Something went wrong in getting the path */
|
|
|
|
if (retval == 0)
|
|
|
|
return (ISC_R_NOTFOUND);
|
|
|
|
/* Caller needs to provide a larger buffer to contain the string */
|
|
|
|
if (retval >= pathlen)
|
|
|
|
return (ISC_R_NOSPACE);
|
2001-07-16 04:09:45 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2001-07-16 03:52:14 +00:00
|
|
|
}
|