support several headers, from <mattiasa@e.kth.se>
use estrdup, emalloc, erealloc git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@9416 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
@@ -72,7 +72,7 @@ struct getargs args[] = {
|
|||||||
"number-or-service" },
|
"number-or-service" },
|
||||||
{ "from", 0, arg_flag, &do_from, "Behave like from",
|
{ "from", 0, arg_flag, &do_from, "Behave like from",
|
||||||
NULL },
|
NULL },
|
||||||
{ "header", 0, arg_string, &header_str, "Header string to print", NULL },
|
{ "headers", 0, arg_string, &header_str, "Headers to print", NULL },
|
||||||
{ "count", 'c', arg_flag, &do_count, "Print number of messages", NULL},
|
{ "count", 'c', arg_flag, &do_count, "Print number of messages", NULL},
|
||||||
{ "version", 0, arg_flag, &do_version, "Print version",
|
{ "version", 0, arg_flag, &do_version, "Print version",
|
||||||
NULL },
|
NULL },
|
||||||
@@ -98,7 +98,7 @@ do_connect (const char *hostname, int port, int nodelay)
|
|||||||
struct addrinfo *ai, *a;
|
struct addrinfo *ai, *a;
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
int error;
|
int error;
|
||||||
int s;
|
int s = -1;
|
||||||
char portstr[NI_MAXSERV];
|
char portstr[NI_MAXSERV];
|
||||||
|
|
||||||
memset (&hints, 0, sizeof(hints));
|
memset (&hints, 0, sizeof(hints));
|
||||||
@@ -157,9 +157,7 @@ write_state_init (struct write_state *w, int fd)
|
|||||||
#endif
|
#endif
|
||||||
w->allociovecs = min(STEP, w->maxiovecs);
|
w->allociovecs = min(STEP, w->maxiovecs);
|
||||||
w->niovecs = 0;
|
w->niovecs = 0;
|
||||||
w->iovecs = malloc(w->allociovecs * sizeof(*w->iovecs));
|
w->iovecs = emalloc(w->allociovecs * sizeof(*w->iovecs));
|
||||||
if (w->iovecs == NULL)
|
|
||||||
err (1, "malloc");
|
|
||||||
w->fd = fd;
|
w->fd = fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,10 +171,8 @@ write_state_add (struct write_state *w, void *v, size_t len)
|
|||||||
w->niovecs = 0;
|
w->niovecs = 0;
|
||||||
} else {
|
} else {
|
||||||
w->allociovecs = min(w->allociovecs + STEP, w->maxiovecs);
|
w->allociovecs = min(w->allociovecs + STEP, w->maxiovecs);
|
||||||
w->iovecs = realloc (w->iovecs,
|
w->iovecs = erealloc (w->iovecs,
|
||||||
w->allociovecs * sizeof(*w->iovecs));
|
w->allociovecs * sizeof(*w->iovecs));
|
||||||
if (w->iovecs == NULL)
|
|
||||||
errx (1, "realloc");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w->iovecs[w->niovecs].iov_base = v;
|
w->iovecs[w->niovecs].iov_base = v;
|
||||||
@@ -225,11 +221,30 @@ doit(int s,
|
|||||||
size_t from_line_length;
|
size_t from_line_length;
|
||||||
time_t now;
|
time_t now;
|
||||||
struct write_state write_state;
|
struct write_state write_state;
|
||||||
|
int numheaders = 1;
|
||||||
|
char **headers = NULL;
|
||||||
|
int i;
|
||||||
|
char *tmp = estrdup(header_str);
|
||||||
|
|
||||||
if (do_from) {
|
if (do_from) {
|
||||||
|
char *tmp2 = tmp;
|
||||||
|
|
||||||
out_fd = -1;
|
out_fd = -1;
|
||||||
if (verbose)
|
if (verbose)
|
||||||
fprintf (stderr, "%s@%s\n", user, host);
|
fprintf (stderr, "%s@%s\n", user, host);
|
||||||
|
while (*tmp != '\0') {
|
||||||
|
tmp = strchr(tmp, ',');
|
||||||
|
if (tmp == NULL)
|
||||||
|
break;
|
||||||
|
tmp++;
|
||||||
|
numheaders++;
|
||||||
|
}
|
||||||
|
|
||||||
|
headers = emalloc(sizeof(char *) * (numheaders + 1));
|
||||||
|
for (i = 0; i < numheaders; i++) {
|
||||||
|
headers[i] = strtok_r(tmp2, ",", &tmp2);
|
||||||
|
}
|
||||||
|
headers[numheaders] = NULL;
|
||||||
} else {
|
} else {
|
||||||
out_fd = open(outfilename, O_WRONLY | O_APPEND | O_CREAT, 0666);
|
out_fd = open(outfilename, O_WRONLY | O_APPEND | O_CREAT, 0666);
|
||||||
if (out_fd < 0)
|
if (out_fd < 0)
|
||||||
@@ -296,12 +311,17 @@ doit(int s,
|
|||||||
if (state == TOP) {
|
if (state == TOP) {
|
||||||
char *copy = beg;
|
char *copy = beg;
|
||||||
|
|
||||||
if (strncasecmp(copy,
|
for (i = 0; i < numheaders; i++) {
|
||||||
header_str,
|
size_t len;
|
||||||
min(p - copy + 1, strlen(header_str))) == 0) {
|
|
||||||
fprintf (stdout, "%.*s\n", (int)(p - copy), copy);
|
len = min(p - copy + 1, strlen(headers[i]));
|
||||||
|
if (strncasecmp(copy, headers[i], len) == 0) {
|
||||||
|
fprintf (stdout, "%.*s\n", (int)(p - copy), copy);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (beg[0] == '.' && beg[1] == '\r' && beg[2] == '\n') {
|
if (beg[0] == '.' && beg[1] == '\r' && beg[2] == '\n') {
|
||||||
|
if (numheaders > 1)
|
||||||
|
fprintf (stdout, "\n");
|
||||||
state = STAT;
|
state = STAT;
|
||||||
if (++retrieved == count) {
|
if (++retrieved == count) {
|
||||||
state = QUIT;
|
state = QUIT;
|
||||||
@@ -450,8 +470,12 @@ doit(int s,
|
|||||||
}
|
}
|
||||||
if (verbose)
|
if (verbose)
|
||||||
fprintf (stderr, "Done\n");
|
fprintf (stderr, "Done\n");
|
||||||
if (!do_from)
|
if (do_from) {
|
||||||
|
free (tmp);
|
||||||
|
free (headers);
|
||||||
|
} else {
|
||||||
write_state_destroy (&write_state);
|
write_state_destroy (&write_state);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -572,12 +596,8 @@ hesiod_get_pobox (const char **user)
|
|||||||
if (strcasecmp(hpo->hesiod_po_type, "pop") != 0)
|
if (strcasecmp(hpo->hesiod_po_type, "pop") != 0)
|
||||||
errx (1, "Unsupported po type %s", hpo->hesiod_po_type);
|
errx (1, "Unsupported po type %s", hpo->hesiod_po_type);
|
||||||
|
|
||||||
ret = strdup(hpo->hesiod_po_host);
|
ret = estrdup(hpo->hesiod_po_host);
|
||||||
if(ret == NULL)
|
*user = estrdup(hpo->hesiod_po_name);
|
||||||
errx (1, "strdup: out of memory");
|
|
||||||
*user = strdup(hpo->hesiod_po_name);
|
|
||||||
if (*user == NULL)
|
|
||||||
errx (1, "strdup: out of memory");
|
|
||||||
hesiod_free_postoffice (context, hpo);
|
hesiod_free_postoffice (context, hpo);
|
||||||
}
|
}
|
||||||
hesiod_end (context);
|
hesiod_end (context);
|
||||||
@@ -599,12 +619,8 @@ hesiod_get_pobox (const char **user)
|
|||||||
if (strcasecmp(hpo->po_type, "pop") != 0)
|
if (strcasecmp(hpo->po_type, "pop") != 0)
|
||||||
errx (1, "Unsupported po type %s", hpo->po_type);
|
errx (1, "Unsupported po type %s", hpo->po_type);
|
||||||
|
|
||||||
ret = strdup(hpo->po_host);
|
ret = estrdup(hpo->po_host);
|
||||||
if(ret == NULL)
|
*user = estrdup(hpo->po_name);
|
||||||
errx (1, "strdup: out of memory");
|
|
||||||
*user = strdup(hpo->po_name);
|
|
||||||
if (*user == NULL)
|
|
||||||
errx (1, "strdup: out of memory");
|
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -644,9 +660,7 @@ parse_pobox (char *a0, const char **host, const char **user)
|
|||||||
|
|
||||||
if (pwd == NULL)
|
if (pwd == NULL)
|
||||||
errx (1, "Who are you?");
|
errx (1, "Who are you?");
|
||||||
*user = strdup (pwd->pw_name);
|
*user = estrdup (pwd->pw_name);
|
||||||
if (*user == NULL)
|
|
||||||
errx (1, "strdup: out of memory");
|
|
||||||
}
|
}
|
||||||
*host = get_pobox (user);
|
*host = get_pobox (user);
|
||||||
return;
|
return;
|
||||||
|
Reference in New Issue
Block a user