~ubuntu-branches/debian/stretch/uswsusp/stretch

« back to all changes in this revision

Viewing changes to suspend-cvs20060928/config.c

  • Committer: Bazaar Package Importer
  • Author(s): Christian Perrier
  • Date: 2008-08-20 09:09:13 UTC
  • mfrom: (0.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080820090913-0eahue1zo8egcxls
Tags: 0.8-1.1
* Non-maintainer upload to fix pending l10n issues.
* Remove extra and useless debian/po/ff/ directory
* Debconf translation updates:
  - Japanese. Closes: #489939
  - German. Closes: #493747
  - French. Closes: #493771
  - Romanian. Closes: #493772
  - Galician. Closes: #494050
  - Finnish. Closes: #494087
  - Italian. Closes: #494096
  - Basque. Closes: #494277
  - Basque. Closes: #494277
  - Czech. Closes: #494410
  - Swedish. Closes: #494412
  - Russian. Closes: #495412
  - Portuguese. Closes: #495451
  - Spanish. Closes: #495499
  - Slovak. Closes: #495516

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * config.c
3
 
 *
4
 
 * Configuration file parser for userland suspend tools
5
 
 *
6
 
 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
7
 
 *
8
 
 * This file is released under the GPLv2.
9
 
 *
10
 
 */
11
 
 
12
 
#include <sys/types.h>
13
 
#include <sys/stat.h>
14
 
#include <fcntl.h>
15
 
#include <unistd.h>
16
 
#include <stdio.h>
17
 
#include <errno.h>
18
 
#include <string.h>
19
 
 
20
 
#include "config.h"
21
 
#include "encrypt.h"
22
 
 
23
 
/**
24
 
 *      parse - read and parse the configuration file
25
 
 */
26
 
 
27
 
static int parse(char *my_name, char *file_name, int parc, struct config_par *parv)
28
 
{
29
 
        char *str, *dst, *fmt, buf[MAX_STR_LEN];
30
 
        FILE *file;
31
 
        int error, i, j, k;
32
 
 
33
 
        file = fopen(file_name, "r");
34
 
        if (!file) {
35
 
                fprintf(stderr, "%s: Could not open configuration file\n",
36
 
                        my_name);
37
 
                return -errno;
38
 
        }
39
 
        error = 0;
40
 
        i = 0;
41
 
        do {
42
 
                str = fgets(buf, MAX_STR_LEN, file);
43
 
                if (!str)
44
 
                        break;
45
 
                i++;
46
 
                /* Ignore comments */
47
 
                if (*str == '#')
48
 
                        continue;
49
 
                /* Skip white space */
50
 
                while (*str == ' ' || *str == '\t' || *str == '\r' || *str == '\n')
51
 
                        str++;
52
 
                /* Skip the lines containing white space only */
53
 
                if (!*str)
54
 
                        continue;
55
 
                /* Compare with parameter names */
56
 
                for (j =  0; j < parc; j++) {
57
 
                        k = strlen(parv[j].name);
58
 
                        if (!strncmp(parv[j].name, str, k)) {
59
 
                                if (!parv[j].ptr)
60
 
                                        break;
61
 
                                str += k;
62
 
                                while (*str == ' ' || *str == '\t')
63
 
                                        str++;
64
 
                                if (*str != ':' && *str != '=') {
65
 
                                        error = -EINVAL;
66
 
                                        break;
67
 
                                }
68
 
                                str++;
69
 
                                while (*str == ' ' || *str == '\t')
70
 
                                        str++;
71
 
                                if (*str) {
72
 
                                        fmt = parv[j].fmt;
73
 
                                        if (!strncmp(fmt, "%s", 2)) {
74
 
                                                dst = parv[j].ptr;
75
 
                                                k = parv[j].len;
76
 
                                                strncpy(dst, str, k - 1);
77
 
                                                k = strlen(dst) - 1;
78
 
                                                if (dst[k] == '\n')
79
 
                                                        dst[k] = '\0';
80
 
                                        } else {
81
 
                                                k = sscanf(str, fmt, parv[j].ptr);
82
 
                                                if (k <= 0)
83
 
                                                        error = -EINVAL;
84
 
                                        }
85
 
                                        break;
86
 
                                }
87
 
                        }
88
 
                }
89
 
                if (j >= parc)
90
 
                        error = -EINVAL;
91
 
        } while (!error);
92
 
        fclose(file);
93
 
        if (error)
94
 
                fprintf(stderr, "%s: Error in configuration file, line %d\n",
95
 
                        my_name, i);
96
 
        return error;
97
 
}
98
 
 
99
 
int get_config(char *my_name, int argc, char *argv[],
100
 
               int parc, struct config_par *parv, char *special)
101
 
{
102
 
        struct stat stat_buf;
103
 
        int ret = 0;
104
 
 
105
 
        if (argc <= 2) {
106
 
                if (!stat(CONFIG_FILE, &stat_buf))
107
 
                        ret = parse(my_name, CONFIG_FILE, parc, parv);
108
 
                if (ret < 0 || argc < 2)
109
 
                        return ret;
110
 
                strncpy(special, argv[1], MAX_STR_LEN - 1);
111
 
                return 0;
112
 
        }
113
 
 
114
 
        if (strncmp(argv[1], "-f", 2)) {
115
 
                fprintf(stderr, "Usage: %s [-f config][resume_device]\n",
116
 
                        my_name);
117
 
                return -EINVAL;
118
 
        }
119
 
 
120
 
        ret = parse(my_name, argv[2], parc, parv);
121
 
        if (ret)
122
 
                return ret;
123
 
 
124
 
        if (argc > 3)
125
 
                strncpy(special, argv[3], MAX_STR_LEN - 1);
126
 
 
127
 
        return 0;
128
 
}