[Toybox] [PATCH] mktemp fixes
Use $TMPDIR if set (necessary on Android, where there is no /tmp). Include full template in error messages. Don't report success on failure with -q. Avoid unnecessary allocation. Fix xx versus XX confusion. diff --git a/toys/lsb/mktemp.c b/toys/lsb/mktemp.c index c1175fe..52e53ee 100644 --- a/toys/lsb/mktemp.c +++ b/toys/lsb/mktemp.c @@ -12,8 +12,8 @@ config MKTEMP help usage: mktemp [-dq] [-p DIR] [TEMPLATE] -Safely create new file and print its name. Default TEMPLATE is -/tmp/tmp.XX and each trailing X is replaced with random char. +Safely create a new file and print its name. The default TEMPLATE is +tmp.XX. The default DIR is $TMPDIR, or /tmp if $TMPDIR is not set. -d, --directoryCreate directory instead of file -p DIR, --tmpdir=DIR Put new file in DIR @@ -29,24 +29,27 @@ GLOBALS( void mktemp_main(void) { - int d_flag = toys.optflags FLAG_d; - char *tmp; + int d_flag = toys.optflags FLAG_d; + char *template = *toys.optargs; + int success; - tmp = *toys.optargs; - - if (!tmp) { -if (!TT.tmpdir) TT.tmpdir = /tmp; -tmp = tmp.xx; + if (!template) { +template = tmp.XX; } - if (TT.tmpdir) tmp = xmprintf(%s/%s, TT.tmpdir ? TT.tmpdir : /tmp, -*toys.optargs ? *toys.optargs : tmp.XX); - if (d_flag ? mkdtemp(tmp) == NULL : mkstemp(tmp) == -1) -if (toys.optflags FLAG_q) - perror_exit(Failed to create temporary %s, -d_flag ? directory : file); + if (!TT.tmpdir) TT.tmpdir = getenv(TMPDIR); + if (!TT.tmpdir) TT.tmpdir = /tmp; + + snprintf(toybuf, sizeof(toybuf), %s/%s, TT.tmpdir, template); - xputs(tmp); + if (d_flag ? mkdtemp(toybuf) == NULL : mkstemp(toybuf) == -1) { +if (toys.optflags FLAG_q) { + toys.exitval = 1; +} else { + perror_exit(Failed to create temporary %s with template %s/%s, +d_flag ? directory : file, TT.tmpdir, template); +} + } - if (CFG_TOYBOX_FREE TT.tmpdir) free(tmp); + xputs(toybuf); } ___ Toybox mailing list Toybox@lists.landley.net http://lists.landley.net/listinfo.cgi/toybox-landley.net
[Toybox] [PATCH] help kill netcfg
netcfg is being removed, but shows up in the toybox roadmap. diff --git a/www/roadmap.html b/www/roadmap.html index 2457b4d..3f1f80f 100755 --- a/www/roadmap.html +++ b/www/roadmap.html @@ -252,7 +252,7 @@ getenforce setenforce chcon restorecon runcon getsebool setsebool load_policy pOther than the toolbox directory, the currently interesting subdirectories in the core repository are fs_mgr, gpttool, init, -logcat, logwrapper, mkbootimg, netcfg, reboot, and run-as./p +logcat, logwrapper, mkbootimg, reboot, and run-as./p ul libfs_mgr/b - subset of mount/li @@ -261,7 +261,6 @@ logcat, logwrapper, mkbootimg, netcfg, reboot, and run-as./p liblogcat/b - read android log format/li liblogwrapper/b - redirect stdio to android log/li libmkbootimg/b - create signed boot image/li -libnetcfg/b - network configuration (sucks in libnetutils)/li libreboot/b - Android's reboot(1)/li librun-as/b - subset of sudo/li /ul @@ -287,7 +286,7 @@ cat chcon chmod chown cmp cp date dd df dmesg du fs_mgr getenforce getevent getprop getsebool gpttool grep hd id ifconfig iftop init insmod ioctl ionice kill ln load_policy log logcat logwrapper ls lsmod lsof md5 -mkbootimg mkdir mount mv nandread netcfg netstat newfs_msdos notify printenv +mkbootimg mkdir mount mv nandread netstat newfs_msdos notify printenv ps r reboot renice restorecon rm rmdir rmmod route run-as runcon schedtop sendevent setconsole setenforce setprop setsebool sleep smd start stop sync top touch umount uptime vmstat watchdogd ___ Toybox mailing list Toybox@lists.landley.net http://lists.landley.net/listinfo.cgi/toybox-landley.net
Re: [Toybox] [PATCH] mktemp fixes
On 02/07/2015 12:04 PM, enh wrote: Use $TMPDIR if set (necessary on Android, where there is no /tmp). Include full template in error messages. Don't report success on failure with -q. Avoid unnecessary allocation. Fix xx versus XX confusion. Apparently I'm not capable of consistently spelling your name in commit -u arguments. diff --git a/toys/lsb/mktemp.c b/toys/lsb/mktemp.c index c1175fe..52e53ee 100644 --- a/toys/lsb/mktemp.c +++ b/toys/lsb/mktemp.c @@ -12,8 +12,8 @@ config MKTEMP help usage: mktemp [-dq] [-p DIR] [TEMPLATE] -Safely create new file and print its name. Default TEMPLATE is -/tmp/tmp.XX and each trailing X is replaced with random char. +Safely create a new file and print its name. The default TEMPLATE is +tmp.XX. The default DIR is $TMPDIR, or /tmp if $TMPDIR is not set. I redid the help text a bit. -d, --directoryCreate directory instead of file -p DIR, --tmpdir=DIR Put new file in DIR @@ -29,24 +29,27 @@ GLOBALS( void mktemp_main(void) { - int d_flag = toys.optflags FLAG_d; - char *tmp; + int d_flag = toys.optflags FLAG_d; + char *template = *toys.optargs; + int success; success isn't used and that's two int declaration lines anyway. - tmp = *toys.optargs; - - if (!tmp) { -if (!TT.tmpdir) TT.tmpdir = /tmp; -tmp = tmp.xx; + if (!template) { +template = tmp.XX; } I tend to avoid curly brackets around single lines unless there's if/else confusion or similar. (Code style thing.) - if (TT.tmpdir) tmp = xmprintf(%s/%s, TT.tmpdir ? TT.tmpdir : /tmp, -*toys.optargs ? *toys.optargs : tmp.XX); - if (d_flag ? mkdtemp(tmp) == NULL : mkstemp(tmp) == -1) -if (toys.optflags FLAG_q) - perror_exit(Failed to create temporary %s, -d_flag ? directory : file); + if (!TT.tmpdir) TT.tmpdir = getenv(TMPDIR); + if (!TT.tmpdir) TT.tmpdir = /tmp; + + snprintf(toybuf, sizeof(toybuf), %s/%s, TT.tmpdir, template); So if we _do_ have tmpdir+template combining to be bigger than the old PATH_MAX, we silently truncate. That seems more like a throw an error situation... - xputs(tmp); + if (d_flag ? mkdtemp(toybuf) == NULL : mkstemp(toybuf) == -1) { I try to avoid == 0 comparisons and such, because aero is special in C. A != 0 test ia a NOP, and for X == 0 we have have !X. And I've just about stopped using NULL entirely. 0 gets typecast to everything, is shorter, and I actually had something get _confused_ by being fed a NULL where it wanted a 0 because there was a typecast on it. (In musl there was a whole argument where they came to the conclusion NULL had to be 0L so it padded right in printf() without causing the unpleasant side effects of pointer typecasts, or something like that.) Neither is a big deal, just an I tend to wander through after and clean those up for consistency sort of heads up. +if (toys.optflags FLAG_q) { + toys.exitval = 1; +} else { + perror_exit(Failed to create temporary %s with template %s/%s, +d_flag ? directory : file, TT.tmpdir, template); +} + } - if (CFG_TOYBOX_FREE TT.tmpdir) free(tmp); + xputs(toybuf); Your comment at the top said not to report failure as success, but you're doing an xputs(toybuf) in the -q failure case anyway? (I added an else, I assume that's right?) Rob ___ Toybox mailing list Toybox@lists.landley.net http://lists.landley.net/listinfo.cgi/toybox-landley.net