1. C Pointers Review - C Pointers - Symbol '*' in C int n = 10; int *ptr = &n; // 'int *' together here is declaring variable 'ptr' // of type 'int *', pointer of int. // &n references n, resovled in the address of n, // which is a 'int *' type. int m = n * *ptr; // first '*' means multiplication, second '*' // means dereferencing. '*ptr' is the 'int value' // pointed by 'ptr'. *ptr = m; // '*' means dereferencing, it will set the 'int' // pointed by 'ptr' to 3. printf("n: %d\n", n); // What should be the output? - Arrays vs. Pointers void func(int *ptr) { *ptr = 3; // recall, '*' here means dereferencing } int i = 10; int *p = &i; // recall, 'int *' here is declaration func(p); // What is the value of i now? Why? - Pointer of pointer type // argv points to an array of 10 entries, each entry is a 'char *' type char *argv[10]; // If we only want to use less than 10 entries in the array, // we can use NULL-terminated method here. NULL-terminated array (each entry is 'char *' type) +------+ +---+---+---+----+---+---+---+---+---+---+ | argv | -> | x | x | x |NULL| | | | | | | +------+ +---+---+---+----+---+---+---+---+---+---+ | | | +----------------+ | +---------------+ v v v NULL-terminated string (each entry is 'char' type) +---+---+---+----+ +---+---+---+----+ +---+---+---+----+ | a | b | c |NULL| | f | o | o |NULL| | b | o | o |NULL| +---+---+---+----+ +---+---+---+----+ +---+---+---+----+ // So that: printf("argv[0]: %s\n", argv[0]); // "abc" printf("argv[1]: %s\n", argv[1]); // "foo" printf("argv[2]: %s\n", argv[2]); // "boo" printf("argv[0][1]: %c\n", argv[0][1]); // 'b' (char type) 2. Linked List struct node_t { int id; char name[10]; struct node_t *next; }; /* Iterate through the sorted list (by node_t::id), and find the position after * which the new 'node' should be inserted in the list. Ensure that the list * is kept sorted. Return the element just before the newly inserted one. * If the new 'node' should be inserted at the beginning, return NULL. */ node_t * find_insert_pos(node_t *head, node_t *node) { if (head == NULL) return NULL; node_t *ret = NULL; // 2.1 your code here while (head != NULL) { // traverse using head if (head->id >= node->id) { break; } else { ret = head; head = head->next; } } return ret; } /* insert a new 'node' into the list 'head', return the new head of the list. node_t * insert(node_t *head, node_t *node) { if (head == NULL) return node; // find the proper position to insert this node pair. node_t *pos = find_insert_pos(head, node); // 2.2 your code here if (pos == NULL) { // insert before head node->next = head; head = node; } else { // insert after pos node->next = pos->next; pos->next = node; } return head; } int main(void) { node_t *student_list = NULL; // init first student Alice node_t *student_1 = (node_t *)malloc(sizeof(node_t)); student_1->id = 1002; strcpy(student_1->name, "Alice"); student_1->next = NULL; // init second student Bob node_t *student_2 = (node_t *)malloc(sizeof(node_t)); student_2->id = 1000; strcpy(student_2->name, "Bob"); student_2->next = NULL; student_list = list_insert(student_list, student_1); student_list = list_insert(student_list, student_2); // now we should have a student list: // <1000, Bob> -> <1002, Alice> -> NULL ... free resources ... return 0; } 3. Shell - Shell implementation // input buffer to store one line input char input[BUFSIZ]; while (1) { parsestate_t parsestate; command_t *cmdlist; // Print the prompt printf("cs202$ "); // Wait and read one line of input fgets(input, BUFSIZ, stdin); // Parse input and build the command list parse_init(&parsestate, input); // The parsed command will be stored as an linked list in cmdlist cmdlist = cmd_line_parse(&parsestate, 0); if (!cmdlist) { printf("Syntax error\n"); continue; } // Execute parsed commands, from the head of the linked list to the end cmd_line_exec(cmdlist); // Free the cmdlist structure cmd_free(cmdlist); } - Executing command(s) list static pid_t cmd_exec(command_t *cmd, int *pass_pipefd); int cmd_line_exec(command_t *cmdlist) { int pipefd = STDIN_FILENO; // read end of last pipe while (cmdlist != NULL) { /*** your code ***/ // Hint: utilize cmd_exec with the aid of pipefd cmdlist = cmdlist->next; } }