int main(int argc, char **argv)
{
- const char *s;
-
#ifdef __GLIBC__
(*(int **)&bb_errno) = __errno_location();
#endif
applet_name = argv[0];
if (applet_name[0] == '-')
applet_name++;
- s = strrchr(applet_name, '/');
- if (s)
- applet_name = s + 1;
+ applet_name = bb_basename(applet_name);
parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
* a directory entry and its stat info are stored here
*/
struct dnode { /* the basic node */
- char *name; /* the dir entry name */
- char *fullname; /* the dir entry name */
+ const char *name; /* the dir entry name */
+ const char *fullname; /* the dir entry name */
int allocated;
struct stat dstat; /* the file stat info */
USE_SELINUX(security_context_t sid;)
static int status = EXIT_SUCCESS;
-static struct dnode *my_stat(char *fullname, char *name, int force_follow)
+static struct dnode *my_stat(const char *fullname, const char *name, int force_follow)
{
struct stat dstat;
struct dnode *cur;
return 0;
dirs = 0;
for (i = 0; i < nfiles; i++) {
- char *name;
+ const char *name;
if (!S_ISDIR(dn[i]->dstat.st_mode))
continue;
name = dn[i]->name;
for (i = 0; i < nfiles; i++) {
struct dnode *cur = dnp[i];
if (cur->allocated)
- free(cur->fullname); /* free the filename */
+ free((char*)cur->fullname); /* free the filename */
free(cur); /* free the dnode */
}
free(dnp); /* free the array holding the dnode pointers */
/* copy the entrys into the file or dir array */
for (d = i = 0; i < nfiles; i++) {
if (S_ISDIR(dn[i]->dstat.st_mode)) {
- char *name;
+ const char *name;
if (!(which & (SPLIT_DIR|SPLIT_SUBDIR)))
continue;
name = dn[i]->name;
continue;
}
fullname = concat_path_file(path, entry->d_name);
- cur = my_stat(fullname, strrchr(fullname, '/') + 1, 0);
+ cur = my_stat(fullname, bb_basename(fullname), 0);
if (!cur) {
free(fullname);
continue;
int test_main(int argc, char **argv)
{
int res;
- char *arg0;
+ const char *arg0;
bool _off;
- arg0 = strrchr(argv[0], '/');
- if (!arg0++) arg0 = argv[0];
+ arg0 = bb_basename(argv[0]);
if (arg0[0] == '[') {
--argc;
if (!arg0[1]) { /* "[" ? */
*/
static bool invalid_name(const char *c)
{
- const char *base_name = strrchr(c, '/');
-
- if (base_name)
- c = base_name + 1;
+ c = bb_basename(c);
while (*c && (isalnum(*c) || *c == '_' || *c == '-'))
c++;
ACTF(name)
{
- const char *tmp = strrchr(fileName, '/');
- if (tmp == NULL)
- tmp = fileName;
- else {
- tmp++;
- if (!*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
- tmp--;
- while (tmp != fileName && *--tmp != '/')
- continue;
- if (*tmp == '/')
- tmp++;
- }
+ const char *tmp = bb_basename(fileName);
+ if (tmp != fileName && !*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
+ tmp--;
+ while (tmp != fileName && *--tmp != '/')
+ continue;
+ if (*tmp == '/')
+ tmp++;
}
return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0;
}
char *concat_path_file(const char *path, const char *filename);
char *concat_subpath_file(const char *path, const char *filename);
+const char *bb_basename(const char *name);
/* NB: can violate const-ness (similarly to strchr) */
char *last_char_is(const char *s, int c);
lib-y += ask_confirmation.o
lib-y += bb_askpass.o
+lib-y += bb_basename.o
lib-y += bb_do_delay.o
lib-y += bb_pwd.o
lib-y += bb_strtonum.o
and therefore comm field contains "exe".
*/
-static const char *bb_basename(const char *name)
-{
- const char *cp = strrchr(name, '/');
- if (cp)
- return cp + 1;
- return name;
-}
-
/* find_pid_by_name()
*
* Modified by Vladimir Oleynik for use with libbb/procps.c
#endif
case 'W':
pbuf = pwd_buf;
- cp = strrchr(pbuf,'/');
+ cp = strrchr(pbuf, '/');
if (cp != NULL && cp != pbuf)
pbuf += (cp-pbuf) + 1;
break;
static void fork_and_execute(int die, char *arg0, char **arg);
static int get_uid_gid(int, const char *);
static void safe_memcpy(char * dest, const char * src, int len);
-static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr);
-static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr);
+static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr);
+static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr);
/* Structs and vars */
static struct config_entry_struct *first_config = NULL;
dest[len] = '\0';
}
-static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr)
+static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr)
{
if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c')
return 2 + addendum;
return 0;
}
-static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr)
+static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr)
{
if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') {
if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n'
int ret;
const char *compat_name = NULL;
const char *dest_name = info->devname;
- char *ptr=NULL;
+ const char *ptr;
char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
int mode, host, bus, target, lun;
unsigned int i;
break;
case AC_MKNEWCOMPAT:
case AC_RMNEWCOMPAT:
- ptr = strrchr(info->devname, '/') + 1;
+ ptr = bb_basename(info->devname);
i = scan_dev_name(info->devname, info->namelen, ptr);
/* nothing found */
*/
{
const char *compat_name = NULL;
- char *ptr;
+ const char *ptr;
struct translate_struct *trans;
unsigned int i;
char mode;
}
}
- ptr = (strrchr(devname, '/') + 1);
+ ptr = bb_basename(devname);
i = scan_dev_name(devname, namelen, ptr);
if (i > 0 && i < 13)
if (col) {
/* This line is a dep description */
- char *mods;
+ const char *mods;
char *modpath;
char *mod;
/* Find the beginning of the module file name */
*col = 0;
- mods = strrchr(buffer, '/');
-
- if (!mods)
- mods = buffer; /* no path for this module */
- else
- mods++; /* there was a path for this module... */
+ mods = bb_basename(buffer);
/* find the path of the module */
modpath = strchr(buffer, '/'); /* ... and this is the path */
/* p points to the first dependable module; if NULL, no dependable module */
if (p && *p) {
char *end = &buffer[l-1];
- char *deps;
+ const char *deps;
char *dep;
char *next;
int ext = 0;
next = end;
/* find the beginning of the module file name */
- deps = strrchr(p, '/');
-
- if (!deps || (deps < p)) {
- deps = p;
-
+ deps = bb_basename(p);
+ if (deps == p) {
while (isblank(*deps))
deps++;
- } else
- deps++;
+ }
/* find the end of the module name in the file name */
if (ENABLE_FEATURE_2_6_MODULES
for (n = optind; n < argc; n++) {
if (ENABLE_FEATURE_2_6_MODULES) {
- const char *afterslash;
-
- afterslash = strrchr(argv[n], '/');
- if (!afterslash) afterslash = argv[n];
- else afterslash++;
- filename2modname(misc_buf, afterslash);
+ filename2modname(misc_buf, bb_basename(argv[n]));
}
if (syscall(__NR_delete_module, ENABLE_FEATURE_2_6_MODULES ? misc_buf : argv[n], flags)) {
setenv1("HTTP_REFERER", referer);
/* set execve argp[0] without path */
- argp[0] = strrchr(purl, '/') + 1;
+ argp[0] = (char*)bb_basename(purl);
/* but script argp[0] must have absolute path and chdiring to this */
script = strrchr(fullpath, '/');
if (!script)
if (alarm_signaled)
break;
- if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
+ if (strcmp(bb_basename(url), httpd_conf) == 0 || ip_allowed == 0) {
/* protect listing [/path]/httpd_conf or IP deny */
#if ENABLE_FEATURE_HTTPD_CGI
FORBIDDEN: /* protect listing /cgi-bin */
/* mknod in /dev based on a path like "/sys/block/hda/hda1" */
static void make_device(char *path, int delete)
{
- char *device_name;
+ const char *device_name;
int major, minor, type, len;
int mode = 0660;
uid_t uid = 0;
/* Determine device name, type, major and minor */
- device_name = strrchr(path, '/') + 1;
+ device_name = bb_basename(path);
type = path[5]=='c' ? S_IFCHR : S_IFBLK;
/* If we have a config file, look up permissions for this device */