Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / disk / smart-c / common.c @ 6eff786e

Historique | Voir | Annoter | Télécharger (1,74 ko)

1
/*
2
 * Copyright (C) 2008-2013 Helmut Grohne <helmut@subdivi.de> - All rights reserved.
3
 *
4
 * This copyrighted material is made available to anyone wishing to use,
5
 * modify, copy, or redistribute it subject to the terms and conditions
6
 * of the GNU General Public License v.2 or v.3.
7
 */
8
#include <errno.h>
9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <string.h>
12
#include <unistd.h>
13
#include "common.h"
14

    
15
extern char **environ;
16

    
17
int writeyes(void)
18
{
19
        puts("yes");
20
        return 0;
21
}
22

    
23
int autoconf_check_readable(const char *path)
24
{
25
        if (0 == access(path, R_OK))
26
                return writeyes();
27
        else {
28
                printf("no (%s is not readable, errno=%d)\n", path, errno);
29
                return 0;
30
        }
31
}
32

    
33
int getenvint(const char *name, int defvalue)
34
{
35
        const char *value;
36
        value = getenv(name);
37
        if (value == NULL)
38
                return defvalue;
39
        return atoi(value);
40
}
41

    
42
static
43
                                    /*@null@ */
44
 /*@observer@ */
45
const char *getenv_composed(const char *name1, const char *name2)
46
{
47
        char **p;
48
        size_t len1 = strlen(name1), len2 = strlen(name2);
49
        for (p = environ; *p; ++p) {
50
                if (0 == strncmp(*p, name1, len1) &&
51
                    0 == strncmp(len1 + *p, name2, len2) &&
52
                    (*p)[len1 + len2] == '=')
53
                        return len1 + len2 + 1 + *p;
54
        }
55
        return NULL;
56
}
57

    
58
void print_warning(const char *name)
59
{
60
        const char *p;
61
        p = getenv_composed(name, "_warning");
62
        if (p == NULL)
63
                p = getenv("warning");
64
        if (p == NULL)
65
                return;
66

    
67
        printf("%s.warning %s\n", name, p);
68
}
69

    
70
void print_critical(const char *name)
71
{
72
        const char *p;
73
        p = getenv_composed(name, "_critical");
74
        if (p == NULL)
75
                p = getenv("critical");
76
        if (p == NULL)
77
                return;
78

    
79
        printf("%s.critical %s\n", name, p);
80
}
81

    
82
void print_warncrit(const char *name)
83
{
84
        print_warning(name);
85
        print_critical(name);
86
}
87

    
88
int fail(const char *message)
89
{
90
        fputs(message, stderr);
91
        fputc('\n', stderr);
92
        return 1;
93
}