stat: code shrink; stop using bss; stop using strncat incorrectly

function                                             old     new   delta
printfs                                                -      28     +28
strcatc                                                -      26     +26
human_time                                            41      44      +3
print_it                                             229     219     -10
buf                                                   30       -     -30
print_statfs                                         420     358     -62
print_stat                                          1089     921    -168
------------------------------------------------------------------------------
(add/remove: 2/1 grow/shrink: 1/3 up/down: 57/-270)          Total: -213 bytes
   text    data     bss     dec     hex filename
 771105    1029    9552  781686   bed76 busybox_old
 770892    1029    9520  781441   bec81 busybox_unstripped
1_8_stable
Denis Vlasenko 2007-10-05 20:31:23 +00:00
parent 91e80c2be7
commit 91e5203205
1 changed files with 97 additions and 94 deletions

View File

@ -21,9 +21,7 @@
#define OPT_DEREFERENCE (1 << 2) #define OPT_DEREFERENCE (1 << 2)
#define OPT_SELINUX (1 << 3) #define OPT_SELINUX (1 << 3)
static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1; static const char *file_type(const struct stat *st)
static char const * file_type(struct stat const *st)
{ {
/* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107 /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107
* for some of these formats. * for some of these formats.
@ -46,7 +44,7 @@ static char const * file_type(struct stat const *st)
return "weird file"; return "weird file";
} }
static char const *human_time(time_t t) static const char *human_time(time_t t)
{ {
/* Old /* Old
static char *str; static char *str;
@ -56,8 +54,12 @@ static char const *human_time(time_t t)
*/ */
/* coreutils 6.3 compat: */ /* coreutils 6.3 compat: */
/*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
#define buf bb_common_bufsiz1
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t)); strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
return buf; return buf;
#undef buf
} }
/* Return the type of the specified file system. /* Return the type of the specified file system.
@ -65,11 +67,10 @@ static char const *human_time(time_t t)
* Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2) * Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
* Still others have neither and have to get by with f_type (Linux). * Still others have neither and have to get by with f_type (Linux).
*/ */
static char const *human_fstype(long f_type) static const char *human_fstype(uint32_t f_type)
{ {
int i;
static const struct types { static const struct types {
long type; uint32_t type;
const char *const fs; const char *const fs;
} humantypes[] = { } humantypes[] = {
{ 0xADFF, "affs" }, { 0xADFF, "affs" },
@ -109,66 +110,79 @@ static char const *human_fstype(long f_type)
{ 0x62656572, "sysfs" }, { 0x62656572, "sysfs" },
{ 0, "UNKNOWN" } { 0, "UNKNOWN" }
}; };
int i;
for (i = 0; humantypes[i].type; ++i) for (i = 0; humantypes[i].type; ++i)
if (humantypes[i].type == f_type) if (humantypes[i].type == f_type)
break; break;
return humantypes[i].fs; return humantypes[i].fs;
} }
static void strcatc(char *str, char c)
{
int len = strlen(str);
str[len++] = c;
str[len] = '\0';
}
static void printfs(char *pformat, const char *msg)
{
strcatc(pformat, 's');
printf(pformat, msg);
}
#if ENABLE_FEATURE_STAT_FORMAT #if ENABLE_FEATURE_STAT_FORMAT
/* print statfs info */ /* print statfs info */
static void print_statfs(char *pformat, const size_t buf_len, const char m, static void print_statfs(char *pformat, const char m,
const char *const filename, void const *data const char *const filename, const void *data
USE_SELINUX(, security_context_t scontext)) USE_SELINUX(, security_context_t scontext))
{ {
struct statfs const *statfsbuf = data; const struct statfs *statfsbuf = data;
if (m == 'n') { if (m == 'n') {
strncat(pformat, "s", buf_len); printfs(pformat, filename);
printf(pformat, filename);
} else if (m == 'i') { } else if (m == 'i') {
strncat(pformat, "Lx", buf_len); strcat(pformat, "Lx");
printf(pformat, statfsbuf->f_fsid); printf(pformat, statfsbuf->f_fsid);
} else if (m == 'l') { } else if (m == 'l') {
strncat(pformat, "lu", buf_len); strcat(pformat, "lu");
printf(pformat, statfsbuf->f_namelen); printf(pformat, statfsbuf->f_namelen);
} else if (m == 't') { } else if (m == 't') {
strncat(pformat, "lx", buf_len); strcat(pformat, "lx");
printf(pformat, (unsigned long) (statfsbuf->f_type)); /* no equiv */ printf(pformat, (unsigned long) (statfsbuf->f_type)); /* no equiv */
} else if (m == 'T') { } else if (m == 'T') {
strncat(pformat, "s", buf_len); printfs(pformat, human_fstype(statfsbuf->f_type));
printf(pformat, human_fstype(statfsbuf->f_type));
} else if (m == 'b') { } else if (m == 'b') {
strncat(pformat, "jd", buf_len); strcat(pformat, "jd");
printf(pformat, (intmax_t) (statfsbuf->f_blocks)); printf(pformat, (intmax_t) (statfsbuf->f_blocks));
} else if (m == 'f') { } else if (m == 'f') {
strncat(pformat, "jd", buf_len); strcat(pformat, "jd");
printf(pformat, (intmax_t) (statfsbuf->f_bfree)); printf(pformat, (intmax_t) (statfsbuf->f_bfree));
} else if (m == 'a') { } else if (m == 'a') {
strncat(pformat, "jd", buf_len); strcat(pformat, "jd");
printf(pformat, (intmax_t) (statfsbuf->f_bavail)); printf(pformat, (intmax_t) (statfsbuf->f_bavail));
} else if (m == 's' || m == 'S') { } else if (m == 's' || m == 'S') {
strncat(pformat, "lu", buf_len); strcat(pformat, "lu");
printf(pformat, (unsigned long) (statfsbuf->f_bsize)); printf(pformat, (unsigned long) (statfsbuf->f_bsize));
} else if (m == 'c') { } else if (m == 'c') {
strncat(pformat, "jd", buf_len); strcat(pformat, "jd");
printf(pformat, (intmax_t) (statfsbuf->f_files)); printf(pformat, (intmax_t) (statfsbuf->f_files));
} else if (m == 'd') { } else if (m == 'd') {
strncat(pformat, "jd", buf_len); strcat(pformat, "jd");
printf(pformat, (intmax_t) (statfsbuf->f_ffree)); printf(pformat, (intmax_t) (statfsbuf->f_ffree));
#if ENABLE_SELINUX #if ENABLE_SELINUX
} else if (m == 'C' && (option_mask32 & OPT_SELINUX)) { } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
strncat(pformat, "s", buf_len); printfs(pformat, scontext);
printf(scontext);
#endif #endif
} else { } else {
strncat(pformat, "c", buf_len); strcatc(pformat, 'c');
printf(pformat, m); printf(pformat, m);
} }
} }
/* print stat info */ /* print stat info */
static void print_stat(char *pformat, const size_t buf_len, const char m, static void print_stat(char *pformat, const char m,
const char *const filename, void const *data const char *const filename, const void *data
USE_SELINUX(, security_context_t scontext)) USE_SELINUX(, security_context_t scontext))
{ {
#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
@ -177,131 +191,118 @@ static void print_stat(char *pformat, const size_t buf_len, const char m,
struct group *gw_ent; struct group *gw_ent;
if (m == 'n') { if (m == 'n') {
strncat(pformat, "s", buf_len); printfs(pformat, filename);
printf(pformat, filename);
} else if (m == 'N') { } else if (m == 'N') {
strncat(pformat, "s", buf_len); strcatc(pformat, 's');
if (S_ISLNK(statbuf->st_mode)) { if (S_ISLNK(statbuf->st_mode)) {
char *linkname = xmalloc_readlink_or_warn(filename); char *linkname = xmalloc_readlink_or_warn(filename);
if (linkname == NULL) { if (linkname == NULL)
bb_perror_msg("cannot read symbolic link '%s'", filename);
return; return;
}
/*printf("\"%s\" -> \"%s\"", filename, linkname); */ /*printf("\"%s\" -> \"%s\"", filename, linkname); */
printf(pformat, filename); printf(pformat, filename);
printf(" -> "); printf(" -> ");
printf(pformat, linkname); printf(pformat, linkname);
free(linkname);
} else { } else {
printf(pformat, filename); printf(pformat, filename);
} }
} else if (m == 'd') { } else if (m == 'd') {
strncat(pformat, "ju", buf_len); strcat(pformat, "ju");
printf(pformat, (uintmax_t) statbuf->st_dev); printf(pformat, (uintmax_t) statbuf->st_dev);
} else if (m == 'D') { } else if (m == 'D') {
strncat(pformat, "jx", buf_len); strcat(pformat, "jx");
printf(pformat, (uintmax_t) statbuf->st_dev); printf(pformat, (uintmax_t) statbuf->st_dev);
} else if (m == 'i') { } else if (m == 'i') {
strncat(pformat, "ju", buf_len); strcat(pformat, "ju");
printf(pformat, (uintmax_t) statbuf->st_ino); printf(pformat, (uintmax_t) statbuf->st_ino);
} else if (m == 'a') { } else if (m == 'a') {
strncat(pformat, "lo", buf_len); strcat(pformat, "lo");
printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO))); printf(pformat, (unsigned long) (statbuf->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)));
} else if (m == 'A') { } else if (m == 'A') {
strncat(pformat, "s", buf_len); printfs(pformat, bb_mode_string(statbuf->st_mode));
printf(pformat, bb_mode_string(statbuf->st_mode));
} else if (m == 'f') { } else if (m == 'f') {
strncat(pformat, "lx", buf_len); strcat(pformat, "lx");
printf(pformat, (unsigned long) statbuf->st_mode); printf(pformat, (unsigned long) statbuf->st_mode);
} else if (m == 'F') { } else if (m == 'F') {
strncat(pformat, "s", buf_len); printfs(pformat, file_type(statbuf));
printf(pformat, file_type(statbuf));
} else if (m == 'h') { } else if (m == 'h') {
strncat(pformat, "lu", buf_len); strcat(pformat, "lu");
printf(pformat, (unsigned long) statbuf->st_nlink); printf(pformat, (unsigned long) statbuf->st_nlink);
} else if (m == 'u') { } else if (m == 'u') {
strncat(pformat, "lu", buf_len); strcat(pformat, "lu");
printf(pformat, (unsigned long) statbuf->st_uid); printf(pformat, (unsigned long) statbuf->st_uid);
} else if (m == 'U') { } else if (m == 'U') {
strncat(pformat, "s", buf_len);
setpwent(); setpwent();
pw_ent = getpwuid(statbuf->st_uid); pw_ent = getpwuid(statbuf->st_uid);
printf(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN"); printfs(pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
} else if (m == 'g') { } else if (m == 'g') {
strncat(pformat, "lu", buf_len); strcat(pformat, "lu");
printf(pformat, (unsigned long) statbuf->st_gid); printf(pformat, (unsigned long) statbuf->st_gid);
} else if (m == 'G') { } else if (m == 'G') {
strncat(pformat, "s", buf_len);
setgrent(); setgrent();
gw_ent = getgrgid(statbuf->st_gid); gw_ent = getgrgid(statbuf->st_gid);
printf(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN"); printfs(pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
} else if (m == 't') { } else if (m == 't') {
strncat(pformat, "lx", buf_len); strcat(pformat, "lx");
printf(pformat, (unsigned long) major(statbuf->st_rdev)); printf(pformat, (unsigned long) major(statbuf->st_rdev));
} else if (m == 'T') { } else if (m == 'T') {
strncat(pformat, "lx", buf_len); strcat(pformat, "lx");
printf(pformat, (unsigned long) minor(statbuf->st_rdev)); printf(pformat, (unsigned long) minor(statbuf->st_rdev));
} else if (m == 's') { } else if (m == 's') {
strncat(pformat, "ju", buf_len); strcat(pformat, "ju");
printf(pformat, (uintmax_t) (statbuf->st_size)); printf(pformat, (uintmax_t) (statbuf->st_size));
} else if (m == 'B') { } else if (m == 'B') {
strncat(pformat, "lu", buf_len); strcat(pformat, "lu");
printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE printf(pformat, (unsigned long) 512); //ST_NBLOCKSIZE
} else if (m == 'b') { } else if (m == 'b') {
strncat(pformat, "ju", buf_len); strcat(pformat, "ju");
printf(pformat, (uintmax_t) statbuf->st_blocks); printf(pformat, (uintmax_t) statbuf->st_blocks);
} else if (m == 'o') { } else if (m == 'o') {
strncat(pformat, "lu", buf_len); strcat(pformat, "lu");
printf(pformat, (unsigned long) statbuf->st_blksize); printf(pformat, (unsigned long) statbuf->st_blksize);
} else if (m == 'x') { } else if (m == 'x') {
strncat(pformat, "s", buf_len); printfs(pformat, human_time(statbuf->st_atime));
printf(pformat, human_time(statbuf->st_atime));
} else if (m == 'X') { } else if (m == 'X') {
strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len); strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
printf(pformat, (unsigned long) statbuf->st_atime); printf(pformat, (unsigned long) statbuf->st_atime);
} else if (m == 'y') { } else if (m == 'y') {
strncat(pformat, "s", buf_len); printfs(pformat, human_time(statbuf->st_mtime));
printf(pformat, human_time(statbuf->st_mtime));
} else if (m == 'Y') { } else if (m == 'Y') {
strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len); strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
printf(pformat, (unsigned long) statbuf->st_mtime); printf(pformat, (unsigned long) statbuf->st_mtime);
} else if (m == 'z') { } else if (m == 'z') {
strncat(pformat, "s", buf_len); printfs(pformat, human_time(statbuf->st_ctime));
printf(pformat, human_time(statbuf->st_ctime));
} else if (m == 'Z') { } else if (m == 'Z') {
strncat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu", buf_len); strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
printf(pformat, (unsigned long) statbuf->st_ctime); printf(pformat, (unsigned long) statbuf->st_ctime);
#if ENABLE_SELINUX #if ENABLE_SELINUX
} else if (m == 'C' && (option_mask32 & OPT_SELINUX)) { } else if (m == 'C' && (option_mask32 & OPT_SELINUX)) {
strncat(pformat, "s", buf_len); printfs(pformat, scontext);
printf(pformat, scontext);
#endif #endif
} else { } else {
strncat(pformat, "c", buf_len); strcatc(pformat, 'c');
printf(pformat, m); printf(pformat, m);
} }
} }
static void print_it(char const *masterformat, char const *filename, static void print_it(const char *masterformat, const char *filename,
void (*print_func) (char *, size_t, char, char const *, void const * void (*print_func) (char*, char, const char*, const void* USE_SELINUX(, security_context_t scontext)),
USE_SELINUX(, security_context_t scontext)), const void *data
void const *data USE_SELINUX(, security_context_t scontext) ) USE_SELINUX(, security_context_t scontext) )
{ {
char *b; /* Create a working copy of the format string */
/* create a working copy of the format string */
char *format = xstrdup(masterformat); char *format = xstrdup(masterformat);
/* Add 2 to accomodate our conversion of the stat '%s' format string /* Add 2 to accomodate our conversion of the stat '%s' format string
* to the printf '%llu' one. */ * to the printf '%llu' one. */
size_t n_alloc = strlen(format) + 2 + 1; char *dest = xmalloc(strlen(format) + 2 + 1);
char *dest = xmalloc(n_alloc); char *b;
b = format; b = format;
while (b) { while (b) {
size_t len; size_t len;
char *p = strchr(b, '%'); char *p = strchr(b, '%');
if (!p) { if (!p) {
/* coreutils 6.3 always print <cr> at the end */ /* coreutils 6.3 always prints <cr> at the end */
/*fputs(b, stdout);*/ /*fputs(b, stdout);*/
puts(b); puts(b);
break; break;
@ -309,10 +310,11 @@ static void print_it(char const *masterformat, char const *filename,
*p++ = '\0'; *p++ = '\0';
fputs(b, stdout); fputs(b, stdout);
/* dest = "%<modifiers>" */
len = strspn(p, "#-+.I 0123456789"); len = strspn(p, "#-+.I 0123456789");
dest[0] = '%'; dest[0] = '%';
memcpy(dest + 1, p, len); memcpy(dest + 1, p, len);
dest[1 + len] = 0; dest[1 + len] = '\0';
p += len; p += len;
b = p + 1; b = p + 1;
@ -324,7 +326,8 @@ static void print_it(char const *masterformat, char const *filename,
bb_putchar('%'); bb_putchar('%');
break; break;
default: default:
print_func(dest, n_alloc, *p, filename, data USE_SELINUX(,scontext)); /* Completes "%<modifiers>" with specifier and printfs */
print_func(dest, *p, filename, data USE_SELINUX(,scontext));
break; break;
} }
} }
@ -335,7 +338,7 @@ static void print_it(char const *masterformat, char const *filename,
#endif #endif
/* Stat the file system and print what we find. */ /* Stat the file system and print what we find. */
static bool do_statfs(char const *filename, char const *format) static bool do_statfs(const char *filename, const char *format)
{ {
struct statfs statfsbuf; struct statfs statfsbuf;
#if ENABLE_SELINUX #if ENABLE_SELINUX
@ -358,7 +361,7 @@ static bool do_statfs(char const *filename, char const *format)
} }
#if ENABLE_FEATURE_STAT_FORMAT #if ENABLE_FEATURE_STAT_FORMAT
if (format == NULL) if (format == NULL) {
#if !ENABLE_SELINUX #if !ENABLE_SELINUX
format = (option_mask32 & OPT_TERSE format = (option_mask32 & OPT_TERSE
? "%n %i %l %t %s %b %f %a %c %d\n" ? "%n %i %l %t %s %b %f %a %c %d\n"
@ -367,7 +370,6 @@ static bool do_statfs(char const *filename, char const *format)
"Block size: %-10s\n" "Block size: %-10s\n"
"Blocks: Total: %-10b Free: %-10f Available: %a\n" "Blocks: Total: %-10b Free: %-10f Available: %a\n"
"Inodes: Total: %-10c Free: %d"); "Inodes: Total: %-10c Free: %d");
print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
#else #else
format = (option_mask32 & OPT_TERSE format = (option_mask32 & OPT_TERSE
? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n": ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n":
@ -385,8 +387,9 @@ static bool do_statfs(char const *filename, char const *format)
"Blocks: Total: %-10b Free: %-10f Available: %a\n" "Blocks: Total: %-10b Free: %-10f Available: %a\n"
"Inodes: Total: %-10c Free: %d\n") "Inodes: Total: %-10c Free: %d\n")
); );
print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
#endif /* SELINUX */ #endif /* SELINUX */
}
print_it(format, filename, print_statfs, &statfsbuf USE_SELINUX(, scontext));
#else /* FEATURE_STAT_FORMAT */ #else /* FEATURE_STAT_FORMAT */
format = (option_mask32 & OPT_TERSE format = (option_mask32 & OPT_TERSE
? "%s %llx %lu " ? "%s %llx %lu "
@ -444,7 +447,7 @@ static bool do_statfs(char const *filename, char const *format)
} }
/* stat the file and print what we find */ /* stat the file and print what we find */
static bool do_stat(char const *filename, char const *format) static bool do_stat(const char *filename, const char *format)
{ {
struct stat statbuf; struct stat statbuf;
#if ENABLE_SELINUX #if ENABLE_SELINUX
@ -612,7 +615,7 @@ int stat_main(int argc, char **argv)
char *format = NULL; char *format = NULL;
int i; int i;
int ok = 1; int ok = 1;
bool (*statfunc)(char const *, char const *) = do_stat; bool (*statfunc)(const char *, const char *) = do_stat;
getopt32(argv, "ftL" getopt32(argv, "ftL"
USE_SELINUX("Z") USE_SELINUX("Z")