-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilitiesS2.c
executable file
·116 lines (111 loc) · 2.21 KB
/
utilitiesS2.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
#include "shell.h"
/**
* blackster - Freedom for colombia!
* @liberty: FREEDOM!
* Return: Only freedom
*/
void blackster(char **liberty)
{
int i;
for (i = 0; liberty[i]; i++)
free(liberty[i]);
free(liberty);
}
/**
* Commentator - Terminates anything after a #
* @getty: Checks if it should be executed or no
* @buffer: Holds the buffer whose comments are to be termianted
* Return: a pointer to the new, comment free, buffer-> neobufer
*/
char *Commentator(int getty, char *buffer)
{
char *neobuf;
int i, j;
if (getty != -1 && buffer[0] != '#')
{
for (i = 0; buffer[i] != 0; i++)
{
if (buffer[i] == '#')
if (buffer[i - 1] == ' ')
break;
}
neobuf = malloc(sizeof(char) * (i + 1));
if (neobuf == 0)
exit(1);
for (j = 0; j < i; j++)
neobuf[j] = buffer[j];
neobuf[j] = 0;
free(buffer);
return (neobuf);
}
free(buffer);
return (0);
}
/**
* intToStr - Integer to strings
* @num: Holds the number
* Return: pointer to a string
*/
char *intToStr(unsigned long int num)
{
unsigned int i, k;
unsigned long int num1;
char *number;
if (num <= 9)
{
number = malloc(sizeof(char) * 2);
number[0] = num + '0';
number[1] = 0;
return (number);
}
num1 = num;
for (i = 0; num1 != 0; i++)
num1 /= 10;
number = malloc((sizeof(char) * i) + 1);
for (k = 0; num != 0; k++)
{
number[k] = (num % 10) + '0';
num /= 10;
}
number[k] = 0;
rev_string(number);
return (number);
}
/**
* rev_string - Reverses a string
* @s: Holds such string
* Return: none
*/
void rev_string(char *s)
{
int a, b;
char c = 0;
for (a = 0; s[a] != 0; a++)
;
for (b = 0; b < a; b++)
{
c = s[a - 1];
s[a - 1] = s[b];
s[b] = c;
a--;
}
}
/**
* writeErrPerm - Writes an error message when no permissions are granted
* @name: Holds the name
* @comm: Holds the command
* @counter: Holds the command counter
* Return: none
*/
void writeErrPerm(char *name, char *comm, int counter)
{
char *number;
number = intToStr(counter);
write(STDERR_FILENO, name, _strlenS(name));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, number, _strlenS(number));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, comm, _strlenS(comm));
write(STDERR_FILENO, ": Permission denied\n", 21);
free(number);
}