-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmallshutils.c
103 lines (88 loc) · 1.76 KB
/
smallshutils.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
/*
This file contains a set of utility functions to help with parsing
user input
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "smallshutils.h"
/*
Removes a trailing "\n" from character string
*/
void cleanTrailingNewlineFromString(char *str)
{
char *newLinePtr = str + (strlen(str) - 1);
if (*newLinePtr == '\n')
{
*newLinePtr = '\0';
}
newLinePtr = NULL;
}
/*
Checks if a string is empty
Uses empty space as delimiter to strtok_r
*/
bool isEmptyString(char *buf)
{
char *bufCopy = calloc(strlen(buf) + 1, sizeof(char));
strcpy(bufCopy, buf);
char *token = NULL;
char *savePtr = NULL;
token = strtok_r(bufCopy, " ", &savePtr);
if (token == NULL)
{
free(bufCopy);
bufCopy = NULL;
return true;
}
free(bufCopy);
bufCopy = NULL;
return false;
}
/*
Checks if char is a comment
*/
bool isComment(char *buf)
{
if (buf[0] == '#')
return true;
return false;
}
/*
Checks if char is ">" or input symbol
*/
bool isInputCharacter(char *character)
{
if (character == NULL)
return false;
return strcmp(character, "<") == 0;
}
/*
Checks if char is "<" or output symbol
*/
bool isOutputCharacter(char *character)
{
if (character == NULL)
return false;
return strcmp(character, ">") == 0;
}
/*
Checks if char is "&" or use background process flag
*/
bool isBackgroundExecChar(char *character)
{
if (character == NULL)
return false;
return strcmp(character, "&") == 0;
}
/*
Checks if str is arguments in entire user input
ie. If it is neither input symbol, output symbol or background exec flag
*/
bool isArgument(char *str)
{
if (str == NULL)
return false;
return !(isInputCharacter(str) || isOutputCharacter(str) || isBackgroundExecChar(str));
}