Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 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> |
| 9 | |
| 10 | int main(int argc, char *argv[]) |
| 11 | { |
| 12 | char *arguments, *pos; |
| 13 | int i, writefd, len = 0; |
| 14 | |
| 15 | if ((writefd = open(EBTD_PIPE, O_WRONLY, 0)) == -1) { |
| 16 | perror("open"); |
| 17 | return -1; |
| 18 | } |
| 19 | |
| 20 | if (argc > EBTD_ARGC_MAX) { |
| 21 | printf("ebtablesd accepts at most %d arguments, %d arguments " |
| 22 | "were specified. If you need this many arguments, " |
| 23 | "recompile this tool with a higher value for " |
| 24 | "EBTD_ARGC_MAX.\n", EBTD_ARGC_MAX - 1, argc - 1); |
| 25 | return -1; |
| 26 | } else if (argc == 1) { |
| 27 | printf("At least one argument is needed.\n"); |
| 28 | return -1; |
| 29 | } |
| 30 | |
| 31 | for (i = 0; i < argc; i++) |
| 32 | len += strlen(argv[i]); |
| 33 | /* Don't forget '\0' */ |
| 34 | len += argc; |
| 35 | if (len > EBTD_CMDLINE_MAXLN) { |
| 36 | printf("ebtablesd has a maximum command line argument length " |
| 37 | "of %d, an argument length of %d was received. If a " |
| 38 | "smaller length is unfeasible, recompile this tool " |
| 39 | "with a higher value for EBTD_CMDLINE_MAXLN.\n", |
| 40 | EBTD_CMDLINE_MAXLN, len); |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | if (!(arguments = (char *)malloc(len))) { |
| 45 | printf("ebtablesu: out of memory.\n"); |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | pos = arguments; |
| 50 | for (i = 0; i < argc; i++) { |
| 51 | strcpy(pos, argv[i]); |
| 52 | pos += strlen(argv[i]) + 1; |
| 53 | } |
| 54 | |
| 55 | *(pos-1) = '\n'; |
| 56 | if (write(writefd, arguments, len) == -1) { |
| 57 | perror("write"); |
| 58 | return -1; |
| 59 | } |
| 60 | return 0; |
| 61 | } |