diff options
author | Bart De Schuymer <bdschuym@pandora.be> | 2005-11-09 22:39:03 +0000 |
---|---|---|
committer | Bart De Schuymer <bdschuym@pandora.be> | 2005-11-09 22:39:03 +0000 |
commit | 6bdb9f4c7111f87ab586095798363a696f9d29ef (patch) | |
tree | 38efaf9ce9cae105628d3bf1c29a6815659164f8 | |
parent | d9a49df57a1a63aa141716c27c3bee31456aaf4e (diff) |
use system call creat to create empty file
-rw-r--r-- | communication.c | 34 |
1 files changed, 9 insertions, 25 deletions
diff --git a/communication.c b/communication.c index f9f173b..dcfd8dc 100644 --- a/communication.c +++ b/communication.c @@ -9,8 +9,7 @@ * All the userspace/kernel communication is in this file. * The other code should not have to know anything about the way the * kernel likes the structure of the table data. - * The other code works with linked lists, lots of linked lists. - * So, the translation is done here. + * The other code works with linked lists. So, the translation is done here. */ #include <getopt.h> @@ -18,6 +17,8 @@ #include <errno.h> #include <stdio.h> #include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> #include <sys/socket.h> #include "include/ebtables_u.h" @@ -184,25 +185,14 @@ static struct ebt_replace *translate_user2kernel(struct ebt_u_replace *u_repl) static void store_table_in_file(char *filename, struct ebt_replace *repl) { - char *command, *data; + char *data; int size; - FILE *file; + int fd; /* Start from an empty file with right priviliges */ - command = (char *)malloc(strlen(filename) + 15); - if (!command) - ebt_print_memory(); - strcpy(command, "cat /dev/null>"); - strcpy(command + 14, filename); - if (system(command)) { + if (!(fd = creat(filename, 0600))) { ebt_print_error("Couldn't create file %s", filename); - goto free_command; - } - strcpy(command, "chmod 600 "); - strcpy(command + 10, filename); - if (system(command)) { - ebt_print_error("Couldn't chmod file %s", filename); - goto free_command; + return; } size = sizeof(struct ebt_replace) + repl->entries_size + @@ -216,17 +206,11 @@ static void store_table_in_file(char *filename, struct ebt_replace *repl) /* Initialize counters to zero, deliver_counters() can update them */ memset(data + sizeof(struct ebt_replace) + repl->entries_size, 0, repl->nentries * sizeof(struct ebt_counter)); - if (!(file = fopen(filename, "wb"))) { - ebt_print_error("Couldn't open file %s", filename); - goto free_data; - } else if (fwrite(data, sizeof(char), size, file) != size) + if (write(fd, data, size) != size) ebt_print_error("Couldn't write everything to file %s", filename); - fclose(file); -free_data: + close(fd); free(data); -free_command: - free(command); } void ebt_deliver_table(struct ebt_u_replace *u_repl) |