make string_to_number() static
[jleu-ebtables.git] / ebtablesu.c
blob3cee145d4070bfbba87e9fc6cdb6a431f2959945
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <errno.h>
10 static void print_help()
12 printf("ebtablesu v"PROGVERSION" ("PROGDATE")\n");
13 printf(
14 "Usage:\n"
15 "ebtablesu open table : copy the kernel table\n"
16 "ebtablesu fopen table file : copy the table from the specified file\n"
17 "ebtablesu free table : remove the table from memory\n"
18 "ebtablesu commit table : commit the table to the kernel\n"
19 "ebtablesu fcommit table file : commit the table to the specified file\n\n"
20 "ebtablesu <ebtables options> : the ebtables specifications\n"
21 " use spaces only to separate options and commands\n"
22 "For the ebtables options, see\n# ebtables -h\nor\n# man ebtables\n"
25 int main(int argc, char *argv[])
27 char *arguments, *pos;
28 int i, writefd, len = 0;
30 if (argc > EBTD_ARGC_MAX) {
31 fprintf(stderr, "ebtablesd accepts at most %d arguments, %d "
32 "arguments were specified. If you need this many "
33 "arguments, recompile this tool with a higher value for"
34 " EBTD_ARGC_MAX.\n", EBTD_ARGC_MAX - 1, argc - 1);
35 return -1;
36 } else if (argc == 1) {
37 fprintf(stderr, "At least one argument is needed.\n");
38 print_help();
39 exit(0);
42 for (i = 0; i < argc; i++)
43 len += strlen(argv[i]);
44 /* Don't forget '\0' */
45 len += argc;
46 if (len > EBTD_CMDLINE_MAXLN) {
47 fprintf(stderr, "ebtablesd has a maximum command line argument "
48 "length of %d, an argument length of %d was received. "
49 "If a smaller length is unfeasible, recompile this tool "
50 "with a higher value for EBTD_CMDLINE_MAXLN.\n",
51 EBTD_CMDLINE_MAXLN, len);
52 return -1;
55 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
56 if (argc != 2) {
57 fprintf(stderr, "%s does not accept options.\n", argv[1]);
58 return -1;
60 print_help();
61 exit(0);
64 if (!(arguments = (char *)malloc(len))) {
65 fprintf(stderr, "ebtablesu: out of memory.\n");
66 return -1;
69 if ((writefd = open(EBTD_PIPE, O_WRONLY, 0)) == -1) {
70 fprintf(stderr, "Could not open the pipe, perhaps ebtablesd is "
71 "not running or you don't have write permission (try "
72 "running as root).\n");
73 return -1;
76 pos = arguments;
77 for (i = 0; i < argc; i++) {
78 strcpy(pos, argv[i]);
79 pos += strlen(argv[i]);
80 *(pos++) = ' ';
83 *(pos-1) = '\n';
84 if (write(writefd, arguments, len) == -1) {
85 perror("write");
86 return -1;
88 return 0;