Subversion Repositories IRC-Archiver

Rev

Rev 2 | Rev 4 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 nishi 1
/* $Id: main.c 3 2024-08-29 18:49:46Z nishi $ */
2
 
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6
#include <sys/stat.h>
7
 
3 nishi 8
#include "ia_util.h"
9
#include "ia_util.h"
10
 
11
int main(int argc, char** argv) {
2 nishi 12
	const char* fn = argv[1] == NULL ? "archiver.ini" : argv[1];
13
	FILE* f = fopen(fn, "r");
3 nishi 14
	if(f == NULL) {
2 nishi 15
		fprintf(stderr, "Could not open the config: %s\n", fn);
16
		return 1;
17
	}
18
 
19
	struct stat s;
20
	stat(fn, &s);
21
 
22
	char* buf = malloc(s.st_size + 1);
23
	fread(buf, s.st_size, 1, f);
24
	buf[s.st_size] = 0;
25
 
26
	int i;
27
	int incr = 0;
28
 
3 nishi 29
	char* host = NULL;
30
	int port = 0;
31
	char* username = NULL;
32
	char* password = NULL;
33
 
34
	for(i = 0;; i++) {
35
		if(buf[i] == 0 || buf[i] == '\n') {
2 nishi 36
			char oldc = buf[i];
37
			buf[i] = 0;
38
			char* line = buf + incr;
3 nishi 39
			if(strlen(line) > 0 && line[0] != '#') {
2 nishi 40
				int j;
3 nishi 41
				for(j = 0; line[j] != 0; j++) {
42
					if(line[j] == '=') {
2 nishi 43
						line[j] = 0;
44
						char* key = line;
45
						char* value = line + j + 1;
46
 
3 nishi 47
						if(strcmp(key, "host") == 0) {
48
							if(host != NULL) free(host);
49
							host = ia_strdup(value);
50
						} else if(strcmp(key, "port") == 0) {
51
							port = atoi(value);
52
						} else if(strcmp(key, "username") == 0) {
53
							if(username != NULL) free(username);
54
							username = ia_strdup(value);
55
						} else if(strcmp(key, "password") == 0) {
56
							if(password != NULL) free(password);
57
							password = ia_strdup(value);
58
						}
2 nishi 59
 
60
						break;
61
					}
62
				}
63
			}
64
			incr = i + 1;
65
			if(oldc == 0) break;
66
		}
67
	}
68
 
69
	free(buf);
70
	fclose(f);
3 nishi 71
 
72
	int st = 0;
73
	if(host == NULL) {
74
		fprintf(stderr, "Specify host\n");
75
		st = 1;
76
	}
77
	if(username == NULL) {
78
		fprintf(stderr, "Specify username\n");
79
		st = 1;
80
	}
81
	if(password == NULL) {
82
		fprintf(stderr, "Specify password\n");
83
		st = 1;
84
	}
85
	if(st == 1) return st;
86
 
87
	if(host != NULL) free(host);
88
	if(username != NULL) free(username);
89
	if(password != NULL) free(password);
2 nishi 90
}