Download Lecture Notes for Command Line Arguments - Slides | CMSC 212 and more Study notes Computer Science in PDF only on Docsity! 1CMSC 212 โ S05 (lect 17) Announcements Program #2 due today Midterm #2 is next Tuesday โ same time and place as last time 2CMSC 212 โ S05 (lect 17) Command Line Arguments int main(int argc, char *argv[]) โ argc - number of arguments (including program name) โ argv - array of command line arguments โข argv[0] - command invoked to start program โข argv[n] - n'th command line argument Example: โ ./foo -file myfile -help โ argv[0] = "./foo" โ argv[1] = "-file" โ argv[2] = "myfile" โ argv[3] = "-help" 5CMSC 212 โ S05 (lect 17) Project 1B Using Table Driven Parsing typedef struct { char *name; int opCode; int numRegisters; int usesMem; } opInfo; static opInfo opTable[] = { { "Load", 1, 1, 1 }, { "Move", 2, 2, 0 }, { "Store", 3, 1, 1 }, { "Add", 4, 3, 0 }, { "Halt", 5, 0, 0 }, { "Negate", 6, 1, 0 }, { "Branch", 7, 2, 1 }, { "Bnn", 8, 1, 1 }, { "Input", 10, 1, 0 }, { "Output", 11, 1, 0 }, { "Data", -1, 0, 1 }, }; 6CMSC 212 โ S05 (lect 17) Environment Variables Keyword, argument pairs Allow passing info into programs Examples: โ PATH - used by shell to find programs โ PRINTER - default printer to use Passed as the third parameter to main โ int main(int argc, char *argv[], char *envp[]) โ consists of a null terminated array of characters โข KEYWORD=VALUE stored in each one C helper functions: โ char *getenv(const char *keyword); โ char *putenv(const char *string); โข where string is KEYWORD=VALUE 7CMSC 212 โ S05 (lect 17) Environment Variable Example Shell Commands to Setup Environement: โ C Shell: setenv FOO_CONFIG ~/foo โ Bash: FOO_CONFIG=~/foo main(int argc, char *argv[], char *envp[]) { char *configDirectory; configDirectory = getenv("FOO_CONFIG"); } 10CMSC 212 โ S05 (lect 17) Learning About Other Processes Wait and waitpid system call โ #include <sys/types.h> โ #include <sys/wait.h> โ pid_t wait(int *status); โข wait until a child process terminates โ pid_t waitpid(pid_t pid, int *status, int options); โข wait until the passed process terminates โ return is the id the the terminated process โ status fills out a status variable โข WIFEXITED(status) - true if the child terminated normally โข WEXITSTATUS(status) - low 8 bits of parameter to exit โข WTERMSIG(status) - signal number that terminated process 11CMSC 212 โ S05 (lect 17) Invoking a new program exec system calls โ int execv(const char *prog, char *const argv[]); โข run the program in the passed prog parameter โข pass the new program argv โ int execvp(const char *file, char *const argv[]); โข like execv, but used the PATH environment variable On success, โ new program launched, the system call does not return On failure, โ returns -1, sets global errno to indicate cause of the error 12CMSC 212 โ S05 (lect 17) Fork and Exec Example char *args[] = { "ls", "-l", "-a", NULL }; โฆ. pid = fork(); if (pid == 0) { ret = execvp("ls", args); if (ret) { perror("execvp"); exit(-1); } } else if (pid > 0) { โฆ } else { โฆ } โฆ.. }