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
63c648ea5f004bf4898d00e9
2023-02-09T22:03:50.679Z
Help Fix Code
v1
null
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel **px = malloc(sizeof(struct pixel *) * height); for (int i = 0; i < height; i++) { px[i] = malloc(sizeof(struct pixel) * width); px[i] = fseek(image, pixel_array_offset, SEEK_SET); } return px; }
1. First, allocate space for m `struct pixel *` values, where m is the height of the image. Each pointer will eventually point to one row of pixel data. 2. For each pointer you just allocated, initialize it to point to heap-allocated space for an entire row of pixel data. 3. Use the given file and pixel_array_offset to initialize the actual struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is consistent with the bitmap file format. NOTE: We've tested this assumption on the Teaching Lab machines, but if you're trying to work on your own computer, we strongly recommend checking this assumption! 4. Return the address of the first `struct pixel *` you initialized.
null
63c648ea5f004bf4898d00e9
2023-02-09T22:16:35.120Z
Help Write Code
v1
null
null
null
Read in the location of the pixel array, the image width, and the image height in the given bitmap file.
63c648ea5f004bf4898d00e9
2023-02-09T22:17:30.381Z
Help Fix Code
v1
null
void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { fseek(image, 10, SEEK_SET); fread(pixel_array_offset, sizeof(int), 1, image); fseek(image, 18, SEEK_SET); fread(width, sizeof(int), 1, image); fseek(image, 22, SEEK_SET); fread(height, sizeof(int), 1, image); }
Read in the location of the pixel array, the image width, and the image height in the given bitmap file.
null
63c648ea5f004bf4898d00e9
2023-02-09T22:19:36.441Z
Help Fix Code
v1
null
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel **px = malloc(sizeof(struct pixel *) * height); for (int i = 0; i < height; i++) { fseek(image, pixel_array_offset, SEEK_SET); fread(px[i], sizeof(struct pixel), width, image); } return px; }
1. First, allocate space for m `struct pixel *` values, where m is the height of the image. Each pointer will eventually point to one row of pixel data. 2. For each pointer you just allocated, initialize it to point to heap-allocated space for an entire row of pixel data. 3. Use the given file and pixel_array_offset to initialize the actual struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is consistent with the bitmap file format. NOTE: We've tested this assumption on the Teaching Lab machines, but if you're trying to work on your own computer, we strongly recommend checking this assumption! 4. Return the address of the first `struct pixel *` you initialized.
null
63c648ea5f004bf4898d00e9
2023-02-09T22:21:09.741Z
Help Fix Code
v1
null
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel **px = malloc(sizeof(struct pixel *) * height); for (int i = 0; i < height; i++) { px[i] = malloc(sizeof(struct pixel) * width); fseek(image, pixel_array_offset, SEEK_SET); fread(px[i], sizeof(struct pixel), width, image); } return px; }
1. First, allocate space for m `struct pixel *` values, where m is the height of the image. Each pointer will eventually point to one row of pixel data. 2. For each pointer you just allocated, initialize it to point to heap-allocated space for an entire row of pixel data. 3. Use the given file and pixel_array_offset to initialize the actual struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is consistent with the bitmap file format. NOTE: We've tested this assumption on the Teaching Lab machines, but if you're trying to work on your own computer, we strongly recommend checking this assumption! 4. Return the address of the first `struct pixel *` you initialized.
null
63c648ea5f004bf4898d00e9
2023-02-09T22:23:21.262Z
Help Fix Code
v1
null
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel **px = malloc(sizeof(struct pixel *) * height); for (int i = 0; i < height; i++) { *px[i] = malloc(sizeof(struct pixel) * width); fseek(image, pixel_array_offset, SEEK_SET); fread(px[i], sizeof(struct pixel), width, image); } return px; }
1. First, allocate space for m `struct pixel *` values, where m is the height of the image. Each pointer will eventually point to one row of pixel data. 2. For each pointer you just allocated, initialize it to point to heap-allocated space for an entire row of pixel data. 3. Use the given file and pixel_array_offset to initialize the actual struct pixel values. Assume that `sizeof(struct pixel) == 3`, which is consistent with the bitmap file format. NOTE: We've tested this assumption on the Teaching Lab machines, but if you're trying to work on your own computer, we strongly recommend checking this assumption! 4. Return the address of the first `struct pixel *` you initialized.
null
63c648ea5f004bf4898d00e9
2023-02-15T15:27:35.356Z
Help Write Code
v1
null
null
null
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. Return: - 0 if successful - 1 if a user by this name already exists in this list - 2 if the given name cannot fit in the 'name' array (don't forget about the null terminator)
63c648ea5f004bf4898d00e9
2023-02-15T15:49:29.967Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) > MAX_NAME - 1) { return 2; } User curr = **user_ptr_add; while (curr.next != NULL) { if(strcmp(curr.name, name) == 0) { return 1; } curr = *curr.next; } User user; strcpy(user.name, name); curr.next = &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. Return: - 0 if successful - 1 if a user by this name already exists in this list - 2 if the given name cannot fit in the 'name' array (don't forget about the null terminator)
null
63c648ea5f004bf4898d00e9
2023-02-15T15:54:29.128Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { User curr = *head; while (curr.next != NULL) { if(strcmp(curr.name, name) == 0) { return &curr; } curr = *curr.next; } return NULL; }
Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists. NOTE: You'll likely need to cast a (const User *) to a (User *) to satisfy the prototype without warnings.
null
63c648ea5f004bf4898d00e9
2023-02-15T15:59:17.617Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { User *curr = (User *) head; while ((*curr).next != NULL) { if(strcmp((*curr).name, name) == 0) { return curr; } curr = (*curr).next; } return NULL; }
Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists. NOTE: You'll likely need to cast a (const User *) to a (User *) to satisfy the prototype without warnings.
null
63c648ea5f004bf4898d00e9
2023-02-15T16:59:14.520Z
Help Fix Code
v1
null
int make_friends(const char *name1, const char *name2, User *head) { if (find_user(name1, head) == NULL || find_user(name2, head) == NULL) { return 4; } else if (strcmp(name1, name2) == 0) { return 3; } User *user1 = find_user(name1, head); User *user1_friends[MAX_FRIENDS] = user1->friends; User *user2 = find_user(name2, head); User *user2_friends[MAX_FRIENDS] = user2->friends; for (int i = 0; i < MAX_FRIENDS; i++) { if (user1_friends[i] == user2) { return 1; } else if (user2_friends[i] == user1) { return 1; } } if (user1_friends[MAX_FRIENDS - 1] != NULL || user2_friends[MAX_FRIENDS - 1] != NULL) { return 2; } else { int i = 0; while (user1_friends[i] != NULL) { i++; } user1_friends[i] = user2; int j = 0; while (user2_friends[j] != NULL) { j++; } user2_friends[j] = user1; } return 0; }
Make two users friends with each other. This is symmetric - a pointer to each user must be stored in the 'friends' array of the other. New friends must be added in the first empty spot in the 'friends' array. Return: - 0 on success. - 1 if the two users are already friends. - 2 if the users are not already friends, but at least one already has MAX_FRIENDS friends. - 3 if the same user is passed in twice. - 4 if at least one user does not exist. Do not modify either user if the result is a failure. NOTE: If multiple errors apply, return the *largest* error code that applies.
null
63c648ea5f004bf4898d00e9
2023-02-15T20:43:23.760Z
General Question
v1
how to print out variable with type time_t
null
null
null
63c648ea5f004bf4898d00e9
2023-02-15T21:13:07.942Z
General Question
v1
how to use time function to find the current time
null
null
null
63c648ea5f004bf4898d00e9
2023-02-15T22:20:16.803Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) > MAX_NAME - 1) { return 2; } if (*user_ptr_add == NULL) { User user; strcpy(user.name, name); *user_ptr_add = &user; return 0; } User *curr = *user_ptr_add; while (curr->next != NULL) { if(strcmp(curr->name, name) == 0) { return 1; } curr = curr->next; } User user; strcpy(user.name, name); curr->next = &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. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator)
null
63c648ea5f004bf4898d00e9
2023-02-15T22:29:14.275Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) > MAX_NAME - 1) { return 2; } if (*user_ptr_add == NULL) { User *user = malloc(sizeof(User)); strcpy(user->name, name); *user_ptr_add = user; return 0; } User *curr = *user_ptr_add; while (curr->next != NULL) { if(strcmp(curr->name, name) == 0) { return 1; } curr = curr->next; } User *user = malloc(sizeof(User)); strcpy(user->name, name); curr->next = 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. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator)
null
63c648ea5f004bf4898d00e9
2023-02-16T00:59:21.213Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { User *curr = (User *) head; if (curr != NULL) { while (curr->next != NULL) { if(strcmp(curr->name, name) == 0) { return curr; } curr = curr->next; } if(strcmp(curr->name, name) == 0) { return curr; } } return NULL; }
Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings.
null
63c648ea5f004bf4898d00e9
2023-02-16T01:03:58.671Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { User *curr = (User *) head; while (curr != NULL) { if(strcmp(curr->name, name) == 0) { return curr; } curr = curr->next; } return NULL; }
Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings.
null
63c648ea5f004bf4898d00e9
2023-02-16T01:04:35.651Z
Help Fix Code
v1
null
void list_users(const User *curr) { printf("User List\n"); while (curr != NULL){ printf("%s\n", curr->name); curr = curr->next; } }
Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line.
null
63c648ea5f004bf4898d00e9
2023-02-16T01:05:13.745Z
Help Fix Code
v1
null
int update_pic(User *user, const char *filename) { if (strlen(filename) > MAX_NAME - 1) { return 2; } else if (fopen(filename, "r") == NULL) { return 1; } else { strcpy(user->profile_pic, filename); return 0; } }
Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long.
null
63c648ea5f004bf4898d00e9
2023-02-16T01:07:29.464Z
Help Fix Code
v1
null
int make_friends(const char *name1, const char *name2, User *head) { if (find_user(name1, head) == NULL || find_user(name2, head) == NULL) { return 4; } else if (strcmp(name1, name2) == 0) { return 3; } User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == user2) { return 1; } else if (user2->friends[i] == user1) { return 1; } } if (user1->friends[MAX_FRIENDS - 1] != NULL || user2->friends[MAX_FRIENDS - 1] != NULL) { return 2; } else { int i = 0; while (user1->friends[i] != NULL) { i++; } user1->friends[i] = user2; int j = 0; while (user2->friends[j] != NULL) { j++; } user2->friends[j] = user1; } return 0; }
Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies.
null
63c648ea5f004bf4898d00e9
2023-02-16T01:32:54.537Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } for (int i = 0; i < MAX_FRIENDS; i++) { if (author->friends[i] == target) { Post *post = NULL; strcpy(post->author, author->name); strcpy(post->contents, contents); *post->date = time(NULL); if (target->first_post != NULL) { Post *temp = target->first_post; post->next = temp; } target->first_post = post; } } return 1; }
Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL
null
63c648ea5f004bf4898d00e9
2023-02-16T01:34:03.119Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } for (int i = 0; i < MAX_FRIENDS; i++) { if (author->friends[i] == target) { Post *post = malloc(sizeof(Post)); strcpy(post->author, author->name); strcpy(post->contents, contents); *post->date = time(NULL); if (target->first_post != NULL) { Post *temp = target->first_post; post->next = temp; } target->first_post = post; } } return 1; }
Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL
null
63c648ea5f004bf4898d00e9
2023-02-16T01:34:45.868Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } for (int i = 0; i < MAX_FRIENDS; i++) { if (author->friends[i] == target) { Post *post = malloc(sizeof(Post)); strcpy(post->author, author->name); strcpy(post->contents, contents); post->date = time(NULL); if (target->first_post != NULL) { Post *temp = target->first_post; post->next = temp; } target->first_post = post; } } return 1; }
Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL
null
63c648ea5f004bf4898d00e9
2023-02-16T01:40:59.801Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } for (int i = 0; i < MAX_FRIENDS; i++) { if (author->friends[i] == target) { Post *post = malloc(sizeof(Post)); strcpy(post->author, author->name); strcpy(post->contents, contents); *post->date = time(NULL); post->next = target->first_post; target->first_post = post; return 0; } } return 1; }
Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL
null
63c648ea5f004bf4898d00e9
2023-02-16T01:41:33.165Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } for (int i = 0; i < MAX_FRIENDS; i++) { if (author->friends[i] == target) { Post *post = malloc(sizeof(Post)); strcpy(post->author, author->name); strcpy(post->contents, contents); post->date = time(NULL); post->next = target->first_post; target->first_post = post; return 0; } } return 1; }
Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL
null
63c648ea5f004bf4898d00e9
2023-02-16T01:42:35.209Z
Question from Code
v1
why does it have Segmentation fault: 11
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } for (int i = 0; i < MAX_FRIENDS; i++) { if (author->friends[i] == target) { Post *post = malloc(sizeof(Post)); strcpy(post->author, author->name); strcpy(post->contents, contents); post->date = time(NULL); post->next = target->first_post; target->first_post = post; return 0; } } return 1; }
null
null
63c648ea5f004bf4898d00e9
2023-02-16T01:55:16.968Z
General Question
v1
what is a Segmentation fault:11 ? how can i deal with it?
null
null
null
63c648ea5f004bf4898d00e9
2023-02-16T02:02:48.584Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } for (int i = 0; i < MAX_FRIENDS; i++) { if (author->friends[i] == target) { Post *post = malloc(sizeof(Post)); strcpy(post->author, author->name); strcpy(post->contents, contents); *post->date = time(NULL); post->next = target->first_post; target->first_post = post; return 0; } } return 1; }
Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL
null
63c648ea5f004bf4898d00e9
2023-02-16T02:26:07.499Z
Help Fix Code
v1
null
int delete_user(const char *name, User **user_ptr_del) { // check whether the user with this name exist or not User *user = find_user(name, *user_ptr_del); if (user == NULL) { return 1; } // unlink this user with others User *curr = *user_ptr_del; if (strcmp(curr->name, name) == 0) { curr->next = NULL; } else { while (curr->next != NULL) { if (strcmp(curr->next->name, name) == 0) { curr->next = curr->next->next; } curr = curr->next; } } // remove this user from any friend's lists for (int i = 0; i < MAX_FRIENDS; i++) { if (user->friends[i] != NULL) { for (int j = 0; j < MAX_FRIENDS; j++) { if (user->friends[i]->friends[j] == user) { user->friends[i]->friends[j] = NULL; } } } } // free all heap memory that was allocated Post *post = user->first_post; while (post != NULL) { free(post->contents); post = post->next; free(post); } free(user); return 0; }
* From the list pointed to by *user_ptr_del, delete the user * with the given name. * Remove the deleted user from any lists of friends. * * Return: * - 0 on success. * - 1 if a user with this name does not exist.
null
63c648ea5f004bf4898d00e9
2023-02-16T03:04:49.414Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { //check whether either User pointer is NULL return 2; } for (int i = 0; i < MAX_FRIENDS; i++) { if (author->friends[i] == target) { // check whether they are friends, then add posts to them Post *post = malloc(sizeof(Post)); strcpy(post->author, author->name); strcpy(post->contents, contents); *post->date = time(NULL); post->next = target->first_post; target->first_post = post; return 0; } } return 1; }
Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL
null
63c648ea5f004bf4898d00e9
2023-02-16T03:10:27.133Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { //check whether either User pointer is NULL return 2; } for (int i = 0; i < MAX_FRIENDS; i++) { if (author->friends[i] == target) { // check whether they are friends, then add posts to them Post *post = malloc(sizeof(Post)); strcpy(post->author, author->name); strcpy(post->contents, contents); post->date = malloc(sizeof(time_t)); *post->date = time(NULL); post->next = target->first_post; target->first_post = post; return 0; } } return 1; }
Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL
null
63c648ea5f004bf4898d00e9
2023-02-16T03:12:59.020Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { //check whether either User pointer is NULL return 2; } for (int i = 0; i < MAX_FRIENDS; i++) { if (author->friends[i] == target) { // check whether they are friends, then add posts to them Post *post = malloc(sizeof(Post)); strcpy(post->author, author->name); post->contents, contents; post->date = malloc(sizeof(time_t)); *post->date = time(NULL); post->next = target->first_post; target->first_post = post; return 0; } } return 1; }
Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL
null
63c648ea5f004bf4898d00e9
2023-03-02T22:18:32.431Z
General Question
v1
how to use wait()
null
null
null
63c648ea5f004bf4898d00e9
2023-03-02T22:28:31.891Z
Question from Code
v1
How many processes are created, including the original parent, when forkloop is called with 2, 3, and 4 as arguments? n arguments?
#include <stdio.h> #include <unistd.h> #include <stdlib.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) { int status; wait(&status); } printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); } return 0; }
null
null
63c648ea5f004bf4898d00e9
2023-03-02T22:53:08.847Z
Question from Code
v1
How many processes are created, including the original parent, when forkloop is called with 2, 3, and 4 as arguments? n arguments?
#include <stdio.h> #include <unistd.h> #include <stdlib.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); } return 0; }
null
null
63c648ea5f004bf4898d00e9
2023-03-02T23:07:55.451Z
Question from Code
v1
Create a copy of forkloop.c called parentcreates.c. In the new file, modify the program so that the new children do not create additional processes. Only the original parent calls fork. Keep the printf call for all processes. The resulting diagram will look something like the following when parentcreates 3 is run. In this case, the parent process creates 3 child processes. Note that the child process ids will not necessarily be in sequence.
#include <stdio.h> #include <unistd.h> #include <stdlib.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); } return 0; }
null
null
63c648ea5f004bf4898d00e9
2023-03-08T18:03:38.669Z
General Question
v1
how to use exec and execl
null
null
null
63c648ea5f004bf4898d00e9
2023-03-08T18:06:18.182Z
General Question
v1
how to use dup2
null
null
null
63c648ea5f004bf4898d00e9
2023-03-08T18:10:28.610Z
General Question
v1
what the difference between dup, pipe and fork
null
null
null
63c648ea5f004bf4898d00e9
2023-03-08T18:11:11.913Z
General Question
v1
what the difference between dup2, pipe, fork and execl
null
null
null
63c648ea5f004bf4898d00e9
2023-03-08T18:13:34.124Z
Explain Code
v1
null
if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); }
null
null
63c648ea5f004bf4898d00e9
2023-03-08T18:29:46.936Z
General Question
v1
how to prints a message to stdout
null
null
null
63c648ea5f004bf4898d00e9
2023-03-08T18:38:58.428Z
General Question
v1
how to get exit code when calling execl
null
null
null
63c648ea5f004bf4898d00e9
2023-03-08T19:02:41.954Z
Help Fix Code
v1
null
#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 */ if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if(fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } // TODO int n = fork(); if (n < 0) { perror("fork"); exit(1); } else if (n == 0) { // child process execl("./validate", "validate", user_id, password, NULL); perror("execl"); exit(1); } else { // parent process int status; pid_t child_pid = wait(&status); if (child_pid == -1) { perror("wait"); exit(1); } if (WIFEXITED(status)) { int child_status = WEXITSTATUS(status); if (child_status == 0) { printf(SUCCESS); } else if (child_status == 2) { printf(INVALID); } else if (child_status == 3) { printf(NO_USER); } } } return 0; }
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. Your program should use the exit status of the validate program to determine which of the three following messages to print: "Password verified" if the user id and password match. "Invalid password" if the user id exists, but the password does not match. "No such user" if the user id is not recognized The exact messages are given in the starter code as defined constants. The only case that should be handled directly in checkpasswd is the case where either the userid or password are too long. This is to prevent sending the wrong number of bytes to validate. Note that in the given password file pass.txt, the "killerwhales:swim" has a user id that is too large, and "monkeys:eatcoconuts" has a password that is too long. The examples are expected to fail, but the other cases should work correctly.
null
63c648ea5f004bf4898d00e9
2023-03-08T19:04:45.152Z
Help Write Code
v1
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. Your program should use the exit status of the validate program to determine which of the three following messages to print: "Password verified" if the user id and password match. "Invalid password" if the user id exists, but the password does not match. "No such user" if the user id is not recognized The exact messages are given in the starter code as defined constants. The only case that should be handled directly in checkpasswd is the case where either the userid or password are too long. This is to prevent sending the wrong number of bytes to validate. Note that in the given password file pass.txt, the "killerwhales:swim" has a user id that is too large, and "monkeys:eatcoconuts" has a password that is too long. The examples are expected to fail, but the other cases should work correctly.
63c648ea5f004bf4898d00e9
2023-03-08T19:33:48.027Z
Help Fix Code
v1
null
#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]; /* 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); } // TODO // check length of user id and password if (strlen(user_id) > MAX_PASSWORD || strlen(password) > MAX_PASSWORD) { printf("The user id or password are too long"); exit(1); } //create pipe int pipefd[2]; pipe(pipefd); int read_end = pipefd[0]; int write_end = pipefd[1]; int n = fork(); //fork if (n < 0) { perror("fork"); exit(1); } else if (n == 0) { // child process close(read_end); dup2(1, write_end); execl("./validate", "validate", user_id, password, NULL); write(write_end, stdout, sizeof(int)); close(write_end); perror("execl"); exit(1); } else { // parent process close(write_end); int status; pid_t child_pid = wait(&status); if (child_pid == -1) { perror("wait"); exit(1); } int child_status; if (WIFEXITED(status)) { close(write_end); read(read_end, &child_status, sizeof(int)); close(read_end); // int child_status = WEXITSTATUS(status); if (child_status == 0) { printf(SUCCESS); } else if (child_status == 2) { printf(INVALID); } else if (child_status == 3) { printf(NO_USER); } } } return 0; }
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. Your program should use the exit status of the validate program to determine which of the three following messages to print: "Password verified" if the user id and password match. "Invalid password" if the user id exists, but the password does not match. "No such user" if the user id is not recognized The exact messages are given in the starter code as defined constants. The only case that should be handled directly in checkpasswd is the case where either the userid or password are too long. This is to prevent sending the wrong number of bytes to validate. Note that in the given password file pass.txt, the "killerwhales:swim" has a user id that is too large, and "monkeys:eatcoconuts" has a password that is too long. The examples are expected to fail, but the other cases should work correctly.
null
63c648ea5f004bf4898d00e9
2023-03-08T19:38:55.015Z
Help Fix Code
v1
null
#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]; /* 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); } // TODO // check length of user id and password if (strlen(user_id) > MAX_PASSWORD || strlen(password) > MAX_PASSWORD) { printf("The user id or password are too long"); exit(1); } //create pipe int pipefd[2]; pipe(pipefd); int read_end = pipefd[0]; int write_end = pipefd[1]; int n = fork(); //fork if (n < 0) { perror("fork"); exit(1); } else if (n == 0) { // child process close(read_end); execl("./validate", "validate", user_id, password, NULL); dup2(1, write_end); close(write_end); perror("execl"); exit(1); } else { // parent process close(write_end); int status; pid_t child_pid = wait(&status); if (child_pid == -1) { perror("wait"); exit(1); } int child_status; if (WIFEXITED(status)) { close(write_end); // read(read_end, &child_status, sizeof(int)); dup2(1, read_end); close(read_end); // int child_status = WEXITSTATUS(status); if (child_status == 0) { printf(SUCCESS); } else if (child_status == 2) { printf(INVALID); } else if (child_status == 3) { printf(NO_USER); } } } return 0; }
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. Your program should use the exit status of the validate program to determine which of the three following messages to print: "Password verified" if the user id and password match. "Invalid password" if the user id exists, but the password does not match. "No such user" if the user id is not recognized The exact messages are given in the starter code as defined constants. The only case that should be handled directly in checkpasswd is the case where either the userid or password are too long. This is to prevent sending the wrong number of bytes to validate. Note that in the given password file pass.txt, the "killerwhales:swim" has a user id that is too large, and "monkeys:eatcoconuts" has a password that is too long. The examples are expected to fail, but the other cases should work correctly.
null
63c648ea5f004bf4898d00e9
2023-03-08T19:50:46.062Z
Help Fix Code
v1
null
#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]; /* 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); } // TODO // check length of user id and password if (strlen(user_id) > MAX_PASSWORD || strlen(password) > MAX_PASSWORD) { printf("The user id or password are too long"); exit(1); } //create pipe int pipefd[2]; if (pipe(pipefd) == -1) { perror("pipe"); exit(1); } int read_end = pipefd[0]; int write_end = pipefd[1]; int n = fork(); //fork if (n < 0) { perror("fork"); exit(1); } else if (n == 0) { // child process close(read_end); dup2(write_end, STDOUT_FILENO); close(write_end); execl("./validate", "validate", user_id, password, NULL); perror("execl"); exit(1); } else { // parent process close(write_end); int status; pid_t child_pid = wait(&status); if (child_pid == -1) { perror("wait"); exit(1); } if (WIFEXITED(status)) { int child_status = WEXITSTATUS(status); if (child_status == 0) { printf(SUCCESS); } else if (child_status == 2) { printf(INVALID); } else if (child_status == 3) { printf(NO_USER); } } close(read_end); } return 0; }
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. Your program should use the exit status of the validate program to determine which of the three following messages to print: "Password verified" if the user id and password match. "Invalid password" if the user id exists, but the password does not match. "No such user" if the user id is not recognized The exact messages are given in the starter code as defined constants. The only case that should be handled directly in checkpasswd is the case where either the userid or password are too long. This is to prevent sending the wrong number of bytes to validate. Note that in the given password file pass.txt, the "killerwhales:swim" has a user id that is too large, and "monkeys:eatcoconuts" has a password that is too long. The examples are expected to fail, but the other cases should work correctly.
null
63c648ea5f004bf4898d00e9
2023-03-08T19:53:56.259Z
Explain Code
v1
null
#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]; /* 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); } // TODO // check length of user id and password if (strlen(user_id) > MAX_PASSWORD || strlen(password) > MAX_PASSWORD) { printf("The user id or password are too long"); exit(1); } //create pipe int pipefd[2]; if (pipe(pipefd) == -1) { perror("pipe"); exit(1); } int read_end = pipefd[0]; int write_end = pipefd[1]; int n = fork(); //fork if (n < 0) { perror("fork"); exit(1); } else if (n == 0) { // child process close(read_end); dup2(write_end, STDOUT_FILENO); close(write_end); execl("./validate", "validate", user_id, password, NULL); perror("execl"); exit(1); } else { // parent process close(write_end); int status; pid_t child_pid = wait(&status); if (child_pid == -1) { perror("wait"); exit(1); } if (WIFEXITED(status)) { int child_status = WEXITSTATUS(status); if (child_status == 0) { printf(SUCCESS); } else if (child_status == 2) { printf(INVALID); } else if (child_status == 3) { printf(NO_USER); } } close(read_end); } return 0; }
null
null
63c648ea5f004bf4898d00e9
2023-03-08T20:38:49.769Z
Help Fix Code
v1
null
#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]; /* 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); } // TODO // check length of user id and password if (strlen(user_id) > MAX_PASSWORD || strlen(password) > MAX_PASSWORD) { printf("The user id or password are too long.\n"); exit(1); } //create pipe int pipefd[2]; if (pipe(pipefd) == -1) { perror("pipe"); exit(1); } int read_end = pipefd[0]; int write_end = pipefd[1]; int n = fork(); //fork if (n < 0) { perror("fork"); exit(1); } else if (n == 0) { // child process close(write_end); dup2(read_end, STDIN_FILENO); close(read_end); execl("./validate", "validate", NULL); perror("execl"); exit(1); } else { // parent process close(read_end); write(write_end, stdin, sizeof(stdin)); close(write_end); int status; pid_t child_pid = wait(&status); if (child_pid == -1) { perror("wait"); exit(1); } if (WIFEXITED(status)) { int child_status = WEXITSTATUS(status); if (child_status == 0) { printf(SUCCESS); } else if (child_status == 2) { printf(INVALID); } else if (child_status == 3) { printf(NO_USER); } } } return 0; }
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. Your program should use the exit status of the validate program to determine which of the three following messages to print: "Password verified" if the user id and password match. "Invalid password" if the user id exists, but the password does not match. "No such user" if the user id is not recognized The exact messages are given in the starter code as defined constants. The only case that should be handled directly in checkpasswd is the case where either the userid or password are too long. This is to prevent sending the wrong number of bytes to validate. Note that in the given password file pass.txt, the "killerwhales:swim" has a user id that is too large, and "monkeys:eatcoconuts" has a password that is too long. The examples are expected to fail, but the other cases should work correctly.
null
63c648ea5f004bf4898d00e9
2023-03-08T20:51:04.604Z
Help Fix Code
v1
null
#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]; /* 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); } // TODO // check length of user id and password if (strlen(user_id) > MAX_PASSWORD || strlen(password) > MAX_PASSWORD) { printf("The user id or password are too long.\n"); exit(1); } //create pipe int pipefd[2]; if (pipe(pipefd) == -1) { perror("pipe"); exit(1); } int read_end = pipefd[0]; int write_end = pipefd[1]; int n = fork(); //fork if (n < 0) { perror("fork"); exit(1); } else if (n == 0) { // child process close(write_end); dup2(read_end, STDIN_FILENO); close(read_end); execl("./validate", "validate", NULL); perror("execl"); exit(1); } else { // parent process close(read_end); write(write_end, user_id, strlen(user_id)); write(write_end, password, strlen(password)); close(write_end); int status; pid_t child_pid = wait(&status); if (child_pid == -1) { perror("wait"); exit(1); } if (WIFEXITED(status)) { int child_status = WEXITSTATUS(status); if (child_status == 0) { printf(SUCCESS); } else if (child_status == 2) { printf(INVALID); } else if (child_status == 3) { printf(NO_USER); } } } return 0; }
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. Your program should use the exit status of the validate program to determine which of the three following messages to print: "Password verified" if the user id and password match. "Invalid password" if the user id exists, but the password does not match. "No such user" if the user id is not recognized The exact messages are given in the starter code as defined constants. The only case that should be handled directly in checkpasswd is the case where either the userid or password are too long. This is to prevent sending the wrong number of bytes to validate. Note that in the given password file pass.txt, the "killerwhales:swim" has a user id that is too large, and "monkeys:eatcoconuts" has a password that is too long. The examples are expected to fail, but the other cases should work correctly.
null
63c648ea5f004bf4898d00e9
2023-03-14T00:33:40.593Z
Help Write Code
v2
null
null
null
/* 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. */ Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. read(fp); return NULL; }
63c648ea5f004bf4898d00e9
2023-03-14T00:34:48.008Z
Help Write Code
v2
null
null
null
/* 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. */ Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. return NULL; }
63c648ea5f004bf4898d00e9
2023-03-14T16:51:40.675Z
Explain Code
v2
null
int is_comment_or_empty(const char *line) { for (int i = 0; i < strlen(line); i++) { if (line[i] == '#') { return 1; } if (line[i] != '\t' && line[i] != ' ') { return 0; } } return 1; }
null
null
63c648ea5f004bf4898d00e9
2023-03-14T18:21:50.954Z
Help Fix Code
v2
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.
Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. Rule *head_rule, *curr_rule; char line[MAXLINE]; while (fgets(line, MAXLINE, fp) != NULL) { // Check if a line of text is a comment or an empty line. if (is_comment_or_empty(line)) { continue; } // Parse the line. char *target = strtok(line, " \t\n"); Dependency *dependencies = NULL; Dependency *curr_dep = NULL; Action *actions = NULL; Action *curr_act = NULL; char *token = strtok(NULL, " \t\n"); // while ((token = strtok(NULL, " \t\n")) != NULL) { if (strcmp(token, ":") == 0) { // Parse the dependency while ((token = strtok(NULL, " \t\n")) != NULL) { Dependency *dep = malloc(sizeof(Dependency)); if (dependencies == NULL) { dependencies = dep; curr_dep = dep; } else { curr_dep->next_dep = dep; curr_dep = dep; } } } else { // Parse the action Action *act = malloc(sizeof(Action)); char **args = malloc(sizeof(char **)); strcpy(args[0], target); int i = 1; while (token != NULL) { strcpy(args[i],token); token = strtok(NULL, " \t\n"); i++; } args[i] = NULL; act->args = args; if (actions == NULL) { actions = act; curr_act = act; } else { curr_act->next_act = act; curr_act = act; } } // Create a new Rule node. Rule *rule = malloc(sizeof(Rule)); strcpy(rule->target, target); rule->dependencies = dependencies; rule->actions = actions; if (head_rule == NULL) { head_rule = rule; curr_rule = rule; } else { curr_rule->next_rule = rule; curr_rule = rule; } } return head_rule; }
null
null
63c648ea5f004bf4898d00e9
2023-03-15T18:24:31.372Z
General Question
v2
Use stat to get the last modified time (mtime) of a file. give example on how to call stat
null
null
null
63c648ea5f004bf4898d00e9
2023-03-15T18:27:29.850Z
General Question
v2
the field of the stat struct that you want is called st_mtim on Linux. It has different names on Mac OSX. what is it?
null
null
null
63c648ea5f004bf4898d00e9
2023-03-15T18:29:34.491Z
General Question
v2
what is execvp? How does it work?
null
null
null
63c648ea5f004bf4898d00e9
2023-03-15T18:45:23.393Z
Help Write Code
v2
null
null
null
implement function run_make: run_make takes a target as its first argument. It finds the rule in the rules data structure corresponding to the target, and evaluates it. If target is NULL, then run_make will evaluate the first target in the rules list. Update each of the dependencies. In other words, recursively evaluate each dependency rule.
63c648ea5f004bf4898d00e9
2023-03-15T21:28:32.600Z
Help Fix Code
v2
for a space-seperated string, divide it by spaced and save each to a char array which is in an array.
Action *curr_act = curr_rule->actions; while (fgets(line, MAXLINE, fp) && line[0] == '\t') { Action *act = malloc(sizeof(Action)); char **args = NULL; int count = 0; while ((token = strtok(line, " \t\n")) != NULL) { strcpy(args[count],token); count++; } act->args = malloc((count + 1) * sizeof(char *)); for (int i = 0; i < count; i++) { args[i] = malloc(strlen(token) + 1); strcpy(act->args[i], args[i]); } act->args[count] = NULL; // Add a NULL point to the end of the array. // Link actions to rule if (curr_rule->actions == NULL) { curr_rule->actions = act; curr_act = act; } else { curr_act->next_act = act; curr_act = act; } }
null
null
63c648ea5f004bf4898d00e9
2023-03-15T21:33:31.382Z
Help Fix Code
v2
for a space-seperated string, divide it by spaced and save each to a char array which is in an array.
Action *curr_act = curr_rule->actions; while (fgets(line, MAXLINE, fp) && line[0] == '\t') { Action *act = malloc(sizeof(Action)); char **args = NULL; int count = 0; while ((token = strtok(line, " \t\n")) != NULL) { args[count] = malloc(sizeof(char) * (strlen(token) + 1)); strcpy(args[count],token); args[count][strlen(token)] = '\0'; count++; } act->args = malloc((count + 1) * sizeof(char *)); for (int i = 0; i < count; i++) { strcpy(act->args[i], args[i]); } act->args[count] = NULL; // Add a NULL point to the end of the array. // Link actions to rule if (curr_rule->actions == NULL) { curr_rule->actions = act; curr_act = act; } else { curr_act->next_act = act; curr_act = act; } }
null
null
63c648ea5f004bf4898d00e9
2023-03-15T23:12:11.276Z
Help Fix Code
v2
Check if the rule already exits.
Dependency *curr_dep = curr_rule->dependencies; token = strtok(NULL, " :\t\n"); while (token != NULL) { Dependency *dep = malloc(sizeof(Dependency)); // Check if the rule already exits. Rule *dep_rule = head_rule; while (dep_rule != NULL) { if (strcmp(dep_rule->target, token) == 0) { // If exits, link that rule to the dependency. dep->rule = dep_rule; break; } dep_rule = dep_rule->next_rule; }
null
null
63c648ea5f004bf4898d00e9
2023-03-15T23:13:11.573Z
Question from Code
v2
what error on the step "strcmp(dep_rule->target, token)"?
Dependency *curr_dep = curr_rule->dependencies; token = strtok(NULL, " :\t\n"); while (token != NULL) { Dependency *dep = malloc(sizeof(Dependency)); Rule *dep_rule = head_rule; while (dep_rule != NULL) { if (strcmp(dep_rule->target, token) == 0) { dep->rule = dep_rule; break; } dep_rule = dep_rule->next_rule; }
null
null
63c648ea5f004bf4898d00e9
2023-03-15T23:14:34.528Z
Question from Code
v2
Do I need to free "rule"?
Rule *curr_rule = head_rule; if (head_rule == NULL) { Rule *rule = malloc(sizeof(Rule)); head_rule = rule; curr_rule = rule; } else { while (curr_rule->next_rule != NULL) { if (strcmp(curr_rule->target, token) == 0) { break; } curr_rule = curr_rule->next_rule; } if (strcmp(curr_rule->target, token) != 0) { Rule *rule = malloc(sizeof(Rule)); curr_rule->next_rule = rule; curr_rule = curr_rule->next_rule; } }
null
null
63c648ea5f004bf4898d00e9
2023-04-05T19:07:44.713Z
Explain Code
v2
null
struct sockaddr_in *self = init_server_addr(PORT); int listenfd = set_up_server_socket(self, 5); while (1) { int fd = accept_connection(listenfd); if (fd < 0) { continue; } char buf[BUFSIZE]; int nbytes; while ((nbytes = read(fd, buf, sizeof(buf) - 1)) > 0) { buf[nbytes] = '\0'; printf("Next message: %s\n", buf); } close(fd); } free(self); close(listenfd); return 0;
null
null
63c648ea5f004bf4898d00e9
2023-04-05T19:09:46.274Z
General Question
v2
how to get the length of a string
null
null
null
63c648ea5f004bf4898d00e9
2023-04-05T19:29:38.579Z
Explain Code
v2
null
struct sockaddr_in *self = init_server_addr(PORT); int listenfd = set_up_server_socket(self, 5); while (1) { int fd = accept_connection(listenfd); if (fd < 0) { continue; } char buf[BUFSIZE]; int nbytes; while ((nbytes = read(fd, buf, sizeof(buf) - 1)) > 0) { buf[nbytes] = '\0'; printf("Next message: %s\n", buf); } close(fd); } free(self); close(listenfd); return 0;
null
null
63c648ea5f004bf4898d00e9
2023-04-05T19:30:07.746Z
General Question
v2
what does memmove do?
null
null
null
63c648ea5f004bf4898d00e9
2023-04-06T00:43:48.057Z
Help Write Code
v2
null
null
null
create two new files: a friend_server.c file (with a stubbed main function) and a Makefile that compiles a program called friend_server. It must use the gcc flags -g, -std=gnu99, -Wall, and -Werror. In addition to building your code, your Makefile must permit choosing a port at compile-time. To do this, first add a #define to your program to define the port number on which the server will expect connections (this is the port based on your student number):
63c648ea5f004bf4898d00e9
2023-04-06T01:28:56.865Z
Explain Code
v2
null
char* users_str = NULL; int len = 0; struct user_node* curr = head; while (curr != NULL) { len += snprintf(NULL, 0, "%s\n", curr->user); curr = curr->next; }
null
null
63c648ea5f004bf4898d00e9
2023-04-06T05:08:50.135Z
Explain Code
v2
null
int on = 1; int status = setsockopt([sock_fd], SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)); if (status == -1) { perror("setsockopt -- REUSEADDR"); }
null
null
63c648ea5f004bf4898d00e9
2023-04-25T20:50:22.427Z
Question from Code
v2
why do we use while(1)?
int fd[2]; int write_val = 1; void send_one(int signal) { write(fd[1], &write_val, sizeof(int)); } int main() { pipe(fd); int pid = fork(); if (pid == 0) { close(fd[0]); struct sigaction sa; sa.sa_handler = send_one; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); sigaction(SIGUSR1, &sa, NULL); while (1) ; } else if (pid > 0) { close(fd[1]); int count = 0; while (1) { int num = 0; int num_read = read(fd[0], &num, sizeof(int)); if (num_read == sizeof(int) && num == 1) { count++; printf("%d\n", count); } } } return 0; }
null
null
63c648ea5f004bf4898d00e9
2023-04-25T21:29:36.400Z
General Question
v2
Write a shell script that adds every subdirectory of your current working directory to your PATH variable. Note that the PATH variable requires an absolute path to each subdirectory. Remember that the environment variable PWD stores the absolute path to the current working directory. (Do not recurse into subdirectories.)
null
null
null
63c648ea5f004bf4898d00e9
2023-04-25T22:00:08.257Z
General Question
v2
how to use execl? Give me examples
null
null
null
63c648ee5f004bf4898d0111
2023-01-17T18:53:22.604Z
General Question
v1
What is a pointer
null
null
null
63c648f15f004bf4898d0125
2023-01-30T21:40:02.512Z
Help Fix Code
v1
null
char permission[9]; int size; int num_larger = 0; scanf("%*s%*d"); while ((scanf("%s%*d%*s%*s%d%*s%*d%*s%*s", permission, &size) != EOF)){ if ((int)strtol(argv[1], NULL, 10) < size){ printf("%d%d", (int)strtol(argv[1], NULL, 10), size); if (argc == 3){ printf(" op1 -%d ", num_larger); num_larger += check_permissions(argv[2], permission); } else{ printf(" op2 -%d ", num_larger); num_larger += 1; } } }
Increase num_larger when the standard input argv[1] is greater than size
null
63c648f15f004bf4898d0125
2023-01-30T21:42:16.783Z
Help Fix Code
v1
null
int num = (int)strtol(*argv[1], NULL, 10);
Convert argv[1] to an int.
null
63c648f15f004bf4898d0125
2023-02-10T20:39:49.246Z
Help Fix Code
v1
null
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel **pixel_data = malloc(sizeof(struct pixel) * height); fseek(image, 10, pixel_array_offset); for (int i = 0; i < height; i ++){ pixel_data[i] = malloc(sizeof(struct pixel) * width); } for (int i = 0; i < height; i ++){ for (int j = 0; j < width; j ++){ struct pixel temp; fread(&temp.blue, 1, 1, image); fread(&temp.green, 1, 1, image); fread(&temp.red, 1, 1, image); pixel_data[i][j] = temp; } } return pixel_data; }
Read and store bitmap pixel array.
null
63c648f15f004bf4898d0125
2023-02-10T21:01:07.328Z
Help Fix Code
v1
null
FILE *image = fopen(argv[1], "rb"); int pixel_array_offset, width, height; read_bitmap_metadata(image, &pixel_array_offset, &width, &height); // Print out metadata. printf("Pixel array offset: %d\n", pixel_array_offset); printf("Width: %d\n", width); printf("Height: %d\n", height); // Read in the pixel data struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height); fclose(image); for (int i = 0; i < height; i ++){ free(pixels[i]); }
free image and pixels
null
63c648f15f004bf4898d0125
2023-02-10T21:09:48.717Z
Help Fix Code
v1
null
void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { fread(pixel_array_offset, 10, 4, image); fseek(image, 18, *pixel_array_offset); fread(width, 4, 1, image); fseek(image, 22, *pixel_array_offset); fread(height, 4, 1, image); }
Update pointers to pixel_array_offsey, width, and height for a bitmap
null
63c648f15f004bf4898d0125
2023-02-15T14:58:21.565Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { // Check if name is too long if (strlen(name) >= MAX_NAME){ } User *new_user = (User*) malloc(sizeof(User)); strncpy(new_user->name, name, MAX_NAME); for (int i = 0; i < MAX_FRIENDS; i ++){ new_user->friends[i] = NULL; } new_user->first_post = NULL; new_user->next = NULL; // If user_ptr_add is empty, add the new user. if (*user_ptr_add == NULL){ // *user_ptr_add = (User*) malloc(sizeof(User)); *user_ptr_add = new_user; return 0; } // If not empty, traverse the list. User *curr = *user_ptr_add; while (curr->next != NULL){ if (curr->name == name){ return 1; } curr = curr->next; } curr->next = (User*) malloc(sizeof(User*)); (curr->next) = new_user; return 0; }
Insert a User into the end of the linked list *user_ptr_add
null
63c648f15f004bf4898d0125
2023-02-15T15:01:22.567Z
Help Fix Code
v1
null
User *new_user = (User*) malloc(sizeof(User)); // If user_ptr_add is empty, add the new user. if (*user_ptr_add == NULL){ *user_ptr_add = new_user; return 0; }
*user_ptr_add is an empty linked list. Make it point to new_user;
null
63c648f15f004bf4898d0125
2023-02-15T22:11:34.197Z
Help Fix Code
v1
null
if (curr1 == NULL){ curr1 = user2; printf("%s", ((*(curr1->friends)[0]).name)); } else{ while (curr1 != NULL){ if (curr1->next == NULL){ break; } curr1 = curr1->next; } curr1->next = user2; }
trying to insert user2 at tail of linked list pointed to by curr1
null
63c648f15f004bf4898d0125
2023-02-15T22:51:56.195Z
Help Fix Code
v1
null
/* Creates and returns pointer to new User with given name and next value. */ User *create_new_user(const char* name, User *next) { User *new_user = calloc(1, sizeof(User)); strncpy(new_user->name, name, MAX_NAME); *(new_user->friends) = calloc(MAX_FRIENDS, sizeof(User)); for (int i = 0; i < MAX_FRIENDS; i ++){ (new_user->friends)[i] = calloc(1, sizeof(User)); (new_user->friends)[i] = NULL; } new_user->first_post = NULL; (new_user->profile_pic)[0] = '\0'; new_user->next = next; return new_user; }
Creates a new dynamically allocated User with dynamically allocated friends attribute
null
63c648f15f004bf4898d0125
2023-02-16T00:30:27.403Z
Help Fix Code
v1
null
int make_friends(const char *name1, const char *name2, User *head) { User *user1; User *user2; // Check if name1 is in list user1 = find_user(name1, head); if (!user1){ return 4; } // Check if name2 is in list user2 = find_user(name2, head); if (!user2){ return 4; } //Check if the same user is passed in twice. if (name1 == name2){ return 3; } // Then, name1 and name2 are in the list, check if users have max friend size. int len1 = list_length(user1->friends); int len2 = list_length(user2->friends); if (len1 >= MAX_FRIENDS || len2 >= MAX_FRIENDS){ return 2; } // Then, check if name1 and name2 are already friends. for (int i = 0; i < MAX_FRIENDS; i++){ if ((user1->friends)[i] != NULL && strcmp((user1->friends)[i]->name, name2)){ return 1; } } // Then, all conditions are met. Add the new friends to both lists. for (int i = 0; i < MAX_FRIENDS; i++){ if (user1->friends[i] == NULL){ user1->friends[i] = user2; break; } } for (int i = 0; i < MAX_FRIENDS; i++){ if (user2->friends[i] == NULL){ user2->friends[i] = user1; break; } } return 0; }
Add user1 to user2's friends array and user 2 to user 1's friends array.
null
63c648f15f004bf4898d0125
2023-02-16T01:14:59.936Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { User *curr = (User*)head; while (curr!= NULL){ if (strcmp(curr->name, name) == 0){ return (User *) curr; } curr = curr->next; } return NULL; }
Find and return the user with name in the linked list starting at head
null
63c648f15f004bf4898d0125
2023-03-15T03:09:24.589Z
Help Fix Code
v2
replace all '\n' in curr_line with '\0'
char curr_line[MAXLINE]; for (int i = 0; i < MAXLINE; i ++){ if (curr_line[i] == '\n'){ curr_line[i] = '\0'; } }
null
null
63c648f15f004bf4898d0125
2023-03-15T13:49:24.809Z
Help Fix Code
v2
Create a new rule and add it to the linked list pointed to by curr_rule
Rule *new_rule; new_rule = create_rule_dependencies(curr_line, first_rule, curr_rule); //printf("%s", new_rule->target); // Check if this is the first rule being parsed if (first_rule == NULL){ first_rule = new_rule; } else{ curr_rule->next_rule = new_rule; //printf("%s", curr_rule->target); } curr_rule = new_rule; printf("%s", curr_rule->target);
null
null
63c648f15f004bf4898d0125
2023-03-15T13:50:06.556Z
Help Fix Code
v2
Create a new rule and add it to the linked list pointed to by curr_rule
Rule *new_rule; new_rule = create_rule_dependencies(curr_line, first_rule, curr_rule); //printf("%s", new_rule->target); // Check if this is the first rule being parsed if (first_rule == NULL){ first_rule = new_rule; } else{ curr_rule->next_rule = new_rule; //printf("%s", curr_rule->target); } curr_rule = new_rule;
null
null
63c648f15f004bf4898d012a
2023-02-14T21:48:14.664Z
Question from Code
v1
segmentation fault, how do I make this run when user_ptpr_add is NULL
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME){ // if name is too long return 2; } User *current = *user_ptr_add; while (current != NULL) { if (strcmp(current->name, name) == 0) { // If a user by this name already exists in this list return 1; } current = current->next; // current should traverse to the end of the linkedlist } // Create user User *new_user = (User*) malloc(sizeof(User)); if (new_user == NULL) { return -1; // failed to allocate memory } strcpy(new_user->name, name); new_user->profile_pic[0] = '\0'; new_user->first_post = NULL; memset(new_user->friends, 0, sizeof(new_user->friends)); new_user->next = NULL; // Add to user_ptr_add == current if (user_ptr_add == NULL){ *user_ptr_add = new_user; } else { User *tail = *user_ptr_add; while (tail->next != NULL) { tail = tail->next; } tail->next = new_user; } return 0; }
null
null
63c648f15f004bf4898d012a
2023-02-14T21:54:08.458Z
Question from Code
v1
how to avoid a memory leak from this code
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME){ // if name is too long return 2; } User *current = *user_ptr_add; while (current != NULL) { if (strcmp(current->name, name) == 0) { // If a user by this name already exists in this list return 1; } current = current->next; // current should traverse to the end of the linkedlist } // Create user User *new_user = (User*) malloc(sizeof(User)); if (new_user == NULL) { return -1; // failed to allocate memory } strcpy(new_user->name, name); new_user->profile_pic[0] = '\0'; new_user->first_post = NULL; memset(new_user->friends, 0, sizeof(new_user->friends)); new_user->next = NULL; // Add to user_ptr_add == current if (user_ptr_add == NULL){ *user_ptr_add = new_user; } else { User *tail = *user_ptr_add; while (tail->next != NULL) { tail = tail->next; } tail->next = new_user; } return 0; }
null
null
63c648f15f004bf4898d012a
2023-02-15T00:42:19.412Z
Question from Code
v1
I get this error "error: passing 'const User *' (aka 'const struct user *') to parameter of type 'User *' (aka 'struct user *') discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers] if (already_friends(author, target) == 1){"
int make_post(const User *author, User *target, char *contents) { if ((author == NULL) || (target == NULL)){ // if either User pointer is NULL return 2; } if (already_friends(author, target) == 1){ // if users exist but are not friends return 1; } // Create Post Post *new_post = (Post*) malloc(sizeof(Post)); if (strcpy(new_post->author, author->name) == NULL) { free(new_post); } if (strcpy(new_post->contents, contents) == NULL) { free(new_post); } time_t curtime; time(&curtime); new_post->date = &curtime; // Insert the new post at the *front* of the user's list of posts. if (target->first_post == NULL) { // If target does not have any post target->first_post = new_post; return 0; } Post *tail = target->first_post; while (tail->next != NULL) { tail = tail->next; } tail->next = new_post; return 0; }
null
null
63c648f15f004bf4898d012a
2023-02-15T01:18:13.347Z
Question from Code
v1
error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if (user->profile_pic[0] != "\0") {
int print_user(const User *user) { if (!user) { // if the user is NULL return -1; } if (user->profile_pic[0] != "\0") { // if user has profile picture, print picture } printf("Name: %s\n", user->name); printf("------------------------------------------"); printf("Friends:\n"); int i = 0; while (i < MAX_FRIENDS && user->friends[i]) { printf("%s\n", user->friends[i]->name); i++; } printf("------------------------------------------"); printf("Posts:\n"); Post *head = user->first_post; while (head->next != NULL) { printf("From: %s\n", user->first_post->author); printf("Date: %s\n\n", ctime(user->first_post->date)); printf("%s\n\n", user->first_post->contents); printf("===\n"); } printf("------------------------------------------"); return 0; }
null
null
63c648f15f004bf4898d012a
2023-02-15T01:47:14.287Z
Question from Code
v1
expression must be a modifiable lvalue for new_user->friends = { NULL };
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME){ // if name is too long return 2; } // Create User User *new_user = (User*) malloc(sizeof(User)); if (new_user == NULL) { free(new_user); return -1; // failed to allocate memory } if (strcpy(new_user->name, name) == NULL) { free(new_user); } new_user->profile_pic[0] = '\0'; new_user->first_post = NULL; new_user->friends = { NULL }; new_user->next = NULL; if (*user_ptr_add == NULL) { // If 0 users exists in the system *user_ptr_add = new_user; return 0; } if (find_user(name, *user_ptr_add) != NULL) { // If a user by this name already exists in this list return 1; } User *tail = *user_ptr_add; while (tail->next != NULL) { tail = tail->next; } tail->next = new_user; return 0; }
null
null
63c648f15f004bf4898d012a
2023-02-15T01:50:24.211Z
Question from Code
v1
error: comparison of array 'user->friends' equal to a null pointer is always false [-Werror,-Wtautological-pointer-compare] if (user->friends == NULL) {
int delete_user(const char *name, User **user_ptr_del) { User *user = find_user(name, *user_ptr_del); if (user == NULL){ // if a user with this name does not exist. return 1; } if (user->friends == NULL) { // user does not have any friends User *current = *user_ptr_del; User *prev = NULL; while (current != NULL) { if (strcmp(current->name, name) == 0) { if (prev == NULL) { // The user to delete is at the head of the list *user_ptr_del = current->next; } else { prev->next = current->next; } free(current); return 0; } prev = current; current = current->next; } return 1; } // Remove the deleted user from any lists of friends. for (int i = 0; i < MAX_FRIENDS; i++){ if (user->friends[i] != NULL){ int j = 0; while (user->friends[i]->friends[j] != NULL) { if (user->friends[i]->friends[j] == user) { // Remove the current user from this friend's list for (int k = j; k < MAX_FRIENDS - 1; k++) { user->friends[i]->friends[k] = user->friends[i]->friends[k + 1]; } user->friends[i]->friends[MAX_FRIENDS - 1] = NULL; break; } j++; } } } // delete the user from the list pointed to by *user_ptr_del User *current = *user_ptr_del; User *prev = NULL; while (current != NULL) { if (strcmp(current->name, name) == 0) { if (prev == NULL) { // The user to delete is at the head of the list *user_ptr_del = current->next; } else { prev->next = current->next; } free(current); return 0; } prev = current; current = current->next; } return 1; }
null
null
63c648f15f004bf4898d012a
2023-02-15T02:03:24.306Z
Question from Code
v1
segmentation fault
int make_post(const User *author, User *target, char *contents) { if ((author == NULL) || (target == NULL)){ // if either User pointer is NULL return 2; } if (already_friends(author, target) == 1){ // if users exist but are not friends return 1; } // Create Post Post *new_post = (Post*) malloc(sizeof(Post)); if (strcpy(new_post->author, author->name) == NULL) { free(new_post); } if (strcpy(new_post->contents, contents) == NULL) { free(new_post); } time_t curtime; time(&curtime); new_post->date = &curtime; // Insert the new post at the front of the user's list of posts. if (target->first_post == NULL) { // If target does not have any post target->first_post = new_post; return 0; } Post *tail = target->first_post; while (tail->next != NULL) { tail = tail->next; } tail->next = new_post; return 0; }
null
null
63c648f15f004bf4898d012a
2023-02-15T02:32:23.860Z
Question from Code
v1
Why doesn't my code print out the post information: from, date, contents
int print_user(const User *user) { if (!user) { // if the user is NULL return -1; } if (strcmp(user->profile_pic, "\0") != 0){ // if user has profile picture, print picture } printf("Name: %s\n", user->name); printf("------------------------------------------\n"); printf("Friends:\n"); int i = 0; while (i < MAX_FRIENDS && user->friends[i]) { printf("%s\n", user->friends[i]->name); i++; } printf("------------------------------------------\n"); printf("Posts:\n"); if (user->first_post == NULL) { // user has no posts printf("------------------------------------------\n"); return 0; } Post *head = user->first_post; while (head->next != NULL) { printf("From: %s\n", user->first_post->author); printf("Date: %s\n\n", ctime(user->first_post->date)); printf("%s\n\n", user->first_post->contents); printf("===\n"); } printf("------------------------------------------\n"); return 0; }
null
null
63c648f15f004bf4898d012a
2023-02-15T02:57:49.014Z
Question from Code
v1
This function doesn't match the output for this "Name: david ------------------------------------------ Friends: bogdan michelle ------------------------------------------ Posts: From: michelle Date: Fri Jan 27 14:40:07 2023 Isn't 209 fun? === From: bogdan Date: Fri Jan 27 14:40:07 2023 david is soooo cool :) ------------------------------------------"
int make_friends(const char *name1, const char *name2, User *head) { User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); if ((user1 == NULL) || (user2 == NULL)) { // if at least one user does not exist. return 4; } if (user1 == user2) { // if the same user is passed in twice. return 3; } if (is_full(user1) || is_full(user2)) { // if the users are not already friends, but at least one already has // MAX_FRIENDS friends. return 2; } if (already_friends(user1, user2) == 0) { // if the two users are already friends. return 1; } int i = 0; while (i < MAX_FRIENDS && user1->friends[i]) { i++; } user1->friends[i] = user2; i = 0; while (i < MAX_FRIENDS && user2->friends[i]) { i++; } user2->friends[i] = user1; return 0; } /* * Print a user profile. * For an example of the required output format, see the example output * linked from the handout. * Return: * - 0 on success. * - 1 if the user is NULL. */ int print_user(const User *user) { if (user == NULL) { // if the user is NULL return -1; } if (strcmp(user->profile_pic, "\0") != 0){ // if user has profile picture, print picture } printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // Print list of friends printf("Friends:\n"); int i = 0; while (i < MAX_FRIENDS && user->friends[i]) { printf("%s\n", user->friends[i]->name); i++; } printf("------------------------------------------\n"); // Print list of posts printf("Posts:\n"); printf("------------------------------------------\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); printf("Date: %s", ctime(current_post->date)); printf("\n%s\n\n", current_post->contents); printf("===", current_post->contents); current_post = current_post->next; } return 0; }
null
null
63c648f15f004bf4898d012a
2023-02-15T03:31:04.156Z
Question from Code
v1
Code prints out an extra character "�" when reading and printing from an ASCII file
int print_user(const User *user) { if (user == NULL) { // if the user is NULL return -1; } if (strcmp(user->profile_pic, "\0") != 0){ char ch; FILE* ptr; ptr = fopen(user->profile_pic, "r"); do { ch = fgetc(ptr); printf("%c", ch); // Checking if character is not EOF. // If it is EOF stop reading. } while (ch != EOF); // Closing the file fclose(ptr); } printf("\nName: %s\n", user->name); printf("------------------------------------------\n"); // Print list of friends printf("Friends:\n"); int i = 0; while (i < MAX_FRIENDS && user->friends[i]) { printf("%s\n", user->friends[i]->name); i++; } printf("------------------------------------------\n"); // Print list of posts printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); printf("Date: %s", ctime(current_post->date)); printf("\n%s\n", current_post->contents); if (current_post->next != NULL){ printf("\n===\n\n"); } current_post = current_post->next; } printf("------------------------------------------\n"); return 0; }
null
null
63c648f15f004bf4898d012a
2023-02-15T03:57:01.883Z
Question from Code
v1
Why is the date printed "Fri May 1 14:13:20 2139" not correct?
int make_post(const User *author, User *target, char *contents) { if ((author == NULL) || (target == NULL)){ // if either User pointer is NULL return 2; } if (already_friends(author, target) == 1){ // if users exist but are not friends return 1; } // Create Post Post *new_post = (Post*) malloc(sizeof(Post)); if (new_post == NULL) { // failed to allocate memory return -1; } if (strcpy(new_post->author, author->name) == NULL) { free(new_post); } new_post->contents = strdup(contents); if (new_post->contents == NULL) { free(new_post); return -1; } time_t curtime; time(&curtime); new_post->date = &curtime; // Insert the new post at the *front* of the user's list of posts. new_post->next = target->first_post; target->first_post = new_post; return 0; }
null
null