📄 addconfigfile.c
字号:
#define _GNU_SOURCE#include <stdio.h>#include <stdlib.h>#include <string.h>#include <firewall.h>/* adds one configuration to the list of already present configurations */struct FirewallConfig *addEntry (struct ConfigEntry *entry, struct FirewallConfig *config) { struct FirewallConfig *newElement; newElement = malloc (sizeof (struct FirewallConfig)); if (!newElement) { fprintf (stderr, "Out of memory!\n"); exit (1); } newElement->entry = entry; newElement->next = config; return newElement;}/* copies lists and allocates memory in the process */struct ElementList *elementListCopy (struct ElementList *list) { struct ElementList *head, *previous, *newElement; head = NULL; previous = NULL; while (list) { newElement = malloc (sizeof (struct ElementList)); if (!newElement) { fprintf (stderr, "Out of memory!\n"); exit (1); } newElement->element = strdup (list->element); newElement->next = NULL; if (head == NULL) { head = newElement; } else { if (previous) { previous->next = newElement; } previous = newElement; } list = list->next; } return head;} /* adds configurations arising from one line of the configuration file to the list of configurations. *line contains the data corresponding to one line. Returns NULL if parsing error occurred. */struct FirewallConfig *addLine (struct ConfigLine *line, struct FirewallConfig *config, int *errno) { *errno = 0; char *tmp; int portno; struct ElementList *uids, *programs, *IPAddresses, *ports; struct ConfigEntry *entry; /* extract all configurations from the one line */ uids = line->uids; programs = line->programs; IPAddresses = line->IPAddresses; ports = line->ports; while (ports) { if (strcmp (ports->element, "*") == 0) { portno = 0; } else { /* check whether we have valid port number */ portno = strtol(ports->element, &tmp, 10); } if ((tmp && strcmp (tmp, ""))|| (portno == 0)) { *errno = ILLEGAL_PORT; return NULL; } entry = malloc (sizeof (struct ConfigEntry)); if (!entry) { fprintf (stderr, "Out of memory!\n"); exit (1); } entry->uids = elementListCopy (uids); entry->programs = elementListCopy (programs); entry->IPAddresses = elementListCopy (IPAddresses); entry->port = portno; config = addEntry (entry, config); ports = ports->next; } return config;}/* prints a list of uids etc */void printStringList ( char *initialString, struct ElementList *list) { printf (initialString); while (list) { printf("%s", list->element); list = list->next; } printf ("\n");} /* sorts the list of configurations */struct FirewallConfig *firewallTableSort (struct FirewallConfig *firewallTable) { int noOfElem; int i; struct FirewallConfig *tmp; /* how long is the list? */ tmp = firewallTable; noOfElem = 0; while (tmp) { noOfElem++; tmp = tmp->next; } if (noOfElem <= 1) { /* nothing to be sorted */ return firewallTable; } /* now create an araray of pointers for quicksort */ struct FirewallConfig *sortingArray[noOfElem]; tmp = firewallTable; for (i = 0; i < noOfElem; i++) { sortingArray[i] = tmp; tmp = tmp->next; } /* sort this array */ qsort (&sortingArray[0], noOfElem, sizeof (struct FirewallConfig *), compareEntry); /* now create a sorted list from the array */ tmp = sortingArray[0]; for (i = 1; i < noOfElem; i++) { tmp->next = sortingArray[i]; tmp = tmp->next; } tmp->next = NULL; return (sortingArray[0]);}/* print out the list of configurations */void printTable (struct FirewallConfig *firewallTable) { while (firewallTable) { printStringList ("uids = ", firewallTable->entry->uids); printStringList ("programs = ", firewallTable->entry->programs); printStringList ("IPAddress = ", firewallTable->entry->IPAddresses); printf ("port = %d\n\n", firewallTable->entry->port); firewallTable = firewallTable->next; }}int main (int argc, char **argv) { size_t len; char *line = NULL; int errno; struct ConfigLine *configLine; struct FirewallConfig *firewallTable = NULL; FILE *fp; if (argc !=2) { fprintf (stderr, "Exactly one argument required!\n"); exit (1); } fp = fopen (argv[1], "r"); if (!fp) { fprintf (stderr, "%s: Could not open %s!\n", argv[0], argv[1]); exit (1); } while (getline (&line, &len, fp) != -1) { configLine = parseLine (line, &errno); if (!configLine) { fprintf (stderr, "Parsing error in line %s; errno = %c !\n", line, errno); exit (1); } firewallTable = addLine (configLine, firewallTable, &errno); if (!firewallTable) { fprintf (stderr, "Parsing error in line %s; errno = %c !\n", line, errno); exit (1); } free (line); line = NULL; } firewallTable = firewallTableSort (firewallTable); printf ("Table after sorting\n"); printTable (firewallTable); exit (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -