Fix basename usage in pacman and utilities

basename() is a rather untrusty function call on a lot of platforms as it
does some weird and different things. To solve this, I added a mbasename
fuction to pacman to take its place, and simply removed its usage in the
utilities (it isn't worth dealing with there).

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2007-10-31 11:43:04 -05:00
parent 4a5e7b6bd1
commit dea9b3bc0f
3 changed files with 35 additions and 9 deletions

View file

@ -57,15 +57,15 @@ static alpm_list_t *pm_targets;
* @param op the operation code requested * @param op the operation code requested
* @param myname basename(argv[0]) * @param myname basename(argv[0])
*/ */
static void usage(int op, char *myname) static void usage(int op, const char * const myname)
{ {
/* prefetch some strings for usage below, which moves a lot of calls /* prefetch some strings for usage below, which moves a lot of calls
* out of gettext. */ * out of gettext. */
char * const str_opt = _("options"); char const * const str_opt = _("options");
char * const str_file = _("file"); char const * const str_file = _("file");
char * const str_pkg = _("package"); char const * const str_pkg = _("package");
char * const str_usg = _("usage"); char const * const str_usg = _("usage");
char * const str_opr = _("operation"); char const * const str_opr = _("operation");
if(op == PM_OP_MAIN) { if(op == PM_OP_MAIN) {
printf("%s: %s <%s> [...]\n", str_usg, myname, str_opr); printf("%s: %s <%s> [...]\n", str_usg, myname, str_opr);
@ -233,6 +233,28 @@ static void cleanup(int signum)
exit(signum); exit(signum);
} }
/** Parse the basename of a program from a path.
* Grabbed from the uClibc source.
* @param path path to parse basename from
*
* @return everything following the final '/'
*/
static char *mbasename(const char *path)
{
const char *s;
const char *p;
p = s = path;
while (*s) {
if (*s++ == '/') {
p = s;
}
}
return (char *)p;
}
/** Parse command-line arguments for each operation. /** Parse command-line arguments for each operation.
* @param argc argc * @param argc argc
* @param argv argv * @param argv argv
@ -432,7 +454,7 @@ static int parseargs(int argc, char *argv[])
} }
if(config->help) { if(config->help) {
usage(config->op, basename(argv[0])); usage(config->op, mbasename(argv[0]));
return(2); return(2);
} }
if(config->version) { if(config->version) {

View file

@ -32,6 +32,8 @@
#include <alpm.h> #include <alpm.h>
#include <alpm_list.h> #include <alpm_list.h>
#define BASENAME "testdb"
int str_cmp(const void *s1, const void *s2) int str_cmp(const void *s1, const void *s2)
{ {
return(strcmp(s1, s2)); return(strcmp(s1, s2));
@ -145,7 +147,7 @@ int main(int argc, char **argv)
} else if(argc == 3 && strcmp(argv[1], "-b") == 0) { } else if(argc == 3 && strcmp(argv[1], "-b") == 0) {
dbpath = argv[2]; dbpath = argv[2];
} else { } else {
fprintf(stderr, "usage: %s -b <pacman db>\n", basename(argv[0])); fprintf(stderr, "usage: %s -b <pacman db>\n", BASENAME);
return(1); return(1);
} }

View file

@ -27,6 +27,8 @@
#include <alpm.h> #include <alpm.h>
#define BASENAME "testpkg"
static void output_cb(pmloglevel_t level, char *fmt, va_list args) static void output_cb(pmloglevel_t level, char *fmt, va_list args)
{ {
if(strlen(fmt)) { if(strlen(fmt)) {
@ -45,7 +47,7 @@ int main(int argc, char **argv)
pmpkg_t *pkg = NULL; pmpkg_t *pkg = NULL;
if(argc != 2) { if(argc != 2) {
fprintf(stderr, "usage: %s <package file>\n", basename(argv[0])); fprintf(stderr, "usage: %s <package file>\n", BASENAME);
return(1); return(1);
} }