-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_func.c
130 lines (115 loc) · 1.81 KB
/
my_func.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
128
129
/*
** my_func.c for bsq in /home/sayyou_y/rendu/CPE-Prog-Elem/CPE_2014_bsq
**
** Made by Youssef Sayyouri
** Login <sayyou_y@epitech.net>
**
** Started on Wed Jan 14 12:02:48 2015 Youssef Sayyouri
** Last update Tue Feb 10 18:46:51 2015 Youssef Sayyouri
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "include/bsq.h"
void my_putchar(char c)
{
write(1, &c, 1);
}
void my_putstr(char *str)
{
int i;
i = 0;
if (str == NULL)
my_putstr("Error: NULL value is prohibited");
else
{
while (str[i])
my_putchar(str[i++]);
}
}
int my_strlen(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
i++;
return (i);
}
int my_getnbr(char *str)
{
int i;
int nbr;
int neg;
i = 0;
nbr = 0;
neg = 1;
if (str == NULL)
return (0);
while (str[i] == '-')
{
neg = neg * -1;
i++;
}
while (str[i] != '\0')
{
if (str[i] < '0' || str[i] > '9')
return (nbr * neg);
nbr = nbr * 10 + (str[i] - '0');
i++;
}
return (nbr * neg);
}
void *my_malloc(size_t s)
{
char *ptr;
ptr = malloc(s);
if (ptr == NULL)
{
my_putstr("Error: malloc failed\n");
exit(1);
}
return (ptr);
}
void show_int_tab(char **char_tab, int **int_tab)
{
int i;
int j;
i = 0;
j = 0;
while (char_tab[i])
{
j = 0;
while (char_tab[i][j])
{
my_putnbr(int_tab[i][j]);
my_putchar(' ');
j++;
}
my_putchar('\n');
i++;
}
}
void my_putnbr(int nbr)
{
int neg;
neg = 0;
if (nbr < 0)
{
my_putchar('-');
if (nbr == -2147483648)
{
neg = 1;
nbr++;
}
nbr = nbr * -1;
}
if (nbr >= 10)
my_putnbr(nbr / 10);
if (neg == 1)
{
neg = 0;
my_putchar(nbr % 10 + '1');
}
else
my_putchar(nbr % 10 + '0');
}