This repository has been archived by the owner on Jun 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·127 lines (118 loc) · 2.64 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
** main.c for Bistromatique in /home/barrau_h/Desktop/Bistromatique FINAL/Bistromatique
**
** Made by Hippolyte Barraud
** Login <barrau_h@epitech.net>
**
** Started on Sun Nov 9 06:07:38 2014 Hippolyte Barraud
** Last update Sat Nov 15 14:01:26 2014 Hippolyte Barraud
*/
#include"include/bistromatique.h"
/*
** Helper function. Given by the suject.
*/
int my_atoi(char *c)
{
int value = 0;
int sign = 1;
if (*c == '+' || *c == '-')
{
if (*c == '-')
sign = -1;
c++;
}
while (*c >= '0' && *c <= '9')
{
value *= 10;
value += (int)(*c - '0');
c++;
}
return (value * sign);
}
/*
** Helper function. Given by the suject.
*/
void check_base(char *b)
{
if (my_strlen(b) < 2)
print_fatal_error(1);
}
/*
** The get_expr given with the subject was real crap. Because the data received
** by this program can be arbitrary long, making a single call to read is
** stupid, for it relies on the hope that the data will be all there at once.
** Instead, we use a buffer to collect what we can until everything is there.
**
** SINCE THE COLLE OF 13/11/2014, this function have been corrected to avoid
** the SIGSEGV discovered back then. The changes are minors, but criticals.
*/
char *get_expr(unsigned int size)
{
char *expr;
char *buffer;
int ret;
unsigned long c;
int buff_s;
buff_s = 9990000;
if (size < 9990000)
buff_s = size;
c = 0;
ret = 0;
if (size <= 1)
print_fatal_error(2);
buffer = malloc_or_die(sizeof(char) * buff_s + 1);
expr = malloc_or_die(sizeof(char) * size + 1);
expr[0] = 0;
buffer[0] = 0;
while (c < size)
{
ret = read(0, buffer, buff_s - 1);
if (ret == -1)
print_fatal_error(4);
else if (ret == 0)
break;
buffer[ret] = 0;
c = c + ret;
my_strcat(expr, buffer);
}
expr[size] = 0;
free(buffer);
return (expr);
}
/*
** Helper function. Given by the suject.
*/
void check_ops(char *ops)
{
if (my_strlen(ops) != 7)
print_fatal_error(5);
}
/*
** Main function. Given by the subject.
** When you use this program to compute very large numbers, use the -p
** parramater to enable progress bar display.
** Ex : ./calc "01" "()+-x/%" 999999999 -p
*/
int main(int ac, char **av)
{
unsigned int size;
char *expr;
int p;
p = 0;
if (ac != 4 && ac != 5)
{
my_putstr("Usage : ");
my_putstr(av[0]);
my_putstr(" base (\"0123456789\") ops (\"()+-*/%\") exp_len\n");
exit(1);
}
else if (ac == 5 && strcmp(av[4], "-p") == 0)
p = 1;
check_base(av[1]);
check_ops(av[2]);
size = my_atoi(av[3]);
expr = get_expr(size);
eval_expr(av[1], av[2], expr, size, p);
free(expr);
exit(0);
}