-
Notifications
You must be signed in to change notification settings - Fork 0
/
quash.c
897 lines (816 loc) · 23.9 KB
/
quash.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
//*****************************************************************************
// @file quash.c
//
// Quash's main file
// Dylan Dreiling
// Hayden Kaustinen
//
//*****************************************************************************
//*****************************************************************************
//
//! \addtogroup quash
//! @{
//
//*****************************************************************************
//******************************************************************************
// Included Files
//******************************************************************************
// Putting "quash.h" above the other includes allows us to ensure
// this file's header's #include statements are self
// contained.
#include "quash.h"
#include <string.h>
#include <errno.h>
//**************************************************************************
// Private Variables
// **************************************************************************
/**
* Keep track of whether Quash should request another command or not.
*/
// NOTE: "static" causes the "running" variable to only be declared in this
// compilation unit (this file and all files that include it). This is similar
// to private in other languages.
static bool running;
//**************************************************************************
// int array to hold background job pids
// **************************************************************************
static int jobpids [MAX_COMMAND_LENGTH];
//**************************************************************************
// bool array to hold if current job is valid/running
// **************************************************************************
static bool job_valid [MAX_COMMAND_LENGTH];
//**************************************************************************
// int to hold how many background jobs have occured
// **************************************************************************
static jobs = 0;
//**************************************************************************
// char array to hold value of $HOME
// **************************************************************************
char home[MAX_COMMAND_LENGTH]; //$HOME variable
//**************************************************************************
// char array to hold value of $PATH
// **************************************************************************
char path[MAX_COMMAND_LENGTH]; //#PATH variable
//**************************************************************************
// char array to hold value of current working directory
// **************************************************************************
char cwd[MAX_COMMAND_LENGTH];
//**************************************************************************
// pointer to token in a given string,
// tokens are substrings delimted by second arg of strtok()
// **************************************************************************
//char* token;
//*****************************************************************************
//
//!
//! \param *buf character buffer to hold directory
//! \param size size of buf array
//!
//! Provides the current working diretory to the array, buf, which is of length size
//!
//! \return None.
//
//*****************************************************************************
char *getcwd(char *buf, size_t size);
//**************************************************************************
// process status
// **************************************************************************
int status;
//**************************************************************************
// child processes
// **************************************************************************
int pid_1, pid_2; //child processes
/**************************************************************************
* Private Functions
**************************************************************************/
/**
* Start the main loop by setting the running flag to true
*/
static void start() {
running = true;
}
/**************************************************************************
* Public Functions
**************************************************************************/
bool is_running() {
return running;
}
void terminate() {
running = false;
}
bool get_command(command_t* cmd, FILE* in) {
if (fgets(cmd->cmdstr, MAX_COMMAND_LENGTH, in) != NULL) {
size_t len = strlen(cmd->cmdstr);
char last_char = cmd->cmdstr[len - 1];
if (last_char == '\n' || last_char == '\r') {
// Remove trailing new line character.
cmd->cmdstr[len - 1] = '\0';
cmd->cmdlen = len - 1;
}
else
cmd->cmdlen = len;
return true;
}
else
return false;
}
/**
* Quash entry point
*
* @param argc argument count from the command line
* @param argv argument vector from the command line
* @return program exit status
*/
int main(int argc, char** argv){
//Command holder argument
command_t cmd;
//initialize variables.
strcpy(home, "/home");
strcpy(path, "/home:/home/ddreilin/EECS_678/Project1/Quash");
strcat(path, ":/home/hkaustin/EECS_678/project1-quash/quash/Quash");
strcat(path, ":/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/lib64/openmpi/bin/:/opt/smlnj/bin/");
getcwd(cwd, sizeof(cwd));
start();
puts("Welcome to Quash!");
puts("Type \"exit\" or \"quit\" to quit");
// Main execution loop
while (is_running() && get_command(&cmd, stdin)) {
// NOTE: I would not recommend keeping anything inside the body of
// this while loop. It is just an example.
// The commands should be parsed, then executed.
manager(&cmd);
}
return EXIT_SUCCESS;
}
//**************************************************************************
// function to parse/manage/execute commands
//**************************************************************************
void manager(command_t* cmd ){
char* temp [MAX_COMMAND_LENGTH];
strcpy(temp, cmd->cmdstr);
char* tokens = strtok(temp, " ");
//cd if the first two chars are cd
if (!strcmp(tokens, "cd")){
run_cd( tokens );
}
//set
else if (!strcmp(tokens, "set")){
run_set( tokens);
}
//echo
else if (!strcmp(tokens, "echo")){
run_echo( tokens );
}
//pwd
else if (!strcmp(tokens, "pwd")){
run_pwd( tokens );
}
//jobs
else if (!strcmp(tokens, "jobs")){
run_jobs( cmd, tokens );
}
//exit command
else if (!strcmp(tokens, "exit") || !strcmp(tokens, "quit") )
terminate(); // Exit Quash
//run an executable
else{
run_exec(cmd, tokens);
}
}
/*******************************************************
*
*
* Run Command functions
*
*
*******************************************************/
//**************************************************************************
// function change directory
//**************************************************************************
void run_cd( char* tokens ){
tokens = strtok(NULL, "");
//go to home
if(tokens == NULL){
if(chdir(home)== 0){
strcpy(cwd, home);
}
}
//go to an absolute directory
else if(!strncmp(tokens, "/", 1)){
char temp [MAX_COMMAND_LENGTH];
temp[0]='\0';
while(tokens != NULL){
strcat(temp,"/");
strcat(temp, tokens);
tokens = strtok(NULL, "/");
}
if(chdir(temp) == 0){
strcpy(cwd, temp);
}
}
//go to a directory inside current working directory.
else{
char temp [MAX_COMMAND_LENGTH];
strcpy(temp,cwd);
while(tokens != NULL){
strcat(temp,"/");
strcat(temp, tokens);
tokens = strtok(NULL, "/");
}
if(chdir(temp) == 0){
strcpy(cwd, temp);
}
}
}
//**************************************************************************
// function to set variable values
//**************************************************************************
void run_set( char* tokens ){
tokens = strtok(NULL, "=");
//PATH variable
if (!strcmp(tokens, "PATH")){
puts("Setting PATH variable");
tokens = strtok(NULL, "");
strcpy(path, tokens);
}
//HOME Variable
else if (!strcmp(tokens, "HOME")){
puts("Setting HOME variable");
tokens = strtok(NULL, "");
puts(tokens);
strcpy(home, tokens);
}
//default
else{
puts("Incorrect set syntax");
}
}
//**************************************************************************
//function to execute echo
//**************************************************************************
void run_echo( char* tokens){
tokens = strtok(NULL, "");
//PATH variable
if (!strcmp(tokens, "$PATH")){
puts(path);
}
//HOME Variable
else if (!strcmp(tokens, "$HOME")){
puts(home);
}
//default
else{
puts("Incorrect echo syntax");
}
}
//**************************************************************************
//function to execute pwd
//**************************************************************************
void run_pwd(){
if (getcwd(cwd, sizeof(cwd)) != NULL){
fprintf(stdout, "Current working dir: %s\n", cwd);
}
else{
perror("getcwd() error");
}
}
//**************************************************************************
//function to execute jobs
//**************************************************************************
void run_jobs(){
for(int i = 0; i < jobs; i++){
if(job_valid[i] == true){
printf("[%d] %d\n", i+1, jobpids[i]);
}
}
}
//**************************************************************************
//function to execute an executable
//**************************************************************************
void run_exec( command_t* cmd, char* tokens ){
//Exec booleans to handle special characters
bool special = false;
bool greaterThan = false;
bool lessThan = false;
bool execPipe = false;
bool background = false;
bool moreArgs = false;
//Inside CWD has "./exe arg" format
if( !strncmp(cmd->cmdstr, "./", 2) ){
//variables needed to execute a command
char temp [MAX_COMMAND_LENGTH];//command
char temp2 [MAX_COMMAND_LENGTH];//arguments
char temp3 [MAX_COMMAND_LENGTH];//special argument
char tempCWD[MAX_COMMAND_LENGTH];//cwd
//Set all arrays to 0
bzero(temp, MAX_COMMAND_LENGTH);
bzero(temp2, MAX_COMMAND_LENGTH);
bzero(temp3, MAX_COMMAND_LENGTH);
bzero(tempCWD, MAX_COMMAND_LENGTH);
//put the name of the executable into temp
strcpy(tempCWD,cwd);
strcpy(temp,tempCWD);
tokens = strtok(cmd->cmdstr, "/");
tokens = strtok(NULL, "/");
char* tokens2 = strtok(tokens, " ");
strcat(temp,"/");
strcat(temp,tokens2);
//put arguments into temp2
tokens2 = strtok(NULL, " ");
while( (tokens2 != NULL) && (!special) ){
//checks for >
if (!strcmp(tokens2, ">")) {
special = true;
tokens2 = strtok(NULL, " ");
if(tokens2 == NULL){
puts("Missing argument after >");
return;
}
greaterThan = true;
}
//checks for <
else if(!strcmp(tokens2, "<")){
special = true;
tokens2 = strtok(NULL, " ");
if(tokens2 == NULL){
puts("Missing argument after <");
return;
}
lessThan = true;
}
//checks for |
else if(!strcmp(tokens2, "|")){
special = true;
tokens2 = strtok(NULL, " ");
if(tokens2 == NULL){
puts("Missing argument after |");
return;
}
execPipe = true;
}
//adds arguments
if(!special){
if(moreArgs){
strcat(temp2," ");
}
//if it is a background operation
if (!strcmp(tokens2, "&")) {
background = true;
special = true;
tokens2 = strtok(NULL, " ");
if(tokens2 != NULL){
puts("argument after &");
return;
}
}
//treat as an argument
else{
strcat(temp2, tokens2);
tokens2 = strtok(NULL, " ");
}
moreArgs = true;
}
//adds special character argument
else{
strcat(temp3, tokens2);
}
}
tokens2 = strtok(NULL, " ");
//If not null after a special character
if( tokens2 != NULL) {
//background with special character
if (!strcmp(tokens2, "&")) {
background = true;
tokens2 = strtok(NULL, " ");
if(tokens2 != NULL){
puts("argument after &");
return;
}
}
else{
puts("invalid syntax");
return;
}
}
//make sure the executable exists
if(access(temp, F_OK) == 0){
if(!execPipe){
pid_1 = fork();
if (pid_1 == 0) {
//> redirection
if(greaterThan){
strcat(tempCWD, "/");
strcat(tempCWD, temp3);
exec_greaterThan(temp, temp2, tempCWD, tokens, background);
}
//< redirection
else if(lessThan){
strcat(tempCWD, "/");
strcat(tempCWD, temp3);
exec_lessThan(temp, temp2, tempCWD, tokens, background);
}
//default execution
else{
exec_default(temp, temp2, tokens, background);
}
}
//only wait if it isn't a background process
if(!background){
if ((waitpid(pid_1, &status, 0)) == -1) {
fprintf(stderr, "Process 1 encountered an error ICWD. ERROR%d", errno);
}
}
else{
jobpids[jobs] = pid_1;
job_valid[jobs] = true;
jobs++;
printf("[%d] %d\n", jobs, pid_1);
}
}
//For piping
else{
int pfd1[2];
pipe(pfd1);
pid_1 = fork();
if (pid_1 == 0) {
close(pfd1[0]);
dup2(pfd1[1], STDOUT_FILENO);
exec_default(temp, temp2, tokens, background);
exit(0);
}
pid_2 = fork();
if (pid_2 == 0) {
close(pfd1[1]);
dup2(pfd1[0], STDIN_FILENO);
strcpy(temp, cwd);
strcat(temp, "/");
strcat(temp, temp3);
read(pfd1[0], temp2, MAX_COMMAND_LENGTH);
exec_default(temp, temp2 , tokens, background);
exit(0);
}
//only wait if it isn't a background process
if(!background){
if ((waitpid(pid_2, &status, 0)) == -1) {
fprintf(stderr, "Process 1 encountered an error in pipe ICWD. ERROR%d", errno);
}
}
else{
jobpids[jobs] = pid_1;
job_valid[jobs] = true;
jobs++;
printf("[%d] %d\n", jobs, pid_1);
}
}
}
else{
puts("Executable does not exist in current working directory");
}
}
//Outside CWD, must use PATH
else{
bool ran = false; //boolean to determine if executable is found/run
char temp [MAX_COMMAND_LENGTH];//copy of PATH
char temp2 [MAX_COMMAND_LENGTH];//Current PATH attempting execution
char temp3 [MAX_COMMAND_LENGTH];//special argument
char tempArgs [MAX_COMMAND_LENGTH];//holds command arguments
char tempCMD [MAX_COMMAND_LENGTH];//name of actual command
char tempCWD [MAX_COMMAND_LENGTH];//copy of current working directory
bzero(temp, MAX_COMMAND_LENGTH);
bzero(temp2, MAX_COMMAND_LENGTH);
bzero(temp3, MAX_COMMAND_LENGTH);
bzero(tempArgs, MAX_COMMAND_LENGTH);
bzero(tempCMD, MAX_COMMAND_LENGTH);
bzero(tempCWD, MAX_COMMAND_LENGTH);
//put arguments into tempArgs
char* tokens2 = strtok(cmd->cmdstr, " ");
strcpy(tempCMD, tokens2);
tokens2 = strtok(NULL, " ");
while( (tokens2 != NULL) && (!special) ){
//checks for >
if (!strcmp(tokens2, ">")) {
special = true;
tokens2 = strtok(NULL, " ");
if(tokens2 == NULL){
puts("Missing argument after >");
return;
}
greaterThan = true;
}
//checks for <
else if(!strcmp(tokens2, "<")){
special = true;
tokens2 = strtok(NULL, " ");
if(tokens2 == NULL){
puts("Missing argument after <");
return;
}
lessThan = true;
}
//checks for |
else if(!strcmp(tokens2, "|")){
special = true;
tokens2 = strtok(NULL, " ");
if(tokens2 == NULL){
puts("Missing argument after |");
return;
}
execPipe = true;
}
//adds arguments
if(!special){
if(moreArgs){
strcat(tempArgs," ");
}
//background operation
if (!strcmp(tokens2, "&")) {
background = true;
special = true;
tokens2 = strtok(NULL, " ");
if(tokens2 != NULL){
puts("argument after &");
return;
}
}
//else treat as argument
else{
strcat(tempArgs, tokens2);
tokens2 = strtok(NULL, " ");
}
moreArgs = true;
}
//adds special character argument
else{
strcat(temp3, tokens2);
}
}
tokens2 = strtok(NULL, " ");
//If not null after a special character
if( tokens2 != NULL) {
//background with special character
if (!strcmp(tokens2, "&")) {
background = true;
tokens2 = strtok(NULL, " ");
if(tokens2 != NULL){
puts("argument after &");
return;
}
}
else{
puts("invalid syntax");
return;
}
}
//temp has all the possible paths, tokens break it up
strcpy(temp, path);
tokens = strtok(temp, ":");
//try to execute for all paths
while( (tokens != NULL) && (!ran) ){
strcpy(temp2, tokens);
strcpy(tempCWD, tokens);
strcat(temp2, "/");
strcat(temp2, tempCMD);
//see if the executable exists for the current path
if(access(temp2, F_OK) == 0){
ran = true;
//not piping
if(!execPipe){
pid_1 = fork();
if (pid_1 == 0) {
//> redirection
if(greaterThan){
strcpy(tempCWD, cwd);
strcat(tempCWD, "/");
strcat(tempCWD, temp3);
exec_greaterThan(temp2, tempArgs, tempCWD, tempCMD, background);
}
//< redirection
else if(lessThan){
strcat(tempCWD, "/");
strcat(tempCWD, temp3);
exec_lessThan(temp2, tempArgs, tempCWD, tempCMD, background);
ran = true;
}
//default execution
else{
exec_default(temp2, tempArgs, tempCMD, background);
}
}
//only wait if it isn't a background process
if(!background){
if ((waitpid(pid_1, &status, 0)) == -1) {
fprintf(stderr, "Process 1 encountered an error ICWD. ERROR%d", errno);
}
}
else{
jobpids[jobs] = pid_1;
job_valid[jobs] = true;
jobs++;
printf("[%d] %d\n", jobs, pid_1);
}
}
//piping
else{
int pfd1[2];
pipe(pfd1);
pid_1 = fork();
if (pid_1 == 0) {
close(pfd1[0]);
dup2(pfd1[1], STDOUT_FILENO);
exec_default(temp2, tempArgs, tempCMD, background);
exit(0);
}
pid_2 = fork();
if (pid_2 == 0) {
close(pfd1[1]);
dup2(pfd1[0], STDIN_FILENO);
strcpy(temp2, tokens);
strcat(temp2, "/");
strcat(temp2, temp3);
//temp ARGS needs to be the output of first fork.
read(pfd1[0], tempArgs, MAX_COMMAND_LENGTH);
exec_default(temp2, tempArgs, tempCMD, background);
}
//only wait if it isn't a background process
if(!background){
if ((waitpid(pid_2, &status, 0)) == -1) {
fprintf(stderr, "Process 1 encountered an error in pipe ICWD. ERROR%d", errno);
}
}
else{
jobpids[jobs] = pid_1;
job_valid[jobs] = true;
jobs++;
printf("[%d] %d\n", jobs, pid_1);
}
}
}
tokens = strtok(NULL, ":");
}
//If none of the paths contained the executable.
if(!ran){
puts("Executable does not exist in PATH.");
}
}
}
/*******************************************************
*
*
* Helper Functions
*
*
*******************************************************/
//**************************************************************************
//change program to write output to file
//**************************************************************************
void exec_greaterThan(char* command, char* args, char* output, char* tokens, bool back){
FILE *fp;
fp = fopen(output, "w");
dup2(fileno(fp), STDOUT_FILENO);
//attemp to execute
if(back){
int tempJob = jobs;
if(args[0] == NULL){
fclose(STDIN_FILENO);
fopen("/dev/null", "r");
if ( (execl(command, command, (char *)0)) < 0) {
fprintf(stderr, "\nError execing %s. ERROR#%d\n", tokens ,errno);
fclose(fp);
return EXIT_FAILURE;
}
fclose(fp);
exit(0);
}
else{
fclose(STDIN_FILENO);
fopen("/dev/null", "r");
if ( (execl(command, command, args, (char *)0)) < 0) {
fprintf(stderr, "\nError execing %s. ERROR#%d\n", tokens ,errno);
fclose(fp);
return EXIT_FAILURE;
}
fclose(fp);
exit(0);
}
}
else{
if(args[0] == NULL){
if ( (execl(command, command, (char *)0)) < 0) {
fprintf(stderr, "\nError execing %s. ERROR#%d\n", tokens ,errno);
fclose(fp);
return EXIT_FAILURE;
}
fclose(fp);
exit(0);
}
else{
if ( (execl(command, command, args, (char *)0)) < 0) {
fprintf(stderr, "\nError execing %s. ERROR#%d\n", tokens ,errno);
fclose(fp);
return EXIT_FAILURE;
}
fclose(fp);
exit(0);
}
}
}
//**************************************************************************
//change program to read in text from file
//**************************************************************************
void exec_lessThan(char* command, char* args, char* output, char* tokens, bool back){
FILE *fp;
fp = fopen(output, "r");
dup2(fileno(fp), STDIN_FILENO);
//attempt to execute
if(back){
if(args[0] == NULL){
fclose(STDIN_FILENO);
fopen("/dev/null", "r");
if ( (execl(command, command, (char *)0)) < 0) {
fprintf(stderr, "\nError execing %s. ERROR#%d\n", tokens ,errno);
fclose(fp);
return EXIT_FAILURE;
}
fclose(fp);
exit(0);
}
else{
fclose(STDIN_FILENO);
fopen("/dev/null", "r");
if ( (execl(command, command, args, (char *)0)) < 0) {
fprintf(stderr, "\nError execing %s. ERROR#%d\n", tokens ,errno);
fclose(fp);
return EXIT_FAILURE;
}
fclose(fp);
exit(0);
}
}
//foreground
else{
if(args[0] == NULL){
if ( (execl(command, command, (char *)0)) < 0) {
fprintf(stderr, "\nError execing %s. ERROR#%d\n", tokens ,errno);
fclose(fp);
return EXIT_FAILURE;
}
fclose(fp);
exit(0);
}
else{
if ( (execl(command, command, args, (char *)0)) < 0) {
fprintf(stderr, "\nError execing %s. ERROR#%d\n", tokens ,errno);
fclose(fp);
return EXIT_FAILURE;
}
fclose(fp);
exit(0);
}
}
}
//**************************************************************************
//execute default executable
//**************************************************************************
void exec_default(char* command, char* args, char* tokens, bool back){
if(back){
int tempJob = jobs;
fclose(STDIN_FILENO);
fopen("/dev/null", "r");
if(args[0] == NULL){
if ( (execl(command, command, (char *)0)) < 0) {
fprintf(stderr, "\nError execing %s. ERROR#%d\n", tokens ,errno);
return EXIT_FAILURE;
}
fopen(STDIN_FILENO, "w");
printf("[%d] %d finished\n", tempJob+1, jobpids[tempJob]);
job_valid[tempJob] = false;
exit(0);
}
else{
if ( (execl(command, command, args, (char *)0)) < 0) {
fprintf(stderr, "\nError execing %s. ERROR#%d\n", tokens ,errno);
return EXIT_FAILURE;
}
fopen(STDIN_FILENO, "r");
printf("[%d] %d finished\n", tempJob+1, jobpids[tempJob]);
job_valid[tempJob] = false;
exit(0);
}
}
else{
if(args[0] == NULL){
if ( (execl(command, command, (char *)0)) < 0) {
fprintf(stderr, "\nError execing %s. ERROR#%d\n", tokens ,errno);
return EXIT_FAILURE;
}
exit(0);
}
else{
if ( (execl(command, command, args, (char *)0)) < 0) {
fprintf(stderr, "\nError execing %s. ERROR#%d\n", tokens ,errno);
return EXIT_FAILURE;
}
exit(0);
}
}
}