From 476c9d3a010c9ec2db658c943141a6cee92cd794 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 11 Dec 2004 02:33:33 +0000 Subject: add a callback mechanism for code that wants to defer committing changes until all the command line args have been processed Reviewed by: ambrisko --- sbin/ifconfig/ifconfig.c | 30 ++++++++++++++++++++++++++++++ sbin/ifconfig/ifconfig.h | 3 +++ 2 files changed, 33 insertions(+) (limited to 'sbin') diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index 3725276..5fc2362 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -446,6 +446,27 @@ cmd_lookup(const char *name) #undef N } +struct callback { + callback_func *cb_func; + void *cb_arg; + struct callback *cb_next; +}; +static struct callback *callbacks = NULL; + +void +callback_register(callback_func *func, void *arg) +{ + struct callback *cb; + + cb = malloc(sizeof(struct callback)); + if (cb == NULL) + errx(1, "unable to allocate memory for callback"); + cb->cb_func = func; + cb->cb_arg = arg; + cb->cb_next = callbacks; + callbacks = cb; +} + /* specially-handled comamnds */ static void setifaddr(const char *, int, int, const struct afswtch *); static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr); @@ -457,6 +478,7 @@ static const struct cmd setifdstaddr_cmd = static int ifconfig(int argc, char *const *argv, const struct afswtch *afp) { + struct callback *cb; int s; if (afp == NULL) @@ -541,6 +563,14 @@ ifconfig(int argc, char *const *argv, const struct afswtch *afp) if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0) Perror("ioctl (SIOCAIFADDR)"); } + + /* + * Do deferred callbacks registered while processing + * command-line arguments. + */ + for (cb = callbacks; cb != NULL; cb = cb->cb_next) + cb->cb_func(s, cb->cb_arg); + close(s); return(0); } diff --git a/sbin/ifconfig/ifconfig.h b/sbin/ifconfig/ifconfig.h index aa09ff8..3de635c 100644 --- a/sbin/ifconfig/ifconfig.h +++ b/sbin/ifconfig/ifconfig.h @@ -56,6 +56,9 @@ struct cmd { }; void cmd_register(struct cmd *); +typedef void callback_func(int s, void *); +void callback_register(callback_func *, void *); + /* * Macros for declaring command functions and initializing entries. */ -- cgit v1.1