-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssFunctions3.c
More file actions
152 lines (137 loc) · 3.71 KB
/
ssFunctions3.c
File metadata and controls
152 lines (137 loc) · 3.71 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "simpleshellmain.h"
/**
* printEnvironments - Print the environment variables to standard output
*
* Description: This function iterates through the environment variables
* and prints each one to the standard output followed by a newline!
*
* Return: Always returns 0.
*/
int printEnvironments(void)
{
unsigned int index;
index = 0;
while (environ[index] != NULL)
{
write(STDOUT_FILENO, environ[index], stringLength(environ[index]));
write(STDOUT_FILENO, "\n", 1);
index++;
}
return (0);
}
/**
* customPuts - Writes a string to standard output
* @stringer: The string to be written
*
* Description: This function writes the given string to the standard output.
*
* Return: This function does not return a value.
*/
void customPuts(char *stringer)
{
unsigned int str_length;
str_length = stringLength(stringer);
write(STDOUT_FILENO, stringer, str_length);
}
/**
* envWYA - Get the value of an environment variable
* @what: Name of the environment variable to retrieve
*
* Description: This function retrieves the value of the specified environment
* variable. If the variable is not found, it returns NULL.
*
* Return: The value of the environment variable or NULL if not found.
*/
char *envWYA(const char *what)
{
struct envWYAvariables eWv;
eWv.envSize = 0;
while (environ[eWv.envSize] != NULL)
{
eWv.envSize++;
}
eWv.cpyenv = NULL;
eWv.cpyenv = envDup(eWv.cpyenv, eWv.envSize);
eWv.sz = stringLength((char *)what);
eWv.i = 0;
while (eWv.cpyenv[eWv.i] != NULL)
{
eWv.chupapi = eWv.cpyenv[eWv.i];
eWv.weigh = stringCompareN((char *)what, eWv.chupapi, eWv.sz);
if (eWv.weigh == 1)
{
eWv.bargain = strtok(eWv.chupapi, "=");
eWv.bargain = strtok(NULL, "\n ");
if (eWv.bargain == 0)
{
write(STDERR_FILENO, "No such file or directory \n", stringLength("No such file or directory \n"));
exit(EXIT_FAILURE);
}
eWv.routeSize = stringLength(eWv.bargain);
eWv.route = malloc(sizeof(char) * eWv.routeSize + 1);
if (eWv.route == NULL)
{
write(STDERR_FILENO, "Malloc Failed\n", stringLength("Malloc Failed\n"));
return (NULL);
}
eWv.route = copyString(eWv.route, eWv.bargain);
byeDoublePointers(eWv.cpyenv, eWv.envSize);
return (eWv.route);
}
eWv.i++;
}
return (NULL);
}
/**
* receptionist - Displays a custom prompt based on file descriptor and file status
* @fileDescriptor: The file descriptor to check
* @fileStatus: The structure containing file status information
*
* Description: Checks the file status and displays a custom prompt if the file
* is a character device.
*
* Return: None
*/
void receptionist(int fileDescriptor, struct stat filestatus)
{
fstat(fileDescriptor, &filestatus);
if (S_ISCHR(filestatus.st_mode))
customPuts("pleasework :( ");
}
/**
* envDup - Duplicate environment variables
* @cpyenv: Array of environment variables to be duplicated
* @envSize: Number of environment variables in the array
*
* Description: This function duplicates an array of environment variables
* and returns the duplicated array.
*
* Return: Duplicated array of environment variables
*/
char **envDup(char **cpyenv, unsigned int envSize)
{
char *mugagno;
unsigned int mugagno_length;
unsigned int i;
cpyenv = malloc(sizeof(char **) * (envSize));
if (cpyenv == NULL)
{
write(STDERR_FILENO, "Malloc Failed\n", stringLength("Malloc Failed\n"));
return (NULL);
}
i = 0;
while (i < envSize)
{
mugagno = environ[i];
mugagno_length = stringLength(mugagno);
cpyenv[i] = malloc(sizeof(char) * mugagno_length + 1);
if (cpyenv[i] == NULL)
{
write(STDERR_FILENO, "Malloc Failed\n", stringLength("Malloc Failed\n"));
return (NULL);
}
copyString(cpyenv[i], environ[i]);
i++;
}
return (cpyenv);
}