CSC20924s A3 CSCSHELL

CSC20924s A3 CSCSHELL
Copyright 2024 Demetres Kostas PhD aka Darlene Heliokinde

include cscshell.h

int cdcscshellconst char targetdir
if targetdir NULL
char userbuffMAXUSERBUF;
if getloginruserbuff, MAXUSERBUF ! 0
perrorruncommand;

struct passwd pwdata getpwnamchar userbuff;
if pwdata NULL
perrorruncommand;

targetdir pwdatapwdir;

ifchdirtargetdir 0
perrorcdcscshell;

int executelineCommand head
ifdef DEBUG
printfBEGIN: Executing line…n;

ifdef DEBUG
printfAll children createdn;

Wait for all the children to finish

ifdef DEBUG
printfAll children finishedn;

ifdef DEBUG
printfEND: Executing line…n;

return NULL;

Forks a new process and execs the command
making sure all file descriptors are set up correctly.

Parent process returns 1 on error.
Any child processes should not return.

int runcommandCommand command
ifdef DEBUG
printfRunning command: sn, commandexecpath;
printfArgvs: ;
if commandargs NULL
printfNULLn;

else if commandargs0 NULL
printfEmptyn;

for int i0; commandargsi ! NULL; i
printfd: s , i1, commandargsi;

printfRedir out: sn Redir in: sn,
commandrediroutpath, commandredirinpath;
printfStdin fd: d Stdout fd: dn,
commandstdinfd, commandstdoutfd;

ifdef DEBUG
printfParent process created child PID d for sn, pid, commandexecpath;

int runscriptchar filepath, Variable root

void freecommandCommand command