File: proc.c

package info (click to toggle)
net-tools 2.10-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,888 kB
  • sloc: ansic: 14,668; makefile: 370; sh: 153
file content (127 lines) | stat: -rw-r--r-- 2,830 bytes parent folder | download | duplicates (3)
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
/* Tolerant /proc file parser. Copyright 1998 Andi Kleen */
/* $Id: proc.c,v 1.5 2007/12/01 18:44:57 ecki Exp $ */
/* Fixme: cannot currently cope with removed fields */

#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include "util.h"

/* Caller must free return string. */

char *proc_gen_fmt(const char *name, int more, FILE * fh,...)
{
    char buf[512], format[512] = "";
    char *title, *head, *hdr;
    va_list ap;
    size_t format_len = 0;
    size_t format_size = sizeof(format);

    if (!fgets(buf, (sizeof buf) - 1, fh))
	return NULL;
    strcat(buf, " ");

    va_start(ap, fh);
    title = va_arg(ap, char *);
    for (hdr = buf; hdr;) {
	while (isspace(*hdr) || *hdr == '|')
	    hdr++;
	head = hdr;
	hdr = strpbrk(hdr, "| \t\n");
	if (hdr)
	    *hdr++ = 0;

	if (!strcmp(title, head)) {
	    const char *arg = va_arg(ap, char *);
	    size_t arg_len = strlen(arg);

	    /* Check if we have enough space for format specifier + space */
	    if (format_len + arg_len + 1 >= format_size) {
		fprintf(stderr, "warning: format buffer overflow in %s\n", name);
		va_end(ap);
		return NULL;
	    }

	    strcpy(format + format_len, arg);
	    format_len += arg_len;

	    title = va_arg(ap, char *);
	    if (!title || !head)
		break;
	} else {
	    /* Check if we have enough space for "%*s" */
	    if (format_len + 3 >= format_size) {
		fprintf(stderr, "warning: format buffer overflow in %s\n", name);
		va_end(ap);
		return NULL;
	    }

	    strcpy(format + format_len, "%*s");
	    format_len += 3;
	}

	/* Check if we have space for the trailing space */
	if (format_len + 1 >= format_size) {
	    fprintf(stderr, "warning: format buffer overflow in %s\n", name);
	    va_end(ap);
	    return NULL;
	}

	format[format_len++] = ' ';
	format[format_len] = '\0';
    }
    va_end(ap);

    if (!more && title) {
	fprintf(stderr, "warning: %s does not contain required field %s\n",
		name, title);
	return NULL;
    }
    return xstrdup(format);
}

/*
 * this will generate a bitmask of present/missing fields in the header of
 * a /proc file.
 */
int proc_guess_fmt(const char *name, FILE *fh, ...)
{
    char buf[512];
    char *tmp;
    int flag = 0;
    va_list ap;

    if (!fgets(buf, (sizeof buf) - 1, fh))
	return -1;
    strcat(buf, "\0");
    va_start(ap, fh);
    while((tmp = va_arg(ap, char *))) {
      int f = va_arg(ap, int);
      if (strstr(buf,tmp) != 0)
        flag |= f;
    }
    va_end(ap);
    return flag;
}


FILE *proc_fopen(const char *name)
{
    static char *buffer;
    static size_t pagesz;
    FILE *fd = fopen(name, "r");

    if (fd == NULL)
      return NULL;

    if (!buffer) {
      pagesz = getpagesize();
      buffer = malloc(pagesz);
    }

    setvbuf(fd, buffer, _IOFBF, pagesz);
    return fd;
}