-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
100 lines (91 loc) · 1.66 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
/*
** main.c for Projet-Bistro in /home/giallo_n/git/Projet-Bistro/res
**
** Made by nathan giallombardo
** Login <giallo_n@epitech.net>
**
** Started on Sat Nov 9 16:49:46 2013 nathan giallombardo
** Last update Sun Nov 10 19:42:58 2013 nathan giallombardo
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "bistromathique.h"
#include "my.h"
int my_atoi(char *);
static void check_base(char *base);
static void check_ops(char *ops);
static char *get_expr(unsigned size);
int main(int ac, char **av)
{
char *expr;
unsigned int size;
if (ac != 4)
{
my_putstr("Usage : ");
my_putstr(av[0]);
my_putstr(" base ops\"()+-*/%\" exp_len\n");
exit(1);
}
check_base(av[1]);
check_ops(av[2]);
size = my_atoi(av[3]);
expr = get_expr(size);
my_putstr(eval_expr(av[1], av[2], expr, size));
return (0);
}
static void check_base(char *b)
{
if (my_strlen(b) < 2)
{
my_putstr("Bad base\n");
exit(1);
}
}
static char *get_expr(unsigned int size)
{
char *expr;
if (size <= 0)
{
my_putstr("Bad expr len\n");
exit(2);
}
expr = malloc(size+1);
if (expr == 0)
{
my_putstr("could not alloc\n");
exit(3);
}
if (read(0, expr, size) != size)
{
my_putstr("could not read\n");
exit(4);
}
expr[size] = 0;
return (expr);
}
static void check_ops(char *ops)
{
if (my_strlen(ops) != 7)
{
my_putstr("Bad ops\n");
exit(5);
}
}
int my_atoi(char *str)
{
int val;
val = 0;
while(*str)
{
if (*str <= '0' && *str >= '9')
{
val = val * 10;
val = val + *str - '0';
}
else
return (val);
*str = *str + 1;
}
return (val);
}