File: configfile.c

package info (click to toggle)
pptpd 1.1.2-1.4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 636 kB
  • ctags: 510
  • sloc: ansic: 3,687; sh: 443; makefile: 81; perl: 70
file content (113 lines) | stat: -rw-r--r-- 2,465 bytes parent folder | download
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
/*
 * configfile.c
 *
 * Methods for accessing the PPTPD config file and searching for
 * PPTPD keywords.
 *
 * $Id: configfile.c,v 1.1.1.1 1999/12/17 16:30:14 patl Exp $
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <string.h>

#include "defaults.h"
#include "configfile.h"

/* Local function prototypes */
static FILE *open_config_file(char *filename);
static void close_config_file(FILE * file);

/*
 * read_config_file
 *
 * This method opens up the file specified by 'filename' and searches
 * through the file for 'keyword'. If 'keyword' is found any string
 * following it is stored in 'value'.
 *
 * args: filename (IN) - config filename
 *       keyword (IN) - word to search for in config file
 *       value (OUT) - value of keyword
 *
 * retn: -1 on error, 0 if keyword not found, 1 on value success
 */
int read_config_file(char *filename, char *keyword, char *value)
{
	FILE *in;
	int len;
	char buffer[MAX_CONFIG_STRING_SIZE], w[MAX_CONFIG_STRING_SIZE],
		v[MAX_CONFIG_STRING_SIZE];

	in = open_config_file(filename);
	if (in == NULL) {
		/* Couldn't find config file, or permission denied */
		return -1;
	}
	while ((fgets(buffer, MAX_CONFIG_STRING_SIZE - 1, in)) != NULL) {
		/* ignore long lines */
		if (buffer[(len = strlen(buffer)) - 1] != '\n') {
			fprintf(stderr, "Long config file line ignored.\n");
			do
				fgets(buffer, MAX_CONFIG_STRING_SIZE - 1, in);
			while (buffer[strlen(buffer) - 1] != '\n');
			continue;
		}
		buffer[len - 1] = '\0';

		/* short-circuit comments */
		if (buffer[0] == '#')
			continue;

		/* check if it's what we want */
		if (sscanf(buffer, "%s %s", w, v) > 0 && !strcmp(w, keyword)) {
			/* found it :-) */
			strcpy(value, v);
			close_config_file(in);
			/* tell them we got it */
			return 1;
		}
	}
	close_config_file(in);
	/* didn't find it - better luck next time */
	return 0;
}

/*
 * open_config_file
 *
 * Opens up the PPTPD config file for reading.
 *
 * args: filename - the config filename (eg. '/etc/pptpd.conf')
 *
 * retn: NULL on error, file descriptor on success
 *
 */
static FILE *open_config_file(char *filename)
{
	FILE *in;
	static int first = 1;

	if ((in = fopen(filename, "r")) == NULL) {
		/* Couldn't open config file */
		if (first) {
			perror(filename);
			first = 0;
		}
		return NULL;
	}
	return in;
}

/*
 * close_config_file
 *
 * Closes the PPTPD config file descriptor
 *
 */
static void close_config_file(FILE * in)
{
	fclose(in);
}