-
Notifications
You must be signed in to change notification settings - Fork 0
/
Custom_getline.c
111 lines (106 loc) · 1.68 KB
/
Custom_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
109
110
111
#include "shell.h"
/**
* readgetline - read input from STDIN
* Return: the input on buff
*
*/
char *readgetline()
{
char b = 0, *buff, *R_BUFF;
int size_buff = SIZEBUF, READ;
int a = 0;
buff = malloc(size_buff);
if (buff == NULL)
{
free(buff);
return (NULL);
}
/*********loop**********/
while (b != EOF && b != '\n')
{
fflush(stdin);
/**********reading*******/
READ = read(STDIN_FILENO, &b, 1);
if (READ == 0)
{
free(buff);
exit(EXIT_SUCCESS);
}
buff[a] = b;
if (buff[0] == '\n')
return (input_NL(buff));
if (a >= size_buff)
{
buff = realloc(buff, (size_buff + 2));
if (buff == NULL)
{
free(buff);
return (NULL);
}
}
a++;
}
buff[a] = '\0';
R_BUFF = str_space(buff);
free(buff);
hashtag_Handler(R_BUFF);
return (R_BUFF);
}
/**
* hashtag_Handler - function that removes everything after #
* @In_buf: input buffer
* Return: no_void
*/
void hashtag_Handler(char *In_buf)
{
int j;
for (j = 0; In_buf[j] != '\0'; j++)
{
if (In_buf[j] == '#' && In_buf[j - 1] == ' ' && In_buf[j + 1] == ' ')
{
In_buf[j] = '\0';
}
}
}
/**
* input_NL - newline handler
* @Str_Handle: String to be handled
*
* Return: return Empty string
*/
char *input_NL(char *Str_Handle)
{
free(Str_Handle);
return ("\0");
}
/**
* str_space - Modifies the input string to remove preceeding whitespaces
* @str: Input to be modifies
* Return: Returns the modified string
*/
char *str_space(char *str)
{
char *buff;
int i, j = 0;
/*****allocate****/
buff = malloc(sizeof(char) * (F_strlen(str) + 1));
if (buff == NULL)
{
free(buff);
return (NULL);
}
for (i = 0; str[i] == ' '; i++)
;
for (; str[i + 1] != '\0'; i++)
{
buff[j] = str[i];
j++;
}
buff[j] = '\0';
if (buff[0] == '\0' || buff[0] == '#')
{
free(buff);
return ("\0");
}
return (buff);
}