diff options
author | Sean Farley <scf@FreeBSD.org> | 2008-02-28 04:09:08 +0000 |
---|---|---|
committer | Sean Farley <scf@FreeBSD.org> | 2008-02-28 04:09:08 +0000 |
commit | 7f08f0dd771fcc25bbb20d4e6678851bc4d1a426 (patch) | |
tree | 6bc4f5b814c2d60a743517a75f2bdb0cf0984d58 /lib/libc | |
parent | f666e89bfb8542d453f39dd7ed3a1678f8e1360f (diff) | |
download | src-7f08f0dd771fcc25bbb20d4e6678851bc4d1a426.tar.gz src-7f08f0dd771fcc25bbb20d4e6678851bc4d1a426.zip |
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
Notes
Notes:
svn path=/head/; revision=176632
Diffstat (limited to 'lib/libc')
-rw-r--r-- | lib/libc/stdlib/getenv.c | 48 |
1 files changed, 36 insertions, 12 deletions
diff --git a/lib/libc/stdlib/getenv.c b/lib/libc/stdlib/getenv.c index 2b7757ed3374..74595b2eae47 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 <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + + +#include "namespace.h" #include <sys/types.h> -#include <err.h> #include <errno.h> #include <stdbool.h> #include <stddef.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> +#include "un-namespace.h" -#include <sys/cdefs.h> -__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); } |