-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.c
235 lines (206 loc) · 3.43 KB
/
history.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "holberton.h"
/*** WRITES STRING TO STDOUT ***/
/**
* _puts3 - writes a string to standard output
* @str: string to write
*
* Return: number of chars printed or -1 on failure
*/
ssize_t _puts3(char *str)
{
ssize_t i, len;
for (i = 0; str[i]; i++)
;
len = write(1, str, i);
if (len != i)
{
perror("Fatal Error");
return (-1);
}
return (len);
}
/**
*print_message - print a string to standart output
* @str: string to print.
* Return: void
*/
void print_message(char *str)
{
long num, len;
num = _strlen(str);
len = write(STDOUT_FILENO, str, num);
if (len != num)
{
perror("fatal error");
exit(1);
}
}
/**
* read_textfile - reads a text file and prints it to the POSIX standard output
* @filename: name of the file to read
* @letters: number of letters it should read and print
*
* Return: actual number of letters it could read and print
*/
void read_textfile(vars_t *vars)
{
char *file;
int fd, r;
char *s, *z;
int i = 0;
int x = 0;
if (vars->array_tokens[1] == NULL)
{
file = "/root/.simple_shell_history";
fd = open(file, O_RDWR);
s = malloc(4096);
if (s == NULL)
{
_puts_error("Fatal Error");
return;
}
while ((r = read(fd, s, 4096)) > 0)
{
z = s;
_putchar('0');
_putchar(' ');
_putchar(' ');
while (*z)
{
if (*z == '\n')
{
i++;
}
z++;
}
i--;
z = s;
while (*z)
{
_putchar(*z);
if (*z == '\n' && x < i)
{
x++;
if (x == i)
break;
else
integer_converter2(x);
_putchar(' ');
_putchar(' ');
}
z++;
}
if (r == -1)
{
_puts_error("Fatal Error");
return;
}
}
free(s);
fd = close(fd);
return;
}
else
new_help_help(vars);
}
/**
* append_text_to_file - appends a text at the end of a file.
* @filename: name of the file
* @text_content: NULL terminated string to add at the end of the file
*
* Return: 1 on success and -1 on failure
*/
int append_text_to_file(vars_t *vars)
{
int ft;
long _write;
char *compare = vars->buffer;
const char *filename = "/root/.simple_shell_history";
if (_strcmpr(vars->buffer, "\n") == 0)
{
return (0);
}
if (_strcmpr2(compare) == 0)
{
return (0);
}
if (filename == NULL)
{
return (-1);
}
ft = open(filename, O_RDWR | O_CREAT | O_APPEND);
if (ft == -1)
{
close(ft);
return (-1);
}
if (vars->buffer != NULL)
{
_write = write(ft, vars->buffer, _strlen(vars->buffer));
}
close(ft);
if (_write == -1)
{
return (-1);
}
return (1);
}
int _strcmpr2(char *strcmp1)
{
int i;
i = 0;
while (strcmp1[i] != '\0')
{
if (strcmp1[0] == ' ')
return (0);
if (strcmp1[1] != ' ')
return (1);
if (strcmp1[i] == ' ')
return (0);
i++;
}
return (1);
}
/**
*print_message - print a string to standart output
* @str: string to print.
* Return: void
*/
void print_message1(char *str)
{
long num, len;
num = _strlen(str);
len = write(STDOUT_FILENO, str, num);
if (len != num)
{
perror("fatal error");
exit(1);
}
}
void _putchar(char c)
{
write(1, &c, 1);
return;
}
char *integer_converter2(unsigned int count)
{
char *numstr;
unsigned int tmp, digits;
tmp = count;
for (digits = 0; tmp != 0; digits++)
tmp /= 10;
numstr = malloc(sizeof(char) * (digits + 1));
if (numstr == NULL)
{
perror("Fatal Error");
exit(100);
}
numstr[digits] = '\0';
for (--digits; count; --digits)
{
numstr[digits] = (count % 10) + '0';
count /= 10;
}
print_message(numstr);
return (0);
}