-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathother_functions.c
106 lines (91 loc) · 1.93 KB
/
other_functions.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
#include "unixshell.h"
/**
* msgerror - A function that prints message not found.
* @name: The name of the shell.
* @value: Number of time program runs.
* @cmd_opt: The pointer to tokenized cmd_opt.
* Return: Nothing.
*
**/
void msgerror(char *name, int value, char **cmd_opt)
{
char c;
c = value + '0';
write(STDERR_FILENO, name, _strlen(name));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, &c, 1);
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, cmd_opt[0], _strlen(cmd_opt[0]));
write(STDERR_FILENO, ": not found\n", 12);
}
/**
* print_env - A function that prints all enviromental variables.
* @env: The pointer to enviromental variables.
*/
void print_env(char **env)
{
size_t i = 0, len = 0;
while (env[i])
{
len = _strlen(env[i]);
write(STDOUT_FILENO, env[i], len);
write(STDOUT_FILENO, "\n", 1);
i++;
}
}
/**
* _atoi - converts a string to an integer
* @s: input string
* Return: integer
*/
int _atoi(char *s)
{
unsigned int count = 0, size = 0, oi = 0, pn = 1, m = 1, i;
while (*(s + count) != '\0')
{
if (size > 0 && (*(s + count) < '0' || *(s + count) > '9'))
break;
if (*(s + count) == '-')
pn *= -1;
if ((*(s + count) >= '0') && (*(s + count) <= '9'))
{
if (size > 0)
m *= 10;
size++;
}
count++;
}
for (i = count - size; i < count; i++)
{
oi = oi + ((*(s + i) - 48) * m);
m /= 10;
}
return (oi * pn);
}
/**
* change_dir - function that changes working directory.
* @path_name: new current working directory.
* Return: 0 on success, -1 on failure.
*/
int change_dir(const char *path_name)
{
char *buf = NULL;
size_t size = 1024;
if (path_name == NULL)
path_name = getcwd(buf, size);
if (chdir(path_name) == -1)
{
perror(path_name);
return (98);
}
return (1);
}
/**
* signal_input - function to handle signals e.g Ctr + C signal.
* @signal: signal to handle.
*/
void signal_input(int signal)
{
(void)signal;
write(STDOUT_FILENO, "\n$ ", 4);
}