1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <pwd.h>
#include <sys/types.h>
#include <glib.h>
#include "file.h"
char *
homedir (int uid)
{
char *s;
static int usr_uid = -1;
static char *usr_s = NULL;
struct passwd *pwd;
if (usr_uid < 0)
usr_uid = getuid ();
if ((uid == usr_uid) && (usr_s))
{
return (g_strdup (usr_s));
}
pwd = getpwuid (uid);
if (pwd)
{
s = g_strdup (pwd->pw_dir);
if (uid == usr_uid)
usr_s = g_strdup (s);
return (s);
}
return (g_strdup
((getenv ("TMPDIR") == NULL) ? "/tmp" : getenv ("TMPDIR")));
}
char *
field (char *s, int field)
{
char buf[4096];
buf[0] = 0;
fword (s, field + 1, buf);
if (buf[0])
{
if ((!strcmp (buf, "NULL")) || (!strcmp (buf, "(null)")))
return (NULL);
return (g_strdup (buf));
}
return NULL;
}
void
fword (char *s, int num, char *wd)
{
char *cur, *start, *end;
int count, inword, inquote, len;
if (!s)
return;
if (!wd)
return;
*wd = 0;
if (num <= 0)
return;
cur = s;
count = 0;
inword = 0;
inquote = 0;
start = NULL;
end = NULL;
while ((*cur) && (count < num))
{
if (inword)
{
if (inquote)
{
if (*cur == '"')
{
inquote = 0;
inword = 0;
end = cur;
count++;
}
}
else
{
if (isspace (*cur))
{
end = cur;
inword = 0;
count++;
}
}
}
else
{
if (!isspace (*cur))
{
if (*cur == '"')
{
inquote = 1;
start = cur + 1;
}
else
start = cur;
inword = 1;
}
}
if (count == num)
break;
cur++;
}
if (!start)
return;
if (!end)
end = cur;
if (end <= start)
return;
len = (int) (end - start);
if (len > 4000)
len = 4000;
if (len > 0)
{
strncpy (wd, start, len);
wd[len] = 0;
}
return;
}
|