-
Notifications
You must be signed in to change notification settings - Fork 0
/
_getline.c
108 lines (95 loc) · 1.84 KB
/
_getline.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
#include "main.h"
#include <stddef.h>
#include <stdlib.h>
/**
* _realloc - Entry point
* Description: 'to reallocate memory'
*
* @ptr: pointer
* @size: size
* Return: 'the new ptr'
*/
void *_realloc(void *ptr, size_t size)
{
void *new_ptr;
char *next_ptr;
char *new_next_ptr;
size_t dup_size;
size_t index = 0; /* counter */
if (size == 0)
{
free(ptr);
return (NULL);
} /* end if */
/* we allocate a new block of memory */
new_ptr = malloc(size);
/* validate allocation */
if (new_ptr == NULL)
{
return (NULL);
} /* end if */
/* copy contents to new blocks */
next_ptr = (char *) ptr;
new_next_ptr = (char *) next_ptr;
if (size < sizeof(ptr))
{
dup_size = size;
} /* end if */
else
{
dup_size = sizeof(ptr);
} /* end else */
for (; index <= dup_size; index++)
{
new_next_ptr[index] = next_ptr[index];
} /* end for */
free(ptr);
return (next_ptr);
} /* end function */
/**
* _getline - Entry point
* Description: 'to read a line of char.'
*
* @linePtr: 'pointer to the line'
* @buff_size: 'buffer'
* @my_file: 'the file'
* Return: raed line
*/
ssize_t _getline(char **linePtr, size_t *buff_size, FILE *my_file)
{
ssize_t bytes_read = 0;
size_t index = 0;
ssize_t result;
char *newSizePtr;
while (1)
{
if (index >= *buff_size)
{
*buff_size += 8;
newSizePtr = _realloc(*linePtr, *buff_size);
if (newSizePtr == NULL)
{
perror("Memory allocation error");
return (-1);
} /* End if */
*linePtr = newSizePtr;
} /* end if */
result = read(fileno(my_file), &((*linePtr)[index]), 1);
if (result == -1)
{
perror("Error reading file");
return (-1);
} /* end if */
else if (result == 0)
{
break;
} /* end else if */
bytes_read++;
if ((*linePtr)[index] == '\n')
{
break;
} /* end if */
index++;
} /* end while */
return (bytes_read);
} /* end function */