aboutsummaryrefslogtreecommitdiff
path: root/miscutils/dc.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-03-17 09:29:43 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-03-17 09:29:43 +0000
commitb44c790e41e281965955a83408f35ea53ecdb3d2 (patch)
tree98514dba64f7b74b9d60882bfeca1e2c4976b033 /miscutils/dc.c
parentd25c33f186c7cf3618c34ce79af94fe156f9ab02 (diff)
downloadbusybox-w32-b44c790e41e281965955a83408f35ea53ecdb3d2.tar.gz
busybox-w32-b44c790e41e281965955a83408f35ea53ecdb3d2.tar.bz2
busybox-w32-b44c790e41e281965955a83408f35ea53ecdb3d2.zip
*: fix various kinds of damage to letter 'c' in Arkadiusz Mickiewicz' surname.
chrt,taskset,dc,eject: shrink crontab: call "vi" with just name, no path watchdog: intercept all fatal signals function old new delta xstrtoull_range - 35 +35 static.sg_commands - 18 +18 packed_usage 23698 23712 +14 watchdog_main 148 151 +3 crontab_main 637 638 +1 base 1 - -1 static.ps 4 - -4 pointer 4 - -4 stack_machine 99 92 -7 run_command 273 260 -13 eject_main 360 343 -17 static.C 30 12 -18 ptok 61 38 -23 xstrtol_range 27 - -27 get_token 35 - -35 taskset_main 586 550 -36 chrt_main 411 372 -39 dc_main 158 117 -41 time_main 1127 1037 -90 ------------------------------------------------------------------------------ (add/remove: 2/5 grow/shrink: 3/9 up/down: 71/-355) Total: -284 bytes text data bss dec hex filename 793680 662 7420 801762 c3be2 busybox_old 793327 662 7412 801401 c3a79 busybox_unstripped
Diffstat (limited to 'miscutils/dc.c')
-rw-r--r--miscutils/dc.c94
1 files changed, 43 insertions, 51 deletions
diff --git a/miscutils/dc.c b/miscutils/dc.c
index ffc3f8df4..68ecd8a3f 100644
--- a/miscutils/dc.c
+++ b/miscutils/dc.c
@@ -8,11 +8,18 @@
8 8
9/* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */ 9/* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */
10 10
11enum { STACK_SIZE = COMMON_BUFSIZE / sizeof(double) };
12 11
13#define stack ((double*)&bb_common_bufsiz1) 12struct globals {
14static unsigned int pointer; 13 unsigned pointer;
15static unsigned char base; 14 unsigned base;
15 double stack[1];
16};
17enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
18#define G (*(struct globals*)&bb_common_bufsiz1)
19#define pointer (G.pointer )
20#define base (G.base )
21#define stack (G.stack )
22
16 23
17static void push(double a) 24static void push(double a)
18{ 25{
@@ -61,34 +68,34 @@ static void divide(void)
61 68
62static void mod(void) 69static void mod(void)
63{ 70{
64 unsigned int d = pop(); 71 unsigned d = pop();
65 72
66 push((unsigned int) pop() % d); 73 push((unsigned) pop() % d);
67} 74}
68 75
69static void and(void) 76static void and(void)
70{ 77{
71 push((unsigned int) pop() & (unsigned int) pop()); 78 push((unsigned) pop() & (unsigned) pop());
72} 79}
73 80
74static void or(void) 81static void or(void)
75{ 82{
76 push((unsigned int) pop() | (unsigned int) pop()); 83 push((unsigned) pop() | (unsigned) pop());
77} 84}
78 85
79static void eor(void) 86static void eor(void)
80{ 87{
81 push((unsigned int) pop() ^ (unsigned int) pop()); 88 push((unsigned) pop() ^ (unsigned) pop());
82} 89}
83 90
84static void not(void) 91static void not(void)
85{ 92{
86 push(~(unsigned int) pop()); 93 push(~(unsigned) pop());
87} 94}
88 95
89static void set_output_base(void) 96static void set_output_base(void)
90{ 97{
91 base = (unsigned char)pop(); 98 base = (unsigned)pop();
92 if ((base != 10) && (base != 16)) { 99 if ((base != 10) && (base != 16)) {
93 bb_error_msg("error, base %d is not supported", base); 100 bb_error_msg("error, base %d is not supported", base);
94 base = 10; 101 base = 10;
@@ -98,14 +105,14 @@ static void set_output_base(void)
98static void print_base(double print) 105static void print_base(double print)
99{ 106{
100 if (base == 16) 107 if (base == 16)
101 printf("%x\n", (unsigned int)print); 108 printf("%x\n", (unsigned)print);
102 else 109 else
103 printf("%g\n", print); 110 printf("%g\n", print);
104} 111}
105 112
106static void print_stack_no_pop(void) 113static void print_stack_no_pop(void)
107{ 114{
108 unsigned int i = pointer; 115 unsigned i = pointer;
109 while (i) 116 while (i)
110 print_base(stack[--i]); 117 print_base(stack[--i]);
111} 118}
@@ -142,12 +149,12 @@ static const struct op operators[] = {
142 {"p", print_no_pop}, 149 {"p", print_no_pop},
143 {"f", print_stack_no_pop}, 150 {"f", print_stack_no_pop},
144 {"o", set_output_base}, 151 {"o", set_output_base},
145 {"", 0} 152 { /* zero filled */ }
146}; 153};
147 154
148static void stack_machine(const char *argument) 155static void stack_machine(const char *argument)
149{ 156{
150 char *endPointer = 0; 157 char *endPointer;
151 double d; 158 double d;
152 const struct op *o = operators; 159 const struct op *o = operators;
153 160
@@ -176,54 +183,39 @@ static void stack_machine(const char *argument)
176 */ 183 */
177static char *get_token(char **buffer) 184static char *get_token(char **buffer)
178{ 185{
179 char *start = NULL; 186 char *current = skip_whitespace(*buffer);
180 char *current; 187 if (*current != '\0') {
181 188 *buffer = skip_non_whitespace(current);
182 current = skip_whitespace(*buffer); 189 return current;
183 if (*current != 0) {
184 start = current;
185 current = skip_non_whitespace(current);
186 *buffer = current;
187 } 190 }
188 return start; 191 return NULL;
189}
190
191/* In Perl one might say, scalar m|\s*(\S+)\s*|g */
192static int number_of_tokens(char *buffer)
193{
194 int i = 0;
195 char *b = buffer;
196 while (get_token(&b)) { i++; }
197 return i;
198} 192}
199 193
200int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 194int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
201int dc_main(int argc, char **argv) 195int dc_main(int argc ATTRIBUTE_UNUSED, char **argv)
202{ 196{
203 /* take stuff from stdin if no args are given */ 197 argv++;
204 if (argc <= 1) { 198 if (!argv[0]) {
205 int i, len; 199 /* take stuff from stdin if no args are given */
206 char *line = NULL; 200 char *line;
207 char *cursor = NULL; 201 char *cursor;
208 char *token = NULL; 202 char *token;
209 while ((line = xmalloc_getline(stdin))) { 203 while ((line = xmalloc_getline(stdin)) != NULL) {
210 cursor = line; 204 cursor = line;
211 len = number_of_tokens(line); 205 while (1) {
212 for (i = 0; i < len; i++) {
213 token = get_token(&cursor); 206 token = get_token(&cursor);
214 *cursor++ = 0; 207 if (!token) break;
208 *cursor++ = '\0';
215 stack_machine(token); 209 stack_machine(token);
216 } 210 }
217 free(line); 211 free(line);
218 } 212 }
219 } else { 213 } else {
220 if (*argv[1] == '-') 214 if (argv[0][0] == '-')
221 bb_show_usage(); 215 bb_show_usage();
222 while (argc >= 2) { 216 do {
223 stack_machine(argv[1]); 217 stack_machine(*argv);
224 argv++; 218 } while (*++argv);
225 argc--;
226 }
227 } 219 }
228 return EXIT_SUCCESS; 220 return EXIT_SUCCESS;
229} 221}