-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_funcs.c
117 lines (107 loc) · 1.73 KB
/
helper_funcs.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
117
#include "shell.h"
/**
* _print - custom write function
* @str: string
*
* Return: len of char printed or -1 on failure
*/
int _print(char *str)
{
int count = -1;
if (str != NULL)
{
count = write(1, str, _strlen(str));
}
return (count);
}
/**
* wrd_counter - counts words in lineptr
* @str: lineptr
*
* Return: count
*/
int wrd_counter(char *str)
{
int cnt = 0, i = 0;
char delim = ' ';
while (str[i] != '\0')
{
if (str[i] != delim)
{
cnt++;
while (str[i] != '\0' && str[i] != delim)
{
if (str[i + 1] == delim)
{
i++;
continue;
}
i++;
}
} else
{
i++;
}
}
if (str[i - 1] == delim)
cnt--;
return (cnt);
}
/**
* explicit_free - frees wrds array
* @wrds: tokens
* @wordCnt: no of tokens
*
*/
void explicit_free(char ***wrds, int wordCnt)
{
if (*wrds != NULL && wordCnt > 0)
{
int i;
for (i = 0; i < wordCnt; i++)
{
free((*wrds)[i]);
(*wrds)[i] = NULL;
}
free(*wrds);
*wrds = NULL;
wordCnt = 0;
}
}
/**
* isEmptyStr - isempty customized
* @str: string to be checked
*
* Return: 1 if empty or 0 if not
*/
int isEmptyStr(const char *str)
{
if (str == NULL)
return (1);
while (*str != '\0')
{
if (*str != ' ' && *str != '\t' && *str != '\n'
&& *str != '\r' && *str != '\f' && *str != '\v')
{
return (0);
}
str++;
}
return (1);
}
/**
* exit_checker - checks for exit string
* @args: array of tokens from cmd
* @wordCount: no of words passed from cmd
* @exitStatus: current exitStatus
* @prevExitStatus: previous command's exit status
*
*/
void exit_checker(char **args, int wordCount,
int *exitStatus, int *prevExitStatus)
{
if (_strcmp(args[0], "exit") == 0)
{
exit_builtin(args, wordCount, exitStatus, prevExitStatus);
}
}