add libbb functions
This commit is contained in:
parent
0aab147904
commit
d97304aef2
|
@ -0,0 +1,9 @@
|
||||||
|
|
||||||
|
#ifndef __LIBBB_H
|
||||||
|
#define __LIBBB_H
|
||||||
|
char *concat_path_file(const char *path, const char *filename);
|
||||||
|
int execable_file(const char *name);
|
||||||
|
char *find_execable(const char *filename);
|
||||||
|
char* last_char_is(const char *s, int c);
|
||||||
|
|
||||||
|
#endif /* __LIBBB_H */
|
|
@ -11,6 +11,7 @@ obj-y += xfuncs.o
|
||||||
obj-y += getopt.o
|
obj-y += getopt.o
|
||||||
obj-y += readkey.o
|
obj-y += readkey.o
|
||||||
obj-y += kfifo.o
|
obj-y += kfifo.o
|
||||||
|
obj-y += libbb.o
|
||||||
obj-$(CONFIG_BZLIB) += bzlib.o bzlib_crctable.o bzlib_decompress.o bzlib_huffman.o bzlib_randtable.o
|
obj-$(CONFIG_BZLIB) += bzlib.o bzlib_crctable.o bzlib_decompress.o bzlib_huffman.o bzlib_randtable.o
|
||||||
obj-$(CONFIG_ZLIB) += zlib.o gunzip.o
|
obj-$(CONFIG_ZLIB) += zlib.o gunzip.o
|
||||||
obj-$(CONFIG_CRC32) += crc32.o
|
obj-$(CONFIG_CRC32) += crc32.o
|
||||||
|
|
|
@ -0,0 +1,87 @@
|
||||||
|
/* vi: set sw=8 ts=8: */
|
||||||
|
/*
|
||||||
|
* Utility routines.
|
||||||
|
*
|
||||||
|
* Copyright (C) many different people.
|
||||||
|
* If you wrote this, please acknowledge your work.
|
||||||
|
*
|
||||||
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <common.h>
|
||||||
|
#include <libbb.h>
|
||||||
|
#include <linux/stat.h>
|
||||||
|
#include <fs.h>
|
||||||
|
#include <xfuncs.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
#include <environment.h>
|
||||||
|
|
||||||
|
/* concatenate path and file name to new allocation buffer,
|
||||||
|
* not adding '/' if path name already has '/'
|
||||||
|
*/
|
||||||
|
char *concat_path_file(const char *path, const char *filename)
|
||||||
|
{
|
||||||
|
char *lc, *str;
|
||||||
|
|
||||||
|
if (!path)
|
||||||
|
path = "";
|
||||||
|
lc = last_char_is(path, '/');
|
||||||
|
while (*filename == '/')
|
||||||
|
filename++;
|
||||||
|
|
||||||
|
str = xmalloc(strlen(path) + (lc==0 ? 1 : 0) + strlen(filename) + 1);
|
||||||
|
sprintf(str, "%s%s%s", path, (lc==NULL ? "/" : ""), filename);
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* check if path points to an executable file;
|
||||||
|
* return 1 if found;
|
||||||
|
* return 0 otherwise;
|
||||||
|
*/
|
||||||
|
int execable_file(const char *name)
|
||||||
|
{
|
||||||
|
struct stat s;
|
||||||
|
return (!stat(name, &s) && S_ISREG(s.st_mode));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* search $PATH for an executable file;
|
||||||
|
* return allocated string containing full path if found;
|
||||||
|
* return NULL otherwise;
|
||||||
|
*/
|
||||||
|
char *find_execable(const char *filename)
|
||||||
|
{
|
||||||
|
char *path, *p, *n;
|
||||||
|
|
||||||
|
p = path = strdup(getenv("PATH"));
|
||||||
|
while (p) {
|
||||||
|
n = strchr(p, ':');
|
||||||
|
if (n)
|
||||||
|
*n++ = '\0';
|
||||||
|
if (*p != '\0') { /* it's not a PATH="foo::bar" situation */
|
||||||
|
p = concat_path_file(p, filename);
|
||||||
|
if (execable_file(p)) {
|
||||||
|
free(path);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
free(p);
|
||||||
|
}
|
||||||
|
p = n;
|
||||||
|
}
|
||||||
|
free(path);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Find out if the last character of a string matches the one given.
|
||||||
|
* Don't underrun the buffer if the string length is 0.
|
||||||
|
*/
|
||||||
|
char* last_char_is(const char *s, int c)
|
||||||
|
{
|
||||||
|
if (s && *s) {
|
||||||
|
size_t sz = strlen(s) - 1;
|
||||||
|
s += sz;
|
||||||
|
if ( (unsigned char)*s == c)
|
||||||
|
return (char*)s;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
Loading…
Reference in New Issue