-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
160 lines (139 loc) · 5.1 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: krios-fu <krios-fu@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/05 19:38:21 by krios-fu #+# #+# */
/* Updated: 2021/01/05 19:38:27 by krios-fu ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/errno.h>
#include <stdlib.h>
size_t ft_strlen(char *);
char *ft_strcpy(char *dst, const char *src);
int ft_strcmp(const char *s1, const char *s2);
ssize_t ft_write(int, const void *, size_t);
ssize_t ft_read(int, void *, size_t);
char *ft_strdup(const char *);
int main()
{
/* STRLEN */
printf("strlen ret: %4zu\n", strlen("holaaaa\n"));
printf("ft_strlen ret: %zu\n", ft_strlen("holaaaa\n"));
printf("strlen ret: %4zu\n", strlen(""));
printf("ft_strlen ret: %zu\n\n", ft_strlen(""));
/* STRCPY */
char *src = "ktal";
char dst1[5];
char dst2[5];
// char *ret = ft_strcpy(str2, src);
// printf("str2 dir: %p\n", str2);
// printf("strcpy ret: %p\n", ret);
printf("strcpy: %s\n", strcpy(dst1, "ho"));
printf("ft_strcpy: %s\n", ft_strcpy(dst2, "ho"));
printf("strcpy: %s\n", strcpy(dst1, src));
printf("ft_strcpy: %s\n", ft_strcpy(dst2, src));
printf("strcpy: %s\n", strcpy(dst1, "hola"));
printf("ft_strcpy: %s\n", ft_strcpy(dst2, "hola"));
// printf("strcpy: %s\n", strcpy(dst1, "holaa"));
// printf("ft_strcpy: %s\n", ft_strcpy(dst2, "holaa"));
char test[8] = "asdfghj";
char test2[8] = "asdfghj";
printf("strcpy: %s\n", strcpy(test2, "hola"));
printf("ft_strcpy: %s\n\n", ft_strcpy(test, "hola"));
// printf("strcpy: %s\n", strcpy("asl;fjl;asdjfjkasdl;fjadjsf", "asl;fjl;asdjfjkasdl;fjadjsf"));
// printf("ft_strcpy: %s\n", ft_strcpy("asl;fjl;asdjfjkasdl;fjadjsf", "asl;fjl;asdjfjkasdl;fjadjsf"));
/* STRCMP */
char *uno = "hola";
char *dos = "holaa";
printf("strcmp: %d\n", strcmp("hola", "holaa"));
printf("strcmp: %d\n", strcmp(uno, dos));
printf("ft_strcmp: %d\n", ft_strcmp(uno, dos));
printf("strcmp: %d\n", strcmp(uno, dos));
printf("ft_strcmp: %d\n", ft_strcmp(uno, dos));
printf("strcmp: %d\n", strcmp("hola", "hol"));
printf("ft_strcmp: %d\n", ft_strcmp("hola", "hol"));
printf("strcmp: %d\n", strcmp("hola", ""));
printf("ft_strcmp: %d\n", ft_strcmp("hola", ""));
printf("strcmp: %d\n", strcmp("hola", "hola"));
printf("ft_strcmp: %d\n", ft_strcmp("hola", "hola"));
printf("strcmp: %d\n", strcmp("", ""));
printf("ft_strcmp: %d\n", ft_strcmp("", ""));
printf("strcmp: %d\n", strcmp("hola", "adios"));
printf("ft_strcmp: %d\n", ft_strcmp("hola", "adios"));
//printf("\xff\xff\n\x31\n");
printf("strcmp: %d\n", strcmp("\xff\xff", "\xff"));
printf("ft_strcmp: %d\n", ft_strcmp("\xff\xff", "\xff"));
printf("strcmp: %d\n", strcmp("\xff", ""));
printf("ft_strcmp: %d\n\n", ft_strcmp("\xff", ""));
/* FT_WRITE */
printf("write: %zd\n", write(1, "hola\n", 5));
printf("ft_write: %zd\n", ft_write(1, "hola\n", 5));
printf("\nwrite: %zd\n", write(1, "hola\n", 3));
printf("\nft_write: %zd\n", ft_write(1, "hola\n", 3));
int a = write(FOPEN_MAX + 1, "abcdefghijklmnopqrstuvwxyz\n", 27);
perror("write errno");
errno = 100;
int b = ft_write(FOPEN_MAX + 1, "abcdefghijklmnopqrstuvwxyz\n", 27);
perror("ft_write errno");
printf("return write = %d\nreturn ft_write = %d\n\n", a, b);
/* FT_READ */
char buf[10];
char buf2[10];
char buf3[10];
char buf4[10];
int fd = open("readthis", O_RDONLY);
printf("read: %4zd\n", read(fd, buf, 9));
printf("%s\n", buf);
close(fd);
fd = open("readthis", O_RDONLY);
printf("ft_read: %zd\n", ft_read(fd, buf2, 9));
printf("%s\n", buf2);
close(fd);
errno = 100;
a = read(FOPEN_MAX, buf, 9);
perror("read errno");
errno = 100;
b = ft_read(FOPEN_MAX, buf, 9);
perror("ft_read errno");
printf("return read = %d\nreturn ft_read = %d\n", a, b);
errno = 100;
fd = open("readths", O_RDONLY);
a = read(fd, buf3, 9);
perror("read errno");
close(fd);
errno = 100;
fd = open("readths", O_RDONLY);
b = ft_read(fd, buf4, 9);
perror("ft_read errno");
printf("return read = %d\nreturn ft_read = %d\n", a, b);
printf("read: %s\nft_read: %s\n\n", buf3, buf4);
/* FT_STRDUP */
char src1[] = "hola";
char *dst;
dst = strdup(src1);
printf("strdup: %s\n", dst);
free(dst);
dst = ft_strdup(src1);
printf("ft_strdup: %s\n", dst);
free(dst);
dst = strdup("a");
printf("strdup: %s\n", dst);
free(dst);
dst = ft_strdup("a");
printf("ft_strdup: %s\n", dst);
free(dst);
dst = strdup("");
printf("strdup: %s\n", dst);
free(dst);
dst = ft_strdup("");
printf("ft_strdup: %s\n", dst);
free(dst);
return (0);
}