48 #include <sys/param.h>
93 #define MAXPATHLEN 4096
111 (void)sprintf(name,
"%s/%s%hx%u", dir, pfx, pid,
curtmp);
114 }
while (access(name,
F_OK) != -1);
144 char *tempname = NULL;
148 #define MAXTMPRETRY 10
163 LOG(
llevError,
"Created file detected in tempnam_secure. Someone hoping for a race condition?\n");
170 file = fdopen(fd,
"w+");
172 LOG(
llevError,
"fdopen() failed in tempnam_secure()!\n");
177 *filename = tempname;
198 if ((dirp =
opendir(path)) != NULL) {
212 status = stat(de->
d_name, &statbuf);
213 if ((status != -1) && (
S_ISDIR(statbuf.st_mode))) {
226 LOG(
llevError,
"Unable to remove directory %s\n", path);
236 #define popen fixed_popen
253 FILE *popen_local(
const char *command,
const char *type) {
257 if (!strcmp(type,
"r")) {
259 }
else if (!strcmp(type,
"w")) {
264 if (pipe(fd) != -1) {
273 if ((fd[1] == pd) || (dup2(fd[1], pd) == pd)) {
277 execl(
"/bin/sh",
"sh",
"-c", command, NULL);
285 if (ret = fdopen(fd[0], type)) {
311 char *c = (
char *)malloc(strlen(str)+1);
318 #define DIGIT(x) (isdigit(x) ? (x)-'0' : \
319 islower(x) ? (x)+10-'a' : (x)+10-'A')
320 #define MBASE ('z'-'a'+1+10)
322 #if !defined(HAVE_STRTOL)
339 long strtol(
register char *str,
char **ptr,
register int base) {
344 if (ptr != (
char **)0)
346 if (base < 0 || base >
MBASE)
348 if (!isalnum(c = *str)) {
362 if (str[1] ==
'x' || str[1] ==
'X')
372 if (!isalnum(c) || (xx =
DIGIT(c)) >= base)
374 if (base == 16 && c ==
'0' && isxdigit(str[2]) && (str[1] ==
'x' || str[1] ==
'X'))
376 for (val = -
DIGIT(c); isalnum(c = *++str) && (xx =
DIGIT(c)) < base; )
380 if (ptr != (
char **)0)
382 return (neg ? val : -val);
401 #if !defined(HAVE_STRNCASECMP)
405 while (*s1 && *s2 && n) {
416 return (
int)(*s1-*s2);
420 #if !defined(HAVE_STRCASECMP)
445 if (*s1 ==
'\0' && *s2 ==
'\0')
447 return (
int)(*s1-*s2);
465 if ((c = *find++) != 0) {
470 if ((sc = *s++) == 0)
479 #if !defined(HAVE_SNPRINTF)
498 int snprintf(
char *dest,
int max,
const char *format, ...) {
502 va_start(var, format);
503 ret = vsprintf(dest, format, var);
526 #if defined(HAVE_STRERROR_R)
528 # if defined(STRERROR_R_CHAR_P)
532 bbuf = (
char *)strerror_r(errnum, buf, size);
533 if ((buf[0] == 0) && (bbuf != NULL))
534 strncpy(buf, bbuf, size);
536 if (strerror_r(errnum, buf, size) != 0) {
538 if (errno == ERANGE) {
539 strncat(buf,
"Too small buffer.", size);
540 }
else if (errno == EINVAL) {
541 strncat(buf,
"Error number invalid.", size);
548 # if defined(HAVE_STRERROR)
549 snprintf(buf, size,
"%s", strerror(errnum));
551 # error If this is C89 the compiler should have strerror!;
574 int result, sum, prev;
594 { NULL, NULL, NULL },
631 if (strlen(name)+strlen(ext) >=
sizeof(buf)) {
632 errno = ENAMETOOLONG;
635 snprintf(buf,
sizeof(buf),
"%s%s", name, ext);
637 if (stat(buf, &st) != 0) {
646 if (uncompressor == NULL) {
648 return fopen(buf,
"rb");
655 if (strpbrk(buf,
"'\\\r\n") != NULL) {
663 if (strlen(uncompressor)+4+strlen(buf)+1 >=
sizeof(buf2)) {
664 errno = ENAMETOOLONG;
667 snprintf(buf2,
sizeof(buf2),
"%s < '%s'", uncompressor, buf);
668 return popen(buf2,
"r");
673 if (stat(name, &st) == 0 && !
S_ISREG(st.st_mode)) {
678 if (strlen(uncompressor)+4+strlen(buf)+5+strlen(name)+1 >=
sizeof(buf2)) {
679 errno = ENAMETOOLONG;
682 snprintf(buf2,
sizeof(buf2),
"%s < '%s' > '%s'", uncompressor, buf, name);
693 chmod(name, st.st_mode);
695 return fopen(name,
"rb");
768 if (!filename || !*filename)
771 strcpy(buf, filename);
773 while ((cp = strchr(cp+1, (
int)
'/'))) {
775 if (stat(buf, &statbuf) || !
S_ISDIR(statbuf.st_mode)) {
char * tempnam_local(const char *dir, const char *pfx)
long strtol(register char *str, char **ptr, register int base)
void make_path_to_file(const char *filename)
void remove_directory(const char *path)
DIR * opendir(const char *)
void close_and_delete(FILE *fp, int compressed)
FILE * tempnam_secure(const char *dir, const char *pfx, char **filename)
#define NROF_COMPRESS_METHODS
char d_name[_MAX_FNAME+1]
char * strdup_local(const char *str)
static FILE * open_and_uncompress_file(const char *ext, const char *uncompressor, const char *name, int flag, int *compressed)
int strncasecmp(const char *s1, const char *s2, int n)
static unsigned int curtmp
int snprintf(char *dest, int max, const char *format,...)
const char * uncomp[NROF_COMPRESS_METHODS][3]
struct dirent * readdir(DIR *)
int strcasecmp(const char *s1, const char *s2)
void LOG(LogLevel logLevel, const char *format,...)
const char * strcasestr_local(const char *s, const char *find)
char * strerror_local(int errnum, char *buf, size_t size)
FILE * open_and_uncompress(const char *name, int flag, int *compressed)