-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.c
1339 lines (1120 loc) · 25.2 KB
/
runtime.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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Title: Runtime environment
* -------------------------------------------------------------------------
* Purpose: Runs commands
* Author: Stefan Birrer
* Version: $Revision: 1.3 $
* Last Modification: $Date: 2009/10/12 20:50:12 $
* File: $RCSfile: runtime.c,v $
* Copyright: (C) 2002 by Stefan Birrer
***************************************************************************/
/***************************************************************************
* ChangeLog:
* -------------------------------------------------------------------------
* $Log: runtime.c,v $
* Revision 1.3 2009/10/12 20:50:12 jot836
* Commented tsh C files
*
* Revision 1.2 2009/10/11 04:45:50 npb853
* Changing the identation of the project to be GNU.
*
* Revision 1.1 2005/10/13 05:24:59 sbirrer
* - added the skeleton files
*
* Revision 1.6 2002/10/24 21:32:47 sempi
* final release
*
* Revision 1.5 2002/10/23 21:54:27 sempi
* beta release
*
* Revision 1.4 2002/10/21 04:49:35 sempi
* minor correction
*
* Revision 1.3 2002/10/21 04:47:05 sempi
* Milestone 2 beta
*
* Revision 1.2 2002/10/15 20:37:26 sempi
* Comments updated
*
* Revision 1.1 2002/10/15 20:20:56 sempi
* Milestone 1
*
***************************************************************************/
#define __RUNTIME_IMPL__
/************System include***********************************************/
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
/************Private include**********************************************/
#include "runtime.h"
#include "io.h"
/************Defines and Typedefs*****************************************/
/* #defines and typedefs should have their names in all caps.
* Global variables begin with g. Global constants with k. Local
* variables should be in all lower case. When initializing
* structures and arrays, line everything up in neat columns.
*/
/************Global Variables*********************************************/
int PATHSIZE = 512;
#define NBUILTINCOMMANDS (sizeof BuiltInCommands / sizeof(char*))
/*Set current running process to non-existent*/
//crpid=-1;
/************Function Prototypes******************************************/
/* run command */
static void
RunCmdFork(commandT*, bool);
/* runs an external program command after some checks */
static void
RunExternalCmd(commandT*, bool, bool);
/* resolves the path and checks for exutable flag */
static bool
ResolveExternalCmd(commandT*, char*);
/* forks and runs a external program */
static void
Exec(char*, commandT*, bool, bool);
/* runs a builtin command */
static void
RunBuiltInCmd(commandT*);
/* checks whether a command is a builtin command */
static bool
IsBuiltIn(char*);
/*Checks whether a command should be run in background*/
static bool
IsBG(commandT*);
/*Makes a job for the job list*/
bgjobL*
MakeJob(pid_t, char*, char*);
/*GetNewJid*/
int
GetNewJid();
/*Creates a string from the argument list */
char*
stringFromArgv(char**, int);
/*Gets a job given its jid */
bgjobL*
GetJobFromJid(int);
/*Gets the most recently add job */
bgjobL*
GetMRJob();
/*Looks for redirection commands and redirects IO*/
void
RedirectIO(commandT*, int*, int*);
/*Resets IO to stdin and stdout*/
void
ResetIO(int*, int*);
aliasL*
makeAlias(commandT*);
void
addAlias(commandT*);
void
deleteAlias(char*);
void
printAliasList();
bool
isAlias(char*);
aliasL*
getAlias(char* cmdName);
/************External Declaration*****************************************/
/**************Implementation***********************************************/
/*
* RunCmd
*
* arguments:
* commandT *cmd: the command to be run
*
* returns: none
*
* Runs the given command.
*/
void
RunCmd(commandT* cmd)
{
if (cmd->argc <= 0){
return;
}
if (IsBG(cmd)){
//printf("this is BGGGG");
RunCmdBg(cmd);
}
else{
RunCmdFork(cmd, TRUE);
}
} /* RunCmd */
/*
* RunCmdFork
*
* arguments:
* commandT *cmd: the command to be run
* bool fork: whether to fork
*
* returns: none
*
* Runs a command, switching between built-in and external mode
* depending on cmd->argv[0].
*/
void
RunCmdFork(commandT* cmd, bool fork)
{
if (cmd->argc <= 0)
return;
if (IsBuiltIn(cmd->argv[0]))
{
RunBuiltInCmd(cmd);
}
else
{
RunExternalCmd(cmd, fork, FALSE);
}
} /* RunCmdFork */
/*
* RunCmdBg
*
* arguments:
* commandT *cmd: the command to be run
*
* returns: none
*
* Runs a command in the background.
*/
void
RunCmdBg(commandT* cmd)
{
// TODO
RunExternalCmd(cmd, TRUE, TRUE);
//free(newCmd);
return;
} /* RunCmdBg */
/*
* RunCmdPipe
*
* arguments:
* commandT *cmd1: the commandT struct for the left hand side of the pipe
* commandT *cmd2: the commandT struct for the right hand side of the pipe
*
* returns: none
*
* Runs two commands, redirecting standard output from the first to
* standard input on the second.
*/
void
RunCmdPipe(commandT* cmd)
{
commandT* t = cmd;
int i = 0;
int j;
while (t != NULL){
t = t->next;
i++;
}
int cmdCount = i;
commandT* temp = cmd;
sigset_t *set;
set = 0;
sigemptyset(set);
sigaddset(set, SIGCHLD);
sigprocmask(SIG_BLOCK, set, 0);
pid_t pid;
int status=0;
/*Create cmdCount file descriptors for piping*/
int fd[cmdCount-1][2];
/*Set pipe for first command reading from stdin, writing to fd[0][1]*/
pipe(fd[0]);
pid=fork();
if ( pid == 0){
/*first child*/
close(fd[0][0]);
/*Redirect stdout to output pipe of file descriptor 0*/
dup2(fd[0][1], STDOUT_FILENO);
close(fd[0][1]);
sigprocmask(SIG_UNBLOCK, set, 0);
setpgid(0,0);
/*Resolve external path*/
char* path = (char*)malloc(sizeof(char)*PATHSIZE);
if(ResolveExternalCmd(temp, path)){
if(execv(path,temp->argv)==-1){
printf("%s\n", path);
printf("Error: %s\n", strerror(errno));
}
}
free(path);
exit(1);
}
else if (pid > 0)
{
/*For all future commands, reading from pipe i-1 and write to i*/
for(i=1; i<cmdCount; i++)
{
temp = temp->next;
/*create pipe to write into unless it is the last command*/
if ((i+1)!=cmdCount)
pipe(fd[i]);
pid = fork();
if (pid == 0)
{
/*set up input for child to read from.*/
close(fd[i-1][1]);
dup2(fd[i-1][0], STDIN_FILENO);
close(fd[i-1][0]);
/*Unless this is the last command, write into current pipe for next command to read*/
if(i+1 != cmdCount)
{
close(fd[i][0]);
dup2(fd[i][1], STDOUT_FILENO);
close(fd[i][1]);
}
/*Close all pipes*/
for(j=0;j<i;j++)
{
close(fd[j][1]);
close(fd[j][0]);
}
sigprocmask(SIG_UNBLOCK, set, 0);
setpgid(0,0);
/*Resolve external path*/
char* path = (char*)malloc(sizeof(char)*PATHSIZE);
if(ResolveExternalCmd(temp, path)){
if(execv(path,temp->argv)==-1){
//printf("%s\n", path);
//printf("Error: %s\n", strerror(errno));
}
}
free(path);
exit(1);
}
}
/*Make sure pipes are closed*/
for (j=0;j<cmdCount-1;j++)
{
close(fd[j][1]);
close(fd[j][0]);
}
sigprocmask(SIG_UNBLOCK, set, 0);
waitpid(pid,&status,WUNTRACED);
while (!WIFEXITED(status));
}
else
{
fprintf(stderr, "error in piping.");
}
}
/* RunCmdPipe */
/*RedirectIO
*
* arguments:
* commandT *cmd: the command to be run
*
* returns: none
*
* Checks for "<" and >".
* Updates argv to include up to first redirection.
* Redirect stdin and stdout.
*/
void
RedirectIO(commandT* cmd, int* stdinFd, int* stdoutFd){
int i = 0;
bool hasRedirection = FALSE;
while (cmd->argv[i] != 0){
if (cmd->argv[i][0] == '<'){
//printf("Found '<'.\n");
RunCmdRedirIn(cmd, cmd->argv[i+1], stdinFd);
hasRedirection = TRUE;
}
else if (cmd->argv[i][0] == '>'){
//printf("Found '>'. Next: %s\n", cmd->argv[i+1]);
RunCmdRedirOut(cmd, cmd->argv[i+1], stdoutFd);
hasRedirection = TRUE;
}
i++;
}
if (hasRedirection){
/*Updating argv*/
i=0;
while ((cmd->argv[i][0] != '>') && (cmd->argv[i][0] != '<')){
i++;
}
/*Free arguments from index i and higher*/
while (cmd->argv[i] != 0){
free(cmd->argv[i]);
cmd->argv[i] = 0;
cmd->argc--;
i++;
}
}
return;
}
/*RedirectIO*/
/*
* RunCmdRedirOut
*
* arguments:
* commandT *cmd: the command to be run
* char *file: the file to be used for standard output
*
* returns: none
*
* Runs a command, redirecting standard output to a file.
*/
void
RunCmdRedirOut(commandT* cmd, char* file, int* stdoutFd)
{
fflush(stdout);
*stdoutFd = dup(1);
int fid = open(file, O_WRONLY | O_CREAT, 0755);
close(1);
dup(fid);
close(fid);
return;
} /* RunCmdRedirOut */
/*
* RunCmdRedirIn
*
* arguments:
* commandT *cmd: the command to be run
* char *file: the file to be used for standard input
*
* returns: none
*
* Runs a command, redirecting a file to standard input.
*/
void
RunCmdRedirIn(commandT* cmd, char* file, int* stdinFd)
{
//printf("file: %s\n", file);
*stdinFd = dup(0);
int fid = open(file, O_RDONLY);
close(0);
dup(fid);
close(fid);
return;
} /* RunCmdRedirIn */
/*
* RunExternalCmd
*
* arguments:
* commandT *cmd: the command to be run
* bool fork: whether to fork
*
* returns: none
*
* Tries to run an external command.
*/
static void
RunExternalCmd(commandT* cmd, bool fork, bool bground)
{
char* path = (char*)malloc(sizeof(char)*PATHSIZE);
if(ResolveExternalCmd(cmd, path)){
Exec(path, cmd, fork, bground);
}
free(path);
} /* RunExternalCmd */
/*ResetIO
*
* arguments:
* int *stdinFd: File descriptor to close and set stdin to
* int *stdoutFd: File descriptor to close and set stdout to
*
* returns: none
*
* Resets IO to stdin and stdout*/
void
ResetIO(int* stdinFd, int* stdoutFd)
{
if (*stdinFd != -1){
close(*stdinFd);
dup(0);
close(0);
}
if (*stdoutFd != -1){
close(*stdoutFd);
dup(1);
close(1);
}
return;
}
/*ResetIO*/
/*
* ResolveExternalCmd
*
* arguments:
* commandT *cmd: the command to be run
*
* returns: bool: whether the given command exists
*
* Determines whether the command to be run actually exists.
*/
static bool
ResolveExternalCmd(commandT* cmd, char* path)
{
//printf("Command: %s\n", cmd->name);
//Check to see if in home directory:
if(*(cmd->argv[0])=='.'){
char* cwd = getCurrentWorkingDir();
//char* cwd;
//getCurrentWorkingDir(cwd);
sprintf(path,"%s/%s",cwd,cmd->name);
free(cwd);
return TRUE;
}
char** memLocs = getPath();
char dest[500];
int i=0;
struct stat buf;
/*If already absolute path*/
if(stat(cmd->name,&buf)==0){
/*Set path = to entered absolute path*/
strcpy(path,cmd->name);
freePath(memLocs);
return TRUE;
}
while(memLocs[i]!=NULL){
//Concatanate Paths with cmd->name:
int size = (snprintf( dest, 499,"%s/%s",memLocs[i],cmd->name)+1)*sizeof(char);
char* exeName = (char*)malloc(size);
sprintf(exeName,"%s/%s",memLocs[i],cmd->name);
//printf("%s/%s\n", memLocs[i], cmd->name);
//Check to see if exists and executable:
if(stat(exeName,&buf)==0){
if(S_ISREG(buf.st_mode) && buf.st_mode & 0111){
strncpy(path,exeName,size);
freePath(memLocs);
free(exeName);
return TRUE;
}
char* error = malloc(PATHSIZE * sizeof(char*));
strcpy(error, "line 1: "); //since script reading is not in the functionality, it will be an error on line 1
strcat(error, cmd->argv[0]);
PrintPError(error);
free(error);
freePath(memLocs);
free(exeName);
return FALSE;
}
i++;
free(exeName);
}
char* error = malloc(PATHSIZE * sizeof(char*));
strcpy(error, "line 1: "); //since script reading is not in the functionality, it will be an error on line 1
strcat(error, cmd->argv[0]);
PrintPError(error);
free(error);
freePath(memLocs);
return FALSE;
} /* ResolveExternalCmd */
/*
* Exec
*
* arguments:
* commandT *cmd: the command to be run
* bool forceFork: whether to fork
*
* returns: none
*
* Executes a command.
*/
static void
Exec(char* path, commandT* cmd, bool forceFork, bool bground)
{
if (cmd->piped){
RunCmdPipe(cmd);
}
else{
pid_t child_pid;
int childStat;
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigprocmask(SIG_BLOCK, &mask, NULL);
child_pid=fork();
/*Fork Worked?*/
if(child_pid >= 0){
/*If child process*/
if(child_pid==0){
int stdinFd = -1;
int stdoutFd = -1;
sigprocmask(SIG_UNBLOCK, &mask, NULL);
RedirectIO(cmd, &stdinFd, &stdoutFd);
if(bground){
cmd->argc--;
free(cmd->argv[cmd->argc]);
cmd->argv[cmd->argc] = 0;
}
setpgid(0,0);
if(execv(path,cmd->argv)==-1)
printf("Error: %s\n", strerror(errno));
ResetIO(&stdinFd, &stdoutFd);
fflush(stdout);
}
/*In Parent Process*/
else{
crpid = child_pid;
crName = stringFromArgv(cmd->argv, cmd->argc);
if (!bground){
sigprocmask(SIG_UNBLOCK, &mask, NULL);
waitpid(crpid, &childStat, WUNTRACED);
crName = NULL;
crpid = 0;
}
else{
AddJob(crpid, stringFromArgv(cmd->argv, cmd->argc-1), "Running");
sigprocmask(SIG_UNBLOCK, &mask, NULL);
}
}
}
}
} /* Exec */
/*
* IsBuiltIn
*
* arguments:
* char *cmd: a command string (e.g. the first token of the command line)
*
* returns: bool: TRUE if the command string corresponds to a built-in
* command, else FALSE.
*
* Checks whether the given string corresponds to a supported built-in
* command.
*/
static bool
IsBuiltIn(char* cmd)
{
/*cd command*/
/* "pwd" command*/
if(!strcmp(cmd,"cd") ||
!strcmp(cmd,"pwd") ||
!strcmp(cmd,"jobs")||
!strcmp(cmd,"fg") ||
!strcmp(cmd,"bg") ||
!strcmp(cmd,"alias")||
!strcmp(cmd,"unalias")||
!strcmp(cmd,"exit")
){
return TRUE;
}
else
return FALSE;
} /* IsBuiltIn */
/*
* RunBuiltInCmd
*
* arguments:
* commandT *cmd: the command to be run
*
* returns: none
*
* Runs a built-in command.
*/
static void
RunBuiltInCmd(commandT* cmd)
{
if(strcmp(cmd->argv[0],"cd")==0){
changeWorkingDir(cmd->argv[1]);
}
/* "pwd" command*/
else if(strcmp(cmd->argv[0],"pwd")==0){
char *dir = getCurrentWorkingDir();
printf("%s\n",dir);
free(dir);
}
else if(!strcmp(cmd->argv[0],"jobs")){
bgjobL* job = bgjobs;
while (job != NULL){
if (!strcmp(job->status, "Running")){
printf("[%d] %s %s &\n", job->jid, job->status, job->name);
}
else{
printf("[%d] %s %s\n", job->jid, job->status, job->name);
}
job = job->next;
}
}
else if (!strcmp(cmd->argv[0],"fg")){
bgjobL* job;
if(cmd->argv[1] != NULL){
job = GetJobFromJid(atoi(cmd->argv[1]));
}
else{
job = GetMRJob();
}
//bring bg process to foreground
if (job != NULL){
tcsetpgrp(job->pid, STDIN_FILENO);
crpid = job->pid;
crName = job->name;
DeleteJob(job->pid);
kill(-crpid, SIGCONT);
int status = 0;
waitpid(crpid, &status, WUNTRACED);
crpid = 0;
crName = NULL;
}
}
else if (!strcmp(cmd->argv[0],"bg")){
bgjobL* job;
if(cmd->argv[1] != NULL){
job = GetJobFromJid(atoi(cmd->argv[1]));
if (job != NULL){
kill(job->pid, SIGCONT);
job->status = "Running";
}
}
else{
job = GetMRJob();
if (job != NULL){
kill(job->pid, SIGCONT);
job->status = "Running";
}
}
}
else if(!strcmp(cmd->argv[0],"alias")){
addAlias(cmd);
}
else if(!strcmp(cmd->argv[0],"unalias")){
deleteAlias(cmd->argv[1]);
}
else if(!strcmp(cmd->argv[0],"exit")){
return;
}
} /* RunBuiltInCmd */
/*
* CheckJobs
*
* arguments: none
*
* returns: none
*
* Checks the status of running jobs.
*/
void
CheckJobs()
{
bgjobL* job = bgjobs;
int wait_settings;
wait_settings = WNOHANG | WUNTRACED | WCONTINUED;
while (job != NULL){
int status;
pid_t pid = waitpid(-1, &status, wait_settings);
if (pid > 0){
if (pid == job->pid){
if (WIFCONTINUED(status)){
job->status = "Running";
}
else{
job->status = "Done";
printf("[%d] %s %s\n", job->jid, job->status, job->name);
DeleteJob(job->pid);
}
}
}
job = job->next;
}
} /* CheckJobs */
/*MakeJobs
*/
bgjobL*
MakeJob(pid_t pid, char* name, char* status){
//printf("making job with name: %s\n", name);
bgjobL* newjob = (bgjobL*)malloc(sizeof(bgjobL));
newjob->pid = pid;
newjob->name = name;
newjob->status = status;
newjob->next = NULL;
newjob->jid = GetNewJid();
//printf("made job\n");
return newjob;
}//MakeJob
/*AddJob
*
*
*
*/
void
AddJob(pid_t pid, char* name, char* status){
bgjobL* current = bgjobs;
//printf("added job\n");
if (current == NULL){
bgjobs = MakeJob(pid, name, status);
return;
}
else{
while (current->next != NULL){
current = current->next;
}
current->next = MakeJob(pid, name, status);
return;
}
}//AddJob
/*
*DeleteJobs
*
*
*
*/
void
DeleteJob(pid_t pid){
bgjobL* current = bgjobs;
bgjobL* prev = current;
if (current == NULL){
return;
}
if (current->pid == pid){
bgjobs = current->next;
free(current);
return;
}
while (current->next != NULL){
if (current->pid == pid){
continue;
}
prev = current;
current = current->next;
}
prev->next = current->next;
free(current->name);
free(current);
}//DeleteJob
/*GetJob
*
*
*
*/
bgjobL*
GetJob(pid_t pid){
bgjobL* job = bgjobs;
while (job != NULL){
if (job->pid == pid){
break;
}
job = job->next;
}
return job;
}//GetJob
/*
*GetMRJob
*
*Gets most recently added background job.
*
*
*/
bgjobL*
GetMRJob(){
bgjobL* job = bgjobs;
while (job->next != NULL)
job = job->next;
return job;
}//GetMRJob
/*GetJobJid
*Gets a job based on its jid
*
*/
bgjobL*
GetJobFromJid(int jid){
bgjobL* job = bgjobs;
while (job != NULL){
if (job->jid == jid){
break;
}
job = job->next;
}
return job;
}//GetJob
/*
* getCurrentWorkingDir (NO ARGS)
*
* arguments: none
*
* returns: outputs current directory
*
* Print Working Directory.
*/
char*
getCurrentWorkingDir(){
long size;
char *buf;
char *ptr =0;
size = pathconf(".", _PC_PATH_MAX);
if((buf = (char*)malloc((size_t)size)) != NULL)
ptr = getcwd(buf,(size_t)size);
return ptr;
}/* getCurrentWorkingDir*/
/*
* changeWorkingDir
*
* arguments: none
*
* returns: outputs current directory
*
* Print Working Directory.
*/
void
changeWorkingDir(char* path){
/*Change directory to given path and set environment "PWD" variable to match*/
if(path==NULL){
char* home = getenv("HOME");
chdir(home);
}
else
chdir(path);
//if(chdir(path)!=0)
// printf("Error: %s\n", strerror(errno));
char *cwd = getCurrentWorkingDir();