-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
125 lines (114 loc) · 3.85 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mhufflep <mhufflep@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/24 18:24:43 by mhufflep #+# #+# */
/* Updated: 2021/04/04 17:22:00 by mhufflep ### ########.fr */
/* */
/* ************************************************************************** */
#include "libasm.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
void test_strdup_and_copy(const char *str, int len)
{
char *copy;
char *temp;
copy = malloc(sizeof(char) * len);
if (!copy)
return ;
printf("\n=============================================> strdup\n");
temp = ft_strdup(str);
printf("ft_strdup:|%s|\n", temp);
free(temp);
temp = strdup(str);
printf("strdup:|%s|\n", temp);
free(temp);
printf("\n=============================================> strcpy\n");
printf("ft_strcpy:|%s|\n", ft_strcpy(copy, str));
printf("strcpy:|%s|\n", strcpy(copy, str));
}
void clean_buf(char *buf, int len)
{
while (--len >= 0)
{
buf[len] = '\0';
}
}
void test_read(void)
{
char buf[2000];
int fd;
errno = 0;
clean_buf(buf, 2000);
fd = open("file", O_RDONLY);
printf("\n=============================================> read\n");
printf("| read(0, NULL, 1000)|return: %zd |", ft_read(0, &buf[0], 1000));
printf("errno:%d\n", errno);
printf("buf:%s\n", buf);
printf("-----------------------------------------------\n\n");
errno = 0;
printf("| read(fd, NULL, 1000)|return: %zd |", ft_read(fd, &buf[0], 1000));
printf("errno:%d\n", errno);
printf("buf:%s\n", buf);
printf("-----------------------------------------------\n\n");
errno = 0;
printf("| read(fd, NULL, 1000)|return: %zd |", ft_read(fd, NULL, 1000));
printf("errno:%d\n", errno);
printf("-----------------------------------------------\n\n");
errno = 0;
printf("| read(fd, buf, -1)|return: %zd |", ft_read(fd, buf, -1));
printf("errno:%d\n", errno);
printf("-----------------------------------------------\n\n");
}
void test_write(const char *str)
{
int fd;
int res;
int len;
len = ft_strlen(str);
errno = 0;
printf("\n=============================================> write\n");
fd = open("file", O_CREAT | O_TRUNC | O_WRONLY, 0777);
res = ft_write(fd, str, len);
printf("| write(fd, str, len) | return: %d | ", res);
printf("errno:%d\n", errno);
printf("------------------------------------------------\n\n");
errno = 0;
res = ft_write(fd, NULL, len);
printf("| write(fd, NULL, len) | return: %d | ", res);
printf("errno:%d\n", errno);
printf("------------------------------------------------\n\n");
errno = 0;
res = ft_write(fd, str, -1);
printf("| write(fd, str, -1) | return: %d | ", res);
printf("errno:%d\n", errno);
printf("------------------------------------------------\n\n");
}
int main(int argc, char **argv)
{
int len;
if (argc >= 2)
{
len = ft_strlen(argv[1]) + 1;
printf("\n=============================================> strlen\n");
printf("ft_strlen:|%zu|\n", ft_strlen(argv[1]));
printf("strlen:|%zu|\n", strlen(argv[1]));
if (argc == 3)
{
printf("\n=============================================> strcmp\n");
printf("ft_strcmp:|%i|\n", ft_strcmp(argv[1], argv[2]));
printf("strcmp:|%i|\n", strcmp(argv[1], argv[2]));
}
test_strdup_and_copy(argv[1], len);
test_write(argv[1]);
test_read();
}
return (0);
}