Size reductions, mostly switching things to use libbb functions.

This commit is contained in:
Rob Landley 2006-05-29 06:43:55 +00:00
parent bba7f08d27
commit a6e131dab3
6 changed files with 26 additions and 91 deletions

View File

@ -41,7 +41,7 @@ int strings_main(int argc, char **argv)
argv += optind; argv += optind;
n = bb_xgetlarg(n_arg, 10, 1, INT_MAX); n = bb_xgetlarg(n_arg, 10, 1, INT_MAX);
string = xcalloc(n + 1, 1); string = xzalloc(n + 1);
n--; n--;
if (argc == 0) { if (argc == 0) {

View File

@ -82,8 +82,7 @@ static uint32_t ttl = DEFAULT_TTL;
/* /*
* Convert host name from C-string to dns length/string. * Convert host name from C-string to dns length/string.
*/ */
static void static void convname(char *a, uint8_t *q)
convname(char *a, uint8_t *q)
{ {
int i = (q[0] == '.') ? 0 : 1; int i = (q[0] == '.') ? 0 : 1;
for(; i < MAX_HOST_LEN-1 && *q; i++, q++) for(; i < MAX_HOST_LEN-1 && *q; i++, q++)
@ -95,8 +94,7 @@ convname(char *a, uint8_t *q)
/* /*
* Insert length of substrings insetad of dots * Insert length of substrings insetad of dots
*/ */
static void static void undot(uint8_t * rip)
undot(uint8_t * rip)
{ {
int i = 0, s = 0; int i = 0, s = 0;
while(rip[i]) i++; while(rip[i]) i++;
@ -111,8 +109,7 @@ undot(uint8_t * rip)
/* /*
* Append message to log file * Append message to log file
*/ */
static void static void log_message(char *filename, char *message)
log_message(char *filename, char *message)
{ {
FILE *logfile; FILE *logfile;
if (!daemonmode) if (!daemonmode)
@ -133,8 +130,7 @@ log_message(char *filename, char *message)
* converting to a length/string substring for that label. * converting to a length/string substring for that label.
*/ */
static int static int getfileentry(FILE * fp, struct dns_entry *s, int verb)
getfileentry(FILE * fp, struct dns_entry *s, int verb)
{ {
unsigned int a,b,c,d; unsigned int a,b,c,d;
char *r, *name; char *r, *name;
@ -168,19 +164,16 @@ restart:
/* /*
* Read hostname/IP records from file * Read hostname/IP records from file
*/ */
static void static void dnsentryinit(int verb)
dnsentryinit(int verb)
{ {
FILE *fp; FILE *fp;
struct dns_entry *m, *prev; struct dns_entry *m, *prev;
prev = dnsentry = NULL; prev = dnsentry = NULL;
if(!(fp = fopen(fileconf, "r"))) fp = bb_xfopen(fileconf, "r");
bb_perror_msg_and_die("open %s",fileconf);
while (1) { while (1) {
if(!(m = (struct dns_entry *)malloc(sizeof(struct dns_entry)))) m = xmalloc(sizeof(struct dns_entry));
bb_perror_msg_and_die("malloc dns_entry");
m->next = NULL; m->next = NULL;
if (getfileentry(fp, m, verb)) if (getfileentry(fp, m, verb))
@ -199,8 +192,7 @@ dnsentryinit(int verb)
/* /*
* Set up UDP socket * Set up UDP socket
*/ */
static int static int listen_socket(char *iface_addr, int listen_port)
listen_socket(char *iface_addr, int listen_port)
{ {
struct sockaddr_in a; struct sockaddr_in a;
char msg[100]; char msg[100];
@ -228,8 +220,7 @@ listen_socket(char *iface_addr, int listen_port)
* Look query up in dns records and return answer if found * Look query up in dns records and return answer if found
* qs is the query string, first byte the string length * qs is the query string, first byte the string length
*/ */
static int static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
{ {
int i; int i;
struct dns_entry *d=dnsentry; struct dns_entry *d=dnsentry;
@ -269,8 +260,7 @@ table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
* Decode message and generate answer * Decode message and generate answer
*/ */
#define eret(s) do { fprintf (stderr, "%s\n", s); return -1; } while (0) #define eret(s) do { fprintf (stderr, "%s\n", s); return -1; } while (0)
static int static int process_packet(uint8_t * buf)
process_packet(uint8_t * buf)
{ {
struct dns_head *head; struct dns_head *head;
struct dns_prop *qprop; struct dns_prop *qprop;
@ -367,8 +357,7 @@ process_packet(uint8_t * buf)
/* /*
* Exit on signal * Exit on signal
*/ */
static void static void interrupt(int x)
interrupt(int x)
{ {
unlink(LOCK_FILE); unlink(LOCK_FILE);
write(2, "interrupt exiting\n", 18); write(2, "interrupt exiting\n", 18);

View File

@ -629,10 +629,7 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
enum { NONE, IFACE, MAPPING } currently_processing = NONE; enum { NONE, IFACE, MAPPING } currently_processing = NONE;
defn = xmalloc(sizeof(struct interfaces_file_t)); defn = xzalloc(sizeof(struct interfaces_file_t));
defn->autointerfaces = NULL;
defn->mappings = NULL;
defn->ifaces = NULL;
f = bb_xfopen(filename, "r"); f = bb_xfopen(filename, "r");
@ -647,10 +644,7 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
if (strcmp(firstword, "mapping") == 0) { if (strcmp(firstword, "mapping") == 0) {
#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
currmap = xmalloc(sizeof(struct mapping_defn_t)); currmap = xzalloc(sizeof(struct mapping_defn_t));
currmap->max_matches = 0;
currmap->n_matches = 0;
currmap->match = NULL;
while ((firstword = next_word(&buf_ptr)) != NULL) { while ((firstword = next_word(&buf_ptr)) != NULL) {
if (currmap->max_matches == currmap->n_matches) { if (currmap->max_matches == currmap->n_matches) {
@ -690,7 +684,7 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
NULL NULL
}; };
currif = xmalloc(sizeof(struct interface_defn_t)); currif = xzalloc(sizeof(struct interface_defn_t));
iface_name = next_word(&buf_ptr); iface_name = next_word(&buf_ptr);
address_family_name = next_word(&buf_ptr); address_family_name = next_word(&buf_ptr);
method_name = next_word(&buf_ptr); method_name = next_word(&buf_ptr);
@ -724,9 +718,6 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
return NULL; return NULL;
} }
currif->max_options = 0;
currif->n_options = 0;
currif->option = NULL;
{ {
llist_t *iface_list; llist_t *iface_list;
@ -845,9 +836,7 @@ static char *setlocalenv(char *format, const char *name, const char *value)
char *here; char *here;
char *there; char *there;
result = xmalloc(strlen(format) + strlen(name) + strlen(value) + 1); result = bb_xasprintf(format, name, value);
sprintf(result, format, name, value);
for (here = there = result; *there != '=' && *there; there++) { for (here = there = result; *there != '=' && *there; there++) {
if (*there == '-') if (*there == '-')
@ -878,11 +867,9 @@ static void set_environ(struct interface_defn_t *iface, const char *mode)
*ppch = NULL; *ppch = NULL;
} }
free(__myenviron); free(__myenviron);
__myenviron = NULL;
} }
__myenviron = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ )); __myenviron = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
environend = __myenviron; environend = __myenviron;
*environend = NULL;
for (i = 0; i < iface->n_options; i++) { for (i = 0; i < iface->n_options; i++) {
if (strcmp(iface->option[i].name, "up") == 0 if (strcmp(iface->option[i].name, "up") == 0
@ -892,19 +879,13 @@ static void set_environ(struct interface_defn_t *iface, const char *mode)
continue; continue;
} }
*(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value); *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
*environend = NULL;
} }
*(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface); *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
*environend = NULL;
*(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name); *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
*environend = NULL;
*(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name); *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
*environend = NULL;
*(environend++) = setlocalenv("%s=%s", "MODE", mode); *(environend++) = setlocalenv("%s=%s", "MODE", mode);
*environend = NULL;
*(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"); *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
*environend = NULL;
} }
static int doit(char *str) static int doit(char *str)
@ -1069,7 +1050,7 @@ static char *run_mapping(char *physical, struct mapping_defn_t * map)
free(logical); free(logical);
logical = new_logical; logical = new_logical;
} else { } else {
/* If we are UNABLE to read a line of output, discard are /* If we are UNABLE to read a line of output, discard our
* freshly allocated memory. */ * freshly allocated memory. */
free(new_logical); free(new_logical);
} }
@ -1177,28 +1158,6 @@ int ifupdown_main(int argc, char **argv)
if (cmds == iface_up) { if (cmds == iface_up) {
target_list = defn->autointerfaces; target_list = defn->autointerfaces;
} else { } else {
#if 0
/* iface_down */
llist_t *new_item;
const llist_t *list = state_list;
while (list) {
new_item = xmalloc(sizeof(llist_t));
new_item->data = bb_xstrdup(list->data);
new_item->link = NULL;
list = target_list;
if (list == NULL)
target_list = new_item;
else {
while (list->link) {
list = list->link;
}
list = new_item;
}
list = list->link;
}
target_list = defn->autointerfaces;
#else
/* iface_down */ /* iface_down */
const llist_t *list = state_list; const llist_t *list = state_list;
while (list) { while (list) {
@ -1206,7 +1165,6 @@ int ifupdown_main(int argc, char **argv)
list = list->link; list = list->link;
} }
target_list = defn->autointerfaces; target_list = defn->autointerfaces;
#endif
} }
} else { } else {
llist_add_to_end(&target_list, argv[optind]); llist_add_to_end(&target_list, argv[optind]);
@ -1308,8 +1266,7 @@ int ifupdown_main(int argc, char **argv)
llist_t *iface_state = find_iface_state(state_list, iface); llist_t *iface_state = find_iface_state(state_list, iface);
if (cmds == iface_up) { if (cmds == iface_up) {
char *newiface = xmalloc(strlen(iface) + 1 + strlen(liface) + 1); char *newiface = bb_xasprintf("%s=%s", iface, liface);
sprintf(newiface, "%s=%s", iface, liface);
if (iface_state == NULL) { if (iface_state == NULL) {
llist_add_to_end(&state_list, newiface); llist_add_to_end(&state_list, newiface);
} else { } else {
@ -1318,17 +1275,7 @@ int ifupdown_main(int argc, char **argv)
} }
} else { } else {
/* Remove an interface from the linked list */ /* Remove an interface from the linked list */
if (iface_state) { free(llist_pop(&iface_state));
llist_t *l = iface_state->link;
free(iface_state->data);
iface_state->data = NULL;
iface_state->link = NULL;
if (l) {
iface_state->data = l->data;
iface_state->link = l->link;
}
free(l);
}
} }
} }
} }

View File

@ -76,7 +76,6 @@
#define _PATH_PROCNET_DEV "/proc/net/dev" #define _PATH_PROCNET_DEV "/proc/net/dev"
#define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6" #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
#define new(p) ((p) = xcalloc(1,sizeof(*(p))))
#if HAVE_AFINET6 #if HAVE_AFINET6
@ -789,7 +788,8 @@ static struct interface *add_interface(char *name)
if (n < 0) if (n < 0)
break; break;
} }
new(new);
new = xzalloc(sizeof(*new));
safe_strncpy(new->name, name, IFNAMSIZ); safe_strncpy(new->name, name, IFNAMSIZ);
nextp = ife ? &ife->next : &int_list; nextp = ife ? &ife->next : &int_list;
new->prev = ife; new->prev = ife;

View File

@ -17,6 +17,7 @@
#include "libnetlink.h" #include "libnetlink.h"
#include "ll_map.h" #include "ll_map.h"
#include "libbb.h"
struct idxmap struct idxmap
{ {
@ -57,9 +58,7 @@ int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
break; break;
if (im == NULL) { if (im == NULL) {
im = malloc(sizeof(*im)); im = xmalloc(sizeof(*im));
if (im == NULL)
return 0;
im->next = *imp; im->next = *imp;
im->index = ifi->ifi_index; im->index = ifi->ifi_index;
*imp = im; *imp = im;

View File

@ -105,7 +105,7 @@ int nameif_main(int argc, char **argv)
if (strlen(*a) > IF_NAMESIZE) if (strlen(*a) > IF_NAMESIZE)
serror("interface name `%s' too long", *a); serror("interface name `%s' too long", *a);
ch = xcalloc(1, sizeof(mactable_t)); ch = xzalloc(sizeof(mactable_t));
ch->ifname = bb_xstrdup(*a++); ch->ifname = bb_xstrdup(*a++);
ch->mac = cc_macaddr(*a++); ch->mac = cc_macaddr(*a++);
if (clist) if (clist)
@ -126,7 +126,7 @@ int nameif_main(int argc, char **argv)
continue; continue;
} }
name_length = strcspn(line_ptr, " \t"); name_length = strcspn(line_ptr, " \t");
ch = xcalloc(1, sizeof(mactable_t)); ch = xzalloc(sizeof(mactable_t));
ch->ifname = bb_xstrndup(line_ptr, name_length); ch->ifname = bb_xstrndup(line_ptr, name_length);
if (name_length > IF_NAMESIZE) if (name_length > IF_NAMESIZE)
serror("interface name `%s' too long", ch->ifname); serror("interface name `%s' too long", ch->ifname);