From cd45e5435762d8c6cd13287df7df16b97755176e Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Wed, 19 Dec 2007 12:46:08 +0000 Subject: Introduce cbasename so we don't have to strdup(basename) and free all the time. --- src/_usage.c | 1 + src/checkown.c | 7 ++---- src/fstabinfo.c | 5 ++--- src/librc-daemon.c | 16 ++++---------- src/librc.c | 57 ++++++++++++------------------------------------- src/mountinfo.c | 3 --- src/rc-depend.c | 8 +++---- src/rc-misc.h | 11 ++++++++++ src/rc-update.c | 6 ++---- src/rc.c | 12 ++--------- src/rc.h | 2 +- src/runscript.c | 15 +++---------- src/start-stop-daemon.c | 6 ++---- 13 files changed, 47 insertions(+), 102 deletions(-) (limited to 'src') diff --git a/src/_usage.c b/src/_usage.c index ce733a97..b079e320 100644 --- a/src/_usage.c +++ b/src/_usage.c @@ -29,6 +29,7 @@ static void usage (int exit_status) { const char * const has_arg[] = { "", "", "[arg]" }; int i; + printf ("Usage: %s [options] ", applet); #ifdef extraopts printf (extraopts); diff --git a/src/checkown.c b/src/checkown.c index ecec917c..f7dd9e6e 100644 --- a/src/checkown.c +++ b/src/checkown.c @@ -30,8 +30,6 @@ * SUCH DAMAGE. */ -#define APPLET "checkown" - #include #include #include @@ -48,7 +46,7 @@ #include "einfo.h" #include "rc-misc.h" -static char *applet = NULL; +static const char *applet; static int do_check (char *path, uid_t uid, gid_t gid, mode_t mode, int file) { @@ -189,10 +187,9 @@ int checkown (int argc, char **argv) struct group *gr = NULL; bool file = 0; + applet = cbasename (argv[0]); int retval = EXIT_SUCCESS; - applet = argv[0]; - while ((opt = getopt_long (argc, argv, getoptstring, longopts, (int *) 0)) != -1) { diff --git a/src/fstabinfo.c b/src/fstabinfo.c index 9782f9da..56957c47 100644 --- a/src/fstabinfo.c +++ b/src/fstabinfo.c @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -87,7 +86,7 @@ static struct mntent *getmntfile (const char *file) } #endif -static const char *applet; +static const char *applet = NULL; #include "_usage.h" #define getoptstring "bmop:t:" getoptstring_COMMON @@ -129,7 +128,7 @@ int fstabinfo (int argc, char **argv) char *file; bool filtered = false; - applet = basename (argv[0]); + applet = cbasename (argv[0]); /* Ensure that we are only quiet when explicitly told to be */ unsetenv ("EINFO_QUIET"); diff --git a/src/librc-daemon.c b/src/librc-daemon.c index 30853f26..cc8e93ff 100644 --- a/src/librc-daemon.c +++ b/src/librc-daemon.c @@ -313,7 +313,6 @@ bool rc_service_daemon_set (const char *service, const char *exec, const char *name, const char *pidfile, bool started) { - char *svc; char *dirpath; char *file = NULL; int i; @@ -330,10 +329,9 @@ bool rc_service_daemon_set (const char *service, const char *exec, errno = EINVAL; return (false); } - svc = xstrdup (service); + dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", - basename (svc), (char *) NULL); - free (svc); + cbasename (service), (char *) NULL); if (exec) { i = strlen (exec) + 6; @@ -417,17 +415,14 @@ bool rc_service_started_daemon (const char *service, const char *exec, int i; char *mexec; bool retval = false; - char *svc; DIR *dp; struct dirent *d; if (! service || ! exec) return (false); - svc = xstrdup (service); - dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc), + dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", cbasename (service), (char *) NULL); - free (svc); i = strlen (exec) + 6; mexec = xmalloc (sizeof (char) * i); @@ -474,15 +469,12 @@ bool rc_service_daemons_crashed (const char *service) char *p; char *token; bool retval = false; - char *svc; if (! service) return (false); - svc = xstrdup (service); - dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc), + dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", cbasename (service), (char *) NULL); - free (svc); if (! (dp = opendir (dirpath))) { free (dirpath); diff --git a/src/librc.c b/src/librc.c index 9330a304..8380ebd9 100644 --- a/src/librc.c +++ b/src/librc.c @@ -378,15 +378,12 @@ bool rc_service_in_runlevel (const char *service, const char *runlevel) { char *file; bool retval; - char *svc; if (! runlevel || ! service) return (false); - svc = xstrdup (service); - file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (svc), + file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, cbasename (service), (char *) NULL); - free (svc); retval = exists (file); free (file); @@ -399,21 +396,18 @@ bool rc_service_mark (const char *service, const rc_service_state_t state) char *file; int i = 0; int skip_state = -1; - char *base; - char *svc; + const char *base; char *init = rc_service_resolve (service); bool skip_wasinactive = false; - if (! service) + if (! init) return (false); - svc = xstrdup (service); - base = basename (svc); + base = cbasename (service); if (state != RC_SERVICE_STOPPED) { if (! exists (init)) { free (init); - free (svc); return (false); } @@ -425,7 +419,6 @@ bool rc_service_mark (const char *service, const rc_service_state_t state) if (i != 0) { free (file); free (init); - free (svc); return (false); } @@ -435,7 +428,6 @@ bool rc_service_mark (const char *service, const rc_service_state_t state) if (state == RC_SERVICE_COLDPLUGGED || state == RC_SERVICE_FAILED) { free (init); - free (svc); return (true); } @@ -449,7 +441,7 @@ bool rc_service_mark (const char *service, const rc_service_state_t state) s != RC_SERVICE_SCHEDULED) && (! skip_wasinactive || s != RC_SERVICE_WASINACTIVE)) { - file = rc_strcatpaths (RC_SVCDIR, rc_parse_service_state(s), base, + file = rc_strcatpaths (RC_SVCDIR, rc_parse_service_state (s), base, (char *) NULL); if (exists (file)) { if ((state == RC_SERVICE_STARTING || @@ -516,7 +508,6 @@ bool rc_service_mark (const char *service, const rc_service_state_t state) free (sdir); } - free (svc); free (init); return (true); } @@ -526,11 +517,10 @@ rc_service_state_t rc_service_state (const char *service) { int i; int state = RC_SERVICE_STOPPED; - char *svc = xstrdup (service); for (i = 0; rc_service_state_names[i].name; i++) { char *file = rc_strcatpaths (RC_SVCDIR, rc_service_state_names[i].name, - basename (svc), (char*) NULL); + cbasename (service), (char*) NULL); if (exists (file)) { if (rc_service_state_names[i].state <= 0x10) state = rc_service_state_names[i].state; @@ -539,7 +529,6 @@ rc_service_state_t rc_service_state (const char *service) } free (file); } - free (svc); if (state & RC_SERVICE_STOPPED) { char **services = rc_services_scheduled_by (service); @@ -603,7 +592,6 @@ static pid_t _exec_service (const char *service, const char *arg) char *file; char *fifo; pid_t pid = -1; - char *svc; file = rc_service_resolve (service); if (! exists (file)) { @@ -613,10 +601,8 @@ static pid_t _exec_service (const char *service, const char *arg) } /* We create a fifo so that other services can wait until we complete */ - svc = xstrdup (service); - fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", basename (svc), + fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", cbasename (service), (char *) NULL); - free (svc); if (mkfifo (fifo, 0600) != 0 && errno != EEXIST) { free (fifo); @@ -674,26 +660,21 @@ bool rc_service_schedule_start (const char *service, char *dir; char *init; char *file; - char *svc; bool retval; /* service may be a provided service, like net */ if (! service || ! rc_service_exists (service_to_start)) return (false); - svc = xstrdup (service); - dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc), + dir = rc_strcatpaths (RC_SVCDIR, "scheduled", cbasename (service), (char *) NULL); - free (svc); if (mkdir (dir, 0755) != 0 && errno != EEXIST) { free (dir); return (false); } init = rc_service_resolve (service_to_start); - svc = xstrdup (service_to_start); - file = rc_strcatpaths (dir, basename (svc), (char *) NULL); - free (svc); + file = rc_strcatpaths (dir, cbasename (service_to_start), (char *) NULL); retval = (exists (file) || symlink (init, file) == 0); free (init); free (file); @@ -705,12 +686,10 @@ librc_hidden_def(rc_service_schedule_start) bool rc_service_schedule_clear (const char *service) { - char *svc = xstrdup (service); - char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc), + char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", cbasename (service), (char *) NULL); bool retval; - free (svc); if (! (retval = rm_dir (dir, true)) && errno == ENOENT) retval = true; free (dir); @@ -787,7 +766,6 @@ bool rc_service_add (const char *runlevel, const char *service) bool retval; char *init; char *file; - char *svc; if (! rc_runlevel_exists (runlevel)) { errno = ENOENT; @@ -814,10 +792,8 @@ bool rc_service_add (const char *runlevel, const char *service) } } - svc = xstrdup (service); - file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (svc), + file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, cbasename (service), (char *) NULL); - free (svc); retval = (symlink (init, file) == 0); free (init); free (file); @@ -828,16 +804,13 @@ librc_hidden_def(rc_service_add) bool rc_service_delete (const char *runlevel, const char *service) { char *file; - char *svc; bool retval = false; if (! runlevel || ! service) return (false); - svc = xstrdup (service); - file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (svc), + file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, cbasename (service), (char *) NULL); - free (svc); if (unlink (file) == 0) retval = true; @@ -868,19 +841,17 @@ librc_hidden_def(rc_services_scheduled_by) char **rc_services_scheduled (const char *service) { - char *svc = xstrdup (service); - char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc), + char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", cbasename (service), (char *) NULL); char **list = NULL; - free (svc); list = ls_dir (dir, LS_INITD); free (dir); return (list); } librc_hidden_def(rc_services_scheduled) -bool rc_service_plugable (char *service) +bool rc_service_plugable (const char *service) { char *list; char *p; diff --git a/src/mountinfo.c b/src/mountinfo.c index ad76d32d..05ce8dd7 100644 --- a/src/mountinfo.c +++ b/src/mountinfo.c @@ -43,7 +43,6 @@ #include #include -#include #include #include #include @@ -367,8 +366,6 @@ int mountinfo (int argc, char **argv) int result; bool quiet; - applet = basename (argv[0]); - /* Ensure that we are only quiet when explicitly told to be */ unsetenv ("EINFO_QUIET"); diff --git a/src/rc-depend.c b/src/rc-depend.c index 8c61d888..9c870448 100644 --- a/src/rc-depend.c +++ b/src/rc-depend.c @@ -29,8 +29,6 @@ * SUCH DAMAGE. */ -#define APPLET "rc-depend" - #include #include @@ -47,6 +45,8 @@ #include "rc-misc.h" #include "strlist.h" +static const char *applet; + rc_depinfo_t *_rc_deptree_load (int *regen) { if (rc_deptree_update_needed ()) { int retval; @@ -62,8 +62,6 @@ rc_depinfo_t *_rc_deptree_load (int *regen) { return (rc_deptree_load ()); } -static char *applet = NULL; - #include "_usage.h" #define getoptstring "t:suT" getoptstring_COMMON static struct option longopts[] = { @@ -98,7 +96,7 @@ int rc_depend (int argc, char **argv) int opt; char *token; - applet = argv[0]; + applet = cbasename (argv[0]); while ((opt = getopt_long (argc, argv, getoptstring, longopts, (int *) 0)) != -1) diff --git a/src/rc-misc.h b/src/rc-misc.h index 570d7e79..bcc6b544 100644 --- a/src/rc-misc.h +++ b/src/rc-misc.h @@ -35,6 +35,7 @@ #include #include #include +#include #ifndef LIB # define LIB "lib" @@ -118,4 +119,14 @@ bool rc_conf_yesno (const char *var); char **env_filter (void); char **env_config (void); +/* cbasename never modifies the argument. As such, if there is a trailing + * slash then an empty string is returned. */ +static inline const char *cbasename (const char *argv0) { + char *l = strrchr (argv0, '/'); + + if (l) + return (++l); + return (argv0); +} + #endif diff --git a/src/rc-update.c b/src/rc-update.c index 3ab52fd1..3c366be4 100644 --- a/src/rc-update.c +++ b/src/rc-update.c @@ -29,8 +29,6 @@ * SUCH DAMAGE. */ -#define APPLET "rc-update" - #include #include #include @@ -46,7 +44,7 @@ #include "rc-misc.h" #include "strlist.h" -static char *applet = NULL; +static const char *applet = NULL; /* Return the number of changes made: * -1 = no changes (error) @@ -161,7 +159,7 @@ int rc_update (int argc, char **argv) int opt; int retval = EXIT_FAILURE; - applet = argv[0]; + applet = cbasename (argv[0]); while ((opt = getopt_long (argc, argv, getoptstring, longopts, (int *) 0)) != -1) diff --git a/src/rc.c b/src/rc.c index 507a1651..7a33e64e 100644 --- a/src/rc.c +++ b/src/rc.c @@ -36,8 +36,6 @@ const char copyright[] = "Copyright (c) 2007 Roy Marples"; -#define APPLET "rc" - #define SYSLOG_NAMES #include @@ -49,7 +47,6 @@ const char copyright[] = "Copyright (c) 2007 Roy Marples"; #include #include #include -#include #include #include #include @@ -93,7 +90,7 @@ extern char **environ; static char *RUNLEVEL = NULL; static char *PREVLEVEL = NULL; -static char *applet = NULL; +static const char *applet = NULL; static char *runlevel = NULL; static char **env = NULL; static char **newenv = NULL; @@ -150,9 +147,6 @@ static void cleanup (void) free (runlevel); } - - free (applet); - applet = NULL; } static int syslog_decode (char *name, CODE *codetab) @@ -815,10 +809,8 @@ int main (int argc, char **argv) bool parallel; int regen = 0; + applet = cbasename (argv[0]); atexit (cleanup); - if (argv[0]) - applet = xstrdup (basename (argv[0])); - if (! applet) eerrorx ("arguments required"); diff --git a/src/rc.h b/src/rc.h index 776c1678..b95c9c5d 100644 --- a/src/rc.h +++ b/src/rc.h @@ -150,7 +150,7 @@ char **rc_service_extra_commands (const char *service); /*! Check if the service is allowed to be hot/cold plugged * @param service to check * @return true if allowed, otherwise false */ -bool rc_service_plugable (char *service); +bool rc_service_plugable (const char *service); /*! Resolves a service name to its full path. * @param service to check diff --git a/src/runscript.c b/src/runscript.c index c52b33ea..6839497e 100644 --- a/src/runscript.c +++ b/src/runscript.c @@ -29,8 +29,6 @@ * SUCH DAMAGE. */ -#define APPLET "runscript" - #include #include #include @@ -40,7 +38,6 @@ #include #include #include -#include #include #include #include @@ -76,7 +73,7 @@ #define ONE_SECOND 1000000000 -static char *applet = NULL; +static const char *applet = NULL; static char *service = NULL; static char *exclusive = NULL; static char *mtime_test = NULL; @@ -353,7 +350,6 @@ static void cleanup (void) } free (exclusive); free (service); - free (applet); free (prefix); free (softlevel); } @@ -502,7 +498,6 @@ static bool svc_exec (const char *arg1, const char *arg2) static bool svc_wait (rc_depinfo_t *depinfo, const char *svc) { char *s; - char *base; char *fifo; struct timespec ts; int nloops = WAIT_MAX * (ONE_SECOND / WAIT_INTERVAL); @@ -524,11 +519,7 @@ static bool svc_wait (rc_depinfo_t *depinfo, const char *svc) } rc_strlist_free (keywords); - s = xstrdup (svc); - base = basename (s); - fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", base, (char *) NULL); - free (s); - + fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", cbasename (svc), (char *) NULL); ts.tv_sec = 0; ts.tv_nsec = WAIT_INTERVAL; @@ -1055,7 +1046,7 @@ int runscript (int argc, char **argv) exit (EXIT_FAILURE); } - applet = xstrdup (basename (argv[1])); + applet = cbasename (argv[1]); if (argc < 3) usage (EXIT_FAILURE); diff --git a/src/start-stop-daemon.c b/src/start-stop-daemon.c index 82fa99a4..ba93a29e 100644 --- a/src/start-stop-daemon.c +++ b/src/start-stop-daemon.c @@ -33,8 +33,6 @@ * SUCH DAMAGE. */ -#define APPLET "start-stop-daemon" - /* nano seconds */ #define POLL_INTERVAL 20000000 #define WAIT_PIDFILE 500000000 @@ -90,7 +88,7 @@ typedef struct schedulelist } schedulelist_t; static schedulelist_t *schedule; -static char *applet; +static const char *applet; static char *changeuser; static char **newenv; @@ -574,7 +572,7 @@ int start_stop_daemon (int argc, char **argv) char *svcname = getenv ("SVCNAME"); char *env; - applet = argv[0]; + applet = cbasename (argv[0]); atexit (cleanup); signal (SIGINT, handle_signal); -- cgit v1.2.3