user_id
stringlengths
24
24
time
stringlengths
24
24
feature_type
stringclasses
5 values
feature_version
stringclasses
2 values
input_question
stringlengths
1
1.24k
input_code
stringlengths
2
18.5k
input_intention
stringlengths
1
2.48k
input_task_description
stringlengths
18
3.32k
63c648c65f004bf4898cff95
2023-01-25T23:46:24.618Z
Help Fix Code
v1
null
void fib(int **sequence, int n) { *sequence = malloc(sizeof(int) * n); *sequence[0] = 0; if (n > 1) { *sequence[1] = 1; for (int i = 2; i < n; i++) { *sequence[i] = *sequence[i - 2] + *sequence[i - 1]; } } } int main(int argc, char **argv) { /* do not change this main function */ int count = strtol(argv[1], NULL, 10); int *fib_sequence; fib(&fib_sequence, count); for (int i = 0; i < count; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
no segmentation fault
null
63c648c65f004bf4898cff95
2023-01-26T17:02:08.983Z
General Question
v1
does scanf take in one line of input at a time?
null
null
null
63c648c65f004bf4898cff95
2023-01-26T17:02:51.552Z
General Question
v1
if there are multiple lines of input, do I need multiple scanf calls, one for each line?
null
null
null
63c648c65f004bf4898cff95
2023-01-28T02:27:24.174Z
Question from Code
v1
why does this code produce a bus error?
int check_permissions(char *file_perm, char *min_perm) { for (int i = 0; i < 9; i++) { if(file_perm[i] == '-' && min_perm[i] != '-') { return 1; } } return 0; } int main(int argc, char** argv) { if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1; } // TODO: Process command line arguments. int min_size = strtol(argv[1], NULL, 10); char *min_permission; if (argc == 3) { min_permission = argv[2]; } scanf("%*s"); //the first line of input from ls -l, to be discarded int count = 0; //the number of files large enough and have the right permission char* file_size; //the size of the file read by scanf char* file_permission; //permission field of a file while (scanf("%*c%s %*s %*s %*s %s %*s %*s %*s %*s",file_permission, file_size) != EOF) { if (strtol(file_size, NULL, 10) >= min_size) { //checks if file is large enough if (argc == 2) { count++; } if (argc == 3){ if(check_permissions(file_permission, min_permission) == 0) { count++; } } } } // TODO: Call check_permissions as part of your solution to count the files to // compute and print the correct value. printf("%d\n",count); return 0; }
null
null
63c648c65f004bf4898cff95
2023-01-28T02:28:26.834Z
Question from Code
v1
why does this code produce a bus error when run from the terminal?
int check_permissions(char *file_perm, char *min_perm) { for (int i = 0; i < 9; i++) { if(file_perm[i] == '-' && min_perm[i] != '-') { return 1; } } return 0; } int main(int argc, char** argv) { if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1; } // TODO: Process command line arguments. int min_size = strtol(argv[1], NULL, 10); char *min_permission; if (argc == 3) { min_permission = argv[2]; } scanf("%*s"); //the first line of input from ls -l, to be discarded int count = 0; //the number of files large enough and have the right permission char* file_size; //the size of the file read by scanf char* file_permission; //permission field of a file while (scanf("%*c%s %*s %*s %*s %s %*s %*s %*s %*s",file_permission, file_size) != EOF) { if (strtol(file_size, NULL, 10) >= min_size) { //checks if file is large enough if (argc == 2) { count++; } if (argc == 3){ if(check_permissions(file_permission, min_permission) == 0) { count++; } } } } // TODO: Call check_permissions as part of your solution to count the files to // compute and print the correct value. printf("%d\n",count); return 0; }
null
null
63c648c65f004bf4898cff95
2023-01-28T02:29:21.548Z
Help Fix Code
v1
null
int check_permissions(char *file_perm, char *min_perm) { for (int i = 0; i < 9; i++) { if(file_perm[i] == '-' && min_perm[i] != '-') { return 1; } } return 0; } int main(int argc, char** argv) { if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1; } // TODO: Process command line arguments. int min_size = strtol(argv[1], NULL, 10); char *min_permission; if (argc == 3) { min_permission = argv[2]; } scanf("%*s"); //the first line of input from ls -l, to be discarded int count = 0; //the number of files large enough and have the right permission char* file_size; //the size of the file read by scanf char* file_permission; //permission field of a file while (scanf("%*c%s %*s %*s %*s %s %*s %*s %*s %*s",file_permission, file_size) != EOF) { if (strtol(file_size, NULL, 10) >= min_size) { //checks if file is large enough if (argc == 2) { count++; } if (argc == 3){ if(check_permissions(file_permission, min_permission) == 0) { count++; } } } } // TODO: Call check_permissions as part of your solution to count the files to // compute and print the correct value. printf("%d\n",count); return 0; }
no bus error
null
63c648c65f004bf4898cff95
2023-01-28T02:32:42.283Z
Question from Code
v1
why is the value of count always 0 at the end?
#include <stdio.h> #include <stdlib.h> // TODO: Implement a helper named check_permissions that matches the prototype below. int check_permissions(char *file_perm, char *min_perm) { for (int i = 0; i < 9; i++) { if(file_perm[i] == '-' && min_perm[i] != '-') { return 1; } } return 0; } int main(int argc, char** argv) { if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1; } // TODO: Process command line arguments. int min_size = strtol(argv[1], NULL, 10); char *min_permission; if (argc == 3) { min_permission = argv[2]; } scanf("%*s"); //the first line of input from ls -l, to be discarded int count = 0; //the number of files large enough and have the right permission char* file_size = malloc(sizeof(long)); //the size of the file read by scanf char* file_permission = malloc(sizeof(char) * 9); //permission field of a file while (scanf("%*c%s %*s %*s %*s %s %*s %*s %*s %*s",file_permission, file_size) != EOF) { if (strtol(file_size, NULL, 10) >= min_size) { //checks if file is large enough if (argc == 2) { count++; } if (argc == 3){ if(check_permissions(file_permission, min_permission) == 0) { count++; } } } } // TODO: Call check_permissions as part of your solution to count the files to // compute and print the correct value. printf("%d\n",count); return 0; }
null
null
63c648c65f004bf4898cff95
2023-01-29T03:33:19.365Z
Question from Code
v1
why does the scarf call not skip the first character in the line of input read?
char* file_size = malloc(sizeof(char) * 31); //the size of the file read by scanf char* file_permission = malloc(sizeof(char) * 31); //permission field of a file scanf("%*c%*c%s %*s %*s %*s %s %*s %*s %*s %*s",file_permission, file_size)
null
null
63c648c65f004bf4898cff95
2023-01-29T03:33:52.034Z
Question from Code
v1
why does the scarf call not skip the first character in the line of input read?
char* file_size = malloc(sizeof(char) * 31); //the size of the file read by scanf char* file_permission = malloc(sizeof(char) * 31); //permission field of a file scanf("%*c%s %*s %*s %*s %s %*s %*s %*s %*s",file_permission, file_size)
null
null
63c648c65f004bf4898cff95
2023-01-29T03:34:34.185Z
Help Fix Code
v1
null
char* file_size = malloc(sizeof(char) * 31); //the size of the file read by scanf char* file_permission = malloc(sizeof(char) * 31); //permission field of a file scanf("%*c%s %*s %*s %*s %s %*s %*s %*s %*s",file_permission, file_size)
scanf skips the first character of the line of input
null
63c648c65f004bf4898cff95
2023-02-12T21:42:06.925Z
General Question
v1
how to display ascii file in terminal in C?
null
null
null
63c648c65f004bf4898cff95
2023-02-14T02:42:41.431Z
General Question
v1
suppose we have the following line at the top of our program #define MAXNAME = 32; and then the declaration char name[MAXNAME]; in the program. What will this declaration line become after the program has passed through the C pre-processor?
null
null
null
63c648c65f004bf4898cff95
2023-02-17T07:02:38.381Z
Question from Code
v1
why does this code produce a bus error?
#include <stdio.h> #include <string.h> int main() { char* dest = "123456789"; char* src = "abc"; strncpy(dest, src, 5); printf("%s\n", dest); return 0; }
null
null
63c648c75f004bf4898cff9f
2023-01-24T03:13:23.598Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> int **split_array(const int *s, int length) { int num_even_indices; if (length % 2 == 0) { num_even_indices = length / 2; } else { num_even_indices = (length / 2) + 1; } int **arr = malloc(2 * sizeof(int*)); arr[0] = malloc(num_even_indices * sizeof(int)); // Array for even indices arr[1] = malloc((length - num_even_indices) * sizeof(int)); // Array for odd indices int even_index; int odd_index; for (int i = 0; i < length; i++) { if (i % 2 == 0) { arr[0][even_index] = s[i]; even_index += 1; } else { arr[1][odd_index] = s[i]; odd_index += 1; } } return arr; } /* Return a pointer to an array of ints with size elements. - strs is an array of strings where each element is the string representation of an integer. - size is the size of the array */ int *build_array(char **strs, int size) { int *arr = malloc(size * sizeof(int)); for (int i = 0; i < size; i++) { // Starting at i = 1 because argv[0] is not used. arr[i] = strtol(strs[i + 1], NULL, 10); } return arr; } int main(int argc, char **argv) { /* Replace the comments in the next two lines with the appropriate arguments. Do not add any additional lines of code to the main function or make other changes. */ int *full_array = build_array(argv, argc - 1); int **result = split_array(full_array, argc - 1); // Length of full_array is argc - 1 because it does not include argv[0]. printf("Original array:\n"); for (int i = 0; i < argc - 1; i++) { printf("%d ", full_array[i]); } printf("\n"); printf("result[0]:\n"); for (int i = 0; i < argc / 2; i++) { printf("%d ", result[0][i]); } printf("\n"); printf("result[1]:\n"); for (int i = 0; i < (argc - 1) / 2; i++) { printf("%d ", result[1][i]); } printf("\n"); free(full_array); free(result[0]); free(result[1]); free(result); return 0; }
Split input array into two arrays. The first array contains the elements of the input array that are at even indices. The second array contains the elements of the input array s that are at odd indices. Print the arrays.
null
63c648c75f004bf4898cff9f
2023-01-25T13:38:16.033Z
Help Fix Code
v1
null
#include <stdio.h> #include <string.h> #include <stdlib.h> struct contact { char name[30]; int phone_number; char address[30]; }; void update_contact(struct contact *friend, char *name, int number, char *address) { strcpy((*friend).name, *name); (*friend).phone_number = number; strcpy((*friend).address, *address); } int main(int argc, char **argv) { struct contact friend; // set initial values for friend strcpy(friend.name, "Jane Doe"); friend.phone_number = 377177377; strcpy(friend.address, "3771 University Avenue"); char *name; int number; char *address; // in hidden code set values for these 3 variables // call update_contact to change the values in friend update_contact(&friend, name, number, address); return 0; }
update the struct contact friend
null
63c648c75f004bf4898cff9f
2023-01-25T13:49:50.203Z
Help Fix Code
v1
null
struct Node { int value; struct Node *next; };
create node struct
null
63c648c75f004bf4898cff9f
2023-01-25T13:50:34.878Z
Help Fix Code
v1
null
struct Node { int value; struct node *next; };
create a node struct
null
63c648c75f004bf4898cff9f
2023-01-25T14:38:29.220Z
Help Fix Code
v1
null
typedef struct node { int value; struct node *next; } Node; int main() { Node *front; Node *curr = front; while ((*curr).next != NULL) { printf("%d ", (*curr).value); curr = (*curr).next; } printf("%d ", (*curr).value); return 0; }
traverse and print the whole linked list
null
63c648c75f004bf4898cff9f
2023-01-25T14:41:10.705Z
Explain Code
v1
null
typedef struct node { int value; struct node *next; } Node; int main() { Node *front; Node *curr = front; while ((*curr).next != NULL) { printf("%d ", (*curr).value); curr = (*curr).next; } printf("%d ", (*curr).value); return 0; }
null
null
63c648c75f004bf4898cff9f
2023-02-06T22:33:29.781Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { // Check if either User pointer is NULL return 2; } int i = 0; while (i < MAX_FRIENDS && author->friends[i] != NULL && author->friends[i] != target) { // Traverse through the author's friends; friendship is symmetrical so we don't need to do the same for target i++; } if ((i == MAX_FRIENDS && author->friends[i - 1] != target) || author->friends[i] == NULL) { // Check if users exist but are not friends return 1; } // We can create the post Post *new_post = malloc(sizeof(Post)); strcpy(new_post->author, author->name); //new_post->contents = malloc(sizeof(strlen(contents))); //strcpy(new_post->contents, contents); // *(new_post->date) = time(NULL); if (target->first_post == NULL) { target->first_post = new_post; } else { new_post->next = target->first_post; target->first_post = new_post; } return 0; }
linked list
null
63c648c75f004bf4898cff9f
2023-02-06T22:35:14.084Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { // Check if either User pointer is NULL return 2; } int i = 0; while (i < MAX_FRIENDS && author->friends[i] != NULL && author->friends[i] != target) { // Traverse through the author's friends; friendship is symmetrical so we don't need to do the same for target i++; } if ((i == MAX_FRIENDS && author->friends[i - 1] != target) || author->friends[i] == NULL) { // Check if users exist but are not friends return 1; } // We can create the post Post *new_post = malloc(sizeof(Post)); strcpy(new_post->author, author->name); new_post->contents = malloc(sizeof(strlen(contents))); strcpy(new_post->contents, contents); *(new_post->date) = time(NULL); if (target->first_post == NULL) { target->first_post = new_post; } else { new_post->next = target->first_post; target->first_post = new_post; } return 0; }
linked list
null
63c648c75f004bf4898cff9f
2023-02-06T22:37:23.107Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { // Check if either User pointer is NULL return 2; } int i = 0; while (i < MAX_FRIENDS && author->friends[i] != NULL && author->friends[i] != target) { // Traverse through the author's friends; friendship is symmetrical so we don't need to do the same for target i++; } if ((i == MAX_FRIENDS && author->friends[i - 1] != target) || author->friends[i] == NULL) { // Check if users exist but are not friends return 1; } // We can create the post Post *new_post = malloc(sizeof(Post)); strcpy(new_post->author, author->name); new_post->contents = malloc(sizeof(strlen(contents))); strcpy(new_post->contents, contents); new_post->date = time(NULL); if (target->first_post == NULL) { target->first_post = new_post; } else { new_post->next = target->first_post; target->first_post = new_post; } return 0; }
linked list
null
63c648c75f004bf4898cff9f
2023-02-06T23:25:02.865Z
Help Fix Code
v1
null
printf("Posts:\n"); Post *curr = user->first_post; while (curr != NULL) { printf("From: %s\n", curr->author); printf("Date: %s\n\n", ctime(curr->date)); printf("%s", curr->contents); curr = curr->next; if (curr != NULL) { printf("===\n\n"); } }
print
null
63c648c75f004bf4898cff9f
2023-02-07T14:37:51.590Z
Help Fix Code
v1
null
User *curr_del = *user_ptr_del; // Edge case: user to be deleted is first in user list if (strcmp(name, curr_del->name) == 0) { *user_ptr_del = curr_del->next; } while ((curr_del->next != NULL) && (strcmp(name, curr_del->next->name) != 0)) { curr_del = curr_del->next; } if ((curr_del->next != NULL) && (strcmp(name, curr_del->next->name) == 0)) { // Normal case: user is not at the beginning or end of the user list curr_del->next = curr_del->next->next; curr_del->next->next = NULL; } else { // Edge case: user is at the end of the user list curr_del->next = NULL; } free(find_user(name, *user_ptr_del)); return 0;
delete user
null
63c648c75f004bf4898cff9f
2023-02-07T14:40:36.932Z
Help Fix Code
v1
null
int delete_user(const char *name, User **user_ptr_del) { if (find_user(name, *user_ptr_del) == NULL) { // Check if a user with this name does not exist return 1; } int index = 0; User *curr = *user_ptr_del; while (curr != NULL) { // Traverse through every user in the user list index = find_friend(curr->friends, name); for (int i = index; i < MAX_FRIENDS; i++) { // Find the friend and delete them + "shift" the friend list elements accordingly // for EACH user if ((i == MAX_FRIENDS - 1) && (curr->friends[MAX_FRIENDS - 1] != NULL)) { // Edge case: when the friend to delete is at the end of the list curr->friends[i] = NULL; } else if (curr->friends[i] == NULL) { break; } else { // Technique for "shifting" elements: delete the user at the current index by // overwriting with the user at the next index curr->friends[i] = curr->friends[i + 1]; } } curr = curr->next; } // Deleting the user User *curr_del = *user_ptr_del; // Edge case: user to be deleted is first in user list if (strcmp(name, curr_del->name) == 0) { *user_ptr_del = curr_del->next; } while ((curr_del->next != NULL) && (strcmp(name, curr_del->next->name) != 0)) { curr_del = curr_del->next; } if ((curr_del->next != NULL) && (strcmp(name, curr_del->next->name) == 0)) { // Normal case: user is not at the beginning or end of the user list curr_del->next = curr_del->next->next; curr_del->next->next = NULL; } else { // Edge case: user is at the end of the user list curr_del->next = NULL; } free(find_user(name, *user_ptr_del)); return 0; }
delete user
null
63c648c75f004bf4898cff9f
2023-02-23T03:28:09.154Z
Help Fix Code
v1
null
Rule *parse_file(FILE *fp) { Rule *head = malloc(sizeof(Rule)); Rule *curr_rule = head; char *line[MAXLINE + 1]; // the +1 accounts for a null terminator from fgets while (fgets(*line, MAXLINE + 1, fp) != NULL) { // if the line is not a comment or empty: if (is_comment_or_empty((const char *) line) == 0) { // if the line is an action: if (strcmp(line[0], " ") == 0) { // CREATE THE ACTIONS // 1. Initialize the args array int size_of_args_arr = size_of_arr(*line + sizeof('\t')); // char *const argv[size_of_args_arr + 1]; // +1 accounts for NULL pointer at end Action *curr_action; // 2. Fill the args array if (head->actions == NULL) { head->actions = malloc(sizeof(Action)); head->actions->args = malloc(sizeof(char *const) * (size_of_args_arr + 1)); fill_args_arr(size_of_args_arr, *line, head->actions->args); curr_action = head->actions; } else { Action *new_action = malloc(sizeof(Action)); new_action->args = malloc(sizeof(char *const) * (size_of_args_arr + 1)); fill_args_arr(size_of_args_arr, *line, new_action->args); curr_action->next_act = new_action; curr_action = new_action; } } // else the line consists of the target // CREATE THE TARGET char *target = find_target(*line); if (head == NULL) { head->target = target; } else { Rule *new_rule = malloc(sizeof(Rule)); new_rule->target = target; curr_rule->next_rule = new_rule; curr_rule = new_rule; } } } }
create linked list
null
63c648c75f004bf4898cffa4
2023-01-19T07:04:08.480Z
General Question
v1
What is a pointer?
null
null
null
63c648c75f004bf4898cffa4
2023-01-19T07:11:07.589Z
General Question
v1
What does the scanf function do?
null
null
null
63c648c75f004bf4898cffa4
2023-01-19T07:12:19.429Z
General Question
v1
Provide an example of the scanf function in code.
null
null
null
63c648c75f004bf4898cffa4
2023-01-20T00:18:25.926Z
Question from Code
v1
What are argc and argv?
int main(int argc, char **argv) { int size = argc - 1; int *score_card[size]; for (int i = 0; i < size; i++) { // NOTE: We haven't covered malloc yet, so don't worry about this line. score_card[i] = malloc(sizeof(int)); *(score_card[i]) = strtol(argv[i + 1], NULL, 10); } printf("Sum: %d\n", sum_card(score_card, size)); return 0; }
null
null
63c648c75f004bf4898cffa4
2023-01-20T00:20:19.121Z
Question from Code
v1
What does line 8 do?
int main(int argc, char **argv) { int size = argc - 1; int *score_card[size]; for (int i = 0; i < size; i++) { // NOTE: We haven't covered malloc yet, so don't worry about this line. score_card[i] = malloc(sizeof(int)); *(score_card[i]) = strtol(argv[i + 1], NULL, 10); } printf("Sum: %d\n", sum_card(score_card, size)); return 0; }
null
null
63c648c75f004bf4898cffa4
2023-01-20T00:21:34.879Z
Explain Code
v1
null
int main(int argc, char **argv) { int size = argc - 1; int *score_card[size]; for (int i = 0; i < size; i++) { // NOTE: We haven't covered malloc yet, so don't worry about this line. score_card[i] = malloc(sizeof(int)); *(score_card[i]) = strtol(argv[i + 1], NULL, 10); } printf("Sum: %d\n", sum_card(score_card, size)); return 0; }
null
null
63c648c75f004bf4898cffa4
2023-01-20T00:23:44.281Z
General Question
v1
What is the strtol function?
null
null
null
63c648c75f004bf4898cffa4
2023-01-28T09:29:40.063Z
General Question
v1
How do I redirect the contents of a text file into standard input?
null
null
null
63c648c75f004bf4898cffa4
2023-01-28T09:39:22.744Z
Question from Code
v1
When given any input, why does the printf output "(null)"?
char *scan; scanf("%s", scan); printf("%s", scan);
null
null
63c648c75f004bf4898cffa4
2023-01-28T09:45:34.682Z
Help Fix Code
v1
null
char *scan; scanf("%s", scan); printf("%s", scan);
Code should read a string of any length from stdin and immediately print the string.
null
63c648c75f004bf4898cffa4
2023-01-28T09:56:35.523Z
Help Fix Code
v1
null
char perm_required[3]; if (argc == 3) { perm_required = "abc"; } else { perm_required = "def"; }
Initialize a string perm_required. Set perm_required to "abc" if argc == 3, and "def" otherwise.
null
63c648c75f004bf4898cffa4
2023-01-28T10:06:24.693Z
General Question
v1
Is it possible to initialize a boolean and reassign its value for use later in a program?
null
null
null
63c648c75f004bf4898cffa4
2023-01-28T10:15:49.144Z
Help Write Code
v1
null
null
null
Initialize a string, then reassign its value so it does not contain the first character.
63c648c75f004bf4898cffa4
2023-01-28T10:20:20.234Z
General Question
v1
Does c allow you to create slices of strings?
null
null
null
63c648c75f004bf4898cffa4
2023-01-28T10:33:32.010Z
General Question
v1
Why do I get "error: unknown type name ‘bool’" when I attempt to declare a boolean?
null
null
null
63c648c75f004bf4898cffa4
2023-01-28T10:40:29.137Z
General Question
v1
How could I print out boolean values using printf?
null
null
null
63c648c75f004bf4898cffa4
2023-02-10T19:24:10.744Z
General Question
v1
Provide an example of code that allocates space on the heap for a two-dimensional array.
null
null
null
63c648c75f004bf4898cffa4
2023-02-10T19:36:35.921Z
Help Write Code
v1
null
null
null
Allocate space on the heap for an array of structs. Set each element of the array to an initialized instance of the struct.
63c648c75f004bf4898cffa4
2023-02-10T19:50:40.459Z
General Question
v1
How can I free the space that was previously allocated on the heap for a two-dimensional array?
null
null
null
63c648c75f004bf4898cffa4
2023-02-15T00:07:19.507Z
Help Write Code
v1
null
null
null
Initialize an empty char array of length n. Create any string s. Return 0 if the char array has enough space to store a copy of s using strcpy including the null terminator, and 1 if it does not have enough space.
63c648c75f004bf4898cffa4
2023-02-15T00:09:13.312Z
General Question
v1
How can I find the length of a char array if its length is unknown?
null
null
null
63c648c75f004bf4898cffa4
2023-02-15T00:24:49.053Z
Help Write Code
v1
null
null
null
Check that a string stored in a char array is equal to another string.
63c648c75f004bf4898cffa4
2023-02-15T08:03:40.725Z
Help Write Code
v1
null
null
null
Create a function that appends a new struct to the end of a linked list. Its argument is of type (struct **list), and (*list) points to the head of the list.
63c648c75f004bf4898cffa4
2023-02-15T08:24:08.233Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { // Initialize user User newuser; strcpy(newuser.name, name); newuser.next = NULL; User *curr = *user_ptr_add; // Traverse list of users while (curr->next != NULL) { // Check if name already exists if (strcmp(curr->name, name) == 0) { return 1; } curr = curr->next; } // Add newuser to end of list curr->next = &newuser; return 0; }
If any node of the linked list user_ptr_add has attribute name equal to argument name, function should return 1. However, the function does not check the last node of the list.
null
63c648c75f004bf4898cffa4
2023-02-15T08:26:54.973Z
Question from Code
v1
How do I ensure that the while loop also checks the last node of linked list user_ptr_add?
int create_user(const char *name, User **user_ptr_add) { // Initialize user User newuser; strcpy(newuser.name, name); newuser.next = NULL; User *curr = *user_ptr_add; // Traverse list of users while (curr->next != NULL) { // Check if name already exists if (strcmp(curr->name, name) == 0) { return 1; } curr = curr->next; } // Add newuser to end of list curr->next = &newuser; return 0; }
null
null
63c648c75f004bf4898cffa4
2023-02-15T09:13:46.894Z
General Question
v1
How can you determine how many elements are currently stored in an array?
null
null
null
63c648c75f004bf4898cffa4
2023-02-15T09:55:28.082Z
Help Write Code
v1
null
null
null
Initialize an array for 10 structs. Add between 1 to 10 structs to the array. Determine how many structs are in the array (do not count indices that don't contain a struct).
63c648c75f004bf4898cffa4
2023-02-15T09:56:48.373Z
General Question
v1
How do I count how many elements are in an array of integers, not counting indices that don't hold any value?
null
null
null
63c648c75f004bf4898cffa4
2023-02-16T00:12:18.913Z
General Question
v1
How can I check if an element of an array has not been assigned yet?
null
null
null
63c648c75f004bf4898cffa4
2023-02-16T00:24:04.079Z
Help Fix Code
v1
null
int delete_user(const char *name, User **user_ptr_del) { User *curr = *user_ptr_del; User *prev = curr; int found = 0; char *friend_names[MAX_FRIENDS]; // Traverse list of users while (curr != NULL) { // Check if current user has given name if (strcmp(curr->name, name) == 0) { found = 1; User *curr_friend; // Iterate over friend list for (int i = 0; i < MAX_FRIENDS; i++) { curr_friend = (curr->friends)[i]; // Check if current user is empty if (curr_friend->name == NULL) { break; } else { // If not, append name to list of friend names friend_names[i] = curr_friend->name; } } // Remove user from user list if (strcmp(prev->name, curr->name) == 0) { // If current user is only user *user_ptr_del = NULL; } else { prev->next = curr->next; } break; } prev = curr; curr = curr->next; } // Check if user was not found if (!(found)) { return 1; } // Remove user from all friends' lists char *curr_friend; for (int i = 0; i < MAX_FRIENDS; i++) { curr_friend = friend_names[i]; printf("%s\n", curr_friend); // Check if current user is empty if (curr_friend == NULL) { break; } // Remove user from this friend's list delete_friend(name, curr_friend, user_ptr_del); } // Free space allocated for user free(curr); return 0; }
The friend_names array should contain the names of friends contained in curr. However, the array also contains garbage values, making it impossible to check for NULL strings.
null
63c648c75f004bf4898cffa4
2023-02-16T00:24:40.111Z
General Question
v1
How do I initialize an array of char * to all NULL values?
null
null
null
63c648c75f004bf4898cffa4
2023-02-16T00:37:40.607Z
General Question
v1
How can I free the space allocated for a struct?
null
null
null
63c648c75f004bf4898cffa4
2023-02-16T01:20:45.282Z
General Question
v1
How do I free space allocated for an array of structs?
null
null
null
63c648c75f004bf4898cffa4
2023-02-16T01:22:36.494Z
Help Fix Code
v1
null
int delete_user(const char *name, User **user_ptr_del) { User *curr = *user_ptr_del; User *prev = curr; int found = 0; char *friend_names[MAX_FRIENDS] = {NULL}; // Traverse list of users while (curr != NULL) { // Check if current user has given name if (strcmp(curr->name, name) == 0) { found = 1; User *curr_friend; // Iterate over friend list for (int i = 0; i < MAX_FRIENDS; i++) { curr_friend = (curr->friends)[i]; // Check if current user is empty if (curr_friend->name == NULL) { break; } else { // If not, append name to list of friend names friend_names[i] = curr_friend->name; } } // Remove user from user list if (strcmp(prev->name, curr->name) == 0) { // If current user is only user *user_ptr_del = NULL; } else { prev->next = curr->next; } break; } prev = curr; curr = curr->next; } // Check if user was not found if (!(found)) { return 1; } // Remove user from all friends' lists char *curr_friend; for (int i = 0; i < MAX_FRIENDS; i++) { curr_friend = friend_names[i]; // Check if current user is empty if (curr_friend == NULL) { break; } // Remove user from this friend's list delete_friend(name, curr_friend, user_ptr_del); } // Free space allocated for user's posts Post *curr_post = curr->first_post; Post *next_post; while (curr_post != NULL) { next_post = curr_post->next; free(curr_post->author); free(curr_post->contents); free(curr_post->date); free(curr_post); curr_post = next_post; } // Free space allocated for user's friends User *friend; for (int i = 0; i < MAX_FRIENDS; i++) { friend = (curr->friends)[i]; free(friend); } // Free space allocated for user printf("%s\n", curr->name); free(curr->name); if (strcmp(curr->profile_pic, "\0") != 0) { free(curr->profile_pic); } free(curr->friends); free(curr); return 0; }
Function should free array of structs on line 83. Program returns invalid pointer error.
null
63c648c75f004bf4898cffa4
2023-03-03T10:29:42.480Z
Question from Code
v1
Why does "Bananas" get printed twice?
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void) { printf("Mangoes\n"); int r = fork(); printf("Apples\n"); if (r == 0) { sleep(1); printf("Oranges\n"); int k = fork(); if (k >= 0) { printf("Bananas\n"); } } else if (r > 0) { printf("Peaches\n"); for (int i = 0; i < 3; i++) { if ((r = fork()) == 0) { sleep(1); printf("Pears\n"); exit(0); printf("Nectarines\n"); } else if (r > 0) { printf("Plums\n"); } } } return 0; }
null
null
63c648c75f004bf4898cffa4
2023-03-03T23:11:58.718Z
Question from Code
v1
Why does bananas get printed twice?
int main(void) { printf("Mangoes\n"); int r = fork(); printf("Apples\n"); if (r == 0) { sleep(1); printf("Oranges\n"); int k = fork(); if (k >= 0) { printf("Bananas\n"); } } }
null
null
63c648c75f004bf4898cffa4
2023-03-10T12:21:21.033Z
Question from Code
v1
Why doesn't the validate program receive all the input from the pipe?
if (r > 0) { // Parent process // Close read end of pipe if ((close(fd[0])) == -1) { perror("close"); } // Write user_id and password to pipe int w_user = write(fd[1], user_id, MAXLINE); int w_pass = write(fd[1], password, MAXLINE); if (w_user == -1 || w_pass == -1) { perror("write"); } // Close write end of pipe if ((close(fd[1])) == -1) { perror("close"); } // Get status of child process int status; wait(&status); if (WIFEXITED(status)) { int exit_status = WEXITSTATUS(status); printf("Status: %d\n", exit_status); // Print message corresponding to status if (exit_status == 0) { printf("%s\n", SUCCESS); } else if (exit_status == 1) { perror("Child error"); } else if (exit_status == 2) { printf("%s\n", INVALID); } else { printf("%s\n", NO_USER); } } else { perror("exit"); } } else if (r == 0) { // Child process // Close write end of pipe if ((close(fd[1])) == -1) { perror("close"); } // Read stdin from pipe if ((dup2(fd[0], fileno(stdin))) == -1) { perror("dup2"); } // Run validate execl("./validate", "validate", NULL); perror("execl"); }
null
null
63c648c75f004bf4898cffa4
2023-03-10T12:26:13.993Z
Help Fix Code
v1
null
if (r > 0) { // Parent process // Close read end of pipe if ((close(fd[0])) == -1) { perror("close"); } // Write user_id and password to pipe int w_user = write(fd[1], user_id, MAXLINE); int w_pass = write(fd[1], password, MAXLINE); if (w_user == -1 || w_pass == -1) { perror("write"); } // Close write end of pipe if ((close(fd[1])) == -1) { perror("close"); } // Get status of child process int status; wait(&status); if (WIFEXITED(status)) { int exit_status = WEXITSTATUS(status); printf("Status: %d\n", exit_status); // Print message corresponding to status if (exit_status == 0) { printf("%s\n", SUCCESS); } else if (exit_status == 1) { perror("Child error"); } else if (exit_status == 2) { printf("%s\n", INVALID); } else { printf("%s\n", NO_USER); } } else { perror("exit"); } } else if (r == 0) { // Child process // Close write end of pipe if ((close(fd[1])) == -1) { perror("close"); } // Read stdin from pipe if ((dup2(fd[0], fileno(stdin))) == -1) { perror("dup2"); } // Run validate execl("./validate", "validate", NULL); perror("execl"); }
Ensure that the validate program can receive both user_id and password from the parent process.
null
63c648c75f004bf4898cffa4
2023-03-10T13:06:15.813Z
General Question
v2
When should I use exit as opposed to return?
null
null
null
63c648c75f004bf4898cffa4
2023-03-15T10:31:32.427Z
General Question
v2
How do I read lines from an open file?
null
null
null
63c648c75f004bf4898cffa4
2023-03-15T10:50:28.012Z
General Question
v2
How would I use strtok?
null
null
null
63c648c75f004bf4898cffa4
2023-03-15T11:25:34.374Z
General Question
v2
Is there an equivalent of the python continue statement in c?
null
null
null
63c648c75f004bf4898cffa4
2023-03-15T13:14:21.349Z
General Question
v2
How can I check whether a string is contained in another string?
null
null
null
63c648c75f004bf4898cffa4
2023-03-15T22:29:28.402Z
Help Write Code
v2
null
null
null
Create an array of strings where user input adds the new string to the array. It is unknown how many strings the user will enter.
63c648c75f004bf4898cffa4
2023-03-15T23:11:24.278Z
General Question
v2
How can I access the contents of a file multiple times without reopening it?
null
null
null
63c648c75f004bf4898cffa4
2023-03-15T23:50:58.773Z
General Question
v2
Why does strtok return garbage values?
null
null
null
63c648c75f004bf4898cffa4
2023-03-17T07:37:52.311Z
General Question
v2
Provide an example of how to use setitimer.
null
null
null
63c648c75f004bf4898cffa4
2023-03-24T22:23:17.740Z
General Question
v2
How is strchr used?
null
null
null
63c648c75f004bf4898cffa4
2023-03-31T18:28:41.088Z
Explain Code
v2
null
int main(void) { // Create the socket FD. int sock_fd = socket(AF_INET, SOCK_STREAM, 0); if (sock_fd < 0) { perror("client: socket"); exit(1); } // Set the IP and port of the server to connect to. struct sockaddr_in server; server.sin_family = AF_INET; server.sin_port = htons(PORT); if (inet_pton(AF_INET, "127.0.0.1", &server.sin_addr) < 1) { perror("client: inet_pton"); close(sock_fd); exit(1); } // Connect to the server. if (connect(sock_fd, (struct sockaddr *)&server, sizeof(server)) == -1) { perror("client: connect"); close(sock_fd); exit(1); } // Get the user to provide a name. char buf[2 * BUF_SIZE + 2]; // 2x to allow for usernames printf("Please enter a username: "); fflush(stdout); int num_read = read(STDIN_FILENO, buf, BUF_SIZE); if (num_read == 0) { close(sock_fd); exit(0); } buf[num_read] = '\0'; if (write(sock_fd, buf, num_read) != num_read) { perror("client: write"); close(sock_fd); exit(1); } // Read input from the user and send it to the server. Echo any output // received from the server. while (1) { num_read = read(STDIN_FILENO, buf, BUF_SIZE); if (num_read == 0) { break; } buf[num_read] = '\0'; if (write(sock_fd, buf, num_read) != num_read) { perror("client: write"); close(sock_fd); exit(1); } num_read = read(sock_fd, buf, sizeof(buf) - 1); if (num_read == 0) { break; } buf[num_read] = '\0'; printf("[Server] %s", buf); } close(sock_fd); return 0; }
null
null
63c648c75f004bf4898cffa4
2023-03-31T18:35:30.853Z
Explain Code
v2
null
int main(void) { struct sockname users[MAX_CONNECTIONS]; for (int index = 0; index < MAX_CONNECTIONS; index++) { users[index].sock_fd = -1; users[index].username = NULL; } int sock_fd = socket(AF_INET, SOCK_STREAM, 0); if (sock_fd < 0) { perror("server: socket"); exit(1); } struct sockaddr_in server; server.sin_family = AF_INET; server.sin_port = htons(PORT); server.sin_addr.s_addr = INADDR_ANY; int on = 1; int status = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)); if (status == -1) { perror("setsockopt -- REUSEADDR"); } memset(&server.sin_zero, 0, 8); if (bind(sock_fd, (struct sockaddr *)&server, sizeof(server)) < 0) { perror("server: bind"); close(sock_fd); exit(1); } if (listen(sock_fd, MAX_BACKLOG) < 0) { perror("server: listen"); close(sock_fd); exit(1); } int max_fd = sock_fd; fd_set all_fds; FD_ZERO(&all_fds); FD_SET(sock_fd, &all_fds); while (1) { fd_set listen_fds = all_fds; if (select(max_fd + 1, &listen_fds, NULL, NULL, NULL) == -1) { perror("server: select"); exit(1); } if (FD_ISSET(sock_fd, &listen_fds)) { int client_fd = accept_connection(sock_fd, users); if (client_fd > max_fd) { max_fd = client_fd; } FD_SET(client_fd, &all_fds); printf("Accepted connection\n"); } for (int index = 0; index < MAX_CONNECTIONS; index++) { if (users[index].sock_fd > -1 && FD_ISSET(users[index].sock_fd, &listen_fds)) { int client_closed = read_from(index, users); if (client_closed > 0) { FD_CLR(client_closed, &all_fds); printf("Client %d disconnected\n", client_closed); } else { printf("Echoing message from client %d\n", users[index].sock_fd); } } } } return 1; }
null
null
63c648c75f004bf4898cffa4
2023-03-31T19:45:16.074Z
General Question
v2
Given username and message strings, how would I construct a new string "<username>: <message>"?
null
null
null
63c648c75f004bf4898cffa4
2023-03-31T21:15:17.722Z
Help Fix Code
v2
User should be able to type a sequence of messages, and server program will echo messages back to user. However, the program begins an infinite loop of printing that message. How do I prevent this?
int max_fd = sock_fd; if (STDIN_FILENO > sock_fd) { max_fd = STDIN_FILENO; } fd_set all_fds; FD_ZERO(&all_fds); FD_SET(sock_fd, &all_fds); FD_SET(STDIN_FILENO, &all_fds); // Read input from the user and send it to the server. Echo any output // received from the server. while (1) { fd_set listen_fds = all_fds; if (select(max_fd + 1, &listen_fds, NULL, NULL, NULL) == -1) { perror("client: select"); exit(1); } /* * We should really send "\r\n" too, so the server can identify partial * reads, but you are not required to handle partial reads in this lab. */ if (write(sock_fd, buf, num_read) != num_read) { perror("client: write"); close(sock_fd); exit(1); } // Check from stdin if (FD_ISSET(STDIN_FILENO, &listen_fds)) { num_read = read(STDIN_FILENO, buf, BUF_SIZE); if (num_read == 0) { break; } buf[num_read] = '\0'; // Send user message to server if (write(sock_fd, buf, num_read) != num_read) { perror("client: write"); close(sock_fd); exit(1); } } // Check from socket if (FD_ISSET(sock_fd, &listen_fds)) { num_read = read(sock_fd, buf, sizeof(buf) - 1); if (num_read == 0) { break; } buf[num_read] = '\0'; // Print server message to stdout printf("[Server] %s", buf); } }
null
null
63c648c75f004bf4898cffa4
2023-04-04T15:29:59.258Z
General Question
v2
How could you use snprintf to repeatedly write additional text to the end of the same string?
null
null
null
63c648c75f004bf4898cffa4
2023-04-04T15:31:54.667Z
General Question
v2
How would one use perror when malloc fails?
null
null
null
63c648c75f004bf4898cffa4
2023-04-23T05:55:32.895Z
General Question
v2
In shell programming, what is the purpose of /dev/null?
null
null
null
63c648c75f004bf4898cffa4
2023-04-23T06:25:25.324Z
Question from Code
v2
This is a shell program. What is the for loop doing?
#!/ usr / bin / sh path=/u/csc209h/winter/pub/shell-prog upper_bound=7 for i in `seq 1 "$upper_bound"` do prev=`expr $i - 1` next=`expr $i + 1` if (! diff "${path}/${i}" "${path}/${prev}" > /dev/null) && (! diff "${path}/${i}" "${path}/${prev}" > /dev/null) then echo "${path}/${i}" is the odd one out #Can exit here exit 0 fi done
null
null
63c648c75f004bf4898cffa4
2023-04-25T21:13:12.460Z
General Question
v2
What causes a compilation error and runtime/logical errors?
null
null
null
63c648c85f004bf4898cffae
2023-03-10T22:06:13.106Z
Explain Code
v2
null
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; int status; int ret; int pipe_fd[2]; if (fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if (fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); };
null
null
63c648c85f004bf4898cffae
2023-03-10T22:14:20.851Z
Help Write Code
v2
null
null
null
Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
63c648c85f004bf4898cffae
2023-03-10T22:35:20.669Z
Help Write Code
v2
null
null
null
The validate program reads the user id and password from stdin, because if they were given as command-line arguments they would be visible to programs such as ps that inspect the state of the system. Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
63c648c85f004bf4898cffae
2023-03-10T22:42:45.583Z
Help Fix Code
v2
The validate program reads the user id and password from stdin, because if they were given as command-line arguments they would be visible to programs such as ps that inspect the state of the system. Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; /* The user will type in a user name on one line followed by a password on the next. DO NOT add any prompts. The only output of this program will be one of the messages defined above. Please read the comments in validate carefully */ int status; int ret; int pipe_fd[2]; if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if(fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); }; if(strlen(user_id) > MAXLINE || strlen(password) > MAXLINE) { perror("username or password is too long\n"); exit(1); } // TODO if ((pipe(pipe_fd)) == -1) { perror("pipe"); exit(1); } int read_end = pipe_fd[0]; int write_end = pipe_fd[1]; ret = fork(); if (ret < 0) { perror("fork"); exit(1); } if (ret == 0) { close(write_end); dup2(read_end, STDIN_FILENO); execl("./validate", "validate", NULL); } else if (ret > 0) { close(read_end); int user_len = strlen(user_id); int passwd_len = strlen(password); write(write_end, &user_len, sizeof(char)); write(write_end, &passwd_len, sizeof(char)); close(write_end); wait(&status); if ((ret = wait(&status)) == -1) { perror("wait"); exit(1); } if (WIFEXITED(status)) { int output = WEXITSTATUS(status); if (output == 0){ printf(SUCCESS); } else if (output == 2){ printf(INVALID); } else if (output == 3) { printf(NO_USER); } } } return 0; }
null
null
63c648c85f004bf4898cffae
2023-03-10T22:46:06.830Z
Help Fix Code
v2
The validate program reads the user id and password from stdin, because if they were given as command-line arguments they would be visible to programs such as ps that inspect the state of the system. Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; /* The user will type in a user name on one line followed by a password on the next. DO NOT add any prompts. The only output of this program will be one of the messages defined above. Please read the comments in validate carefully */ int status; int ret; int pipe_fd[2]; if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if(fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); }; if(strlen(user_id) > MAXLINE || strlen(password) > MAXLINE) { perror("username or password is too long\n"); exit(1); } // TODO if ((pipe(pipe_fd)) == -1) { perror("pipe"); exit(1); } int read_end = pipe_fd[0]; int write_end = pipe_fd[1]; ret = fork(); if (ret < 0) { perror("fork"); exit(1); } if (ret == 0) { close(write_end); dup2(read_end, STDIN_FILENO); execl("./validate", "validate", NULL); } else if (ret > 0) { close(read_end); write(write_end, &user_id, sizeof(char)); write(write_end, &password, sizeof(char)); close(write_end); wait(&status); if ((ret = wait(&status)) == -1) { perror("wait"); exit(1); } if (WIFEXITED(status)) { int output = WEXITSTATUS(status); if (output == 0){ printf(SUCCESS); } else if (output == 2){ printf(INVALID); } else if (output == 3) { printf(NO_USER); } } } return 0; }
null
null
63c648c85f004bf4898cffae
2023-03-10T22:52:50.222Z
Help Fix Code
v2
The validate program reads the user id and password from stdin, because if they were given as command-line arguments they would be visible to programs such as ps that inspect the state of the system. Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; /* The user will type in a user name on one line followed by a password on the next. DO NOT add any prompts. The only output of this program will be one of the messages defined above. Please read the comments in validate carefully */ int status; int ret; int pipe_fd[2]; if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if(fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); }; if(strlen(user_id) > MAXLINE || strlen(password) > MAXLINE) { perror("username or password is too long\n"); exit(1); } // TODO if ((pipe(pipe_fd)) == -1) { perror("pipe"); exit(1); } int read_end = pipe_fd[0]; int write_end = pipe_fd[1]; ret = fork(); if (ret < 0) { perror("fork"); exit(1); } if (ret == 0) { close(write_end); dup2(read_end, STDIN_FILENO); execl("./validate", "validate", NULL); close(read_end); exit(0); } else if (ret > 0) { close(read_end); write(write_end, &user_id, sizeof(char)); write(write_end, &password, sizeof(char)); close(write_end); wait(&status); if ((ret = wait(&status)) == -1) { perror("wait"); exit(1); } if (WIFEXITED(status)) { int output = WEXITSTATUS(status); if (output == 0){ printf(SUCCESS); } else if (output == 2){ printf(INVALID); } else if (output == 3) { printf(NO_USER); } } } return 0; }
null
null
63c648c85f004bf4898cffae
2023-03-10T22:59:34.312Z
Help Fix Code
v2
The validate program reads the user id and password from stdin, because if they were given as command-line arguments they would be visible to programs such as ps that inspect the state of the system. Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; /* The user will type in a user name on one line followed by a password on the next. DO NOT add any prompts. The only output of this program will be one of the messages defined above. Please read the comments in validate carefully */ int status; int ret; int pipe_fd[2]; if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if(fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); }; if(strlen(user_id) > MAXLINE || strlen(password) > MAXLINE) { perror("username or password is too long\n"); exit(1); } // TODO if ((pipe(pipe_fd)) == -1) { perror("pipe"); exit(1); } int read_end = pipe_fd[0]; int write_end = pipe_fd[1]; ret = fork(); if (ret < 0) { perror("fork"); exit(1); } if (ret == 0) { close(write_end); dup2(read_end, STDIN_FILENO); execl("./validate", "validate", NULL); close(read_end); exit(0); } else if (ret > 0) { int len_user = strlen(user_id); int len_pswd = strlen(password); close(read_end); write(write_end, &user_id, len_user + 1); write(write_end, &password, len_pswd + 1); close(write_end); wait(&status); if ((ret = wait(&status)) == -1) { perror("wait"); exit(1); } if (WIFEXITED(status)) { int output = WEXITSTATUS(status); if (output == 0){ printf(SUCCESS); } else if (output == 2){ printf(INVALID); } else if (output == 3) { printf(NO_USER); } } } return 0; }
null
null
63c648c85f004bf4898cffae
2023-03-10T23:03:19.176Z
Help Fix Code
v2
The validate program reads the user id and password from stdin, because if they were given as command-line arguments they would be visible to programs such as ps that inspect the state of the system. Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; /* The user will type in a user name on one line followed by a password on the next. DO NOT add any prompts. The only output of this program will be one of the messages defined above. Please read the comments in validate carefully */ int status; int ret; int pipe_fd[2]; if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if(fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); }; if(strlen(user_id) > MAXLINE || strlen(password) > MAXLINE) { perror("username or password is too long\n"); exit(1); } // TODO if ((pipe(pipe_fd)) == -1) { perror("pipe"); exit(1); } int read_end = pipe_fd[0]; int write_end = pipe_fd[1]; ret = fork(); if (ret < 0) { perror("fork"); exit(1); } if (ret == 0) { close(write_end); dup2(read_end, STDIN_FILENO); execl("./validate", "validate", NULL); } else if (ret > 0) { int len_user = strlen(user_id); int len_pswd = strlen(password); close(read_end); write(write_end, user_id, len_user + 1); write(write_end, password, len_pswd + 1); close(write_end); wait(&status); if ((ret = wait(&status)) == -1) { perror("wait"); exit(1); } if (WIFEXITED(status)) { int output = WEXITSTATUS(status); if (output == 0){ printf(SUCCESS); } else if (output == 2){ printf(INVALID); } else if (output == 3) { printf(NO_USER); } } } return 0; }
null
null
63c648c85f004bf4898cffae
2023-03-10T23:08:29.009Z
Help Fix Code
v2
The validate program reads the user id and password from stdin, because if they were given as command-line arguments they would be visible to programs such as ps that inspect the state of the system. Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; /* The user will type in a user name on one line followed by a password on the next. DO NOT add any prompts. The only output of this program will be one of the messages defined above. Please read the comments in validate carefully */ int status; int ret; int pipe_fd[2]; if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if(fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); }; if(strlen(user_id) > MAXLINE || strlen(password) > MAXLINE) { perror("username or password is too long\n"); exit(1); } // TODO if ((pipe(pipe_fd)) == -1) { perror("pipe"); exit(1); } int read_end = pipe_fd[0]; int write_end = pipe_fd[1]; ret = fork(); if (ret < 0) { perror("fork"); exit(1); } if (ret == 0) { close(write_end); dup2(read_end, STDIN_FILENO); execl("./validate", "validate", NULL); } else if (ret > 0) { int len_user = strlen(user_id); int len_pswd = strlen(password); close(read_end); write(write_end, user_id, len_user + 2); write(write_end, password, len_pswd + 2); close(write_end); wait(&status); if ((ret = wait(&status)) == -1) { perror("wait"); exit(1); } if (WIFEXITED(status)) { int output = WEXITSTATUS(status); if (output == 0){ printf(SUCCESS); } else if (output == 2){ printf(INVALID); } else if (output == 3) { printf(NO_USER); } } } }
null
null
63c648c85f004bf4898cffae
2023-03-10T23:10:58.541Z
Help Fix Code
v2
The validate program reads the user id and password from stdin, because if they were given as command-line arguments they would be visible to programs such as ps that inspect the state of the system. Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; /* The user will type in a user name on one line followed by a password on the next. DO NOT add any prompts. The only output of this program will be one of the messages defined above. Please read the comments in validate carefully */ int status; int ret; int pipe_fd[2]; if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if(fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); }; if(strlen(user_id) > MAXLINE || strlen(password) > MAXLINE) { perror("username or password is too long\n"); exit(1); } // TODO if ((pipe(pipe_fd)) == -1) { perror("pipe"); exit(1); } int read_end = pipe_fd[0]; int write_end = pipe_fd[1]; ret = fork(); if (ret < 0) { perror("fork"); exit(1); } else if (ret == 0) { close(write_end); dup2(read_end, STDIN_FILENO); execl("./validate", "validate", NULL); } int len_user = strlen(user_id); int len_pswd = strlen(password); close(read_end); write(write_end, user_id, len_user); write(write_end, password, len_pswd); close(write_end); wait(&status); if ((ret = wait(&status)) == -1) { perror("wait"); exit(1); } if (WIFEXITED(status)) { int output = WEXITSTATUS(status); if (output == 0){ printf(SUCCESS); } else if (output == 2){ printf(INVALID); } else if (output == 3) { printf(NO_USER); } } }
null
null
63c648c85f004bf4898cffae
2023-03-11T20:50:51.927Z
Help Write Code
v2
null
null
null
our first task is to implement parse_file, so that it reads a makefile and constructs a corresponding linked data structure. The necessary structs are defined in pmake.h, and the comments at the top of the file explain what each line of a makefile will contain. You may not change the struct definitions. pmake.h also contains function prototypes for functions that are either already provided, or that you will need to write.
63c648c85f004bf4898cffae
2023-03-13T14:05:02.352Z
Explain Code
v2
null
int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); x += 2; fprintf(stderr, "outside %d", x); return 0; }
null
null
63c648c85f004bf4898cffae
2023-03-13T14:08:34.454Z
Question from Code
v2
What does the program print to stderr if the user presses Ctrl+C at the moment when the program is at position B?
int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); x += 2; fprintf(stderr, "outside %d", x); return 0; }
null
null
63c648c85f004bf4898cffae
2023-03-13T14:09:31.552Z
Question from Code
v2
What does the program print to stderr if the user presses Ctrl+C at the moment when the program is at position C?
int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); x += 2; fprintf(stderr, "outside %d", x); return 0; }
null
null
63c648c85f004bf4898cffae
2023-03-13T14:15:22.144Z
Question from Code
v2
What does the program print to stderr if the user presses Ctrl+C before x += 2?
int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); x += 2; fprintf(stderr, "outside %d", x); return 0; }
null
null
63c648c85f004bf4898cffae
2023-03-13T14:16:17.110Z
Question from Code
v2
What does the program print to stderr if the user presses Ctrl+C after x += 2?
int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); x += 2; fprintf(stderr, "outside %d", x); return 0; }
null
null
63c648c85f004bf4898cffae
2023-03-13T14:18:37.248Z
Question from Code
v2
What does the program print to stderr if the user presses Ctrl+C right after calling x += 2?
int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); x += 2; fprintf(stderr, "outside %d", x); return 0; }
null
null