summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'util.c')
-rw-r--r--util.c42
1 files changed, 36 insertions, 6 deletions
diff --git a/util.c b/util.c
index cb9b043..382fbcb 100644
--- a/util.c
+++ b/util.c
@@ -39,14 +39,15 @@ const char * temp_dir()
if (!tmpdir)
{
- const char *dirs[] = { getenv("TMPDIR"), P_tmpdir, "/tmp", NULL };
- const char **dir;
+ const char *dirs[] = { getenv("TMPDIR"), P_tmpdir, "/tmp" };
+ int i;
- for (dir = dirs; *dir; dir++)
- if (access(*dir, W_OK) == 0) {
- tmpdir = *dir;
+ for (i = 0; i < (sizeof(dirs) / sizeof(dirs[0])); i++) {
+ if (access(dirs[i], W_OK) == 0) {
+ tmpdir = dirs[i];
break;
}
+ }
if (tmpdir)
{
_log("Storing temporary files in %s\n", tmpdir);
@@ -110,7 +111,7 @@ void strlower(char *dest, size_t destlen, const char *src)
int isempty(const char *string)
{
- return string && string[0] == '\0';
+ return !string || string[0] == '\0';
}
const char * strncpy_omit(char* dest, const char* src, size_t n, int (*omit_func)(int))
@@ -137,7 +138,32 @@ int omit_unprintables(int c) { return c>= '\x00' && c <= '\x1f'; }
int omit_shellescapes(int c) { return strchr(shellescapes, c) != NULL; }
int omit_specialchars(int c) { return omit_unprintables(c) || omit_shellescapes(c); }
int omit_whitespace(int c) { return c == ' ' || c == '\t'; }
+int omit_whitespace_newline(int c) { return omit_whitespace(c) || c == '\n'; }
+
+#ifndef HAVE_STRCASESTR
+char *
+strcasestr (const char *haystack, const char *needle)
+{
+ char *p, *startn = 0, *np = 0;
+
+ for (p = haystack; *p; p++) {
+ if (np) {
+ if (toupper(*p) == toupper(*np)) {
+ if (!*++np)
+ return startn;
+ } else
+ np = 0;
+ } else if (toupper(*p) == toupper(*needle)) {
+ np = needle + 1;
+ startn = p;
+ }
+ }
+ return 0;
+}
+#endif
+
+#ifndef __OpenBSD__
size_t strlcpy(char *dest, const char *src, size_t size)
{
char *pdest = dest;
@@ -177,6 +203,7 @@ size_t strlcat(char *dest, const char *src, size_t size)
return len + (psrc - src);
}
+#endif /* ! __OpenBSD__ */
void strrepl(char *str, const char *chars, char repl)
{
@@ -248,6 +275,9 @@ const char * strncpy_tochar(char *dest, const char *src, size_t max, const char
{
const char *psrc = src;
char *pdest = dest;
+ if (isempty(psrc)) {
+ return NULL;
+ }
while (*psrc && --max > 0 && !strchr(stopchars, *psrc)) {
*pdest = *psrc;
pdest++;