-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathss-manage_errors.c
More file actions
136 lines (128 loc) · 2.38 KB
/
ss-manage_errors.c
File metadata and controls
136 lines (128 loc) · 2.38 KB
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
#include "shell.h"
/**
* err_num - converts a string to an integer
* @s: the string to be converted
* Return: 0 if no numbers in string,
* converted integer else -1
*
*/
int err_num(char *s)
{
int i = 0;
unsigned long int result = 0;
if (*s == '+')
s++;
for (i = 0; s[i] != '\0'; i++)
{
if (s[i] >= '0' && s[i] <= '9')
{
result *= 10;
result += (s[i] - '0');
if (result > INT_MAX)
return (-1);
}
else
return (-1);
}
return (result);
}
/**
* print_error - prints an error message
* @info: the parameter & return info struct
* @estr: string containing specified error type
* Return: 0 if no numbers in string,
* converted integer else -1
*/
void print_error(info_s *info, char *estr)
{
puts_err(info->prog_name);
puts_err(": ");
print_dec(info->lines, STDERR_FILENO);
puts_err(": ");
puts_err(info->argv[0]);
puts_err(": ");
puts_err(estr);
}
/**
* print_dec - function prints a decimal (integer) number (base 10)
* @input: the input
* @fd: the filedescriptor to write to
*
* Return: number of characters printed
*/
int print_dec(int input, int fd)
{
int (*__putchar)(char) = _putchar;
int i, count = 0;
unsigned int _abs_, current;
if (fd == STDERR_FILENO)
__putchar = putchar_err;
if (input < 0)
{
_abs_ = -input;
__putchar('-');
count++;
}
else
_abs_ = input;
current = _abs_;
for (i = 1000000000; i > 1; i /= 10)
{
if (_abs_ / i)
{
__putchar('0' + current / i);
count++;
}
current %= i;
}
__putchar('0' + current);
count++;
return (count);
}
/**
* change_base - converter function, a clone of itoa
* @num: number
* @base: base
* @flags: argument flags
*
* Return: string
*/
char *change_base(long int num, int base, int flags)
{
char sign = 0;
char *ptr;
unsigned long n = num;
static char *array;
static char buffer[50];
if (!(flags & BAS_CHANGE_UNSIG) && num < 0)
{
n = -num;
sign = '-';
}
array = flags & BAS_CHANGE_LOWER ? "0123456789abcdef" : "0123456789ABCDEF";
ptr = &buffer[49];
*ptr = '\0';
do {
*--ptr = array[n % base];
n /= base;
} while (n != 0);
if (sign)
*--ptr = sign;
return (ptr);
}
/**
* handle_comments - function replaces first instance of '#' with '\0'
* @buf: address of the string to modify
*
* Return: 0;
*/
void handle_comments(char *buf)
{
int i;
for (i = 0; buf[i] != '\0'; i++)
if (buf[i] == '#' && (!i || buf[i - 1] == ' '))
{
buf[i] = '\0';
break;
}
}