forked from blackav/ejudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ej-ncheck.c
145 lines (118 loc) · 3.25 KB
/
ej-ncheck.c
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* -*- c -*- */
/* Copyright (C) 2010-2015 Alexander Chernov <[email protected]> */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "ejudge/config.h"
#include "ejudge/ej_limits.h"
#include "ejudge/version.h"
#include "ejudge/ncheck_packet.h"
#include "ejudge/xalloc.h"
#include "ejudge/osdeps.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
static const unsigned char *program_name;
static unsigned char *program_dir;
static unsigned char *config_file;
static void
die(const char *format, ...)
__attribute__((noreturn, format(printf, 1, 2)));
static void
die(const char *format, ...)
{
char buf[1024];
va_list args;
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
fprintf(stderr, "%s: fatal: %s\n", program_name, buf);
exit(1);
}
static void
get_program_dir(const unsigned char *program_path)
{
unsigned char *workdir = 0;
unsigned char fullpath[EJ_PATH_MAX];
if (os_IsAbsolutePath(program_path)) {
program_dir = os_DirName(program_path);
os_normalize_path(program_dir);
return;
}
workdir = os_GetWorkingDir();
snprintf(fullpath, sizeof(fullpath), "%s/%s", workdir, program_path);
xfree(workdir); workdir = 0;
os_normalize_path(fullpath);
program_dir = os_DirName(fullpath);
}
static void
get_config_file(void)
{
unsigned char buf[EJ_PATH_MAX];
if (config_file) return;
snprintf(buf, sizeof(buf), "%s/ncheck.cfg", program_dir);
config_file = xstrdup(buf);
}
static void
print_version(void)
{
printf("%s %s, compiled %s\n", program_name, compile_version, compile_date);
exit(0);
}
static void
print_help(void)
{
exit(0);
}
struct config_global_data
{
struct generic_section_config g;
};
#define XFSIZE(t, x) (sizeof(((t*) 0)->x))
#define CONFIG_OFFSET(x) XOFFSET(struct config_global_data, x)
#define CONFIG_SIZE(x) XFSIZE(struct config_global_data, x)
#define CONFIG_PARAM(x, t) { #x, t, CONFIG_OFFSET(x), CONFIG_SIZE(x) }
static const struct config_parse_info config_global_params[] =
{
//CONFIG_PARAM(sleep_time, "d"),
//CONFIG_PARAM(spool_dir, "s"),
//CONFIG_PARAM(work_dir, "s"),
//CONFIG_PARAM(cache_dir, "s"),
{ 0, 0, 0, 0 }
};
static const struct config_section_info params[] __attribute__((unused)) =
{
{ "global", sizeof(struct config_global_data), config_global_params, 0, 0, 0 },
{ NULL, 0, NULL }
};
int
main(int argc, char *argv[])
{
int i;
if (argc <= 0 || !argv[0]) {
fprintf(stderr, "invalid program name\n");
return 1;
}
program_name = os_GetLastname(argv[0]);
get_program_dir(argv[0]);
for (i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "--version")) {
print_version();
} else if (!strcmp(argv[i], "--help")) {
print_help();
} else {
die("invalid option: %s", argv[i]);
}
}
get_config_file();
return 0;
}