source: branches/stable/mindi-busybox/applets/busybox.c @ 821

Last change on this file since 821 was 821, checked in by Bruno Cornec, 14 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

File size: 3.7 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*
3 * BusyBox' main applet dispatcher.
4 *
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6 */
7#include "busybox.h"
8#include <stdio.h>
9#include <string.h>
10#include <unistd.h>
11#include <errno.h>
12#include <stdlib.h>
13#if ENABLE_LOCALE_SUPPORT
14#include <locale.h>
15#else
16#define setlocale(x,y)
17#endif
18
19const char *bb_applet_name ATTRIBUTE_EXTERNALLY_VISIBLE;
20
21#ifdef CONFIG_FEATURE_INSTALLER
22/*
23 * directory table
24 *      this should be consistent w/ the enum, busybox.h::Location,
25 *      or else...
26 */
27static const char usr_bin [] ="/usr/bin";
28static const char usr_sbin[] ="/usr/sbin";
29
30static const char* const install_dir[] = {
31    &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
32    &usr_bin [4], /* "/bin" */
33    &usr_sbin[4], /* "/sbin" */
34    usr_bin,
35    usr_sbin
36};
37
38/* abstract link() */
39typedef int (*__link_f)(const char *, const char *);
40
41/* create (sym)links for each applet */
42static void install_links(const char *busybox, int use_symbolic_links)
43{
44    __link_f Link = link;
45
46    char *fpc;
47    int i;
48    int rc;
49
50    if (use_symbolic_links)
51        Link = symlink;
52
53    for (i = 0; applets[i].name != NULL; i++) {
54        fpc = concat_path_file(
55            install_dir[applets[i].location], applets[i].name);
56        rc = Link(busybox, fpc);
57        if (rc!=0 && errno!=EEXIST) {
58            bb_perror_msg("%s", fpc);
59        }
60        free(fpc);
61    }
62}
63
64#else
65#define install_links(x,y)
66#endif /* CONFIG_FEATURE_INSTALLER */
67
68int main(int argc, char **argv)
69{
70    const char *s;
71
72    bb_applet_name=argv[0];
73    if (*bb_applet_name == '-') bb_applet_name++;
74    for (s = bb_applet_name; *s ;)
75        if (*(s++) == '/') bb_applet_name = s;
76
77    /* Set locale for everybody except `init' */
78    if(ENABLE_LOCALE_SUPPORT && getpid() != 1)
79        setlocale(LC_ALL, "");
80
81    run_applet_by_name(bb_applet_name, argc, argv);
82    bb_error_msg_and_die("applet not found");
83}
84
85int busybox_main(int argc, char **argv)
86{
87    /*
88     * This style of argument parsing doesn't scale well
89     * in the event that busybox starts wanting more --options.
90     * If someone has a cleaner approach, by all means implement it.
91     */
92    if (ENABLE_FEATURE_INSTALLER && argc > 1 && !strcmp(argv[1], "--install")) {
93        int use_symbolic_links = 0;
94        int rc = 0;
95        char *busybox;
96
97        /* to use symlinks, or not to use symlinks... */
98        if (argc > 2) {
99            if ((strcmp(argv[2], "-s") == 0)) {
100                use_symbolic_links = 1;
101            }
102        }
103
104        /* link */
105        busybox = xreadlink("/proc/self/exe");
106        if (busybox) {
107            install_links(busybox, use_symbolic_links);
108            free(busybox);
109        } else {
110            rc = 1;
111        }
112        return rc;
113    }
114
115    /* Deal with --help.  (Also print help when called with no arguments) */
116
117    if (argc==1 || !strcmp(argv[1],"--help") ) {
118        if (argc>2) {
119            run_applet_by_name(bb_applet_name=argv[2], 2, argv);
120        } else {
121            const struct BB_applet *a;
122            int col, output_width;
123
124            if (ENABLE_FEATURE_AUTOWIDTH) {
125                /* Obtain the terminal width.  */
126                get_terminal_width_height(0, &output_width, NULL);
127                /* leading tab and room to wrap */
128                output_width -= 20;
129            } else output_width = 60;
130
131            printf("%s\n\n"
132                   "Usage: busybox [function] [arguments]...\n"
133                   "   or: [function] [arguments]...\n\n"
134                   "\tBusyBox is a multi-call binary that combines many common Unix\n"
135                   "\tutilities into a single executable.  Most people will create a\n"
136                   "\tlink to busybox for each function they wish to use and BusyBox\n"
137                   "\twill act like whatever it was invoked as!\n"
138                   "\nCurrently defined functions:\n", bb_msg_full_version);
139
140            col=0;
141            for(a = applets; a->name;) {
142                col += printf("%s%s", (col ? ", " : "\t"), (a++)->name);
143                if (col > output_width && a->name) {
144                    printf(",\n");
145                    col = 0;
146                }
147            }
148            printf("\n\n");
149            exit(0);
150        }
151    } else run_applet_by_name(argv[1], argc-1, argv+1);
152
153    bb_error_msg_and_die("applet not found");
154}
Note: See TracBrowser for help on using the repository browser.