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
63c648795f004bf4898cfccf
2023-01-26T07:13:22.758Z
General Question
v1
How to read multiple lines using scanf ?
null
null
null
63c648795f004bf4898cfccf
2023-01-26T07:14:32.656Z
General Question
v1
How to go to next line while reading using scanf
null
null
null
63c648795f004bf4898cfccf
2023-01-26T07:54:21.672Z
General Question
v1
How to skip a line while reading using scanf
null
null
null
63c648795f004bf4898cfccf
2023-01-26T07:55:26.265Z
General Question
v1
How to skip the first line while reading using scanf
null
null
null
63c648795f004bf4898cfccf
2023-01-26T07:57:24.683Z
General Question
v1
What are the reasons for segmentation fault ?
null
null
null
63c648795f004bf4898cfccf
2023-01-26T08:01:29.792Z
Question from Code
v1
Why segmentation fault occur in this code
int main(int argc, char** argv) { if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1; } int filesize=0; int threshold = strtol(argv[1], NULL, 10); printf("%d\n", threshold); char *permissions=NULL; int count = 0; scanf("%*s %*d"); while(scanf("%s %*d %*s %*s %d %*s %*d %*s %*s\n", permissions,&filesize) != EOF) { if(argc == 2) { if(filesize > threshold){ count +=1; } } } printf("%d\n", count); return 0; }
null
null
63c648795f004bf4898cfccf
2023-01-26T09:09:07.415Z
General Question
v1
how to compare strings in c ?
null
null
null
63c648795f004bf4898cfccf
2023-01-26T09:18:21.494Z
General Question
v1
how to compare characters in c ?
null
null
null
63c648795f004bf4898cfccf
2023-01-26T09:53:15.810Z
Explain Code
v1
null
gcc -Wall -Werror -std=c99
null
null
63c648795f004bf4898cfccf
2023-01-29T01:21:50.585Z
Question from Code
v1
What is the type of &(permissions[1] ?
char *permissions= malloc(sizeof(char *));
null
null
63c648795f004bf4898cfccf
2023-01-29T01:22:48.590Z
Question from Code
v1
What is the type of permissions[1] ?
char *permissions= malloc(sizeof(char *));
null
null
63c648795f004bf4898cfccf
2023-01-29T01:23:12.819Z
Question from Code
v1
What is the type of &(permissions[1]) ?
char *permissions= malloc(sizeof(char *));
null
null
63c648795f004bf4898cfccf
2023-01-29T01:52:38.885Z
General Question
v1
How should I initialize strings ?
null
null
null
63c648795f004bf4898cfccf
2023-02-08T01:28:52.810Z
Question from Code
v1
Why does this give the warning incompatible integer to pointer conversion passing 'unsigned char' to parameter of type 'void *' [-Wint-conversion] ?
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { int m = height; int n = width; fseek(image, pixel_array_offset,SEEK_SET); unsigned char **arr = malloc(sizeof(struct pixel *) * m); for(int i=0;i<m;i++) { arr[i] = malloc(sizeof(struct pixel) * 3 * n); int j=0; while(j< 3 * n) { fread(arr[i][j], sizeof(unsigned char), 1, image); fread(arr[i][j+1], sizeof(unsigned char), 1, image); fread(arr[i][j+2], sizeof(unsigned char), 1, image); j += 3; } } }
null
null
63c648795f004bf4898cfccf
2023-02-08T01:40:50.041Z
General Question
v1
How can i free all the dynamically allocated memory ?
null
null
null
63c648795f004bf4898cfccf
2023-02-08T23:29:35.176Z
Question from Code
v1
Why does this give segmentation fault ?
int create_user(const char *name, User **user_ptr_add) { User *curr_user_ptr = *user_ptr_add; while(curr_user_ptr->next != NULL) { if(curr_user_ptr->name == name) { return 1; } curr_user_ptr = curr_user_ptr->next; } if(sizeof(name) > 32) { return 2; } User *new_user_ptr = curr_user_ptr->next; strncpy(new_user_ptr->name,name, 32); return 0; }
null
null
63c648795f004bf4898cfccf
2023-02-08T23:34:40.559Z
Question from Code
v1
how to allocate memory to new_user_ptr ?
int create_user(const char *name, User **user_ptr_add) { User *curr_user_ptr = *user_ptr_add; while(curr_user_ptr->next != NULL) { if(curr_user_ptr->name == name) { return 1; } curr_user_ptr = curr_user_ptr->next; } if(sizeof(name) > 32) { return 2; } User *new_user_ptr = curr_user_ptr->next; strncpy(new_user_ptr->name,name, 32); return 0; }
null
null
63c648795f004bf4898cfccf
2023-02-08T23:35:46.573Z
Question from Code
v1
why does this give segmentation fault ?
int create_user(const char *name, User **user_ptr_add) { User *curr_user_ptr = *user_ptr_add; while(curr_user_ptr->next != NULL) { if(curr_user_ptr->name == name) { return 1; } curr_user_ptr = curr_user_ptr->next; } if(sizeof(name) > 32) { return 2; } User *new_user_ptr = malloc(sizeof(char) * 32); new_user_ptr = curr_user_ptr->next; strncpy(new_user_ptr->name,name, 32);\ return 0; }
null
null
63c648795f004bf4898cfccf
2023-02-08T23:46:39.454Z
Question from Code
v1
Why segmentation fault ?
int create_user(const char *name, User **user_ptr_add) { User *curr_user_ptr = *user_ptr_add; while(curr_user_ptr->next != NULL) { if(curr_user_ptr->name == name) { return 1; } curr_user_ptr = curr_user_ptr->next; } if(strlen(name) > 31) { return 2; } User *new_user_ptr = malloc(sizeof(User *)); new_user_ptr = curr_user_ptr->next; strncpy(new_user_ptr->name,name, 32);\ return 0; }
null
null
63c648795f004bf4898cfccf
2023-02-08T23:48:07.009Z
Question from Code
v1
How to initilize new_user_ptr ?
int create_user(const char *name, User **user_ptr_add) { User *curr_user_ptr = *user_ptr_add; while(curr_user_ptr->next != NULL) { if(curr_user_ptr->name == name) { return 1; } curr_user_ptr = curr_user_ptr->next; } if(strlen(name) > 31) { return 2; } User *new_user_ptr = malloc(sizeof(User)); new_user_ptr = curr_user_ptr->next; strncpy(new_user_ptr->name,name, 32);\ return 0; }
null
null
63c648795f004bf4898cfccf
2023-02-08T23:53:34.720Z
Question from Code
v1
Why segmentation fault ?
typedef struct user { char name[MAX_NAME]; char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. struct post *first_post; struct user *friends[MAX_FRIENDS]; struct user *next; } User; int create_user(const char *name, User **user_ptr_add) { User *curr_user_ptr = *user_ptr_add; while(curr_user_ptr->next != NULL) { if(curr_user_ptr->name == name) { return 1; } curr_user_ptr = curr_user_ptr->next; } if(strlen(name) > 31) { return 2; } User *new_user_ptr = malloc(sizeof(User)); new_user_ptr = curr_user_ptr->next; strncpy(new_user_ptr->name,name, 32); // new_user_ptr->profile_pic = NULL; new_user_ptr->first_post = NULL; // new_user_ptr->friends = NULL; new_user_ptr->next = NULL; return 0; }
null
null
63c648795f004bf4898cfccf
2023-02-08T23:56:59.668Z
Question from Code
v1
Why segmentation fault ?
typedef struct user { char name[MAX_NAME]; char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. struct post *first_post; struct user *friends[MAX_FRIENDS]; struct user *next; } User; int create_user(const char *name, User **user_ptr_add) { User *curr_user_ptr = *user_ptr_add; while(curr_user_ptr != NULL) { if(curr_user_ptr->name == name) { return 1; } curr_user_ptr = curr_user_ptr->next; } if(strlen(name) > 31) { return 2; } User *new_user_ptr = malloc(sizeof(User)); new_user_ptr = curr_user_ptr->next; strncpy(new_user_ptr->name,name, 32); // new_user_ptr->profile_pic = NULL; new_user_ptr->first_post = NULL; // new_user_ptr->friends = NULL; new_user_ptr->next = NULL; return 0; }
null
null
63c648795f004bf4898cfccf
2023-02-08T23:59:01.631Z
Question from Code
v1
Why segmentation fault ?
typedef struct user { char name[MAX_NAME]; char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. struct post *first_post; struct user *friends[MAX_FRIENDS]; struct user *next; } User; int create_user(const char *name, User **user_ptr_add) { User *curr_user_ptr = *user_ptr_add; while(curr_user_ptr != NULL) { if(curr_user_ptr->name == name) { return 1; } curr_user_ptr = curr_user_ptr->next; } if(strlen(name) > 31) { return 2; } User *new_user_ptr = malloc(sizeof(User)); new_user_ptr = curr_user_ptr; strncpy(new_user_ptr->name,name, 32); // new_user_ptr->profile_pic = NULL; new_user_ptr->first_post = NULL; // new_user_ptr->friends = NULL; new_user_ptr->next = NULL; return 0; }
null
null
63c648795f004bf4898cfccf
2023-02-09T00:04:45.728Z
Question from Code
v1
Why segmentation fault ?
typedef struct user { char name[MAX_NAME]; char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. struct post *first_post; struct user *friends[MAX_FRIENDS]; struct user *next; } User; int create_user(const char *name, User **user_ptr_add) { User *curr_user_ptr = malloc(sizeof(User)); curr_user_ptr = *user_ptr_add; while(curr_user_ptr->next != NULL) { if(curr_user_ptr->name == name) { return 1; } curr_user_ptr = curr_user_ptr->next; } if(strlen(name) > 31) { return 2; } User *new_user_ptr = malloc(sizeof(User)); strncpy(new_user_ptr->name,name, 32); // new_user_ptr->profile_pic = NULL; new_user_ptr->first_post = NULL; // new_user_ptr->friends = NULL; new_user_ptr->next = NULL; curr_user_ptr->next = new_user_ptr; return 0; }
null
null
63c648795f004bf4898cfccf
2023-02-09T00:06:36.743Z
Question from Code
v1
Why segmentation fault ?
typedef struct user { char name[MAX_NAME]; char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. struct post *first_post; struct user *friends[MAX_FRIENDS]; struct user *next; } User; int create_user(const char *name, User **user_ptr_add) { User *curr_user_ptr = malloc(sizeof(User *)); curr_user_ptr = *user_ptr_add; while(curr_user_ptr->next != NULL) { if(curr_user_ptr->name == name) { return 1; } curr_user_ptr = curr_user_ptr->next; } if(strlen(name) > 31) { return 2; } User *new_user_ptr = malloc(sizeof(User *)); strncpy(new_user_ptr->name,name, 32); // new_user_ptr->profile_pic = NULL; new_user_ptr->first_post = NULL; // new_user_ptr->friends = NULL; new_user_ptr->next = NULL; curr_user_ptr->next = new_user_ptr; return 0; }
null
null
63c648795f004bf4898cfccf
2023-02-09T06:27:25.920Z
General Question
v1
what is the format of type_t when to be written in a string?
null
null
null
63c648795f004bf4898cfccf
2023-02-09T06:33:24.682Z
General Question
v1
what is the format of long * when to be written in a string?
null
null
null
63c648795f004bf4898cfccf
2023-02-09T07:17:58.844Z
Question from Code
v1
Why segmentation fault ?
int print_user(const User *user) { FILE *file_ptr = fopen(user->profile_pic, "r"); char *profile_img_char= NULL; while(fread(profile_img_char, sizeof(char), 1, file_ptr) != 0) { printf("%s", profile_img_char); } fclose(file_ptr); }
null
null
63c648795f004bf4898cfccf
2023-02-09T07:19:01.434Z
Question from Code
v1
Why segmentation fault ?
int print_user(const User *user) { FILE *file_ptr = fopen(user->profile_pic, "r"); char *profile_img_char= malloc(sizeof(char *)); while(fread(profile_img_char, sizeof(char), 1, file_ptr) != 0) { printf("%s", profile_img_char); } fclose(file_ptr); }
null
null
63c648795f004bf4898cfccf
2023-02-09T07:19:52.268Z
Question from Code
v1
Why segmentation fault ?
typedef struct user { char name[MAX_NAME]; char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. struct post *first_post; struct user *friends[MAX_FRIENDS]; struct user *next; } User; int print_user(const User *user) { FILE *file_ptr = fopen(user->profile_pic, "r"); char *profile_img_char= malloc(sizeof(char *)); while(fread(profile_img_char, sizeof(char), 1, file_ptr) != 0) { printf("%s", profile_img_char); } fclose(file_ptr); }
null
null
63c648795f004bf4898cfccf
2023-02-12T00:39:42.581Z
Question from Code
v1
Why segmentation fault ?
typedef struct post { char author[MAX_NAME]; char *contents; time_t *date; struct post *next; } Post; Post *post_ptr = malloc(sizeof(Post)); strncpy(post_ptr->author, author->name, 31); post_ptr->author[31] = '\0'; post_ptr->contents = contents; time_t *time_ptr = NULL; *time_ptr = time(NULL); post_ptr->date = time_ptr; post_ptr->next = target->first_post; target->first_post = post_ptr->next; return 0;
null
null
63c648795f004bf4898cfccf
2023-02-12T07:15:44.823Z
Question from Code
v1
Why segmentation fault ?
while (user_list != NULL) { delete_user(user_list->name, &user_list); } return 0; int delete_user(const char *name, User **user_ptr_del) { User *user_ptr = find_user(name, *user_ptr_del); if(user_ptr == NULL) { return 1; } User *curr_usr = user_ptr_del[0]; while (strcmp(curr_usr->next->name, name) != 0) { curr_usr = curr_usr->next; } curr_usr->next = curr_usr->next->next; int i = 0; while(i <= 10 && user_ptr->friends[i] != NULL) { User *curr_friend_ptr = user_ptr->friends[i]; int j = 0; while(strcmp(curr_friend_ptr->friends[j]->name, user_ptr->name) != 0) { j += 1; } for(int k = j;k<10;k++) { if(curr_friend_ptr->friends[k] != NULL) { curr_friend_ptr->friends[k] = curr_friend_ptr->friends[k+1]; } } i += 1; } return 0; }
null
null
63c648795f004bf4898cfccf
2023-02-12T07:17:40.683Z
Question from Code
v1
Why segmentation fault ?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "friends.h" #define INPUT_BUFFER_SIZE 256 #define INPUT_ARG_MAX_NUM 12 #define DELIM " \n" /* * Print a formatted error message to stderr. */ void error(char *msg) { fprintf(stderr, "Error: %s\n", msg); } /* * Read and process commands * Return: -1 for quit command * 0 otherwise */ int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) { User *user_list = *user_list_ptr; printf("a %s\n", user_list[0].name); if (cmd_argc <= 0) { return 0; } else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) { return -1; } else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) { switch (create_user(cmd_argv[1], user_list_ptr)) { case 1: error("user by this name already exists"); break; case 2: error("username is too long"); break; } } else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) { list_users(user_list); } else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) { User *user = find_user(cmd_argv[1], user_list); if (user == NULL) { error("user not found"); } else { switch (update_pic(user, cmd_argv[2])) { case 1: error("file not found"); break; case 2: error("filename too long"); break; } } } else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) { if (delete_user(cmd_argv[1], user_list_ptr) == 1) { error("user by this name does not exist"); } } else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) { switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) { case 1: error("users are already friends"); break; case 2: error("at least one user you entered has the max number of friends"); break; case 3: error("you must enter two different users"); break; case 4: error("at least one user you entered does not exist"); break; } } else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) { // first determine how long a string we need int space_needed = 0; for (int i = 3; i < cmd_argc; i++) { space_needed += strlen(cmd_argv[i]) + 1; } // allocate the space char *contents = malloc(space_needed); // copy in the bits to make a single string strcpy(contents, cmd_argv[3]); for (int i = 4; i < cmd_argc; i++) { strcat(contents, " "); strcat(contents, cmd_argv[i]); } User *author = find_user(cmd_argv[1], user_list); User *target = find_user(cmd_argv[2], user_list); switch (make_post(author, target, contents)) { case 1: error("the users are not friends"); break; case 2: error("at least one user you entered does not exist"); break; } } else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) { User *user = find_user(cmd_argv[1], user_list); if (print_user(user) == 1) { error("user not found"); } } else { error("Incorrect syntax"); } return 0; } /* * Tokenize the string stored in cmd. * Return the number of tokens, and store the tokens in cmd_argv. */ int tokenize(char *cmd, char **cmd_argv) { int cmd_argc = 0; char *next_token = strtok(cmd, DELIM); while (next_token != NULL) { if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) { error("too many arguments!"); cmd_argc = 0; break; } cmd_argv[cmd_argc] = next_token; cmd_argc++; next_token = strtok(NULL, DELIM); } return cmd_argc; } int main(int argc, char* argv[]) { int batch_mode = (argc == 2); char input[INPUT_BUFFER_SIZE]; FILE *input_stream; // Create the heads of the empty data structure User *user_list = NULL; if (batch_mode) { input_stream = fopen(argv[1], "r"); if (input_stream == NULL) { perror("Error opening file"); exit(1); } } else { // interactive mode input_stream = stdin; } printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> "); while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { // only echo the line in batch mode since in interactive mode the user // just typed the line if (batch_mode) { printf("%s", input); } char *cmd_argv[INPUT_ARG_MAX_NUM]; int cmd_argc = tokenize(input, cmd_argv); if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { break; // can only reach if quit command was entered } printf("> "); } if (batch_mode) { fclose(input_stream); } // Delete all users. This should free all heap memory that was allocated // during the run of the program. Uncomment after you've implemented // deleting users. printf("Freeing all heap space.\n"); printf("z %s\n", user_list->name); while (user_list != NULL) { printf("y %s\n", user_list->name); delete_user(user_list->name, &user_list); printf("w %s\n", user_list->name); } return 0; } int delete_user(const char *name, User **user_ptr_del) { User *user_ptr = find_user(name, *user_ptr_del); if(user_ptr == NULL) { return 1; } User *curr_usr = user_ptr_del[0]; while (strcmp(curr_usr->next->name, name) != 0) { curr_usr = curr_usr->next; } curr_usr->next = curr_usr->next->next; int i = 0; while(i <= 10 && user_ptr->friends[i] != NULL) { User *curr_friend_ptr = user_ptr->friends[i]; int j = 0; while(strcmp(curr_friend_ptr->friends[j]->name, user_ptr->name) != 0) { j += 1; } for(int k = j;k<10;k++) { if(curr_friend_ptr->friends[k] != NULL) { curr_friend_ptr->friends[k] = curr_friend_ptr->friends[k+1]; } } i += 1; } return 0; }
null
null
63c648795f004bf4898cfccf
2023-02-12T07:22:17.285Z
Question from Code
v1
Why segmentation fault ?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "friends.h" #define INPUT_BUFFER_SIZE 256 #define INPUT_ARG_MAX_NUM 12 #define DELIM " \n" /* * Print a formatted error message to stderr. */ void error(char *msg) { fprintf(stderr, "Error: %s\n", msg); } /* * Read and process commands * Return: -1 for quit command * 0 otherwise */ int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) { User *user_list = *user_list_ptr; printf("a %s\n", user_list[0].name); if (cmd_argc <= 0) { return 0; } else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) { return -1; } else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) { switch (create_user(cmd_argv[1], user_list_ptr)) { case 1: error("user by this name already exists"); break; case 2: error("username is too long"); break; } } else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) { list_users(user_list); } else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) { User *user = find_user(cmd_argv[1], user_list); if (user == NULL) { error("user not found"); } else { switch (update_pic(user, cmd_argv[2])) { case 1: error("file not found"); break; case 2: error("filename too long"); break; } } } else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) { if (delete_user(cmd_argv[1], user_list_ptr) == 1) { error("user by this name does not exist"); } } else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) { switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) { case 1: error("users are already friends"); break; case 2: error("at least one user you entered has the max number of friends"); break; case 3: error("you must enter two different users"); break; case 4: error("at least one user you entered does not exist"); break; } } else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) { // first determine how long a string we need int space_needed = 0; for (int i = 3; i < cmd_argc; i++) { space_needed += strlen(cmd_argv[i]) + 1; } // allocate the space char *contents = malloc(space_needed); // copy in the bits to make a single string strcpy(contents, cmd_argv[3]); for (int i = 4; i < cmd_argc; i++) { strcat(contents, " "); strcat(contents, cmd_argv[i]); } User *author = find_user(cmd_argv[1], user_list); User *target = find_user(cmd_argv[2], user_list); switch (make_post(author, target, contents)) { case 1: error("the users are not friends"); break; case 2: error("at least one user you entered does not exist"); break; } } else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) { User *user = find_user(cmd_argv[1], user_list); if (print_user(user) == 1) { error("user not found"); } } else { error("Incorrect syntax"); } return 0; } /* * Tokenize the string stored in cmd. * Return the number of tokens, and store the tokens in cmd_argv. */ int tokenize(char *cmd, char **cmd_argv) { int cmd_argc = 0; char *next_token = strtok(cmd, DELIM); while (next_token != NULL) { if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) { error("too many arguments!"); cmd_argc = 0; break; } cmd_argv[cmd_argc] = next_token; cmd_argc++; next_token = strtok(NULL, DELIM); } return cmd_argc; } int main(int argc, char* argv[]) { int batch_mode = (argc == 2); char input[INPUT_BUFFER_SIZE]; FILE *input_stream; // Create the heads of the empty data structure User *user_list = NULL; if (batch_mode) { input_stream = fopen(argv[1], "r"); if (input_stream == NULL) { perror("Error opening file"); exit(1); } } else { // interactive mode input_stream = stdin; } printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> "); while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { // only echo the line in batch mode since in interactive mode the user // just typed the line if (batch_mode) { printf("%s", input); } char *cmd_argv[INPUT_ARG_MAX_NUM]; int cmd_argc = tokenize(input, cmd_argv); if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { break; // can only reach if quit command was entered } printf("> "); } if (batch_mode) { fclose(input_stream); } // Delete all users. This should free all heap memory that was allocated // during the run of the program. Uncomment after you've implemented // deleting users. printf("Freeing all heap space.\n"); printf("z %s\n", user_list->name); while (user_list != NULL) { printf("y %s\n", user_list->name); delete_user(user_list->name, &user_list); printf("w %s\n", user_list->name); } return 0; } int delete_user(const char *name, User **user_ptr_del) { User *user_ptr = find_user(name, *user_ptr_del); if(user_ptr == NULL) { return 1; } User *curr_usr = user_ptr_del[0]; if(curr_usr->next == NULL) { curr_usr = NULL; } else { while (strcmp(curr_usr->next->name, name) != 0) { curr_usr = curr_usr->next; } curr_usr->next = curr_usr->next->next; } int i = 0; while(i <= 10 && user_ptr->friends[i] != NULL) { User *curr_friend_ptr = user_ptr->friends[i]; int j = 0; while(strcmp(curr_friend_ptr->friends[j]->name, user_ptr->name) != 0) { j += 1; } for(int k = j;k<10;k++) { if(curr_friend_ptr->friends[k] != NULL) { curr_friend_ptr->friends[k] = curr_friend_ptr->friends[k+1]; } } i += 1; } return 0; }
null
null
63c648795f004bf4898cfccf
2023-02-12T07:23:11.238Z
Question from Code
v1
Why segmentation fault ?
int delete_user(const char *name, User **user_ptr_del) { User *user_ptr = find_user(name, *user_ptr_del); if(user_ptr == NULL) { return 1; } User *curr_usr = user_ptr_del[0]; if(curr_usr->next == NULL) { curr_usr = NULL; } else { while (strcmp(curr_usr->next->name, name) != 0) { curr_usr = curr_usr->next; } curr_usr->next = curr_usr->next->next; } int i = 0; while(i <= 10 && user_ptr->friends[i] != NULL) { User *curr_friend_ptr = user_ptr->friends[i]; int j = 0; while(strcmp(curr_friend_ptr->friends[j]->name, user_ptr->name) != 0) { j += 1; } for(int k = j;k<10;k++) { if(curr_friend_ptr->friends[k] != NULL) { curr_friend_ptr->friends[k] = curr_friend_ptr->friends[k+1]; } } i += 1; } return 0; }
null
null
63c648795f004bf4898cfccf
2023-02-14T11:16:48.834Z
Question from Code
v1
Why valgrind gives error of reading zero bytes after allocation ?
int print_user(const User *user) { if(user == NULL) { return 1; } FILE *file_ptr = fopen(user->profile_pic, "r"); if(file_ptr == NULL) {return 1;} if(file_ptr != NULL) { char *profile_img_char = malloc(sizeof(char)); if(profile_img_char== NULL) { perror("malloc"); exit(5); } while(fread(profile_img_char, sizeof(char), 1, file_ptr) != 0) { printf("%s", profile_img_char); } printf("\n"); fclose(file_ptr); free(profile_img_char); } }
null
null
63c648795f004bf4898cfccf
2023-02-14T11:18:23.344Z
Question from Code
v1
How to prevent valgrind from giving error of reading zero bytes after allocation ?
int print_user(const User *user) { if(user == NULL) { return 1; } FILE *file_ptr = fopen(user->profile_pic, "r"); if(file_ptr == NULL) {return 1;} if(file_ptr != NULL) { char *profile_img_char = malloc(sizeof(char)); if(profile_img_char== NULL) { perror("malloc"); exit(5); } while(fread(profile_img_char, sizeof(char), 1, file_ptr) != 0) { printf("%s", profile_img_char); } printf("\n"); fclose(file_ptr); free(profile_img_char); } }
null
null
63c648795f004bf4898cfccf
2023-02-14T11:20:26.158Z
Question from Code
v1
Why does The while loop will always execute at least once, even if fread returns 0 (indicating that there is no more data to read from the file) ?
int print_user(const User *user) { if(user == NULL) { return 1; } FILE *file_ptr = fopen(user->profile_pic, "r"); if(file_ptr == NULL) {return 1;} if(file_ptr != NULL) { char *profile_img_char = malloc(sizeof(char)); if(profile_img_char== NULL) { perror("malloc"); exit(5); } while(fread(profile_img_char, sizeof(char), 1, file_ptr) != 0) { printf("%s", profile_img_char); } printf("\n"); fclose(file_ptr); free(profile_img_char); } }
null
null
63c648795f004bf4898cfccf
2023-02-14T11:27:02.785Z
General Question
v1
Does the while loop in c check condition after executing once ?
null
null
null
63c648795f004bf4898cfccf
2023-02-14T11:28:20.906Z
Question from Code
v1
Why is the while loop condition checked after the loop body executes ?
int print_user(const User *user) { if(user == NULL) { return 1; } FILE *file_ptr = fopen(user->profile_pic, "r"); if(file_ptr == NULL) {return 1;} if(file_ptr != NULL) { char *profile_img_char = malloc(sizeof(char)); if(profile_img_char== NULL) { perror("malloc"); exit(5); } while(fread(profile_img_char, sizeof(char), 1, file_ptr) != 0) { printf("%s", profile_img_char); } printf("\n"); fclose(file_ptr); free(profile_img_char); } }
null
null
63c648795f004bf4898cfccf
2023-02-14T11:33:45.420Z
Question from Code
v1
Does read move the pointer to parse the file ?
int print_user(const User *user) { if(user == NULL) { return 1; } FILE *file_ptr = fopen(user->profile_pic, "r"); if(file_ptr == NULL) {return 1;} if(file_ptr != NULL) { char *profile_img_char = malloc(sizeof(char)); if(profile_img_char== NULL) { perror("malloc"); exit(5); } while(fread(profile_img_char, sizeof(char), 1, file_ptr) != 0) { printf("%s", profile_img_char); } printf("\n"); fclose(file_ptr); free(profile_img_char); } }
null
null
63c648795f004bf4898cfccf
2023-02-14T11:47:03.534Z
Question from Code
v1
Why does this give the error "Invalid read of size 1" ?
int print_user(const User *user) { if(user == NULL) { return 1; } FILE *file_ptr = fopen(user->profile_pic, "r"); if(file_ptr == NULL) {return 1;} if(file_ptr != NULL) { char *profile_img_char = malloc(sizeof(char)); if(profile_img_char== NULL) { perror("malloc"); exit(5); } while(fread(profile_img_char, sizeof(char), 1, file_ptr) != 0) { printf("%s", profile_img_char); } printf("\n"); fclose(file_ptr); free(profile_img_char); } }
null
null
63c648795f004bf4898cfccf
2023-02-14T11:47:55.056Z
Question from Code
v1
Is profile_img_char initialised ?
int print_user(const User *user) { if(user == NULL) { return 1; } FILE *file_ptr = fopen(user->profile_pic, "r"); if(file_ptr == NULL) {return 1;} if(file_ptr != NULL) { char *profile_img_char = malloc(sizeof(char)); if(profile_img_char== NULL) { perror("malloc"); exit(5); } while(fread(profile_img_char, sizeof(char), 1, file_ptr) != 0) { printf("%s", profile_img_char); } printf("\n"); fclose(file_ptr); free(profile_img_char); } }
null
null
63c648795f004bf4898cfccf
2023-02-14T11:48:23.194Z
Question from Code
v1
How to initialize profile_img_char ?
int print_user(const User *user) { if(user == NULL) { return 1; } FILE *file_ptr = fopen(user->profile_pic, "r"); if(file_ptr == NULL) {return 1;} if(file_ptr != NULL) { char *profile_img_char = malloc(sizeof(char)); if(profile_img_char== NULL) { perror("malloc"); exit(5); } while(fread(profile_img_char, sizeof(char), 1, file_ptr) != 0) { printf("%s", profile_img_char); } printf("\n"); fclose(file_ptr); free(profile_img_char); } }
null
null
63c648795f004bf4898cfccf
2023-02-14T11:49:58.238Z
Help Fix Code
v1
null
int print_user(const User *user) { if(user == NULL) { return 1; } FILE *file_ptr = fopen(user->profile_pic, "r"); if(file_ptr == NULL) {return 1;} if(file_ptr != NULL) { char *profile_img_char = malloc(sizeof(char)); if(profile_img_char== NULL) { perror("malloc"); exit(5); } while(fread(profile_img_char, sizeof(char), 1, file_ptr) != 0) { printf("%s", profile_img_char); } printf("\n"); fclose(file_ptr); free(profile_img_char); } }
To not give the "Invalid read of size 0" error due to read
null
63c648795f004bf4898cfccf
2023-02-15T09:38:20.856Z
General Question
v1
How to initialize a empty character array ?
null
null
null
63c648795f004bf4898cfccf
2023-03-09T05:26:27.255Z
Question from Code
v1
Why do the values read by parent not written in the process called by excel?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define MAX_PASSWORD 10 #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 ret; int status; /* 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 */ 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) > MAX_PASSWORD) { printf(NO_USER); exit(3); } if(strlen(password) > MAX_PASSWORD) { printf(INVALID); exit(2); } int fd[2]; if (pipe(fd) == -1) { perror("pipe"); } ret = fork(); if (ret > 0) { if ((dup2(fd[1], fileno(stdout))) == -1) { perror("dup2"); exit(1); } if ((close(fd[0])) == -1) { perror("close"); } if ((close(fd[1])) == -1) { perror("close"); } } else if (ret == 0) { if ((dup2(fd[0], fileno(stdin))) == -1) { perror("dup2"); exit(1); } if ((close(fd[0])) == -1) { perror("close"); } if ((close(fd[1])) == -1) { perror("close"); } execl("./validate", "validate", NULL); } else { perror("fork"); exit(1); } printf("start wait\n"); wait(&status); printf("wait end\n"); printf("%d",WEXITSTATUS(status)); if(WEXITSTATUS(status) == 0) { printf(SUCCESS); } if(WEXITSTATUS(status) == 2) { printf(INVALID); } if(WEXITSTATUS(status) == 3) { printf(NO_USER); } printf("end\n"); return 0; }
null
null
63c648795f004bf4898cfccf
2023-03-09T05:29:51.426Z
Question from Code
v1
Why do the values read by parent not written in the process called by excel?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define MAX_PASSWORD 10 #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 ret; int status; /* 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 */ 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) > MAX_PASSWORD) { printf(NO_USER); exit(3); } if(strlen(password) > MAX_PASSWORD) { printf(INVALID); exit(2); } int fd[2]; if (pipe(fd) == -1) { perror("pipe"); } ret = fork(); if (ret > 0) { if ((dup2(fd[1], fileno(stdout))) == -1) { perror("dup2"); exit(1); } if ((close(fd[0])) == -1) { perror("close"); } if ((close(fd[1])) == -1) { perror("close"); } printf("start wait\n"); wait(&status); printf("wait end\n"); printf("%d",WEXITSTATUS(status)); if(WEXITSTATUS(status) == 0) { printf(SUCCESS); } if(WEXITSTATUS(status) == 2) { printf(INVALID); } if(WEXITSTATUS(status) == 3) { printf(NO_USER); } printf("end\n"); return 0; } else if (ret == 0) { if ((dup2(fd[0], fileno(stdin))) == -1) { perror("dup2"); exit(1); } if ((close(fd[0])) == -1) { perror("close"); } if ((close(fd[1])) == -1) { perror("close"); } execl("./validate", "validate", NULL); } else { perror("fork"); exit(1); } }
null
null
63c648795f004bf4898cfccf
2023-03-09T05:37:44.927Z
General Question
v1
how to write to the standard input of a process created by execl in c
null
null
null
63c648795f004bf4898cfccf
2023-03-09T05:41:05.701Z
General Question
v1
how to write to a process created by excel
null
null
null
63c648795f004bf4898cfccf
2023-03-09T05:51:42.337Z
General Question
v1
how to get address of a process created by execl
null
null
null
63c648795f004bf4898cfccf
2023-03-10T01:55:23.336Z
Question from Code
v1
Why segmentation fault ?
Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. char *cmd_line = NULL; Rule *data_structure = malloc(sizeof(Rule)); while(fgets(cmd_line, MAXLINE, fp) != NULL) { if(is_comment_or_empty(cmd_line) == 1) { continue; } if (cmd_line[0] != '\t' && cmd_line[0] != ' ') { char *curr_token; curr_token = strtok(cmd_line, " "); if(data_structure->target == NULL) { data_structure->target = curr_token; } else { Rule *curr_node = data_structure; while(curr_node->next_rule != NULL && curr_node->target != curr_token) { curr_node = curr_node->next_rule; } if(curr_node->target != curr_token) { curr_node->next_rule->target = curr_token; } } while(curr_token != NULL) { curr_token = strtok(NULL, " "); if (strcmp(curr_token, ":") == 0) { continue; } Rule *curr_node = data_structure; while(curr_node->next_rule != NULL && curr_node->target != curr_token) { curr_node = curr_node->next_rule; } if(curr_node->target != curr_token) { curr_node->next_rule->target = curr_token; } } } } return data_structure; }
null
null
63c648795f004bf4898cfccf
2023-03-10T01:56:45.411Z
Question from Code
v1
Why segmentation fault ?
Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. char *cmd_line = NULL; Rule *data_structure = malloc(sizeof(Rule)); while(fgets(cmd_line, MAXLINE, fp) != NULL) { if(is_comment_or_empty(cmd_line) == 1) { continue; } if (cmd_line[0] != '\t' && cmd_line[0] != ' ') { char *curr_token = NULL; curr_token = strtok(cmd_line, " "); if(data_structure->target == NULL) { data_structure->target = curr_token; } else { Rule *curr_node = data_structure; while(curr_node->next_rule != NULL && curr_node->target != curr_token) { curr_node = curr_node->next_rule; } if(curr_node->target != curr_token) { curr_node->next_rule->target = curr_token; } } while(curr_token != NULL) { curr_token = strtok(NULL, " "); if (strcmp(curr_token, ":") == 0) { continue; } Rule *curr_node = data_structure; while(curr_node->next_rule != NULL && curr_node->target != curr_token) { curr_node = curr_node->next_rule; } if(curr_node->target != curr_token) { curr_node->next_rule->target = curr_token; } } } } return data_structure; }
null
null
63c648795f004bf4898cfccf
2023-03-10T02:01:22.841Z
Question from Code
v1
Why segmentation fault ?
Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. char *cmd_line = malloc(sizeof(char) * MAXLINE); Rule *data_structure = malloc(sizeof(Rule)); while(fgets(cmd_line, MAXLINE, fp) != NULL) { if(is_comment_or_empty(cmd_line) == 1) { continue; } if (cmd_line[0] != '\t' && cmd_line[0] != ' ') { char *curr_token = malloc(sizeof(char) * MAXLINE); curr_token = strtok(cmd_line, " "); if(data_structure->target == NULL) { data_structure->target = curr_token; } else { Rule *curr_node = data_structure; while(curr_node->next_rule != NULL && curr_node->target != curr_token) { curr_node = curr_node->next_rule; } if(curr_node->target != curr_token) { curr_node->next_rule->target = curr_token; } } while(curr_token != NULL) { curr_token = strtok(NULL, " "); if (strcmp(curr_token, ":") == 0) { continue; } Rule *curr_node = data_structure; while(curr_node->next_rule != NULL && curr_node->target != curr_token) { curr_node = curr_node->next_rule; } if(curr_node->target != curr_token) { curr_node->next_rule->target = curr_token; } } } } return data_structure; } typedef struct rule_node { char *target; Dependency *dependencies; Action *actions; struct rule_node *next_rule; } Rule;
null
null
63c648795f004bf4898cfccf
2023-03-10T02:01:51.741Z
Question from Code
v1
Which line causes segmentation fault ?
Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. char *cmd_line = malloc(sizeof(char) * MAXLINE); Rule *data_structure = malloc(sizeof(Rule)); while(fgets(cmd_line, MAXLINE, fp) != NULL) { if(is_comment_or_empty(cmd_line) == 1) { continue; } if (cmd_line[0] != '\t' && cmd_line[0] != ' ') { char *curr_token = malloc(sizeof(char) * MAXLINE); curr_token = strtok(cmd_line, " "); if(data_structure->target == NULL) { data_structure->target = curr_token; } else { Rule *curr_node = data_structure; while(curr_node->next_rule != NULL && curr_node->target != curr_token) { curr_node = curr_node->next_rule; } if(curr_node->target != curr_token) { curr_node->next_rule->target = curr_token; } } while(curr_token != NULL) { curr_token = strtok(NULL, " "); if (strcmp(curr_token, ":") == 0) { continue; } Rule *curr_node = data_structure; while(curr_node->next_rule != NULL && curr_node->target != curr_token) { curr_node = curr_node->next_rule; } if(curr_node->target != curr_token) { curr_node->next_rule->target = curr_token; } } } } return data_structure; } typedef struct rule_node { char *target; Dependency *dependencies; Action *actions; struct rule_node *next_rule; } Rule;
null
null
63c648795f004bf4898cfccf
2023-03-10T02:06:46.307Z
Question from Code
v1
Which line causes segmentation fault ?
Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. char *cmd_line = NULL; Rule *data_structure = malloc(sizeof(Rule)); while(fgets(cmd_line, MAXLINE, fp) != NULL) { if(is_comment_or_empty(cmd_line) == 1) { continue; } if (cmd_line[0] != '\t' && cmd_line[0] != ' ') { char *curr_token = NULL; curr_token = strtok(cmd_line, " "); if(data_structure->target == NULL) { data_structure->target = curr_token; } else { Rule *curr_node = data_structure; while(curr_node->next_rule != NULL && curr_node->target != curr_token) { curr_node = curr_node->next_rule; } if(curr_node->target != curr_token) { Rule *new_rule = malloc(sizeof(Rule)); curr_node->next_rule = new_rule; curr_node->next_rule->target = curr_token; } } while(curr_token != NULL) { curr_token = strtok(NULL, " "); if (strcmp(curr_token, ":") == 0) { continue; } Rule *curr_node = data_structure; while(curr_node->next_rule != NULL && curr_node->target != curr_token) { curr_node = curr_node->next_rule; } if(curr_node->target != curr_token) { Rule *new_rule = malloc(sizeof(Rule)); curr_node->next_rule = new_rule; curr_node->next_rule->target = curr_token; } } } } return data_structure; }
null
null
63c648795f004bf4898cfccf
2023-03-10T02:07:16.131Z
Question from Code
v1
Why segmentation fault ?
Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. char *cmd_line = NULL; Rule *data_structure = malloc(sizeof(Rule)); while(fgets(cmd_line, MAXLINE, fp) != NULL) { if(is_comment_or_empty(cmd_line) == 1) { continue; } if (cmd_line[0] != '\t' && cmd_line[0] != ' ') { char *curr_token = NULL; curr_token = strtok(cmd_line, " "); if(data_structure->target == NULL) { data_structure->target = curr_token; } else { Rule *curr_node = data_structure; while(curr_node->next_rule != NULL && curr_node->target != curr_token) { curr_node = curr_node->next_rule; } if(curr_node->target != curr_token) { Rule *new_rule = malloc(sizeof(Rule)); curr_node->next_rule = new_rule; curr_node->next_rule->target = curr_token; } } while(curr_token != NULL) { curr_token = strtok(NULL, " "); if (strcmp(curr_token, ":") == 0) { continue; } Rule *curr_node = data_structure; while(curr_node->next_rule != NULL && curr_node->target != curr_token) { curr_node = curr_node->next_rule; } if(curr_node->target != curr_token) { Rule *new_rule = malloc(sizeof(Rule)); curr_node->next_rule = new_rule; curr_node->next_rule->target = curr_token; } } } } return data_structure; }
null
null
63c648795f004bf4898cfccf
2023-03-10T02:08:50.220Z
Question from Code
v1
Why segmentation fault ?
Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. char *cmd_line = = malloc(sizeof(char) * MAXLINE); Rule *data_structure = malloc(sizeof(Rule)); while(fgets(cmd_line, MAXLINE, fp) != NULL) { if(is_comment_or_empty(cmd_line) == 1) { continue; } if (cmd_line[0] != '\t' && cmd_line[0] != ' ') { char *curr_token = NULL; curr_token = strtok(cmd_line, " "); if(data_structure->target == NULL) { data_structure->target = curr_token; } else { Rule *curr_node = data_structure; while(curr_node->next_rule != NULL && curr_node->target != curr_token) { curr_node = curr_node->next_rule; } if(curr_node->target != curr_token) { Rule *new_rule = malloc(sizeof(Rule)); curr_node->next_rule = new_rule; curr_node->next_rule->target = curr_token; } } while(curr_token != NULL) { curr_token = strtok(NULL, " "); if (strcmp(curr_token, ":") == 0) { continue; } Rule *curr_node = data_structure; while(curr_node->next_rule != NULL && curr_node->target != curr_token) { curr_node = curr_node->next_rule; } if(curr_node->target != curr_token) { Rule *new_rule = malloc(sizeof(Rule)); curr_node->next_rule = new_rule; curr_node->next_rule->target = curr_token; } } } } return data_structure; }
null
null
63c648795f004bf4898cfccf
2023-03-17T18:53:49.098Z
Question from Code
v2
fp is file pointer to a file containing 100 integers. Why segmentation fault ?
FILE *fp; if ((fp = fopen(argv[2], "r")) == NULL) { perror("fopen"); exit(1); } for (;;) { int num = (random() * 99) / RAND_MAX; fseek(fp, SEEK_SET, num); char *ptr = NULL; fgets(ptr, 8, fp); int rand_num_read = strtol(ptr, NULL, 10); fprintf(stderr, "%d\n", rand_num_read); }
null
null
63c648795f004bf4898cfccf
2023-03-17T19:23:04.006Z
Question from Code
v2
Why only 0 printed ? fp points to file containing 100 integers from 0 to 99
FILE *fp; if ((fp = fopen(argv[2], "r")) == NULL) { perror("fopen"); exit(1); } for (;;) { int num = (random() * 99) / RAND_MAX; fseek(fp, SEEK_SET, num); int *ptr = malloc(sizeof(int)); fread(ptr, sizeof(int), 1, fp); printf("#%d#\n", *ptr); }
null
null
63c648795f004bf4898cfccf
2023-03-30T06:32:39.061Z
General Question
v2
Why error client: connect: Connection refused in terminal when I run ./chat_client after running ./chat_server & where i use port 30000 for both of them and port 53655 for the makefile?
null
null
null
63c648795f004bf4898cfccf
2023-03-30T22:17:28.617Z
Question from Code
v2
How to add the socket with the server to all_fds?
#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <unistd.h> #ifndef PORT #define PORT 30000 #endif #define BUF_SIZE 128 int main(void) { int sock_fd = socket(AF_INET, SOCK_STREAM, 0); if (sock_fd < 0) { perror("client: socket"); exit(1); } 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); } if (connect(sock_fd, (struct sockaddr *)&server, sizeof(server)) == -1) { perror("client: connect"); close(sock_fd); exit(1); } char buf[2 * BUF_SIZE + 2]; 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); } int max_fd = sock_fd; fd_set all_fds; FD_ZERO(&all_fds); FD_SET(sock_fd, &all_fds); FD_SET(0, &all_fds); 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
63c648795f004bf4898cfccf
2023-04-03T02:53:21.086Z
Question from Code
v2
Why segmentation fault?
#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <unistd.h> #include "helper.c" #ifndef PORT #define PORT 53655 #endif #define MAX_BACKLOG 5 #define MAX_CONNECTIONS 12 #define BUF_SIZE 128 typedef struct client { int client_fd; char *client_username; char partial_read[BUF_SIZE]; struct client *next; } Client; int accept_connection(int fd, Client *client_linked_list) { int client_fd = accept(fd, NULL, NULL); if (client_fd < 0) { perror("server: accept"); close(fd); exit(1); } Client *curr_node = client_linked_list; while (curr_node->next != NULL) { if (curr_node->next->client_fd != -1) { break; } curr_node = curr_node->next; } Client *new_node = malloc(sizeof(Client)); new_node->client_fd = client_fd; new_node->client_username = NULL; curr_node->next = new_node; return client_fd; } int read_from(Client *curr_client, User *user_list) { int fd = curr_client->client_fd; char buf[BUF_SIZE + 1]; int num_read = read(fd, &buf, BUF_SIZE); buf[num_read] = '\0'; char *new_message = helper(buf, curr_client->client_username, user_list); if (num_read == 0 || write(fd, new_message, strlen(new_message)) != strlen(new_message)) { curr_client->client_fd = -1; return fd; } return 0; }
null
null
63c648795f004bf4898cfccf
2023-04-03T03:28:35.385Z
Question from Code
v2
Question: Why segmentation fault?
#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <unistd.h> #include "helper.c" #ifndef PORT #define PORT 53655 #endif #define MAX_BACKLOG 5 #define MAX_CONNECTIONS 12 #define BUF_SIZE 128 typedef struct client { int client_fd; char *client_username; char partial_read[BUF_SIZE]; struct client *next; } Client; int accept_connection(int fd, Client *client_linked_list) { int client_fd = accept(fd, NULL, NULL); if (client_fd < 0) { perror("server: accept"); close(fd); exit(1); } Client *curr_node = client_linked_list; if (curr_node == NULL) { client_linked_list = malloc(sizeof(Client)); client_linked_list->client_fd = client_fd; client_linked_list->client_username = NULL; } else { while (curr_node->next != NULL) { if (curr_node->next->client_fd != -1) { break; } curr_node = curr_node->next; } Client *new_node = malloc(sizeof(Client)); new_node->client_fd = client_fd; new_node->client_username = NULL; curr_node->next = new_node; } return client_fd; } int read_from(Client *curr_client, User *user_list) { int fd = curr_client->client_fd; char buf[BUF_SIZE + 1]; int num_read = read(fd, &buf, BUF_SIZE); buf[num_read] = '\0'; char *new_message = helper(buf, curr_client->client_username, user_list); if (num_read == 0 || write(fd, new_message, strlen(new_message)) != strlen(new_message)) { curr_client->client_fd = -1; return fd; } return 0; }
null
null
63c648795f004bf4898cfccf
2023-04-03T03:37:57.382Z
Question from Code
v2
Why segmentation fault?
#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <unistd.h> #include "helper.c" #ifndef PORT #define PORT 53655 #endif #define MAX_BACKLOG 5 #define MAX_CONNECTIONS 12 #define BUF_SIZE 128 typedef struct client { int client_fd; char *client_username; char partial_read[BUF_SIZE]; struct client *next; } Client; int accept_connection(int fd, Client *client_linked_list) { int client_fd = accept(fd, NULL, NULL); if (client_fd < 0) { perror("server: accept"); close(fd); exit(1); } Client *curr_node = client_linked_list; if (curr_node == NULL) { client_linked_list = malloc(sizeof(Client)); client_linked_list->client_fd = client_fd; client_linked_list->client_username = NULL; } else { while (curr_node->next != NULL) { if (curr_node->next->client_fd != -1) { break; } curr_node = curr_node->next; } Client *new_node = malloc(sizeof(Client)); new_node->client_fd = client_fd; new_node->client_username = NULL; curr_node->next = new_node; } return client_fd; } if (FD_ISSET(sock_fd, &listen_fds)) { int client_fd = accept_connection(sock_fd, client_linked_list); if (client_fd > max_fd) { max_fd = client_fd; } FD_SET(client_fd, &all_fds); printf("Accepted connection\n"); write(client_fd, "Please enter a username: ", 26); char buf[BUF_SIZE + 1]; int num_read = read(client_fd, &buf, BUF_SIZE); buf[num_read] = '\0'; printf("1\n"); Client *curr_client = client_linked_list; while (curr_client->client_fd != client_fd) { curr_client = curr_client->next; } printf("2\n"); if (curr_client->client_username == NULL) { printf("3\n"); curr_client->client_username = malloc(sizeof(char) * (strlen(buf) + 1)); printf("4\n"); strcpy(curr_client->client_username, buf); printf("5\n"); curr_client->client_username[num_read] = '\0'; printf("6\n"); } write(client_fd, "Go ahead and enter user commands\n", 34); }
null
null
63c648795f004bf4898cfccf
2023-04-04T04:35:30.270Z
Question from Code
v2
Why doesnt readfrom read from the client in one go
int read_from(Client *curr_client, User *user_list) { int fd = curr_client->client_fd; printf("5\n"); char buf[BUF_SIZE] = {'\0'}; int inbuf = 0; int room = sizeof(buf); char *after = buf; int num_read = 0; while ((num_read = read(fd, after, room)) > 0) { inbuf = BUF_SIZE; int where = 0; while ((where = find_network_newline(buf, inbuf)) > 0) { printf("9#%d##%s##%lu#\n", where, buf, strlen(buf)); buf[where - 2] = '\0'; printf("Next message: %s\n", buf); inbuf = BUF_SIZE - where; for (int j = 0; j < inbuf; j++) { buf[j] = buf[where + j]; } for (int i = inbuf; i < BUF_SIZE; i++) { buf[i] = '\0'; } } printf("WHERE#%d#\n", where); after = &(buf[inbuf]); room = BUF_SIZE - inbuf; } printf("6\n"); printf("#BUF#%s##%lu#\n", buf, strlen(buf)); char *new_message = helper(buf, curr_client->client_username, user_list); printf("#%s#@%s@#%s#@%s@\n", buf, curr_client->client_username, curr_client->client_user->name, new_message); printf("#%lu#@%lu@#%lu#@%lu@\n", strlen(buf), strlen(curr_client->client_username), strlen(curr_client->client_user->name), strlen(new_message)); if (write(fd, new_message, strlen(new_message)) != strlen(new_message)) { printf("7\n"); curr_client->client_fd = -1; return fd; } printf("8\n"); return 0; } int find_network_newline(const char *buf, int n) { int index = 0; while (index < n - 1) { if (buf[index] == '\r' && buf[index + 1] == '\n') { return index + 2; } index = index + 1; } return -1; }
null
null
63c6487d5f004bf4898cfced
2023-01-22T22:33:51.649Z
Explain Code
v1
null
int populate_array(int sin, int *sin_array) { int digit; for(int i = 0; int < 10; i++) { if(sin == 0) {return 1;} sin_array[i + 8] = sin % 10; sin /= 10; return 0; }
null
null
63c6487d5f004bf4898cfced
2023-01-22T22:39:48.586Z
Help Fix Code
v1
null
int populate_array(int sin, int *sin_array) { int digit; for(int i = 0; int < 10; i++) { if(sin == 0) {return 1;} sin_array[9 - i] = sin % 10; sin /= 10; } return 0; }
create an array of integers from the digits in sin
null
63c6487d5f004bf4898cfced
2023-01-25T01:51:06.788Z
Help Fix Code
v1
null
void fib(int *fib_sequence, int n) { int *fib_sequence = malloc(sizeof(int) * n); for(int i = 0; i < n + 1; i++) { if(i == 0) {fib_sequence[i] = 0;} else if(i == 1 || i == 2) {fib_sequence[i] = 1;} else {fib_sequence[i] = fib_sequence[i-2] + fib_sequence[i-1];} } }
create dynamic array of fibonacci sequence of n items
null
63c6487d5f004bf4898cfced
2023-01-25T02:36:16.757Z
Help Fix Code
v1
null
void fib(int **fib_sequence, int n) { fib_sequence = malloc(sizeof(int*) * n); for(int i = 0; i < n; i++) { if(i == 0) {*fib_sequence[i] = 0;} else if(i == 1 || i == 2) {*fib_sequence[i] = 1;} else {*fib_sequence[i] = *fib_sequence[i-2] + *fib_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; }
create a dynamically allocated array of fibonacci sequence of size n
null
63c6487d5f004bf4898cfced
2023-01-25T03:04:59.250Z
Help Fix Code
v1
null
void fib(int **fib_sequence, int n) { int *heap_memory = malloc(sizeof(int) * n); for(int i = 0; i < n; i++) { if(i == 0) {heap_memory[i] = 0;} else if(i == 1 || i == 2) {heap_memory[i] = 1;} else {heap_memory[i] = heap_memory[i-2] + heap_memory[i-1];} } *fib_sequence = heap_memory; }
null
null
63c6487d5f004bf4898cfced
2023-01-25T03:06:05.956Z
Help Fix Code
v1
null
void fib(int **fib_sequence, int n) { int *heap_memory = malloc(sizeof(int) * n); for(int i = 0; i < n; i++) { if(i == 0) {heap_memory[i] = 0;} else if(i == 1 || i == 2) {heap_memory[i] = 1;} else {heap_memory[i] = heap_memory[i-2] + heap_memory[i-1];} } *fib_sequence = heap_memory; }
create a dynamically allocated array of fibonacci sequence numbers of size n
null
63c6487d5f004bf4898cfced
2023-01-25T03:44:23.790Z
Help Fix Code
v1
null
void fib(int **fib_sequence, int n) { fib_sequence = malloc(sizeof(int) * n); for(int i = 0; i < n; i++) { if(i == 0) {*fib_sequence[0] = 0;} else if(i == 1) {*fib_sequence[1] = 1;} else {*fib_sequence[i] = *fib_sequence[i-2] + *fib_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, 2); for (int i = 0; i < 1; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
store the first n values in the Fibonacci sequence in a dynamically-allocated array
null
63c6487d5f004bf4898cfced
2023-01-25T16:14:10.205Z
Help Fix Code
v1
null
int **split_array(const int *s, int length) { int *array_even = malloc(sizeof(int) * length / 2 + length % 2); int *array_odd = malloc(sizeof(int) * length / 2); int (*arrays)[2] = {array_even, array_odd}; for(int i = 0; i < length; i++) { if(i % 2 == 0) {array_even[i / 2] = s[i];} else {array_odd[i / 2] = s[i];} } return arrays; }
Return a pointer to an array of two dynamically allocated arrays. The first array contains the elements of the input array s that are at even indices. The second array contains the elements of the input array s that are at odd indices.
null
63c6487d5f004bf4898cfced
2023-02-02T17:39:51.942Z
Help Fix Code
v1
null
int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "Usage: greeting message name\n"); return 1; } char greeting[20]; char *name = argv[2]; // Your code goes here strncpy(greeting, argv[1], 20); if (strlen(greeting) < 19) { greeting[strlen(greeting)] = ' '; greeting[strlen(argv[1]) + 1] = '\0'; } int i = 0; int length = strlen(greeting); while (i < strlen(name) && length < 19) { greeting[length + 1] = name[i]; greeting[length + 2] = '\0'; length = strlen(greeting); i++; } printf("%s\n", greeting); return 0; }
Write code to personalize the greeting string by appending a space and then the string pointed to by name.
null
63c6487d5f004bf4898cfced
2023-02-06T03:01:41.152Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) > MAX_NAME) { return 2; } struct user *curr_user = *user_ptr_add; while (curr_user->next != NULL) { if (strcmp(curr_user->name, name) == 0) { return 1; } curr_user = curr_user->next; } return 0; }
return 1 if a user with name already exists
null
63c6487d5f004bf4898cfced
2023-02-06T03:11:43.548Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) > MAX_NAME) { return 2; } struct user *curr_user = *user_ptr_add; while (curr_user->next != NULL) { if (strcmp(curr_user->name, name) == 0) { return 1; } curr_user = curr_user->next; } struct user *new_user = malloc(sizeof(struct user*)); strcpy(new_user->name, name); curr_user->next = new_user; return 0; }
Create a new user with the given name. Insert it at the tail of the list of users whose head is pointed to by *user_ptr_add.
null
63c6487d5f004bf4898cfced
2023-02-06T05:20:41.079Z
Help Fix Code
v1
null
int i = 0; int j = 0; while (i < MAX_FRIENDS) { // Determine the first available slots in user1 and user2 friend lists. if ((curr_user1->friends)[i] == NULL) { while (j < MAX_FRIENDS) { if ((curr_user1->friends)[j] == NULL) { j = j + MAX_FRIENDS + 1; } j++; } i = i + MAX_FRIENDS + 1; } // Check if the users are already friends. else if ((curr_user1->friends)[i] == curr_user2) { already_friends = 1; i = i + MAX_FRIENDS + 1; } i++; }
Add curr_user1 to the first available spot in the curr_user2 friends array, and vice versa.
null
63c6487d5f004bf4898cfced
2023-03-02T23:14:56.781Z
Help Fix Code
v1
null
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(1); } else if (n == 0) { printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid()> exit(0); } } return 0; }
Only original parents call fork, and new children do not create additional processes.
null
63c6487d5f004bf4898cfced
2023-03-02T23:15:48.566Z
Help Fix Code
v1
null
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(1); } else if (n == 0) { printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid()> exit(0); } } return 0; }
Only original parents call fork, and new children do not create additional processes.
null
63c6487d5f004bf4898cfced
2023-03-02T23:26:30.095Z
Help Fix Code
v1
null
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(1); } printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); if (n == 0) { exit(0); } } for (int i = 0; i < iterations; i++) { wait(NULL); } return 0; }
Only the original parent calls fork, and new children do not create additional processes
null
63c6487d5f004bf4898cfced
2023-03-02T23:28:29.699Z
Help Fix Code
v1
null
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(1); } else if (n == 0) { printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid()> exit(0); } } for (int i = 0; i < iterations; i++) { wait(NULL); } return 0; }
Only the original parent calls fork, and new children do not create additional processes
null
63c6487d5f004bf4898cfced
2023-03-02T23:29:37.584Z
Help Fix Code
v1
null
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(1); } else if (n == 0) { printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); exit(0); } } for (int i = 0; i < iterations; i++) { wait(NULL); } return 0; }
Only the original parent calls fork, and new children do not create additional processes
null
63c6487d5f004bf4898cfced
2023-03-06T19:19:32.480Z
Help Fix Code
v1
null
Rule *parse_file(FILE *fp) { // Create a pointer to the first rule in the linked list. Rule *first_rule = create_rule(fp, NULL); // Create new rules while the file is not empty. Rule *prev_rule = first_rule; Rule *new_rule; while (new_rule != NULL) { // Attempt to create a new rule from current position in Makefile. new_rule = create_rule(fp, first_rule); // Link the newly created rule to the previous rule if successful. if (new_rule != NULL) { prev_rule->next_rule = create_rule(fp, first_rule); prev_rule = prev_rule->next_rule; } } // Return a pointer to the first rule in the linked list. return first_rule; }
/* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */
null
63c6487d5f004bf4898cfced
2023-03-06T19:22:46.039Z
Help Fix Code
v1
null
Rule *create_rule(FILE *fp, Rule *first_rule) { char line[MAXLINE]; char buffer[MAXLINE]; // Read line from fp. fgets(line, MAXLINE, fp); // Skip comment and empty lines. while (is_comment_or_empty(buffer) == 1) { // Return NULL pointer if end of file is reached. if (fgets(line, MAXLINE, fp) == NULL) { return NULL; } } // Read in the rule's target name. int len_target = 0; while (line[len_target] != ' ') { buffer[len_target] = line[len_target]; len_target += 1; } buffer[len_target] = '\0'; // Determine whether a rule with target name already exists in the list. Rule *rule = find_rule(first_rule, buffer); if (rule == NULL) { rule = malloc(sizeof(Rule)); rule->dependencies = NULL; rule->actions = NULL; rule->next_rule = NULL; } // Set rule's target attribute to the read target. strcpy(rule->target, buffer); // Read in the dependencies, if they exist. int i = len_target + 3; int len_dep; Rule *dep; Dependency *curr_dep; while (line[i] != '\n') { len_dep = 0; // Read a depenency name, if it exists. while (line[i] != ' ' && line[i] != '\n') { buffer[len_dep] = line[i]; i += 1; len_dep += 1; } // Add to the list of dependencies if a dependency name was read. if (len_dep > 0) { buffer[len_dep] = '\0'; // Attempt to find the dependency in the existing list of rules. dep = find_rule(first_rule, buffer); // Create a new rule on the heap if it DNE. if (dep == NULL) { dep = malloc(sizeof(Rule)); strcpy(dep->target, buffer); dep->dependencies = NULL; dep->actions = NULL; dep->next_rule = NULL; } // Create a new dependency object on the heap. Dependency *new_dep = malloc(sizeof(Dependency)); new_dep->rule = dep; new_dep->next_dep = NULL; // Add the dependency to the list of dependencies in rule. if (rule->dependencies == NULL) { rule->dependencies = new_dep; } else { curr_dep = rule->dependencies; while (curr_dep->next_dep != NULL) { curr_dep = curr_dep->next_dep; } curr_dep->next_dep = new_dep; } } i += 1; } // Read in arguments from fp and save it to rule's actions. int len_arg; int num_args; int curr_arg; Action *prev_action = NULL; while (fgets(line, MAXLINE, fp) != NULL && line[0] == ' ') { // Skip comment or empty lines. while (is_comment_or_empty(line) == 1) { fgets(line, MAXLINE, fp); } // If action lines have finished, exit the loop. if (line[0] != ' ' || fp == NULL) { break; } // Determine the number of arguments in the line. i = 0; num_args = 0; while (line[i] != '\n') { while (line[i] != ' ') { i += 1; } num_args += 1; i += 1; } // Create an Action node. Action *action = malloc(sizeof(Action)); action->args = malloc(sizeof(char *) * num_args); action->next_act = NULL; i = 0; curr_arg = 0; while (line[i] != '\n') { len_arg = 0; // Read in one argument. while (line[i] != ' ') { buffer[len_arg] = line[i]; len_arg += 1; i += 1; } buffer[len_arg] = '\0'; // Save argument into args array. strcpy(action->args[curr_arg], buffer); i += 1; curr_arg += 1; } // Add the Action node to the list of actions. if (prev_action == NULL) { rule->actions = action; } else { prev_action->next_act = action; } prev_action = action; } // Move file pointer back one line. fseek(fp, -strlen(line), SEEK_CUR); // Return a pointer to the newly created Rule. return rule; }
Read from the open file fp, and create a new Rule object that represents one rule in the Makefile contained in the file. Return a pointer to the Rule.
null
63c6487d5f004bf4898cfced
2023-03-06T19:32:11.839Z
Help Fix Code
v1
null
Rule *parse_file(FILE *fp) { // Create a pointer to the first rule in the linked list. Rule *first_rule = create_rule(fp, NULL); // Create new rules while the file is not empty. Rule *prev_rule = first_rule; Rule *new_rule; while (new_rule != NULL) { // Attempt to create a new rule from current position in Makefile. new_rule = create_rule(fp, first_rule); // Link the newly created rule to the previous rule if successful. if (new_rule != NULL) { prev_rule->next_rule = create_rule(fp, first_rule); prev_rule = prev_rule->next_rule; } } // Return a pointer to the first rule in the linked list. return first_rule; } Rule *create_rule(FILE *fp, Rule *first_rule) { char line[MAXLINE]; char buffer[MAXLINE]; // Read line from fp. fgets(line, MAXLINE, fp); // Skip comment and empty lines. while (is_comment_or_empty(buffer) == 1) { // Return NULL pointer if end of file is reached. if (fgets(line, MAXLINE, fp) == NULL) { return NULL; } } // Read in the rule's target name. int len_target = 0; while (line[len_target] != ' ') { buffer[len_target] = line[len_target]; len_target += 1; } buffer[len_target] = '\0'; // Determine whether a rule with target name already exists in the list. Rule *rule = find_rule(first_rule, buffer); if (rule == NULL) { rule = malloc(sizeof(Rule)); rule->dependencies = NULL; rule->actions = NULL; rule->next_rule = NULL; } // Set rule's target attribute to the read target. rule->target = malloc(sizeof(char) * (strlen(buffer) + 1)); strcpy(rule->target, buffer); // Read in the dependencies, if they exist. int i = len_target + 3; int len_dep; Rule *dep; Dependency *curr_dep; while (line[i] != '\n') { len_dep = 0; // Read a depenency name, if it exists. while (line[i] != ' ' && line[i] != '\n') { buffer[len_dep] = line[i]; i += 1; len_dep += 1; } // Add to the list of dependencies if a dependency name was read. if (len_dep > 0) { buffer[len_dep] = '\0'; // Attempt to find the dependency in the existing list of rules. dep = find_rule(first_rule, buffer); // Create a new rule on the heap if it DNE. if (dep == NULL) { dep = malloc(sizeof(Rule)); dep->target = malloc(sizeof(char) * (strlen(buffer) + 1)); strcpy(dep->target, buffer); dep->dependencies = NULL; dep->actions = NULL; dep->next_rule = NULL; } // Create a new dependency object on the heap. Dependency *new_dep = malloc(sizeof(Dependency)); new_dep->rule = dep; new_dep->next_dep = NULL; // Add the dependency to the list of dependencies in rule. if (rule->dependencies == NULL) { rule->dependencies = new_dep; } else { curr_dep = rule->dependencies; while (curr_dep->next_dep != NULL) { curr_dep = curr_dep->next_dep; } curr_dep->next_dep = new_dep; } } i += 1; } // Read in arguments from fp and save it to rule's actions. int len_arg; int num_args; int curr_arg; Action *prev_action = NULL; while (fgets(line, MAXLINE, fp) != NULL && line[0] == ' ') { // Skip comment or empty lines. while (is_comment_or_empty(line) == 1) { fgets(line, MAXLINE, fp); } // If action lines have finished, exit the loop. if (line[0] != ' ' || fp == NULL) { break; } // Determine the number of arguments in the line. i = 0; num_args = 0; while (line[i] != '\n') { while (line[i] != ' ') { i += 1; } num_args += 1; i += 1; } // Create an Action node. Action *action = malloc(sizeof(Action)); action->args = malloc(sizeof(char *) * num_args); action->next_act = NULL; i = 0; curr_arg = 0; while (line[i] != '\n') { len_arg = 0; // Read in one argument. while (line[i] != ' ') { buffer[len_arg] = line[i]; len_arg += 1; i += 1; } buffer[len_arg] = '\0'; // Save argument into args array. action->args[curr_arg] = malloc(sizeof(char) * (strlen(buffer)+1)); strcpy(action->args[curr_arg], buffer); i += 1; curr_arg += 1; } // Add the Action node to the list of actions. if (prev_action == NULL) { rule->actions = action; } else { prev_action->next_act = action; } prev_action = action; } // Move file pointer back one line. fseek(fp, -strlen(line), SEEK_CUR); // Return a pointer to the newly created Rule. return rule; } /* Search for a rule with the same target as the input target. Return a pointer to the Rule, if it exists. Otherwise, return NULL. */ Rule *find_rule(Rule *first_rule, char *target) { Rule *curr_rule = first_rule; Dependency *curr_dep; // Search for a rule with a matching target in the list of rules and their dependencies. while (curr_rule != NULL) { // Return a pointer to a rule if its target matches the input string. if (strcmp(target, curr_rule->target)) { return curr_rule; } // Return a pointer to a rule in a list of dependencies if its target matches the input string. curr_dep = curr_rule->dependencies; while (curr_dep != NULL) { if (strcmp(target, curr_dep->rule->target)) { return curr_dep->rule; } curr_dep = curr_dep->next_dep; } // Otherwise, continue to traverse the list of rules. curr_rule = curr_rule->next_rule; } // If no rule is found, return NULL. return NULL; }
Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file.
null
63c6487d5f004bf4898cfced
2023-03-06T22:10:05.775Z
Help Fix Code
v1
null
// Read line from fp. fgets(line, MAXLINE, fp); // Skip comment and empty lines. while (is_comment_or_empty(line) == 1) { // Return NULL pointer if end of file is reached. if (fgets(line, MAXLINE, fp) == NULL) { return NULL; } } // Read in the rule's target name. int len_target = 0; while (line[len_target] != ' ') { buffer[len_target] = line[len_target]; len_target += 1; } buffer[len_target] = '\0'; // Determine whether a rule with target name already exists in the list. Rule *rule = find_rule(first_rule, buffer); if (rule == NULL) { rule = malloc(sizeof(Rule)); rule->dependencies = NULL; rule->actions = NULL; rule->next_rule = NULL; } // Set rule's target attribute to the read target. rule->target = malloc(sizeof(char) * (strlen(buffer) + 1)); strcpy(rule->target, buffer);
Define the target attribute of a rule object to be the first word in a line.
null
63c6487d5f004bf4898cfced
2023-03-06T22:12:58.606Z
Help Fix Code
v1
null
rule->target = malloc(sizeof(char) * (strlen(buffer) + 1)); strcpy(rule->target, buffer);
Copy the contents of buffer into the target attribute of rule.
null
63c6487d5f004bf4898cfced
2023-03-07T16:49:58.570Z
Help Fix Code
v1
null
char line[MAXLINE]; // Read line from fp. fgets(line, MAXLINE, fp);
Read in a line from fp and store the string into line.
null
63c6487d5f004bf4898cfced
2023-03-10T02:29:37.611Z
General Question
v2
How do you create a new process within a program to call a separate executable file?
null
null
null
63c6487d5f004bf4898cfced
2023-03-10T17:43:31.225Z
General Question
v2
How do you enter enter key in execl
null
null
null
63c6487d5f004bf4898cfced
2023-03-17T20:58:36.154Z
Help Fix Code
v2
Change the signal handler to print a message to standard out (use the MESSAGE format in the starter code) that provides both the total number of reads completed and the time elapsed (s seconds).
#include <stdio.h> #include <signal.h> #include <stdlib.h> #include <unistd.h> #include <sys/time.h> /* Message to print in the signal handling function. */ #define MESSAGE "%ld reads were done in %ld seconds.\n" /* Global variables to store number of read operations and seconds elap> */ long num_reads, seconds; /* Signal handler. */ void handle(int signal) { fprintf(stdout, MESSAGE, num_reads, seconds); exit(0); } /* The first command-line argument is the number of seconds to set a ti> * The second argument is the name of a binary file containing 100 ints. * Assume both of these arguments are correct. */ int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "Usage: time_reads s filename\n"); exit(1); } seconds = strtol(argv[1], NULL, 10); FILE *fp; if ((fp = fopen(argv[2], "r")) == NULL) { perror("fopen"); exit(1); } /* Signal handler */ struct sigaction sig_action; sig_action.sa_handler = &handle; sig_action.sa_flags = 0; sigemptyset(&sig_action.sa_mask); if (sigaction(SIGPROF, &sig_action, NULL) != 0) { perror("sigaction"); exit(1); } /* Timer */ struct itimerval timer; timer.it_value.tv_sec = seconds; timer.it_value.tv_usec = 0; timer.it_interval.tv_sec = 0; timer.it_interval.tv_usec = 0; if (setitimer(ITIMER_PROF, &timer, NULL) == -1) { perror("timer"); exit(1); } int randomi; int random_offset; num_reads = 0; /* In an infinite loop, read an int from a random location in the f> * and print it to stderr. */ for (;;) { random_offset = random() % 100 * sizeof(int); fseek(fp, random_offset, SEEK_SET); fread(&randomi, sizeof(randomi), 1, fp); printf("%d\n", randomi); num_reads += 1; } return 1; // something is wrong if we ever get here! }
null
null
63c6487d5f004bf4898cfced
2023-03-23T14:21:34.127Z
General Question
v2
how do you determine if there is a certain element in a ps_set
null
null
null
63c6487d5f004bf4898cfced
2023-03-27T21:04:11.580Z
General Question
v2
How do I broadcast a message to a client
null
null
null
63c6487d5f004bf4898cfced
2023-03-28T21:15:51.434Z
Help Write Code
v2
null
null
null
update the client so it monitors just two file descriptors: the socket with the server and stdin
63c6487d5f004bf4898cfced
2023-04-04T17:45:42.090Z
Help Fix Code
v2
Return string
char **list_users(const User *curr) { // Determine the amount of memory required to be allocated. int memory_size = 1; memory_size += sizeof("User List\n"); User *curr_m = (User *)curr; while (curr_m != NULL) { memory_size += sizeof(curr_m->name) + 2; curr_m = curr_m->next; } // Allocate the memory. char **user_list = malloc(memory_size); if (user_list == NULL) { perror("malloc"); exit(1); } // Copy the user list into a buffer. char buf[memory_size]; strcpy(buf, "User List\n"); while (curr != NULL) { strcat(buf, "\t"); strcat(buf, curr->name); strcat(buf, "\n"); curr = curr->next; } // Return the list of dynamically allocated strings. *user_list = buf; return user_list; }
null
null
63c6487e5f004bf4898cfcfc
2023-01-29T15:55:39.779Z
General Question
v1
how to pass in the array
null
null
null
63c6487e5f004bf4898cfcfc
2023-01-29T15:57:13.194Z
Explain Code
v1
null
int main(int argc, char **argv) { // TODO: Verify that command line arguments are valid. if (argc < 2 || argc > 2){ return 2; } else{ int sin_number = strtol(argv[1], NULL, 10); int *array[9]; if ((populate_array(sin_number, array) | check_sin(array)) == 1){ printf("Invalid SIN \n"); return 1; } printf("Valid SIN\n"); // TODO: Parse arguments and then call the two helpers in sin_helpers.c // to verify the SIN given as a command line argument. return 0; } }
null
null
63c6487e5f004bf4898cfcfc
2023-01-29T15:58:29.482Z
Help Fix Code
v1
null
int main(int argc, char **argv) { // TODO: Verify that command line arguments are valid. if (argc < 2 || argc > 2){ return 2; } else{ int sin_number = strtol(argv[1], NULL, 10); int *array[9]; if ((populate_array(sin_number, array) | check_sin(array)) == 1){ printf("Invalid SIN \n"); return 1; } printf("Valid SIN\n"); // TODO: Parse arguments and then call the two helpers in sin_helpers.c // to verify the SIN given as a command line argument. return 0; } }
how to run without errors
null
63c6487e5f004bf4898cfcfc
2023-01-29T16:00:35.674Z
Help Fix Code
v1
null
int main(int argc, char **argv) { // TODO: Verify that command line arguments are valid. if (argc < 2 || argc > 2){ return 2; } else{ int sin_number = strtol(argv[1], NULL, 10); int array[9]; if ((populate_array(sin_number, array) | check_sin(array)) == 1){ printf("Invalid SIN \n"); return 1; } printf("Valid SIN\n"); // TODO: Parse arguments and then call the two helpers in sin_helpers.c // to verify the SIN given as a command line argument. return 0; } }
why does my main not run?
null