Fixed 'ls -s' so it actually displays block sizes again.
[oweals/busybox.git] / utility.c
index f3c184e7308f6d91dcfe2622843814f6db4eb809..89b601d04cf60bb2be414d5c3a8afcbca5bf4abc 100644 (file)
--- a/utility.c
+++ b/utility.c
@@ -40,6 +40,7 @@
 #define bb_need_full_version
 #define BB_DECLARE_EXTERN
 #include "messages.c"
+#include "usage.h"
 
 #include <stdio.h>
 #include <string.h>
@@ -76,12 +77,29 @@ const char mtab_file[] = "/proc/mounts";
 #  endif
 #endif
 
-extern void usage(const char *usage)
+static struct BB_applet *applet_using;
+
+extern void show_usage(void)
 {
-       fprintf(stderr, "%s\n\nUsage: %s\n\n", full_version, usage);
+       static const char no_help[] = "No help available.\n";
+
+       const char *usage_string = no_help;
+       int i;
+
+       if (applet_using->usage_index >= 0) {
+               usage_string = usage_messages;
+               for (i=applet_using->usage_index ; i>0 ; ) {
+                       if (!*usage_string++) {
+                               --i;
+                       }
+               }
+       }
+       fprintf(stderr, "%s\n\nUsage: %s %s\n\n", full_version,
+                       applet_using->name, usage_string);
        exit(EXIT_FAILURE);
 }
 
+
 static void verror_msg(const char *s, va_list p)
 {
        fflush(stdout);
@@ -1377,6 +1395,14 @@ extern char * xstrndup (const char *s, int n) {
 }
 #endif
 
+#if defined BB_IFCONFIG || defined BB_ROUTE
+/* Like strncpy but make sure the resulting string is always 0 terminated. */  
+extern char * safe_strncpy(char *dst, const char *src, size_t size)
+{   
+       dst[size-1] = '\0';
+       return strncpy(dst, src, size-1);   
+}
+#endif
 
 #if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
 extern int vdprintf(int d, const char *format, va_list ap)
@@ -1694,6 +1720,17 @@ struct BB_applet *find_applet_by_name(const char *name)
                        applet_name_compare);
 }
 
+void run_applet_by_name(const char *name, int argc, char **argv)
+{
+       /* Do a binary search to find the applet entry given the name. */
+       if ((applet_using = find_applet_by_name(name)) != NULL) {
+               applet_name = applet_using->name;
+               if (argv[1] && strcmp(argv[1], "--help") == 0)
+                       show_usage();
+               exit((*(applet_using->main)) (argc, argv));
+       }
+}
+
 #if defined BB_DD || defined BB_TAIL
 unsigned long parse_number(const char *numstr,
                const struct suffix_mult *suffixes)