minor fix to return E_USAGE on -V instead of exit(0);
[oss-qm-packages.git] / lib / proc.c
blobbf34dbec210f70c70f4ae8048c3a3b2660e907e5
1 /* Tolerant /proc file parser. Copyright 1998 Andi Kleen */
2 /* $Id: proc.c,v 1.4 1999/01/05 20:54:00 philip Exp $ */
3 /* Fixme: cannot currently cope with removed fields */
5 #include <string.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <ctype.h>
10 /* Caller must free return string. */
12 char *proc_gen_fmt(char *name, int more, FILE * fh,...)
14 char buf[512], format[512] = "";
15 char *title, *head, *hdr;
16 va_list ap;
18 if (!fgets(buf, (sizeof buf) - 1, fh))
19 return NULL;
20 strcat(buf, " ");
22 va_start(ap, fh);
23 title = va_arg(ap, char *);
24 for (hdr = buf; hdr;) {
25 while (isspace(*hdr) || *hdr == '|')
26 hdr++;
27 head = hdr;
28 hdr = strpbrk(hdr, "| \t\n");
29 if (hdr)
30 *hdr++ = 0;
32 if (!strcmp(title, head)) {
33 strcat(format, va_arg(ap, char *));
34 title = va_arg(ap, char *);
35 if (!title || !head)
36 break;
37 } else {
38 strcat(format, "%*s"); /* XXX */
40 strcat(format, " ");
42 va_end(ap);
44 if (!more && title) {
45 fprintf(stderr, "warning: %s does not contain required field %s\n",
46 name, title);
47 return NULL;
49 return strdup(format);
52 /*
53 * this will generate a bitmask of present/missing fields in the header of
54 * a /proc file.
56 int proc_guess_fmt(char *name, FILE *fh, ...)
58 char buf[512];
59 char *tmp;
60 int flag = 0;
61 va_list ap;
63 if (!fgets(buf, (sizeof buf) - 1, fh))
64 return -1;
65 strcat(buf, "\0");
66 va_start(ap, fh);
67 while((tmp = va_arg(ap, char *))) {
68 int f = va_arg(ap, int);
69 if (strstr(buf,tmp) != 0)
70 flag |= f;
72 va_end(ap);
73 return flag;