Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/miscutils/dc.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/miscutils/dc.c
r821 r1770 1 1 /* vi: set sw=4 ts=4: */ 2 #include "busybox.h" 3 #include <ctype.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <unistd.h> 2 /* 3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 4 */ 5 6 #include "libbb.h" 8 7 #include <math.h> 9 8 10 9 /* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */ 11 10 12 static double stack[100]; 11 enum { STACK_SIZE = COMMON_BUFSIZE / sizeof(double) }; 12 13 #define stack ((double*)&bb_common_bufsiz1) 13 14 static unsigned int pointer; 14 15 static unsigned char base; … … 16 17 static void push(double a) 17 18 { 18 if (pointer >= (sizeof(stack) / sizeof(*stack)))19 if (pointer >= STACK_SIZE) 19 20 bb_error_msg_and_die("stack overflow"); 20 21 stack[pointer++] = a; … … 88 89 static void set_output_base(void) 89 90 { 90 base =(unsigned char)pop();91 base = (unsigned char)pop(); 91 92 if ((base != 10) && (base != 16)) { 92 fprintf(stderr, "Error: base = %d is not supported.\n", base);93 base =10;93 bb_error_msg("error, base %d is not supported", base); 94 base = 10; 94 95 } 95 96 } … … 100 101 printf("%x\n", (unsigned int)print); 101 102 else 102 printf("%g\n", print);103 printf("%g\n", print); 103 104 } 104 105 105 106 static void print_stack_no_pop(void) 106 107 { 107 unsigned int i =pointer;108 unsigned int i = pointer; 108 109 while (i) 109 110 print_base(stack[--i]); … … 116 117 117 118 struct op { 118 const char *name;119 const char name[4]; 119 120 void (*function) (void); 120 121 }; … … 142 143 {"f", print_stack_no_pop}, 143 144 {"o", set_output_base}, 144 { 0,0}145 {"", 0} 145 146 }; 146 147 … … 161 162 } 162 163 163 while (o->name != 0) {164 while (o->name[0]) { 164 165 if (strcmp(o->name, argument) == 0) { 165 (*(o->function))();166 o->function(); 166 167 return; 167 168 } 168 169 o++; 169 170 } 170 bb_error_msg_and_die("%s: syntax error .", argument);171 bb_error_msg_and_die("%s: syntax error", argument); 171 172 } 172 173 … … 176 177 static char *get_token(char **buffer) 177 178 { 178 char *start 179 char *current = *buffer;180 181 while (isspace(*current)) { current++; }179 char *start = NULL; 180 char *current; 181 182 current = skip_whitespace(*buffer); 182 183 if (*current != 0) { 183 184 start = current; 184 while (!isspace(*current) && *current != 0) { current++; }185 current = skip_non_whitespace(current); 185 186 *buffer = current; 186 187 } … … 197 198 } 198 199 200 int dc_main(int argc, char **argv); 199 201 int dc_main(int argc, char **argv) 200 202 { … … 205 207 char *cursor = NULL; 206 208 char *token = NULL; 207 while ((line = bb_get_chomped_line_from_file(stdin))) {209 while ((line = xmalloc_getline(stdin))) { 208 210 cursor = line; 209 211 len = number_of_tokens(line); … … 216 218 } 217 219 } else { 218 if (*argv[1] =='-')220 if (*argv[1] == '-') 219 221 bb_show_usage(); 220 222 while (argc >= 2) {
Note:
See TracChangeset
for help on using the changeset viewer.