fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / dejagnu / example / calc / calc.c
blob119971a2c22cca205c78cc9970c40bfa8c638572
1 #include "calc.h"
3 #ifdef HAVE_STDLIB_H
4 #include <stdlib.h>
5 #endif
6 #include <ctype.h>
7 #include <stdio.h>
9 static int words();
11 #define NWORD 10
12 #define SIZE 100
13 #define VERSION "1.1"
15 int main()
17 char line[SIZE];
18 int nword;
19 char *words[NWORD];
21 while(printf("calc: "), fflush(stdout), fgets(line,SIZE,stdin) != NULL) {
22 if((nword = split(line,words,NWORD)) == 0) continue;
23 if(strcmp(words[0],"add") == 0) {
24 if(nword != 3) {
25 printf("Usage: add #1 #2\n");
26 } else {
27 printf("%d",atoi(words[1]) + atoi(words[2]));
29 } else if(strcmp(words[0],"multiply") == 0) {
30 if(nword != 3) {
31 printf("Usage: multiply #1 #2\n");
32 } else {
33 int i1 = atoi(words[1]);
34 if(i1 == 2) i1 = 3; /* this is a bug */
35 printf("%d",i1*atoi(words[2]));
37 } else if(strcmp(words[0],"quit") == 0) {
38 break;
39 } else if(strcmp(words[0],"version") == 0) {
40 printf("Version: %s",VERSION);
41 } else {
42 printf("Unknown command: %s",words[0]);
44 printf("\n");
47 return(0);
50 int
51 split(line,words,nword)
52 char *line;
53 char **words;
54 int nword; /* number of elements in words */
56 int i;
58 while(isspace(*line)) line++;
59 if(*line == '\0') return(0);
61 for(i = 0;i < nword;i++) {
62 words[i] = line;
63 while(*line != '\0' && !isspace(*line)) line++;
64 if(*line == '\0') break;
65 *line++ = '\0';
66 while(isspace(*line)) line++;
69 return(i);