OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [userland/] [route/] [lib/] [proc.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 745 simons
/* Tolerant /proc file parser. Copyright 1998 Andi Kleen */
2
/* $Id: proc.c,v 1.1 2002-03-17 19:58:53 simons Exp $ */
3
/* Fixme: cannot currently cope with removed fields */
4
 
5
#include <string.h>
6
#include <stdarg.h>
7
#include <stdio.h>
8
#include <ctype.h>
9
 
10
/* Caller must free return string. */
11
 
12
char *proc_gen_fmt(char *name, int more, FILE * fh,...)
13
{
14
    char buf[512], format[512] = "";
15
    char *title, *head, *hdr;
16
    va_list ap;
17
 
18
    if (!fgets(buf, (sizeof buf) - 1, fh))
19
        return NULL;
20
    strcat(buf, " ");
21
 
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;
31
 
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 */
39
        }
40
        strcat(format, " ");
41
    }
42
    va_end(ap);
43
 
44
    if (!more && title) {
45
        fprintf(stderr, "warning: %s does not contain required field %s\n",
46
                name, title);
47
        return NULL;
48
    }
49
    return strdup(format);
50
}
51
 
52
/*
53
 * this will generate a bitmask of present/missing fields in the header of
54
 * a /proc file.
55
 */
56
int proc_guess_fmt(char *name, FILE *fh, ...)
57
{
58
    char buf[512];
59
    char *tmp;
60
    int flag = 0;
61
    va_list ap;
62
 
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;
71
    }
72
    va_end(ap);
73
    return flag;
74
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.