-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_2.c
146 lines (136 loc) · 2.95 KB
/
func_2.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
#include "main.h"
#define BUF_READ 4096
#define INIT_LPTR\
do {\
*n = b_count;\
for (i = 0; buf_read[i] != '\n' && buf_read[i] != '\0'; i++)\
(*lineptr)[i] = buf_read[i];\
if (buf_read[i] == '\n')\
{\
(*lineptr)[i] = '\n';\
(*lineptr)[++i] = '\0';\
} \
else\
(*lineptr)[i] = '\0';\
_strcpy(temp, buf_read);\
for (i = 0; i < count; i++)\
buf_read[i] = 0;\
for (i = b_count; temp[i] != '\0'; i++)\
buf_read[j++] = temp[i];\
nlne--;\
} \
while (0)
/**
* _strlen - count the length of string
* @str: string to count
* Return: length of string
*/
size_t _strlen(const char *str)
{
size_t i, count = 0;
if (str == NULL)
return (-1);
for (i = 0; str[i] != '\0'; i++)
count++;
return (count);
}
/**
* _strcpy - copy string to another another memory location
* @src: source of string to copy
* @dest: copy string to destination
* Return: pointer to destination
*/
char *_strcpy(char *dest, const char *src)
{
int i;
if (src == NULL || dest == NULL)
return (NULL);
for (i = 0; src[i] != '\0'; i++)
dest[i] = src[i];
dest[i] = '\0';
return (dest);
}
/**
* _getline - read each line from standard input
* @lineptr: a storage of the read line
* @n: size of line pointer
* @stream: file stream to read from
* Return: -1 for end of file or error, or read size
*/
ssize_t _getline(char **lineptr, size_t *n, int stream)
{
static char buf_read[BUF_READ] = "";
static ssize_t rc = BUF_READ;
static int nlne = 1;
char hld_read[BUF_READ / 2] = "", temp[BUF_READ] = "";
size_t b_count = 0, i, count = BUF_READ, j = 0;
if (nlne <= 1 && buf_read[0] == '\0')
{
nlne = 0;
rc = read(stream, hld_read, sizeof(hld_read));
if (rc == 0)
return (-1);
_strcat(buf_read, hld_read);
for (i = 0; buf_read[i] != 0; i++)
if (buf_read[i] == '\n')
nlne++;
}
if ((rc == -1 && buf_read[0] == '\0') || (rc == 0 && nlne == -1))
return (-1);
for (i = 0; buf_read[i] != '\n' && buf_read[i] != '\0'; i++)
b_count++;
if (buf_read[i] == '\n')
b_count++;
if (*n == 0)
*lineptr = NULL;
if (*lineptr == NULL)
{
*lineptr = malloc(sizeof(char) * (b_count + 1));
if (*lineptr == NULL)
return (-1);
}
else
{
free(*lineptr);
*lineptr = malloc(sizeof(char) * (b_count + 1));
if (*lineptr == NULL)
return (-1);
}
INIT_LPTR;
return (rc);
}
/**
* free_mem - free memory(ies)
* @args: free pointer to pointers
* @a: free pointer
* @b: free pointer
* Description: if any argument won't be used, set it to NULL
* Return: Nothing
*/
void free_mem(char **args, char *a, char *b)
{
int i;
if (a != NULL)
free(a);
if (b != NULL)
free(b);
if (args != NULL)
{
for (i = 0; args[i] != NULL; i++)
free(args[i]);
free(args);
}
}
/**
* error_alias - write error message for alias
* @message: error message
* @arg: alias argument
*/
void error_alias(char *message, char *arg)
{
write(er, "alias: ", 7);
write(er, arg, _strlen(arg));
write(er, " ", 1);
write(er, message, _strlen(message));
write(er, "\n", 1);
}