-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssFunctions4.c
More file actions
83 lines (76 loc) · 1.96 KB
/
ssFunctions4.c
File metadata and controls
83 lines (76 loc) · 1.96 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
#include "simpleshellmain.h"
/**
* byeDoublePointers - Frees the memory allocated for a double pointer array.
* @array: The double pointer array to be freed.
* @length: The length of the array.
*
* Description: This function frees the memory allocated for each string in
* the array and then frees the memory allocated for the array itself.
*
* Return: No return value.
*/
void byeDoublePointers(char **arr, unsigned int len)
{
unsigned int i;
i = 0;
while (i < len)
{
free(arr[i]);
i++;
}
free(arr);
}
/**
* stringChoppedUp - Tokenizes input string.
* @inputString: Input string to be tokenized.
*
* Description: This function tokenizes the input string based on whitespace
* characters and returns an array of tokens.
*
* Return: Pointer to an array of tokens.
*/
char **stringChoppedUp(char *inputString)
{
char **wholeBody;
char *limbs;
unsigned int i;
wholeBody = malloc(sizeof(char) * 1024);
if (wholeBody == NULL)
{
write(STDERR_FILENO, "Malloc Failed \n", stringLength("Malloc Failed \n"));
exit(EXIT_FAILURE);
}
limbs = strtok(inputString, "\n\t\r ");
i = 0;
while (limbs != NULL)
{
wholeBody[i] = limbs;
limbs = strtok(NULL, "\n\t\r ");
i++;
}
wholeBody[i] = NULL;
return (wholeBody);
}
/**
* adiosMuchachos - Free allocated memory for tokens and related variables
* @tokens: The array of strings (tokens) to be freed
* @path: The path string to be freed
* @line: The line string to be freed
* @fullpath: The full path string to be freed (optional)
* @flag: An integer flag indicating whether to
* free fullpath (1 for yes, 0 for no)
*
* Description: This function frees the memory allocated for various strings
* and the tokens array. If the flag is set to 1,
* it also frees the fullpath string.
*
* Return: No return value.
*/
void adiosMuchachos(char **tokens, char *path, char *line, char *fullpath, int idibtgog)
{
free(path);
free(tokens);
free(line);
if (idibtgog == 1)
free(fullpath);
}