~ubuntu-branches/ubuntu/saucy/biosdevname/saucy-proposed

« back to all changes in this revision

Viewing changes to src/read_proc.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2011-02-23 17:58:36 UTC
  • Revision ID: james.westby@ubuntu.com-20110223175836-4f0cbcno9zm0lmdu
Tags: upstream-0.3.7
ImportĀ upstreamĀ versionĀ 0.3.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (c) 2006 Dell, Inc.
 
3
 *  by Matt Domsch <Matt_Domsch@dell.com>
 
4
 *  Licensed under the GNU General Public license, version 2.
 
5
 *   Copied from from net-tools-1.60, also under the GNU GPL v2,
 
6
 *   and modified for use here.
 
7
 */
 
8
 
 
9
#define _GNU_SOURCE
 
10
#include <stdio.h>
 
11
#include <string.h>
 
12
#include <ctype.h>
 
13
#include <stdlib.h>
 
14
#include <errno.h>
 
15
#include "eths.h"
 
16
 
 
17
#define _PATH_PROCNET_DEV "/proc/net/dev"
 
18
 
 
19
static struct network_device *add_interface(struct libbiosdevname_state *state,
 
20
                                            const char *name)
 
21
{
 
22
        struct network_device *i;
 
23
        i = malloc(sizeof(*i));
 
24
        if (!i)
 
25
                return NULL;
 
26
        memset(i, 0, sizeof(*i));
 
27
        INIT_LIST_HEAD(&i->node);
 
28
        strncpy(i->kernel_name, name, sizeof(i->kernel_name));
 
29
        list_add_tail(&i->node, &state->network_devices);
 
30
        return i;
 
31
}
 
32
 
 
33
 
 
34
static char *get_name(char **namep, char *p)
 
35
{
 
36
    int count = 0;
 
37
    char *name;
 
38
    while (isspace(*p))
 
39
        p++;
 
40
    name = *namep = p;
 
41
    while (*p) {
 
42
        if (isspace(*p))
 
43
            break;
 
44
        if (*p == ':') {        /* could be an alias */
 
45
            char *dot = p, *dotname = name;
 
46
            *name++ = *p++;
 
47
            count++;
 
48
            while (isdigit(*p)){
 
49
                *name++ = *p++;
 
50
                count++;
 
51
                if (count == (IFNAMSIZ-1))
 
52
                      break;
 
53
            }
 
54
            if (*p != ':') {    /* it wasn't, backup */
 
55
                p = dot;
 
56
                name = dotname;
 
57
            }
 
58
            if (*p == '\0')
 
59
                return NULL;
 
60
            p++;
 
61
            break;
 
62
        }
 
63
        *name++ = *p++;
 
64
        count++;
 
65
        if (count == (IFNAMSIZ-1))
 
66
              break;
 
67
    }
 
68
    *name++ = '\0';
 
69
    return p;
 
70
}
 
71
 
 
72
 
 
73
 
 
74
int get_interfaces(struct libbiosdevname_state *state)
 
75
{
 
76
        FILE *fh;
 
77
        int err;
 
78
        char *line = NULL;
 
79
        size_t linelen = 0;
 
80
 
 
81
        fh = fopen(_PATH_PROCNET_DEV, "r");
 
82
        if (!fh) {
 
83
                fprintf(stderr, "Error: cannot open %s (%s).\n",
 
84
                        _PATH_PROCNET_DEV, strerror(errno));
 
85
                return 1;
 
86
        }
 
87
        if (getline(&line, &linelen, fh) == -1 /* eat line */
 
88
            || getline(&line, &linelen, fh) == -1) {
 
89
                err = -1;
 
90
                goto out;
 
91
        }
 
92
 
 
93
        err = 0;
 
94
        while (getline(&line, &linelen, fh) != -1) {
 
95
                char *s, *name;
 
96
                s = get_name(&name, line);
 
97
                add_interface(state, name);
 
98
        }
 
99
        if (ferror(fh))
 
100
                err = -1;
 
101
 
 
102
out:
 
103
        free(line);
 
104
        fclose(fh);
 
105
        return err;
 
106
}