-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.c
124 lines (109 loc) · 1.81 KB
/
tools.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
#include "main.h"
/**
* itoa - convert integer to array
* @n: the given number
*
* Return: a pointer to the null terminated string
*/
char *itoa(unsigned int n)
{
int len = 0, i = 0;
char *s;
len = intlen(n);
s = malloc(len + 1);
if (!s)
return (NULL);
while (n / 10)
{
s[i] = (n % 10) + '0';
n /= 10;
i++;
}
s[i] = (n % 10) + '0';
array_rev(s, len);
s[i + 1] = '\0';
return (s);
}
/**
* _atoi - converts character to integer
* @c: the given character
*
* Return: An integer
*/
int _atoi(char *c)
{
unsigned int val = 0;
int sign = 1;
if (c == NULL)
return (0);
while (*c)
{
if (*c == '-')
sign *= (-1);
else
val = (val * 10) + (*c - '0');
c++;
}
return (sign * val);
}
/**
* intlen - Determine the number of digit int integer
* @num: the given number
*
* Return: the length of the integer
*/
int intlen(int num)
{
int len = 0;
while (num != 0)
{
len++;
num /= 10;
}
return (len);
}
/**
* print_error - prints error
* @data: the data structure pointer
*
* Return: (Success) a positive number
* ------- (Fail) a negative number
*/
int print_error(sh_t *data)
{
PRINT("hsh: ");
PRINT(itoa(data->index));
PRINT(": ");
PRINT(data->args[0]);
PRINT(": ");
PRINT(data->error_msg);
return (0);
}
/**
* write_history - prints error
* @data: the data structure pointer
*
* Return: (Success) a positive number
* ------- (Fail) a negative number
*/
int write_history(sh_t *data __attribute__((unused)))
{
char *filename = "history";
char *line_of_history = "this is a line of history";
ssize_t fd, w;
int len;
if (!filename)
return (-1);
fd = open(filename, O_RDWR | O_APPEND);
if (fd < 0)
return (-1);
if (line_of_history)
{
while (line_of_history[len])
len++;
w = write(fd, line_of_history, len);
if (w < 0)
return (-1);
}
return (1);
}