-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject2a.c
114 lines (104 loc) · 2.46 KB
/
project2a.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
/* CS370 - Ross Rydman - Project 2 */
/*
Intermediate requirements:
Print prompt while waiting for input
Print a prompt until user exits
Accept commands from user and execute until exit
If command not found - print error
*/
#include<termios.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#define BUFF_SIZE 256
#define ARG_COUNT 20
void splitString(char *str, char **splitstr)
{
char *p;
int i = 0;
p = strtok(str," \n");
while(p != NULL)
{
splitstr[i] = malloc(strlen(p) + 1);
if (splitstr[i]){
strcpy(splitstr[i], p);
}
i++;
p = strtok (NULL, " \n");
}
}
int main(void)
{
int exited = 0;
int broken = 0;
//struct termios origConfig;
// Get original config
//tcgetattr(0, &origConfig);
// Copy config
//struct termios newConfig = origConfig;
// Adjust config
//newConfig.c_lflag &= ~(ICANON|ECHO);
//newConfig.c_cc[VMIN] = 10;
//newConfig.c_cc[VTIME] = 2;
// Set the new config
//tcsetattr(0, TCSANOW, &newConfig);
while (exited != 1) {
// print prompt
char *cwdbuf = (char *)malloc(100);
getcwd(cwdbuf,100);
printf("%s -> ", cwdbuf);
free(cwdbuf);
// read input (break on up down and \n)
char *inbuffer = (char *)calloc(BUFF_SIZE, sizeof(char));
fgets(inbuffer, BUFF_SIZE, stdin);
// if broken on up or down
if (broken == 1){
// clear input
// execute appropriate command to generate corresponding msg
// print message
}
else {
// parse input into an array
char *command[ARG_COUNT] = {NULL};
splitString(inbuffer, command);
// Loop again if no input provided
if (command[0] == NULL){
continue;
}
// if cd command then change working directory
else if (strcmp(command[0], "cd") == 0) {
printf("CD entered - need to implement!\n");
}
// if exit command, break
else if (strcmp(command[0], "exit") == 0) {
exited = 1;
continue;
}
// if not cd or exit, execute command
else {
// execute command
unsigned int pid = fork();
int status;
int errno;
if (pid == -1){
printf("Could not fork!");
} else if (pid == 0){
close(2);
dup(1);
int success = execvp(command[0], command);
if (success == -1)
printf("Command not found!\n");
exit(errno);
} else {
waitpid(pid, &status, WUNTRACED);
}
}
}
}
// restore input config
//tcsetattr(0, TCSANOW, &origConfig);
return 0;
}