freebsd-dev/usr.sbin/pkg_install/lib/str.c
David E. O'Brien 2bae1ab6a5 Remove s_strl*(). I am not sure what was thought they accomplished.
When reading the code I had to stop, say "ok, what does *these*
modifications of strl*() do?  Pull out grep.  Oh, not in add/, maybe above
in ../lib/?  Yep.  So what do they do?  Comments above them are misleading,
guess I'll have to read the code.  Oh, they just test strl* against the
size and return the result of the test.  Now I can continue to read the
code I was.

The uses of s_strl*() then test that result and errx()'s.
Lets think about the "optimized" code I am removing:

In general the compiler pushes the three args to strl* onto the stack and calls
s_strl*.  s_strl* has to indirectly access 3 args from the stack.  Then push
them on the stack a 2nd time for the real strl* call.  s_strl* then pops the
return from strl* off the stack; or moves it from the register it was returned
in, to the register where tests can happen.  s_strl* then pops the three
arguments to strl*.  Perform the test, push the result of the test, or move it
from the result register to the return value register.  The caller to s_strl*
now has to either pop the return value of s_strl* or move it from the return
value register to the test register.  The caller then pops the three args to
s_strl* off the stack (the same args that s_strl* itself had to pop off after
the real call to strl*).  The s_strl* caller then performs a simular test to
what has already been done, and conditionally jumps.  By doing things this way, we've given the compiler optimizer less to work with.

Also, please don't forget the that call to s_strl* has possibly jumped to code
not in the cache due to being far away from the calling code, thus causing a
pipeline stall.

So where is the "optimization" from s_strl*?
It isn't code clarity.
It isn't code execution speed.  It isn't code size either.
2001-07-28 01:59:58 +00:00

131 lines
2.4 KiB
C

#ifndef lint
static const char rcsid[] =
"$FreeBSD$";
#endif
/*
* FreeBSD install - a package for the installation and maintainance
* of non-core utilities.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Jordan K. Hubbard
* 18 July 1993
*
* Miscellaneous string utilities.
*
*/
#include "lib.h"
/* Return the filename portion of a path */
char *
basename_of(char *str)
{
char *basename = str + strlen(str) - 1;
while (basename != str && basename[-1] != '/')
--basename;
return basename;
}
char *
strconcat(char *s1, char *s2)
{
static char tmp[FILENAME_MAX];
tmp[0] = '\0';
strncpy(tmp, s1 ? s1 : s2, FILENAME_MAX);
if (s1 && s2)
strncat(tmp, s2, FILENAME_MAX - strlen(tmp));
return tmp;
}
/* Get a string parameter as a file spec or as a "contents follow -" spec */
char *
get_dash_string(char **str)
{
char *s = *str;
if (*s == '-')
*str = copy_string(s + 1);
else
*str = fileGetContents(s);
return *str;
}
/* Rather Obvious */
char *
copy_string(char *str)
{
char *ret;
if (!str)
ret = NULL;
else {
ret = (char *)malloc(strlen(str) + 1);
strcpy(ret, str);
}
return ret;
}
/* Return TRUE if 'str' ends in suffix 'suff' */
Boolean
suffix(char *str, char *suff)
{
char *idx;
Boolean ret = FALSE;
idx = strrchr(str, '.');
if (idx && !strcmp(idx + 1, suff))
ret = TRUE;
return ret;
}
/* Assuming str has a suffix, brutally murder it! */
void
nuke_suffix(char *str)
{
char *idx;
idx = strrchr(str, '.');
if (idx)
*idx = '\0'; /* Yow! Don't try this on a const! */
}
/* Lowercase a whole string */
void
str_lowercase(char *str)
{
while (*str) {
*str = tolower(*str);
++str;
}
}
char *
get_string(char *str, int max, FILE *fp)
{
int len;
if (!str)
return NULL;
str[0] = '\0';
while (fgets(str, max, fp)) {
len = strlen(str);
while (len && isspace(str[len - 1]))
str[--len] = '\0';
if (len)
return str;
}
return NULL;
}