-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.c
114 lines (95 loc) · 1.58 KB
/
helper.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
#include "shell.h"
/**
*_putchar - a function to put a character
*@c: character to put.
*Return : nothin;
*/
void _putchar(char c)
{
write(STDERR_FILENO, &c, 1);
}
/**
*_putstr - a function to put a string
*@s: array of characters (string)
* Return: Nothing.
*/
void _putstr(char *s)
{
int i;
for (i = 0; s[i]; i++)
write(STDERR_FILENO, &s[i], 1);
}
/**
*_strlen - a function to count the array of char length
*@s: array of characters (string)
*Return: length
*/
int _strlen(char *s)
{
int i;
for (i = 0; s[i]; i++)
;
return (i);
}
/**
*_putnum - prints unsigned integer
*@num: number
*Return: 0 on success, 1 on failure
*/
int _putnum(unsigned int num)
{
unsigned int num_cp = num;
char *num_str;
int i, size = 0;
if (num == 0)
{
_putchar('0');
return (0);
}
while (num_cp)
{
size++;
num_cp /= 10;
}
num_str = (char *)malloc(sizeof(char) * size);
if (num_str == NULL)
{
return (1);
}
i = size - 1;
while (num)
{
num_str[i] = '0' + (num % 10);
num /= 10;
i--;
}
i = 0;
while (i < size)
{
_putchar(num_str[i]);
i++;
}
free(num_str);
return (0);
}
/**
*print_stderr - function for printing error messages.
*@argv: the name of the command or executable.
*@count: error count.
*@av: coints the file name
*@prompt: cointains the prompt
*
*Return: 127 or -1
*/
int print_stderr(char *argv, unsigned int count, char *av, char *prompt)
{
_putstr(argv);
_putchar(':'), _putchar(' ');
if (_putnum(count))
return (-1);
_putchar(':'), _putchar(' ');
_putstr(av);
_putchar(':'), _putchar(' ');
_putstr(prompt);
return (127);
}