annotate main.c @ 912:f4f5132d5ac7

Stat cleanup. From the mailing list: Ok, first thing: clean up the help text. I realize what's there is copied verbatim from the man page, but that man page sucks. ("modification time" vs "change time"?) Took a bit of finagling to fit it in 80x24, but just made it. GLOBALS() indent was still tab, change to two spaces. And I tend to put a blank line between options lib/args.c automatically fills out and normal globals. We never do anything with date_stat_format() but immediately print it, might as well make the function do it. The types[] array in do_stat() is a rough edge. Hmmm... there's no else case that sets the type in case it was unknown (such as 0). In theory, this never happens. In practice it means I can cheat slightly, given this observation: $ find linux -name stat.h | xargs grep 'S_IF[A-Z]*[ \t]' linux/include/uapi/linux/stat.h:#define S_IFMT 00170000 linux/include/uapi/linux/stat.h:#define S_IFSOCK 0140000 linux/include/uapi/linux/stat.h:#define S_IFLNK 0120000 linux/include/uapi/linux/stat.h:#define S_IFREG 0100000 linux/include/uapi/linux/stat.h:#define S_IFBLK 0060000 linux/include/uapi/linux/stat.h:#define S_IFDIR 0040000 linux/include/uapi/linux/stat.h:#define S_IFCHR 0020000 linux/include/uapi/linux/stat.h:#define S_IFIFO 0010000 I.E. the only place the I_IFBLAH constants occur a stat.h header in current linux code is in the generic stuff, it doesn't vary per target. (The access permission bits are actually subtly standardized in posix due to the command line arguments to chmod, although I'm sure cygwin finds a way to break. But the type fields, not so much. But linux has to be binary compatible with itself foreverish, and that's all I really care about.) So, we have ALMOST have this going by twos, except there's no 8 and there is a 1. so let's make the 1 the default, feed a blank string into the 8... No, duh: octal. So it's actually 2, 4, 6, 8, 10, 12. So make the loop look like: filetype = statf->st_mode & S_IFMT; TT.ftname = types; for (i = 1; filetype != (i*8192) && i < 7; i++) TT.ftname += strlen(TT.ftname)+1; Yes that's linux-specific, and I think I'm ok with that. Printing all zeroes and pretending that's nanosecond resolution... either support it or don't. Let's see, supporting it is stat->st_atim.tv_nsec and similar... no mention of nanoseconds in strftime() (et tu, posix2008?) so pass it as a second argument and append it by hand... (Need to test that against musl...) When we hit an unknown type in print_it() we print the literal character, which is right for %% but what about an unknown option? $ stat -c %q / ? Eh, I guess that's a "don't care". It didn't die with an error, that's the important thing. I have a horrible idea for compressing the switch/case blocks, but should probably check this in and get some sleep for right now...
author Rob Landley <rob@landley.net>
date Tue, 28 May 2013 00:28:45 -0500
parents 60a430b7791e
children 812e8c5d026f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
1 /* Toybox infrastructure.
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
2 *
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
3 * Copyright 2006 Rob Landley <rob@landley.net>
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
4 */
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
5
1
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
6 #include "toys.h"
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
7
21
6475d6c46066 Add pwd. Consolidate toy list information under toylist.h.
Rob Landley <rob@landley.net>
parents: 20
diff changeset
8 // Populate toy_list[].
1
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
9
88
021fe1a818c3 Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents: 65
diff changeset
10 #undef NEWTOY
021fe1a818c3 Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents: 65
diff changeset
11 #undef OLDTOY
021fe1a818c3 Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents: 65
diff changeset
12 #define NEWTOY(name, opts, flags) {#name, name##_main, opts, flags},
021fe1a818c3 Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents: 65
diff changeset
13 #define OLDTOY(name, oldname, opts, flags) {#name, oldname##_main, opts, flags},
021fe1a818c3 Small cleanup to prepare for cross-compile friendly make install.
Rob Landley <rob@landley.net>
parents: 65
diff changeset
14
1
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
15 struct toy_list toy_list[] = {
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
16 #include "generated/newtoys.h"
1
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
17 };
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
18
873
d90f14e011b8 Fix some comments from way back when toybox first started (in 2006), when I was still cleaning busybox-isms out of my head...
Rob Landley <rob@landley.net>
parents: 782
diff changeset
19 // global context for this command.
1
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
20
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
21 struct toy_context toys;
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
22 union global_union this;
53
41d55b5d49fd Add start of mke2fs/gene2fs, and some other stuff I've been working on.
Rob Landley <rob@landley.net>
parents: 25
diff changeset
23 char toybuf[4096];
1
59d58fab67c6 Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents: 0
diff changeset
24
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
25 struct toy_list *toy_find(char *name)
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
26 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
27 int top, bottom, middle;
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
28
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
29 // If the name starts with "toybox" accept that as a match. Otherwise
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
30 // skip the first entry, which is out of order.
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
31
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
32 if (!strncmp(name,"toybox",6)) return toy_list;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
33 bottom = 1;
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
34
873
d90f14e011b8 Fix some comments from way back when toybox first started (in 2006), when I was still cleaning busybox-isms out of my head...
Rob Landley <rob@landley.net>
parents: 782
diff changeset
35 // Binary search to find this command.
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
36
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
37 top = ARRAY_LEN(toy_list)-1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
38 for (;;) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
39 int result;
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 138
diff changeset
40
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
41 middle = (top+bottom)/2;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
42 if (middle<bottom || middle>top) return NULL;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
43 result = strcmp(name,toy_list[middle].name);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
44 if (!result) return toy_list+middle;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
45 if (result<0) top=--middle;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
46 else bottom = ++middle;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
47 }
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
48 }
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
49
122
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
50 // Figure out whether or not anything is using the option parsing logic,
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
51 // because the compiler can't figure out whether or not to optimize it away
402
2551e517b800 Expand comments.
Rob Landley <rob@landley.net>
parents: 373
diff changeset
52 // on its' own. NEED_OPTIONS becomes a constant allowing if() to optimize
2551e517b800 Expand comments.
Rob Landley <rob@landley.net>
parents: 373
diff changeset
53 // stuff out via dead code elimination.
122
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
54
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
55 #undef NEWTOY
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
56 #undef OLDTOY
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
57 #define NEWTOY(name, opts, flags) opts ||
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
58 #define OLDTOY(name, oldname, opts, flags) opts ||
125
a4af344fb349 Make warning go away.
Rob Landley <rob@landley.net>
parents: 122
diff changeset
59 static const int NEED_OPTIONS =
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 197
diff changeset
60 #include "generated/newtoys.h"
122
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
61 0; // Ends the opts || opts || opts...
ee49aa0dc731 Show the compiler how to optimize out the option parsing logic when nothing
Rob Landley <rob@landley.net>
parents: 121
diff changeset
62
402
2551e517b800 Expand comments.
Rob Landley <rob@landley.net>
parents: 373
diff changeset
63 // Setup toybox global state for this command.
2551e517b800 Expand comments.
Rob Landley <rob@landley.net>
parents: 373
diff changeset
64
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
65 void toy_init(struct toy_list *which, char *argv[])
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
66 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
67 // Drop permissions for non-suid commands.
370
c7a26e26ad08 Add TOYBOX_SUID.
Rob Landley <rob@landley.net>
parents: 257
diff changeset
68
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
69 if (CFG_TOYBOX_SUID) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
70 uid_t uid = getuid(), euid = geteuid();
370
c7a26e26ad08 Add TOYBOX_SUID.
Rob Landley <rob@landley.net>
parents: 257
diff changeset
71
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
72 if (!(which->flags & TOYFLAG_STAYROOT)) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
73 if (uid != euid) xsetuid(euid=uid);
769
098ab2654fa0 TOYBOX_DEBUG warns about lack of suid bit when running a STAYROOT command, but it shouldn't warn just because the multiplexer command "toybox" is stayroot.
Rob Landley <rob@landley.net>
parents: 761
diff changeset
74 } else if (CFG_TOYBOX_DEBUG && uid && which != toy_list)
098ab2654fa0 TOYBOX_DEBUG warns about lack of suid bit when running a STAYROOT command, but it shouldn't warn just because the multiplexer command "toybox" is stayroot.
Rob Landley <rob@landley.net>
parents: 761
diff changeset
75 error_msg("Not installed suid root");
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
76
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
77 if ((which->flags & TOYFLAG_NEEDROOT) && euid) error_exit("Not root");
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
78 }
370
c7a26e26ad08 Add TOYBOX_SUID.
Rob Landley <rob@landley.net>
parents: 257
diff changeset
79
696
99ca30ad3d2b Add rebound support to intercept error_exit() and longjmp instead.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
80 // Free old toys contents (to be reentrant), but leave rebound if any
370
c7a26e26ad08 Add TOYBOX_SUID.
Rob Landley <rob@landley.net>
parents: 257
diff changeset
81
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
82 if (toys.optargs != toys.argv+1) free(toys.optargs);
696
99ca30ad3d2b Add rebound support to intercept error_exit() and longjmp instead.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
83 memset(&toys, 0, offsetof(struct toy_context, rebound));
373
5e68c7cab1a4 Make toy_init() reentrant, or else xexec() has funky errors.
Rob Landley <rob@landley.net>
parents: 370
diff changeset
84
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
85 toys.which = which;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
86 toys.argv = argv;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
87 if (NEED_OPTIONS && which->options) get_optflags();
697
ca9a1d8e2531 Set optc when optstring NULL.
Rob Landley <rob@landley.net>
parents: 696
diff changeset
88 else {
ca9a1d8e2531 Set optc when optstring NULL.
Rob Landley <rob@landley.net>
parents: 696
diff changeset
89 toys.optargs = argv+1;
ca9a1d8e2531 Set optc when optstring NULL.
Rob Landley <rob@landley.net>
parents: 696
diff changeset
90 for (toys.optc=0; toys.optargs[toys.optc]; toys.optc++);
ca9a1d8e2531 Set optc when optstring NULL.
Rob Landley <rob@landley.net>
parents: 696
diff changeset
91 }
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
92 toys.old_umask = umask(0);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
93 if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask);
3
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
94 }
266a462ed18c Next drop of toysh, plus more infratructure.
landley@driftwood
parents: 2
diff changeset
95
402
2551e517b800 Expand comments.
Rob Landley <rob@landley.net>
parents: 373
diff changeset
96 // Like exec() but runs an internal toybox command instead of another file.
2551e517b800 Expand comments.
Rob Landley <rob@landley.net>
parents: 373
diff changeset
97 // Only returns if it can't find the command, otherwise exit() when done.
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
98 void toy_exec(char *argv[])
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
99 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
100 struct toy_list *which;
121
933766b0bd4b Allow applets with optarg string NULL to use toy.optargs[].
Rob Landley <rob@landley.net>
parents: 93
diff changeset
101
895
60a430b7791e Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents: 873
diff changeset
102 if (!(which = toy_find(argv[0]))) return;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
103 toy_init(which, argv);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
104 toys.which->toy_main();
761
6a558bf5de65 Elie De Brauwer pointed out that xputs() isn't reliably reporting errors because there's no flush. Rather than change the output granularity, flush before exit and check errors there. (We still need xputc() doing it so "yes" doesn't continue forever.)
Rob Landley <rob@landley.net>
parents: 712
diff changeset
105 if (fflush(NULL) || ferror(stdout)) perror_exit("write");
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
106 exit(toys.exitval);
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
107 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
108
402
2551e517b800 Expand comments.
Rob Landley <rob@landley.net>
parents: 373
diff changeset
109 // Multiplexer command, first argument is command to run, rest are args to that.
2551e517b800 Expand comments.
Rob Landley <rob@landley.net>
parents: 373
diff changeset
110 // If first argument starts with - output list of command install paths.
2551e517b800 Expand comments.
Rob Landley <rob@landley.net>
parents: 373
diff changeset
111
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 182
diff changeset
112 void toybox_main(void)
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
113 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
114 static char *toy_paths[]={"usr/","bin/","sbin/",0};
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
115 int i, len = 0;
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
116
704
e45ab88c477d Init global context to toybox multiplexer early so error_exit() doesn't segfault trying to print current command name, and change TOYBOX_DEBUG+TOYBOX_SUID complaint about not having the suid bit set to warning rather than exit..
Rob Landley <rob@landley.net>
parents: 697
diff changeset
117 toys.which = toy_list;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
118 if (toys.argv[1]) {
895
60a430b7791e Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents: 873
diff changeset
119 if (CFG_TOYBOX_HELP && !strcmp(toys.argv[1], "--help")) {
60a430b7791e Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents: 873
diff changeset
120 if (toys.argv[2]) toys.which = toy_find(toys.argv[2]);
60a430b7791e Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents: 873
diff changeset
121 if (toys.which) {
60a430b7791e Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents: 873
diff changeset
122 show_help();
60a430b7791e Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents: 873
diff changeset
123 return;
60a430b7791e Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents: 873
diff changeset
124 }
60a430b7791e Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents: 873
diff changeset
125 } else {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
126 toy_exec(toys.argv+1);
895
60a430b7791e Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents: 873
diff changeset
127 if (toys.argv[1][0] == '-') goto list;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
128 }
895
60a430b7791e Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents: 873
diff changeset
129
60a430b7791e Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents: 873
diff changeset
130 error_exit("Unknown command %s",toys.argv[1]);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
131 }
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
132
895
60a430b7791e Add --help option to toybox command when TOYBOX_HELP is enabled.
Rob Landley <rob@landley.net>
parents: 873
diff changeset
133 list:
873
d90f14e011b8 Fix some comments from way back when toybox first started (in 2006), when I was still cleaning busybox-isms out of my head...
Rob Landley <rob@landley.net>
parents: 782
diff changeset
134 // Output list of command.
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
135 for (i=1; i<ARRAY_LEN(toy_list); i++) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
136 int fl = toy_list[i].flags;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
137 if (fl & TOYMASK_LOCATION) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
138 if (toys.argv[1]) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
139 int j;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
140 for (j=0; toy_paths[j]; j++)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
141 if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
142 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
143 len += printf("%s ",toy_list[i].name);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
144 if (len>65) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
145 xputc('\n');
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
146 len=0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
147 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
148 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
149 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
150 xputc('\n');
2
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
151 }
67b517913e56 Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents: 1
diff changeset
152
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
153 int main(int argc, char *argv[])
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
154 {
712
a950dd960593 Cleanup i18n support (#ifdefectomy, move global init to process launch). Teach make.sh to emit "#define FLAG_x 0" for options inside disabled USE macros so we can unconditionally refer to them.
Rob Landley <rob@landley.net>
parents: 710
diff changeset
155 if (CFG_TOYBOX_I18N) setlocale(LC_ALL, "");
a950dd960593 Cleanup i18n support (#ifdefectomy, move global init to process launch). Teach make.sh to emit "#define FLAG_x 0" for options inside disabled USE macros so we can unconditionally refer to them.
Rob Landley <rob@landley.net>
parents: 710
diff changeset
156
782
3d7526f6115b Use basename() where appropriate.
Rob Landley <rob@landley.net>
parents: 769
diff changeset
157 // Trim path off of command name
3d7526f6115b Use basename() where appropriate.
Rob Landley <rob@landley.net>
parents: 769
diff changeset
158 *argv = basename(*argv);
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
159
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
160 // Call the multiplexer, adjusting this argv[] to be its' argv[1].
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
161 // (It will adjust it back before calling toy_exec().)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
162 toys.argv = argv-1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
163 toybox_main();
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 647
diff changeset
164 return 0;
0
e651a31d5416 Starting a new project. Just a bit past the "hello world" stage...
landley@driftwood
parents:
diff changeset
165 }