-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
125 lines (114 loc) · 2.23 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
#include <stdio.h>
#include <malloc.h>
int my_puts(const char *string) {
int ind = 0;
while (string[ind] != '\0') {
int res = putchar(string[ind++]);
if (res == EOF) {
return res;
}
}
if (putchar('\n') == EOF) {
return EOF;
};
return '\n';
}
char *my_strchr(char *str, int chr) {
int ind = 0;
while (str[ind] != '\0') {
if (str[ind] == chr) {
return &str[ind];
}
++ind;
}
if (chr == '\0') {
return &str[ind];
}
return NULL;
}
int my_strlen(const char *str) {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
char *my_strcpy(char *dst, const char *src) {
int ind = 0;
while ((dst[ind] = src[ind]) != '\0') {
++ind;
}
return dst;
}
char *my_strncpy(char *dst, const char *src, size_t num) {
size_t ind = 0;
while (ind < num && src[ind] != '\0') {
dst[ind] = src[ind];
++ind;
}
if (src[ind] == '\0') {
dst[ind] = '\0';
}
return dst;
}
char *my_strcat(char *dst, const char *str) {
my_strcpy(my_strchr(dst, '\0'), str);
return dst;
}
char *my_strncat(char *dst, const char *str, size_t num) {
my_strncpy(my_strchr(dst, '\0'), str, num);
return dst;
}
char *my_fgets(char *str, int num, FILE *stream) {
int ind = 0;
while (ind < num) {
int res = fgetc(stream);
if (res == EOF || res == '\0') {
break;
}
str[ind++] = res;
if (res == '\n') {
break;
}
}
str[ind] = '\0';
return str;
}
char *my_strdup(const char *str) {
char *res = malloc(my_strlen(str));
my_strcpy(res, str);
return res;
}
ssize_t my_getline(char **lineptr, size_t *n, FILE *stream) {
if (*lineptr == NULL) {
*n = 128;
*lineptr = malloc(*n);
if (*lineptr == NULL) {
return -1;
}
}
size_t ind = 0;
int read_chr;
while ((read_chr = fgetc(stream)) != '\n' && read_chr != EOF && read_chr != '\0') {
if (ind == *n) {
*n *= 2;
*lineptr = realloc(*lineptr, *n);
if (*lineptr == NULL) {
return -1;
}
}
*lineptr[ind] = read_chr;
}
if (read_chr == EOF && !feof(stream)) {
return -1;
}
if (read_chr == '\n') {
*lineptr[ind++] = read_chr;
*lineptr[ind] = '\0';
}
return ind;
}
int main() {
printf("%s", my_strdup("meuw"));
return 0;
}