-
Notifications
You must be signed in to change notification settings - Fork 0
/
access.c
140 lines (124 loc) · 2.4 KB
/
access.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
#include "shell.h"
/**
*_strtok_r - tokenizes a string
*@string: string to be tokenized
*@delim: delimiter to be used to tokenize the string
*@save_ptr: pointer to be used to keep track of the next token
*
*Return: The next available token
*/
char *_strtok_r(char *string, char *delim, char **save_ptr)
{
char *finish;
if (string == NULL)
string = *save_ptr;
if (*string == '\0')
{
*save_ptr = string;
return (NULL);
}
string += _strspn(string, delim);
if (*string == '\0')
{
*save_ptr = string;
return (NULL);
}
finish = string + _strcspn(string, delim);
if (*finish == '\0')
{
*save_ptr = finish;
return (string);
}
*finish = '\0';
*save_ptr = finish + 1;
return (string);
}
/**
* _atoi - changes a string to an integer
* @s: the string to be changed
*
* Return: the converted int
*/
int _atoi(char *s)
{
unsigned int n = 0;
do {
if (*s == '-')
return (-1);
else if ((*s < '0' || *s > '9') && *s != '\0')
return (-1);
else if (*s >= '0' && *s <= '9')
n = (n * 10) + (*s - '0');
else if (n > 0)
break;
} while (*s++);
return (n);
}
/**
* _realloc - reallocates a memory block
* @ptr: pointer to the memory previously allocated with a call to malloc
* @old_size: size of ptr
* @new_size: size of the new memory to be allocated
*
* Return: pointer to the address of the new memory block
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
void *temp_block;
unsigned int i;
if (ptr == NULL)
{
temp_block = malloc(new_size);
return (temp_block);
}
else if (new_size == old_size)
return (ptr);
else if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
else
{
temp_block = malloc(new_size);
if (temp_block != NULL)
{
for (i = 0; i < min(old_size, new_size); i++)
*((char *)temp_block + i) = *((char *)ptr + i);
free(ptr);
return (temp_block);
}
else
return (NULL);
}
}
/**
* ctrl_c_handler - handles the signal raised by CTRL-C
* @signum: signal number
*
* Return: void
*/
void ctrl_c_handler(int signum)
{
if (signum == SIGINT)
print("\n($) ", STDIN_FILENO);
}
/**
* remove_comment - removes/ignores everything after a '#' char
* @input: input to be used
*
* Return: void
*/
void remove_comment(char *input)
{
int i = 0;
if (input[i] == '#')
input[i] = '\0';
while (input[i] != '\0')
{
if (input[i] == '#' && input[i - 1] == ' ')
break;
i++;
}
input[i] = '\0';
}