Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lecture Notes for Command Line Arguments - Slides | CMSC 212, Study notes of Computer Science

Material Type: Notes; Professor: Hollingsworth; Class: INTRO TO LOW-LEVEL PROG; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-e86-1
koofers-user-e86-1 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 15

Toggle sidebar

Related documents


Partial preview of the text

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 { โ€ฆ } โ€ฆ.. }
Docsity logo



Copyright ยฉ 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved