-
Notifications
You must be signed in to change notification settings - Fork 0
/
spares.c
114 lines (100 loc) · 2.03 KB
/
spares.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
/*
Unused functions developed during the get_next_line project.
*/
//Function searches for char and returns first location. 0 not found. 1 is 1st.
int ft_strsrch(const char *str, char ch)
{
unsigned int i;
int loc;
loc = 0;
i = 0;
while (str[i] != '\0')
{
if (str[i] == ch)
return(i + 1);
i++;
}
return(loc);
}
/*Test strsrch
//gcc -Wall -Werror -Wextra get_next_line_utils.c && ./a.out
#include <stdio.h>
int main(void)
{
char *str="Hello";
char ch='3';
printf("Str: %s, Ch: %c, Out: %d\n", str, ch, ft_strsrch(str, ch));
return (0);
}
//*/
//Function reads from a file descriptor, creating a string for its output.
char *ft_fetch(int fd, int buffsize)
{
char *string;
int rdout;
rdout = 0;
string = malloc ((buffsize + 1) * sizeof(char));
if (!string)
return ((char *) 0);
string[0] = '\0';
rdout = read(fd, string, buffsize);
if (rdout == -1 || string[0] == '\0')
{
free(string);
return ((char *) 0);
}
string[buffsize] = '\0';
return (string);
}
/* Test | gcc -Wall -Werror -Wextra get_next_line_utils.c && ./a.out
#include <fcntl.h>
#include <stdio.h>
int main(void)
{
char *fetched;
int fd;
int buffsize;
int i;
fd = open("sample.txt", O_RDONLY, 0);
buffsize = 6;
i = 1;
while (i < 20)
{
fetched = ft_fetch(fd, buffsize);
printf("Read%d: %s\n", i, fetched);
free(fetched);
i++;
}
return (0);
}
//*/
//Function counts on a static variable array. Input determines counter mode.
int *ft_count(int ctr)
{
static int count[2] = {0, 0};
if (ctr == 1)
count[0] += 1;
else if (ctr == 2)
count[1] += 1;
else if (ctr += 3)
{
count[0] += 1;
count[1] += 1;
}
else
return count;
return count;
}
/*Test count
//gcc -Wall -Werror -Wextra get_next_line_utils.c && ./a.out
#include <stdio.h>
int main(void)
{
int *p;
p = ft_count(3);
printf("1 - Var[0]: %d, Var[1]: %d\n", p[0], p[1]);
p = ft_count(3);
printf("2 - Var[0]: %d, Var[1]: %d\n", p[0], p[1]);
return (0);
}
//*/