-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.c
121 lines (89 loc) · 2.75 KB
/
util.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
118
119
120
121
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "util.h"
#include "disp.h"
#define DELIM_TOKEN " \t\r\f\a\n"
#define COMMAND_DELIM_TOKEN ";"
#define BUF_DEFAULT 128
/*
* Splits each line into tokens by DELIM_TOKEN
* Stores number of tokens into pointer variable 'argc'
*
* Returns: Array of tokens **tokens
*/
char **parseline(char *line, int *argc) {
// bsize Defines expected size of array. Default BUF_DEFAULT
// count is used as a counter variable to assign values to array
int bsize = BUF_DEFAULT, count = 0;
char *token;
char **tokens = (char**)malloc(bsize * sizeof(char*));
for(int i = 0; i < bsize; i++) {
tokens[i] = NULL;
}
// Tokenizing the string: initial token extracted
token = strtok(line, DELIM_TOKEN);
do {
// Add token value from previous iteration into array of tokens
tokens[count++] = token;
strcat(tokens[count-1], "\0"); // null terminating strings for bugfix
// In case of overflow of array, reassign memory
if(count >= bsize) {
bsize += BUF_DEFAULT;
tokens = realloc(tokens, bsize * sizeof(char*));
}
// extract the next token
token = strtok(NULL, DELIM_TOKEN);
} while(token != NULL);
for(int i = count + 1; i < bsize; i++) {
tokens[i] = NULL;
}
// tokens[count++] = "\0";
// Saves the number of tokens extracted into pointer 'argc'
*argc = count;
// Return the array of tokens
return tokens;
}
/*
* Splits the command into separate commands based on COMMAND_DELIM_TOKEN
* Stores number of commands into pointer variable 'num'
*
* Returns: Array of commands **commands
*/
char **splitlines(char *line, int *num) {
// bsize Defines expected size of array. Default BUF_DEFAULT
// count is used as a counter variable to assign values to array
int bsize = BUF_DEFAULT, count = 0;
char *command;
char **commands = (char**)malloc(bsize * sizeof(char*));
// Tokenizing the string: initial token extracted
command = strtok(line, COMMAND_DELIM_TOKEN);
do {
// Add token value from previous iteration into array of tokens
commands[count++] = command;
strcat(commands[count-1], "\0"); // null terminating strings for bugfix
// In case of overflow of array, reassign memory
if(count >= bsize) {
bsize += BUF_DEFAULT;
commands = realloc(commands, bsize * sizeof(char*));
}
// extract the next token
command = strtok(NULL, ";");
} while(command != NULL);
// commands[count++] = "\0";
// Saves the number of tokens extracted into pointer 'num'
*num = count;
return commands;
}
// SIGNAL HANDLERS
void sig_int(int signum) {
}
void sig_stop(int signum) {
}
void sig_tstp(int signum) {
}
void sig_child(int signum) {
// perror("\nProcess terminated");
return;
}