From 7ee4756ce96b87edc06d0cde27a17ff060f1a08e Mon Sep 17 00:00:00 2001 From: scf Date: Thu, 28 Feb 2008 04:09:08 +0000 Subject: Replace the use of warnx() with direct output to stderr using _write(). This reduces the size of a statically-linked binary by approximately 100KB in a trivial "return (0)" test application. readelf -S was used to verify that the .text section was reduced and that using strlen() saved a few more bytes over using sizeof(). Since the section of code is only called when environ is corrupt (program bug), I went with fewer bytes over fewer cycles. I made minor edits to the submitted patch to make the output resemble warnx(). Submitted by: kib bz Approved by: wes (mentor) MFC after: 5 days --- lib/libc/stdlib/getenv.c | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) (limited to 'lib/libc/stdlib') diff --git a/lib/libc/stdlib/getenv.c b/lib/libc/stdlib/getenv.c index 2b7757e..74595b2 100644 --- a/lib/libc/stdlib/getenv.c +++ b/lib/libc/stdlib/getenv.c @@ -23,23 +23,25 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include +__FBSDID("$FreeBSD$"); + + +#include "namespace.h" #include -#include #include #include #include #include #include +#include +#include "un-namespace.h" -#include -__FBSDID("$FreeBSD$"); - - -static const char CorruptEnvFindMsg[] = - "environment corrupt; unable to find %.*s"; +static const char CorruptEnvFindMsg[] = "environment corrupt; unable to find "; static const char CorruptEnvValueMsg[] = - "environment corrupt; missing value for %s"; + "environment corrupt; missing value for "; /* @@ -97,6 +99,26 @@ static void __attribute__ ((destructor)) __clean_env_destructor(void); /* + * A simple version of warnx() to avoid the bloat of including stdio in static + * binaries. + */ +static void +__env_warnx(const char *msg, const char *name, size_t nameLen) +{ + static const char nl[] = "\n"; + static const char progSep[] = ": "; + + _write(STDERR_FILENO, _getprogname(), strlen(_getprogname())); + _write(STDERR_FILENO, progSep, sizeof(progSep) - 1); + _write(STDERR_FILENO, msg, strlen(msg)); + _write(STDERR_FILENO, name, nameLen); + _write(STDERR_FILENO, nl, sizeof(nl) - 1); + + return; +} + + +/* * Inline strlen() for performance. Also, perform check for an equals sign. * Cheaper here than peforming a strchr() later. */ @@ -341,7 +363,8 @@ __build_env(void) envVars[envNdx].valueSize = strlen(envVars[envNdx].value); } else { - warnx(CorruptEnvValueMsg, envVars[envNdx].name); + __env_warnx(CorruptEnvValueMsg, envVars[envNdx].name, + strlen(envVars[envNdx].name)); errno = EFAULT; goto Failure; } @@ -356,8 +379,8 @@ __build_env(void) activeNdx = envVarsTotal - 1; if (__findenv(envVars[envNdx].name, nameLen, &activeNdx, false) == NULL) { - warnx(CorruptEnvFindMsg, (int)nameLen, - envVars[envNdx].name); + __env_warnx(CorruptEnvFindMsg, envVars[envNdx].name, + nameLen); errno = EFAULT; goto Failure; } @@ -527,7 +550,8 @@ __merge_environ(void) if (origEnviron != NULL) for (env = origEnviron; *env != NULL; env++) { if ((equals = strchr(*env, '=')) == NULL) { - warnx(CorruptEnvValueMsg, *env); + __env_warnx(CorruptEnvValueMsg, *env, + strlen(*env)); errno = EFAULT; return (-1); } -- cgit v1.1