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
⌀ |
---|---|---|---|---|---|---|---|
63c648bd5f004bf4898cff3b | 2023-03-17T02:27:03.658Z | Help Write Code | v2 | null | null | null | write every integer from 0 to 99 to a binary file fp |
63c648bd5f004bf4898cff3b | 2023-03-17T02:39:09.966Z | Help Write Code | v2 | null | null | null | use the random() function to generate random numbers from 0 to 99 |
63c648bd5f004bf4898cff3b | 2023-03-17T03:25:45.899Z | General Question | v2 | how to handle sigprof | null | null | null |
63c648bd5f004bf4898cff3b | 2023-03-17T05:13:22.762Z | General Question | v2 | how to print a macro message that contains format specifiers | null | null | null |
63c648bd5f004bf4898cff3b | 2023-03-17T05:30:48.724Z | Help Fix Code | v2 | set a timer to seconds seconds. use ITIMER_PROF to make the itimer send a SIGPRO | struct itimerval timer;
struct timeval val_next;
struct timeval val_curr;
val_next.tv_sec = 0;
val_next.tv_usec = 0;
val_curr.tv_sec = seconds;
val_curr.tv_usec = 0;
timer.it_interval = val_next;
timer.it_value = val_curr;
setitimer(ITIMER_PROF, &timer, NULL);
| null | null |
63c648bd5f004bf4898cff3b | 2023-03-17T05:33:07.243Z | Help Fix Code | v2 | set a timer to seconds seconds. use ITIMER_PROF to make the itimer send a SIGPROF | struct itimerval timer;
struct timeval val_next;
struct timeval val_curr;
val_next.tv_sec = 0;
val_next.tv_usec = 0;
val_curr.tv_sec = seconds;
val_curr.tv_usec = 0;
timer.it_interval = val_next;
timer.it_value = val_curr;
setitimer(ITIMER_PROF, &timer, NULL);
| null | null |
63c648bd5f004bf4898cff3b | 2023-03-17T09:35:35.025Z | General Question | v2 | what does timeout expired mean | null | null | null |
63c648bd5f004bf4898cff3b | 2023-03-17T10:01:14.924Z | Explain Code | v2 | null | struct itimerval timer;
struct timeval val_next;
struct timeval val_curr;
val_next.tv_sec = 0;
val_next.tv_usec = 0;
val_curr.tv_sec = seconds;
val_curr.tv_usec = 0;
timer.it_interval = val_next;
timer.it_value = val_curr;
setitimer(ITIMER_PROF, &timer, NULL);
| null | null |
63c648bd5f004bf4898cff3b | 2023-03-17T10:20:59.678Z | Question from Code | v2 | why is my code not expiring after 1 second | int seconds = 1;
struct itimerval timer;
struct timeval val_next;
struct timeval val_curr;
val_next.tv_sec = 0;
val_next.tv_usec = 0;
val_curr.tv_sec = seconds;
val_curr.tv_usec = 0;
timer.it_interval = val_next;
timer.it_value = val_curr;
setitimer(ITIMER_PROF, &timer, NULL);
| null | null |
63c648bd5f004bf4898cff3b | 2023-03-23T04:13:01.944Z | General Question | v2 | how to remove first n characters of char array | null | null | null |
63c648bd5f004bf4898cff3b | 2023-03-23T04:40:48.177Z | Explain Code | v2 | null | char buf[30] = {'\0'} | null | null |
63c648bd5f004bf4898cff3b | 2023-03-26T23:59:18.410Z | Help Write Code | v2 | null | null | null | monitor 2 file descriptors: server and stdin |
63c648bd5f004bf4898cff3b | 2023-03-27T00:11:12.736Z | Explain Code | v2 | null | fd_set listen_fds = all_fds;
if (select(max_fd + 1, &listen_fds, NULL, NULL, NULL) == -1) {
perror("server: select");
exit(1);
}
// Is it the original socket? Create a new connection ...
if (FD_ISSET(sock_fd, &listen_fds)) {
int client_fd = accept_connection(sock_fd, users);
if (client_fd > max_fd) {
max_fd = client_fd;
}
FD_SET(client_fd, &all_fds);
printf("Accepted connection\n");
}
| null | null |
63c648bd5f004bf4898cff3b | 2023-03-27T00:48:05.664Z | General Question | v2 | why am i getting connect: connection refused | null | null | null |
63c648bd5f004bf4898cff3b | 2023-03-29T01:32:14.540Z | Question from Code | v2 | how do i prevent line 3 from actually writing to the fd | int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
} | null | null |
63c648bd5f004bf4898cff3b | 2023-03-29T01:34:03.767Z | General Question | v2 | how to check if a file descriptor is closed | null | null | null |
63c648bd5f004bf4898cff3b | 2023-03-29T07:20:25.670Z | General Question | v2 | how to initialize a char array of unknown size that can be concatenated multiple times without overwriting | null | null | null |
63c648bd5f004bf4898cff3b | 2023-03-30T07:45:00.576Z | General Question | v2 | how to use snprintf to concatenate | null | null | null |
63c648bd5f004bf4898cff3b | 2023-03-30T07:50:05.804Z | Explain Code | v2 | null | char buffer[100] = {'hi'};
snprintf(buffer, 100, "%s %s\n", buffer, "friend"); | null | null |
63c648bd5f004bf4898cff3b | 2023-03-30T07:57:55.654Z | Question from Code | v2 | should line 2 have parentheses? | int len = 0;
len += strlen("hi") + 1; | null | null |
63c648bd5f004bf4898cff3b | 2023-04-05T03:22:13.526Z | General Question | v2 | how to intialize a char array to null terminators | null | null | null |
63c648bd5f004bf4898cff3b | 2023-04-05T03:23:34.759Z | Help Fix Code | v2 | initialize array s and set it to null terminators | int len = 5;
char s[len + 1] = {'\0'}; | null | null |
63c648bd5f004bf4898cff3b | 2023-04-05T04:46:27.819Z | General Question | v2 | why did i get a "malloc success" message | null | null | null |
63c648bd5f004bf4898cff3b | 2023-04-06T11:05:44.635Z | General Question | v2 | can i use a string literal as the second argument of write () | null | null | null |
63c648bd5f004bf4898cff3b | 2023-04-06T11:06:46.035Z | General Question | v2 | does write() to a client automatically include network newline | null | null | null |
63c648bd5f004bf4898cff3b | 2023-04-06T21:48:16.882Z | Help Fix Code | v2 | keep accepting partial reads from client until client disconnects | int main() {
int on = 1;
User *user_list = NULL; // Create head of empty user list
char *username = malloc(sizeof(char) * 32);
setbuf(stdout, NULL); // stdout will not be buffered
struct sockaddr_in *self = init_server_addr(PORT);
int listenfd = set_up_server_socket(self, 5);
int nbytes = 0;
int status = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
(const char *) &on, sizeof(on));
if (status == -1) {
perror("setsockopt -- REUSEADDR");
}
while (1) {
int fd = accept_connection(listenfd);
if (fd < 0) {
continue;
}
char buf[BUFSIZE] = {'\0'};
int inbuf = 0; // How many bytes currently in buf?
int room = sizeof(buf); // How many bytes remaining in buf?
char *after = buf; // Pointer to position after the data in buf
nbytes = 0;
while ((nbytes == read(fd, after, room)) > 0) {
inbuf += nbytes;
int where;
// Account for partial reads by determining if a full
// line has been read from the client.
while ((where = find_network_newline(buf, inbuf)) > 0) {
buf[where - 2] = '\0';
printf("Full command: %s\n", buf);
char *cmd_argv[INPUT_ARG_MAX_NUM];
int cmd_argc = tokenize(buf, cmd_argv, fd);
if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list, fd, username) == -1) {
break;
}
inbuf -= where;
// Move characters after the full line to the front of buf.
memmove(buf, &buf[where], inbuf);
}
// Prepare for next read.
after = &(buf[inbuf]);
room = BUFSIZE - inbuf;
}
close(fd);
printf("Connection now closed\n");
}
free(self);
close(listenfd);
return 0;
} | null | null |
63c648bd5f004bf4898cff3b | 2023-04-06T22:40:50.031Z | General Question | v2 | why does my client connection close after client has entered one line | null | null | null |
63c648bd5f004bf4898cff3b | 2023-04-06T23:47:36.373Z | General Question | v2 | client sometimes gets syntax error in stty -icanon mode | null | null | null |
63c648bd5f004bf4898cff3b | 2023-04-24T01:30:53.401Z | General Question | v2 | if write() is called on a file descriptor and is blocked, what is happening? | null | null | null |
63c648bd5f004bf4898cff40 | 2023-02-01T01:04:33.943Z | Explain Code | v1 | null | printf("brenden"); | null | null |
63c648bd5f004bf4898cff40 | 2023-02-12T19:44:47.574Z | Explain Code | v1 | null | strcmp("br", "br"); | null | null |
63c648bd5f004bf4898cff40 | 2023-02-13T06:25:19.172Z | Help Fix Code | v1 | null | int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
}
if (friends_check(author, target) != 0){
return 1;
}
Post* new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
strcpy(new_post->contents, contents);
time_t *now = malloc(sizeof(time_t));
*now = time(NULL);
new_post->date = now;
new_post->next = target->first_post;
target->first_post = new_post;
return 0;
} | post | null |
63c648bd5f004bf4898cff40 | 2023-02-13T06:27:52.384Z | Help Fix Code | v1 | null | int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
}
if (friends_check(author, target) != 0){
return 1;
}
Post* new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
strcpy(new_post->contents, contents);
time_t *now = malloc(sizeof(time_t));
*now = time(NULL);
new_post->date = now;
new_post->next = target->first_post;
target->first_post = new_post;
return 0;
} | /*
* 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 |
63c648bd5f004bf4898cff40 | 2023-02-13T06:35:07.682Z | Help Fix Code | v1 | null | int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
}
if (friends_check(author, target) != 0){
return 1;
}
Post* new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
time_t *now = malloc(sizeof(time_t));
*now = time(NULL);
new_post->date = now;
new_post->next = target->first_post;
target->first_post = new_post;
return 0;
} | /*
* 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
*/
However, 5488 segmentation fault ./friendme | null |
63c648bd5f004bf4898cff40 | 2023-02-13T06:36:31.256Z | Help Fix Code | v1 | null | User *find_user(const char *name, const User *head) {
User *curr = (User *)head;
while (strcmp(curr->name, name) != 0 && curr != NULL){
curr = curr->next;
}
if (curr == NULL){
return NULL;
}else{
return curr;
}
} | /*
* 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 |
63c648bd5f004bf4898cff40 | 2023-02-13T06:46:30.369Z | Help Fix Code | v1 | null | User *find_user(const char *name, const User *head) {
User *curr = (User *)head;
while (strcmp(curr->name, name) != 0 && curr != NULL){
curr = curr->next;
}
if (curr == NULL){
return NULL;
}else{
return curr;
}
} | segmentation fault | null |
63c648bd5f004bf4898cff40 | 2023-02-14T04:14:55.387Z | Help Fix Code | v1 | null | int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
}
if (friends_check(author, target) != 0){
return 1;
}
Post* new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
strcpy(new_post->contents, contents);
time_t *now = malloc(sizeof(time_t));
*now = time(NULL);
new_post->date = now;
new_post->next = target->first_post;
target->first_post = new_post;
return 0;
} | ==25412== Address 0x4a75700 is 32 bytes inside a block of size 160 alloc'd
==25412== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==25412== by 0x109C4A: create_user (friends.c:81)
==25412== by 0x109470: process_args (friendme.c:31)
==25412== by 0x109A24: main (friendme.c:166) | null |
63c648bd5f004bf4898cff40 | 2023-02-14T04:23:12.235Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > 31){
return 2; // return 2 if the given name cannot fit in the 'name' array
}
User* curr = *user_ptr_add;
// if no user has been created
if (curr == NULL){
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("malloc"); // prints an error message to stderr
return 3;
}
strcpy(new_user->name, name);
// Initialize 'next', 'first_post' and every element in 'friends' to NULL
new_user->next = NULL;
new_user->first_post = NULL;
for (int i = 0; i < MAX_FRIENDS; i++){
(new_user->friends)[i] = NULL;
}
*user_ptr_add = new_user;
return 0;
}
while (strcmp(curr->name, name) != 0 && curr->next != NULL){
curr = curr->next;
}
if (strcmp(curr->name, name) == 0){
return 1; // return 1 if a user by this name already exists in this list
}else{
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("malloc"); // prints an error message to stderr
return 3;
}
strcpy(new_user->name, name);
new_user->next = NULL;
new_user->first_post = NULL;
for (int i = 0; i < MAX_FRIENDS; i++){
(new_user->friends)[i] = NULL;
}
curr->next = new_user;
return 0;
}
} | Address 0x4a75700 is 32 bytes inside a block of size 160 alloc'd | null |
63c648bd5f004bf4898cff40 | 2023-02-14T21:15:49.419Z | Help Fix Code | v1 | null | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int friends_check(const User *user1, const User *user2);
void view_post(const Post* view);
void break_up(User *name, const User *target);
/*
* Check whether two users are friends
*
* Return:
* - 0 if they are friends
* - 1 if either of the user is NULL
* - 2 if they are not friends
*/
int friends_check(const User *user1, const User *user2){
if(user1 == NULL || user2 == NULL){
return 1;
}
for(int i = 0; i < MAX_FRIENDS; i++){
if ((user1->friends)[i] == user2){
return 0;
}
}
return 2;
}
/*
* Print out a post, including its author, date and contents.
*/
void view_post(const Post* view){
printf("From: %s\n", view->author);
printf("Date: %s", ctime(view->date));
printf("\n%s\n", view->contents);
}
/*
* Remove the user'target' from the 'friends' arrray of the user 'name'
*/
void break_up(User *name, const User *target){
if (name == NULL || target == NULL){
return;
}
for(int i = 0; i < MAX_FRIENDS; i++){
if ((name->friends)[i] == target){
name->friends[i] = 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)
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) > 31){
return 2; // return 2 if the given name cannot fit in the 'name' array
}
User* curr = *user_ptr_add;
// if no user has been created
if (curr == NULL){
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("malloc"); // prints an error message to stderr
return 3;
}
strcpy(new_user->name, name);
// Initialize 'next', 'first_post' and every element in 'friends' to NULL
new_user->next = NULL;
new_user->first_post = NULL;
new_user->profile_pic[0] = '\0';
for (int i = 0; i < MAX_FRIENDS; i++){
(new_user->friends)[i] = NULL;
}
*user_ptr_add = new_user;
return 0;
}
while (strcmp(curr->name, name) != 0 && curr->next != NULL){
curr = curr->next;
}
if (strcmp(curr->name, name) == 0){
return 1; // return 1 if a user by this name already exists in this list
}else{
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("malloc"); // prints an error message to stderr
return 3;
}
strcpy(new_user->name, name);
new_user->next = NULL;
new_user->first_post = NULL;
new_user->profile_pic[0] = '\0';
for (int i = 0; i < MAX_FRIENDS; i++){
(new_user->friends)[i] = NULL;
}
curr->next = new_user;
return 0;
}
}
/*
* 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.
*/
User *find_user(const char *name, const User *head) {
User *curr = (User *)head;
while (curr != NULL && strcmp(curr->name, name) != 0){
curr = curr->next;
}
if (curr == NULL){
return NULL;
}else{
return curr;
}
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
while(curr != NULL){
printf("\t%s\n", curr->name);
curr = curr->next;
}
}
/*
* 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.
*/
int update_pic(User *user, const char *filename) {
if (strlen(filename) > 31){
return 2;
}
FILE *temp = fopen(filename, "r");
if (temp == NULL){
fclose(temp);
return 1; // return 1 if the file does not exist or cannot be opened.
}
strcpy(user->profile_pic, filename);
int error = fclose(temp);
if (error != 0) {
fprintf(stderr, "fclose failed\n");
return 1;
}
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.
*/
int make_friends(const char *name1, const char *name2, User *head) {
User* user1 = find_user(name1, head);
User* user2 = find_user(name2, head);
int user1_friend = MAX_FRIENDS; // The first empty spot index in the 'friends' array of user1
int user2_friend = MAX_FRIENDS; // The first empty spot index in the 'friends' array of user2
if (user1 == NULL || user2 == NULL){
return 4;
}else if (user1 == user2){
return 3;
}
for (int i = 0; i < MAX_FRIENDS; i ++){
if ((user1->friends)[i] == user2 || (user2->friends)[i] == user1){
return 1;
}
if ((user1->friends)[i] == NULL && user1_friend == MAX_FRIENDS){
user1_friend = i;
}
if ((user2->friends)[i] == NULL && user2_friend == MAX_FRIENDS){
user2_friend = i;
}
}
if (user1_friend == MAX_FRIENDS || user2_friend == MAX_FRIENDS){
return 2;
}else{
(user1->friends)[user1_friend] = user2;
(user2->friends)[user2_friend] = 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){
return 1;
}
FILE *pic = fopen(user->profile_pic, "r");
if (pic != NULL){
char line[43];
while(fgets(line, 43, pic) != NULL){
printf("%s", line);
}
printf("\n");
}
fclose(pic);
printf("Name: %s\n", user->name);
printf("------------------------------------------\n");
printf("Friends:\n");
for(int i = 0; i < MAX_FRIENDS ; i ++){
if ((user->friends)[i] != NULL){
printf("%s\n", ((user->friends)[i])->name);
}
}
printf("------------------------------------------\n");
printf("Posts:\n");
if (user->first_post != NULL){
view_post(user->first_post);
Post* other_post = (user->first_post)->next;
while(other_post != NULL){
printf("\n===\n\n");
view_post(other_post);
other_post = other_post->next;
}
}
printf("------------------------------------------\n");
return 0;
}
/*
* 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
*/
int make_post(const User *author, User *target, char *contents) {
if (author == NULL || target == NULL){
return 2;
}
if (friends_check(author, target) != 0){
return 1;
}
Post* new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
time_t *now = malloc(sizeof(time_t));
if (now == NULL) {
perror("malloc"); // prints an error message to stderr
return 3;
}
*now = time(NULL);
new_post->date = now;
new_post->next = target->first_post;
target->first_post = new_post;
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.
*/
int delete_user(const char *name, User **user_ptr_del) {
User* target = find_user(name, *user_ptr_del);
if (target == NULL){
return 1;
}
for(int i = 0; i < MAX_FRIENDS; i++){
if((target->friends)[i] != NULL){
// Delete the user from his/her friend's friend-list.
break_up((target->friends)[i], target);
target->friends[i] = NULL;
}
}
Post* target_post = target->first_post;
while (target_post != NULL){
Post* temp = target_post->next;
free((target_post)->date);
free((target_post)->contents);
free(target_post);
target_post = temp;
}
User* curr = *user_ptr_del;
while(curr != NULL){
// The first user is the user needed to be deleted.
if (curr == target){
*user_ptr_del = target->next;
free(target);
return 0;
}
// The user needed to be deleted is not the first user.
if ((curr->next) == target){
curr->next = target->next;
free(target);
return 0;
}
curr= curr->next;
}
return 1;
}
| ==53683== 160 bytes in 1 blocks are still reachable in loss record 1 of 2
==53683== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==53683== by 0x109C4A: create_user (friends.c:81)
==53683== by 0x109470: process_args (friendme.c:31)
==53683== by 0x109A24: main (friendme.c:166) | null |
63c648bf5f004bf4898cff4f | 2023-02-08T21:10:32.145Z | General Question | v1 | Assume `struct pixel` is already declared. Allocate space for m `struct pixel *` values, for some integer m | null | null | null |
63c648bf5f004bf4898cff4f | 2023-02-08T21:12:01.695Z | General Question | v1 | Assume `struct pixel` is already declared. Allocate space for m `struct pixel *` values, for some integer m. Then, for each pointer allocated, initialize it to point to heap-allocated space for an entire row of pixel data | null | null | null |
63c648bf5f004bf4898cff4f | 2023-02-15T02:15:40.774Z | General Question | v1 | I have a text file and want to print it | null | null | null |
63c648bf5f004bf4898cff4f | 2023-02-15T02:16:28.529Z | General Question | v1 | How do I print file contents using fgets | null | null | null |
63c648c15f004bf4898cff63 | 2023-01-17T18:48:50.310Z | General Question | v1 | What's your name? | null | null | null |
63c648c15f004bf4898cff63 | 2023-01-26T20:01:01.381Z | General Question | v1 | What type does malloc function accept | null | null | null |
63c648c15f004bf4898cff68 | 2023-02-14T19:54:35.941Z | General Question | v1 | how do I know the first empty index of an array | null | null | null |
63c648c15f004bf4898cff68 | 2023-02-14T19:56:04.942Z | General Question | v1 | what is the value of empty indexes in the array in C | null | null | null |
63c648c15f004bf4898cff68 | 2023-03-14T22:52:15.768Z | Help Write Code | v2 | null | null | null | please fix my seg faults
|
63c648c15f004bf4898cff68 | 2023-03-14T23:40:15.539Z | Help Fix Code | v2 | fix it according to docstrings | /* 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) {
// printf("%d", find_num_words("ian is a lil bitch"));
char line[MAXLINE];
//todo: check malloc return values
Rule *head = malloc(sizeof(Rule));
Rule *curr_rule = head;
while (fgets(line, MAXLINE, fp) != NULL) {
line[strcspn(line, "\n")] = '\0';
if (is_comment_or_empty(line) == 0 && line[0] != '\n') { // target line or action line
printf("\"%s\"\n", line);
if (line[0] != '\t') { // target line
printf("wtf\n");
char *token = strtok(line, "\r \n"); // first word in line
strcpy(curr_rule->target, token);
strcat(curr_rule->target, "\0"); // check
//fill dependencies
token = strtok(NULL, ": "); // obtain first dependency, if it exists
//TODO: cleanup code
if (token) {
Dependency *curr_dep = malloc(sizeof(Dependency));
curr_rule->dependencies = curr_dep;
token = strtok(NULL, " ");
while (token) {
curr_dep->next_dep = malloc(sizeof(Dependency));
curr_dep = curr_dep->next_dep;
token = strtok(NULL, " ");
}
curr_dep->next_dep = NULL; // terminate when none left
} else {
curr_rule->dependencies = NULL;
}
curr_rule->actions = NULL;
}
curr_rule->next_rule = NULL;
curr_rule = curr_rule->next_rule;
}
}
return head;
} | null | null |
63c648c25f004bf4898cff6d | 2023-01-19T02:04:30.856Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
char phone[11];
int num;
scanf("%s %d", phone, &num);
if (num == -1) {
printf("%s", phone);
return 0;
}
else if (-1 < num < 10) {
printf("%d", phone[num]);
return 0;
}
else {
printf("ERROR");
return 1;
}
} | null | null |
63c648c25f004bf4898cff6d | 2023-01-19T02:12:21.307Z | Help Fix Code | v1 | null | #include <stdio.h>
int main() {
char phone[11];
int num;
scanf("%s %d", phone, &num);
if (num == -1) {
printf("%s", phone);
return 0;
}
else if (-1 < num && num < 10) {
printf("%c", phone[num]);
return 0;
}
else {
printf("ERROR");
return 1;
}
} | null | null |
63c648c25f004bf4898cff6d | 2023-01-26T00:05:23.731Z | Help Fix Code | v1 | null | int **split_array(const int *s, int length) {
if (length > 0) {
if (length%2 == 0) {
int *even = malloc(sizeof(int) * (length/2));
int *odd = malloc(sizeof(int) * (length/2));
int *od = *odd;
int *ev = *even;
}
else {
int *even = malloc(sizeof(int) * (length/2 + 1));
int *odd = malloc(sizeof(int) * (length/2));
int *od = *odd;
int *ev = *even;
}
int *od = *odd;
int *ev = *even;
for (int i = 0; i < length; i+=2) {
ev[i] = *s[i];
}
if (length > 1) {
for (int i = 1; i < length; i+=2) {
od[i] = *s[i];
}
}
int result[2] = {ev, od};
int **ptr = &result;
return ptr;
}
} | Return a pointer to an array of two dynamically allocated arrays of ints.
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 |
63c648c25f004bf4898cff6d | 2023-01-26T17:24:07.716Z | Help Fix Code | v1 | null | int check_permissions(char *curr_per, char *req_per) {
for (int i = 0; i < 9; i++) {
if (req_per[i] != '-' && req_per[i] != curr_per[i]) {
return 1;
}
}
return 0;
}
int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
int count = 0;
// TODO: Process command line arguments.
scanf("%*s %*d");
char permissions[11];
int size;
while (scanf("%10s %*s %*s %d %*s %*d %*s %*s", permissions, &size) != EOF) {
printf("%s, %d\n", permissions, size);
}
/* while (scanf("%s %*s %*s %d %*s %*d %*s %*s", permissions, &size) != EOF) {
if (argc == 3) {
if (check_permissions(permissions + 1, argv[2]) == 0 && size > *argv[1]) {
count ++;
}
}
else {
if (size > *argv[1]) {
count ++;
}
}
} */
printf("%d\n", count);
// TODO: Call check_permissions as part of your solution to count the files to
// compute and print the correct value.
return 0;
} | take input in format of ls -l command and print the permissions and size of each file | null |
63c648c25f004bf4898cff6d | 2023-01-28T01:09:06.393Z | Help Fix Code | v1 | null | int check_sin(int *sin_array) {
for (int i = 0; i < 9; i++) {
if (i % 2 != 0) {
int multiplied = sin_array[i] * 2;
if (10 <= multiplied && multiplied <= 99) {
int num1 = multiplied % 10;
multiplied = multiplied / 10;
int num2 = multiplied % 10;
sin_array[i] = num1 + num2;
}
else {
sin_array[i] = multiplied;
}
}
}
int total;
for (int i = 0; i < 9; i++) {
total += sin_array[i];
}
if (total % 10 == 0) {
return 0;
}
return 1;
} | Multiply each digit at odd indices of sin_array by 2. If the product is a one-digit number, put it in the corresponding position of the result in sin_array.
If the product is a two-digit number, add the digits together and put the sum in the
corresponding position of the result in sin_array.
Add digits together to get the sum. If sum is divisible by 10, return 0. Otherwise, return 1. | null |
63c648c25f004bf4898cff72 | 2023-01-27T22:45:50.751Z | Explain Code | v1 | null | int main(int argc, char **argv){
int *full_array = build_array(&strs, 3);
int **result = split_array(full_array, 3);
printf("Original array: \n");
for (int i = 0; i < argc - 1; i++){
printf("%d ", full_array[i]);
}
printf("\n");
printf("result[0]: \n");
for (int i = 0; i < argc - 1; i++){
printf("%d ", result[0][i]);
}
printf("\n");
printf("result[1]: \n");
for (int i = 0; i < argc - 1; i++){
printf("%d ", result[1][i]);
}
printf("\n");
free(full_array);
free(result[0]);
free(result[1]);
free(result);
return 0;
} | null | null |
63c648c25f004bf4898cff72 | 2023-02-10T02:49:25.074Z | Question from Code | v1 | How to read in location of the pixel array | /*
* Read in the location of the pixel array, the image width, and the image
* height in the given bitmap file.
*/
void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) {
} | null | null |
63c648c25f004bf4898cff72 | 2023-02-10T02:50:48.322Z | Explain Code | v1 | null | void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) {
fseek(image, 9, SEEK_SET);
fread(&pixel_array_offset, sizeof(int), 4, image);
fseek(image, 17, SEEK_SET);
fread(&width, sizeof(int), 4, image);
fseek(image, 21, SEEK_SET);
fread(&height, sizeof(int), 4, image);
} | null | null |
63c648c25f004bf4898cff72 | 2023-02-10T02:52:22.345Z | Explain 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), 3, image);
fseek(image, 18, SEEK_SET);
fread(&width, sizeof(int), 3, image);
fseek(image, 22, SEEK_SET);
fread(&height, sizeof(int), 3, image);
} | null | null |
63c648c25f004bf4898cff72 | 2023-02-10T03:00:20.416Z | General Question | v1 | when you use fseek and then fread, does fread read the position fseek set the file to? | null | null | null |
63c648c25f004bf4898cff72 | 2023-02-10T03:03:34.106Z | Question from Code | v1 | when you use fseek and then fread, does fread read the offset at 10 and then 3 additional bytes, or will it only read the 3 additional bytes? | 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), 3, image);
} | null | null |
63c648c25f004bf4898cff72 | 2023-02-10T03:04:52.649Z | Explain 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, 1, 4, image);
fseek(image, 18, SEEK_SET);
fread(&width, 1, 4, image);
fseek(image, 22, SEEK_SET);
fread(&height, 1, 4, image);
} | null | null |
63c648c25f004bf4898cff72 | 2023-02-10T03:05:56.414Z | Explain Code | v1 | null | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **pixel_array = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
pixel_array[i] = malloc(sizeof(struct pixel) * width);
}
fseek(image, pixel_array_offset, SEEK_SET);
for (int i = 0; i < height; i++){
for (int j = 0; i < width; i++){
fread(pixel_array[i][j], sizeof(int), 3, image);
}
}
return pixel_array;
} | null | null |
63c648c25f004bf4898cff72 | 2023-02-10T03:15:11.102Z | Question from Code | v1 | How to initialize the struct pixel values? What am I doing wrong? | struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **pixel_array = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
pixel_array[i] = malloc(sizeof(struct pixel) * width);
}
fseek(image, pixel_array_offset, SEEK_SET);
for (int i = 0; i < height; i++){
for (int j = 0; i < width; i++){
fread(pixel_array[i][j], 1, 3, image);
}
}
return pixel_array;
} | null | null |
63c648c25f004bf4898cff72 | 2023-02-10T03:25:06.272Z | Question from Code | v1 | Am I reading the location of the pixel array, the image width, and the image height in the given bitmap file right? | void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) {
fseek(image, 10, SEEK_SET);
fread(&pixel_array_offset, 1, 4, image);
fseek(image, 18, SEEK_SET);
fread(&width, 1, 4, image);
fseek(image, 22, SEEK_SET);
fread(&height, 1, 4, image);
} | null | null |
63c648c25f004bf4898cff72 | 2023-02-10T03:27:41.241Z | 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, 1, sizeof(int), image);
fseek(image, 18, SEEK_SET);
fread(&width, 1, sizeof(int), image);
fseek(image, 22, SEEK_SET);
fread(&height, 1, sizeof(int), image);
} | Read in the location of the pixel array, the image width, and the image height in the given bitmap file. | null |
63c648c25f004bf4898cff72 | 2023-02-10T03:40:57.582Z | Help Fix Code | v1 | null | /*
* Read in pixel array by following these instructions:
*
* 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.
*/
struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
struct pixel **pixel_array = malloc(sizeof(struct pixel *) * height);
for (int i = 0; i < height; i++){
pixel_array[i] = malloc(sizeof(struct pixel));
}
fseek(image, pixel_array_offset, SEEK_SET);
for (int i = 0; i < height; i++){
for (int j = 0; i < width; i++){
fread(&pixel_array[i][j], 1, 3, image);
}
}
return pixel_array;
} | Read in pixel array by following these instructions:
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 |
63c648c25f004bf4898cff72 | 2023-02-14T05:37:57.591Z | Question from Code | v1 | What is the line to compile it into an executable named a.out that it will print the value 5 to stdout? (You do not need to include the options to display warnings, build the symbol table for the debugger or select the C standard.) | #include <stdio.h>
int main() {
#ifdef MACRO
printf("%d\n", MACRO);
#endif
return 0;
}
| null | null |
63c648c25f004bf4898cff72 | 2023-02-14T05:44:07.269Z | General Question | v1 | suppose we have the following line at the top of our program
#define MAXNAME = 32;
and then the declaration
char name[MAXNAME];
in the program. What will this declaration line become after the program has passed
through the C pre-processor?
| null | null | null |
63c648c25f004bf4898cff72 | 2023-02-15T16:45:42.310Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
if (*user_ptr_add == NULL){
if (strlen(name) > 31){
return 2;
}
User *user_ptr_add = malloc(sizeof(User));
strcpy(user_ptr_add->name, name);
return 0;
}
User *curr = NULL;
User *curr_next = *user_ptr_add;
while (curr_next != NULL){
if (curr_next->name == name){
return 1;
}
curr = curr_next;
curr_next = curr_next->next;
}
if (strlen(name) > 31){
return 2;
}
User *created_new_user = malloc(sizeof(User));
strcpy(created_new_user->name, name);
curr->next = created_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.
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 |
63c648c25f004bf4898cff72 | 2023-02-15T16:57:25.617Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
if (*user_ptr_add == NULL){
if (strlen(name) > 31){
return 2;
}
User *user_ptr_add = malloc(sizeof(User));
strcpy(user_ptr_add->name, name);
user_ptr_add->next = NULL;
return 0;
}
User *curr = NULL;
User *curr_next = *user_ptr_add;
while (curr_next != NULL){
if (strcmp(curr_next->name, name) == 0){
return 1;
}
curr = curr_next;
curr_next = curr_next->next;
}
if (strlen(name) > 31){
return 2;
}
User *created_new_user = malloc(sizeof(User));
strcpy(created_new_user->name, name);
curr->next = created_new_user;
created_new_user->next = NULL;
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 |
63c648c25f004bf4898cff72 | 2023-02-15T17:01:04.442Z | Help Fix Code | v1 | null | int create_user(const char *name, User **user_ptr_add) {
if ((*user_ptr_add) == NULL){
if (strlen(name) > 31){
return 2;
}
User (*user_ptr_add) = malloc(sizeof(User));
strcpy((*user_ptr_add).name, name);
(*user_ptr_add).next = NULL;
return 0;
}
User *curr = NULL;
User *curr_next = *user_ptr_add;
while (curr_next != NULL){
if (strcmp(curr_next->name, name) == 0){
return 1;
}
curr = curr_next;
curr_next = curr_next->next;
}
if (strlen(name) > 31){
return 2;
}
User *created_new_user = malloc(sizeof(User));
strcpy(created_new_user->name, name);
curr->next = created_new_user;
created_new_user->next = NULL;
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 |
63c648c25f004bf4898cff72 | 2023-02-15T17:31:20.395Z | 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 |
63c648c25f004bf4898cff72 | 2023-02-15T20:25:20.360Z | Question from Code | v1 | am I allocating space for the date in the Post struct correctly | #include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME 32 // Max username and profile_pic filename lengths
#define MAX_FRIENDS 10 // Max number of friends a user can have
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;
typedef struct post {
char author[MAX_NAME];
char *contents;
time_t *date;
struct post *next;
} Post;
int make_post(const User *author, User *target, char *contents) {
char *already_friends[2] = {"False", "False"};
time_t curtime;
time(&curtime);
// check if either User pointer is NULL
if ((author == NULL) || (target == NULL)){
return 2;
}
// check if users are friends
for (int i = 0; i < MAX_FRIENDS; i++){
if ((*target).friends[i] != NULL){
if (strcmp((*target).friends[i]->name, (*author).name) == 0){
already_friends[0] = "True";
}
}
if ((*author).friends[i] != NULL){
if (strcmp((*author).friends[i]->name, (*target).name) == 0){
already_friends[1] = "True";
}
}
}
if ((strcmp(already_friends[0], "True") != 0) && (strcmp(already_friends[1], "True") != 0)){
return 1;
}
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
new_post->date = malloc(sizeof(time_t));
new_post->date = &curtime;
new_post->next = NULL;
if (target->first_post == NULL){
target->first_post = new_post;
}else {
Post *old_post = target->first_post;
new_post->next = old_post;
target->first_post = new_post;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72 | 2023-02-15T20:27:21.797Z | Help Fix Code | v1 | null | #include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME 32 // Max username and profile_pic filename lengths
#define MAX_FRIENDS 10 // Max number of friends a user can have
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;
typedef struct post {
char author[MAX_NAME];
char *contents;
time_t *date;
struct post *next;
} Post;
int make_post(const User *author, User *target, char *contents) {
char *already_friends[2] = {"False", "False"};
time_t curtime;
time(&curtime);
// check if either User pointer is NULL
if ((author == NULL) || (target == NULL)){
return 2;
}
// check if users are friends
for (int i = 0; i < MAX_FRIENDS; i++){
if ((*target).friends[i] != NULL){
if (strcmp((*target).friends[i]->name, (*author).name) == 0){
already_friends[0] = "True";
}
}
if ((*author).friends[i] != NULL){
if (strcmp((*author).friends[i]->name, (*target).name) == 0){
already_friends[1] = "True";
}
}
}
if ((strcmp(already_friends[0], "True") != 0) && (strcmp(already_friends[1], "True") != 0)){
return 1;
}
Post *new_post = malloc(sizeof(Post));
strcpy(new_post->author, author->name);
new_post->contents = contents;
new_post->date = malloc(sizeof(time_t));
new_post->date = &curtime;
new_post->next = NULL;
if (target->first_post == NULL){
target->first_post = new_post;
}else {
Post *old_post = target->first_post;
new_post->next = old_post;
target->first_post = new_post;
}
return 0;
}
| allocate space for the date in the Post struct | null |
63c648c25f004bf4898cff72 | 2023-02-15T21:17:36.870Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
User *delete_user = find_user(name, *user_ptr_del);
if (delete_user == NULL){
printf("user NULL\n");
return 1;
}
// delete user from their friends' list
for(int i = 0; i < MAX_FRIENDS; i++){
if ((*delete_user).friends[i] != NULL){
User *friend = (*delete_user).friends[i];
for (int j = 0; j < MAX_FRIENDS; j++){
if (strcmp((*friend).friends[j]->name, name) == 0){
(*friend).friends[j] = NULL;
}
}
}
}
// free the post the user recieved
Post *curr_post = delete_user->first_post;
while (curr_post != NULL){
free(curr_post);
}
// delete user from the user list
User *curr_before = NULL;
User *curr = *user_ptr_del;
User *curr_next = curr->next;
while (curr != NULL){
if (strcmp(name, curr->name) == 0){
if (curr_before == NULL && curr_next == NULL){
curr = NULL;
}else if (curr_before == NULL){
curr->next = NULL;
}else{ // curr_next == NULL
curr_before->next = NULL;
}
}
}
free(curr);
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 |
63c648c25f004bf4898cff72 | 2023-02-15T22:09:39.650Z | Question from Code | v1 | I am getting a segmentationi fault running this part of the code. What am I doing wrong? | int delete_user(const char *name, User **user_ptr_del) {
User *delete_user = find_user(name, *user_ptr_del);
if (delete_user == NULL){
return 1;
}
// delete user from their friends' list
for(int i = 0; i < MAX_FRIENDS; i++){
if ((*delete_user).friends[i] != NULL){
User *friend = (*delete_user).friends[i];
// printf("%s\n", friend->name);
for (int j = 0; j < MAX_FRIENDS; j++){
if(friend->friends[j] != NULL){
// printf("%s\n", friend->friends[j]->name);
if (strcmp((*friend).friends[j]->name, name) == 0){
(*friend).friends[j] = NULL;
}
}
}
}
}
// free the post the user recieved
Post *curr_post = delete_user->first_post;
while (curr_post != NULL){
free(curr_post);
curr_post = curr_post->next;
}
// delete user from the user list
User *curr_before = NULL;
User *curr = *user_ptr_del;
User *curr_next = curr->next;
// printf("%s\n", curr_before->name);
printf("%s\n", curr->name);
printf("%s\n", curr_next->name);
while (curr != NULL){
// printf("curr_before %s\n", curr_before->name);
printf("curr %s\n", curr->name);
printf("curr_next %s\n", curr_next->name);
if (strcmp(name, curr->name) == 0){
if (curr_before == NULL && curr_next == NULL){
printf("curr_before and curr_next is NULL\n");
curr_before = NULL;
curr = NULL;
curr_next = NULL;
// free(curr);
}else if (curr_before == NULL){
printf("curr_before is NULL\n");
*user_ptr_del = curr_next;
curr_before = NULL;
curr = NULL;
curr_next = NULL;
// free(curr);
}else if (curr_next == NULL){
printf("curr_next is NULL\n");
curr_before = NULL;
curr = NULL;
curr_next = NULL;
// free(curr);
}else{
printf("all are not empty\n");
curr_before = NULL;
curr = NULL;
curr_next = NULL;
// free(curr);
}
}
curr_before = curr;
curr = curr_next;
curr_next = curr_next->next;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72 | 2023-02-15T22:12:42.854Z | Help Fix Code | v1 | null | int delete_user(const char *name, User **user_ptr_del) {
User *delete_user = find_user(name, *user_ptr_del);
if (delete_user == NULL){
return 1;
}
// delete user from their friends' list
for(int i = 0; i < MAX_FRIENDS; i++){
if ((*delete_user).friends[i] != NULL){
User *friend = (*delete_user).friends[i];
// printf("%s\n", friend->name);
for (int j = 0; j < MAX_FRIENDS; j++){
if(friend->friends[j] != NULL){
// printf("%s\n", friend->friends[j]->name);
if (strcmp((*friend).friends[j]->name, name) == 0){
(*friend).friends[j] = NULL;
}
}
}
}
}
// free the post the user recieved
Post *curr_post = delete_user->first_post;
while (curr_post != NULL){
free(curr_post);
curr_post = curr_post->next;
}
// delete user from the user list
User *curr_before = NULL;
User *curr = *user_ptr_del;
User *curr_next = curr->next;
while (curr != NULL){
if (strcmp(name, curr->name) == 0){
if (curr_before == NULL && curr_next == NULL){
// printf("curr_before and curr_next is NULL\n");
curr_before = NULL;
curr = NULL;
curr_next = NULL;
free(curr);
}else if (curr_before == NULL){
// printf("curr_before is NULL\n");
*user_ptr_del = curr_next;
curr_before = NULL;
curr = NULL;
curr_next = NULL;
free(curr);
}else if (curr_next == NULL){
// printf("curr_next is NULL\n");
curr_before = NULL;
curr = NULL;
curr_next = NULL;
free(curr);
}else{
// printf("all are not empty\n");
curr_before = NULL;
curr = NULL;
curr_next = NULL;
free(curr);
}
}
curr_before = curr;
curr = curr_next;
curr_next = curr_next->next;
}
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 |
63c648c25f004bf4898cff72 | 2023-02-16T17:45:47.977Z | Help Fix Code | v1 | null | char *copy(char *dest, const char *src, int capacity) {
size_t i;
for (i = 0; i < capacity && src[i] != '\0'; i++){
dest[i] = src[i];
for ( ; i < capacity; i++){
dest[i] = '\0';
}
}
return dest;
} | Write the copy function to perform exactly as strncpy does, with one
exception: your copy function will guarantee that dest is always
null-terminated. Capacity is expected to be the number of bytes of
memory allocated to dest.
You shoud read the man page to learn how strncpy works.
NOTE: You must write this function without using any string functions.
The only function that should depend on string.h is memset. | null |
63c648c25f004bf4898cff72 | 2023-02-16T17:47:21.681Z | Help Fix Code | v1 | null | char *copy(char *dest, const char *src, int capacity) {
int i;
for (i = 0; i < capacity && src[i] != '\0'; i++){
dest[i] = src[i];
}
dest[capacity] = '\0';
return dest;
} | Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest. You shoud read the man page to learn how strncpy works. NOTE: You must write this function without using any string functions. The only function that should depend on string.h is memset. | null |
63c648c25f004bf4898cff72 | 2023-02-17T21:11:56.637Z | Help Fix Code | v1 | null | char *copy(char *dest, const char *src, int capacity) {
int i;
for (i = 0; i < capacity && src[i] != '\0'; i++){
dest[i] = src[i];
for ( ; i < capacity; i++){
dest[i] = '\0';
}
}
return dest;
} | Write the copy function to perform exactly as strncpy does, with one
exception: your copy function will guarantee that dest is always
null-terminated. Capacity is expected to be the number of bytes of
memory allocated to dest.
You shoud read the man page to learn how strncpy works.
NOTE: You must write this function without using any string functions.
The only function that should depend on string.h is memset. | null |
63c648c25f004bf4898cff72 | 2023-03-03T04:22:06.187Z | General Question | v1 | How to make it so that each process creates exactly one a new process | null | null | null |
63c648c25f004bf4898cff72 | 2023-03-03T04:25:01.533Z | Help Fix Code | v1 | null | #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;
}
| each process creates exactly one a new process | null |
63c648c25f004bf4898cff72 | 2023-03-03T04:26:27.587Z | Help Fix Code | v1 | null | #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;
}
| the new children do not create additional processes. Only the original parent calls fork | null |
63c648c25f004bf4898cff72 | 2023-03-03T04:32:23.232Z | Help Fix Code | v1 | null | #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++) {
if(i == 0){
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| the new children do not create additional processes. Only the original parent calls fork. Keep the printf call for all processes. | null |
63c648c25f004bf4898cff72 | 2023-03-03T04:46:40.721Z | Help Fix Code | v1 | null | #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++) {
if(i == 0){
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
#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++) {
if(i == 0){
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| each process creates exactly one a new process. Keep the printf call for all processes | null |
63c648c25f004bf4898cff72 | 2023-03-08T19:41:01.962Z | Question from Code | v1 | how to create a new process in checkpasswd.c to run the validate program | #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
// NOTE: MAX_PASSWORD must be less than MAXLINE/2
#define MAXLINE 32
#define MAX_PASSWORD 10
#define PASSWORD_FILE "pass.txt"
/* Reads two chunks from stdin, and checks if they match a user id
* and password pair from a password file. The first chunk (MAX_PASSWORD bytes)
* will contain a user id, and the second chunk (MAX_PASSWORD bytes) will
* contain a password.
*
* The program exits with a value of
* 0 if the user id and password match,
* 1 if there is an error,
* 2 if the user id is found but the password does not match, and
* 3 if the user id is not found in the password file.
*/
/* Note that read will read at most MAX_PASSWORD bytes. This means that the
* longest password we can support is MAX_PASSWORD. We can't count on the
* caller to send the null termination character, so we have to ensure
* that we terminate the string that is sent anyway.
*
* If the input is coming from the console/keyboard, then the write calls
* are managed by the shell which will process each piece of input
* separately.
* However, if the input is coming from a pipe, there is no guarantee that
* two writes generated by checkpasswd will send the data such that it
* will be read by the two reads below. The means that it is important for
* checkpasswd to send exactly as many bytes that validate is expecting.
* Otherwise the behaviour may be unpredicatable.
*
* This also means that checkpasswd should report an invalid password
* or no user_id if the user input is longer than MAX_PASSWORD.
*/
int main(void){
int n, user_length;
char userid[MAXLINE];
char password[MAXLINE];
if((n = read(STDIN_FILENO, userid, MAX_PASSWORD)) == -1) {
perror("read");
exit(1);
} else if(n == 0) {
fprintf(stderr, "Error: could not read from stdin");
exit(1);
}
// Make sure user id is null-terminated
if(n <= MAX_PASSWORD) {
userid[n] ='\0';
}
// Remove newline character if it exists
char *newline;
if((newline = strchr(userid, '\n')) != NULL) {
*newline = '\0';
}
if((n = read(STDIN_FILENO, password, MAX_PASSWORD)) == -1) {
perror("read");
exit(1);
} else if(n == 0) {
fprintf(stderr, "Error: could not read from stdin");
exit(1);
}
// Make sure password is null-terminated
if(n <= MAX_PASSWORD) {
password[n] ='\0';
}
// Remove newline character if it exists
if((newline = strchr(password, '\n')) != NULL) {
*newline = '\0';
}
// Prepare the userid:password string to compare to password file entries.
// We expect userid to have enough space to concatenate ":" + password
// but we will play it safe and use strncat
strncat(userid, ":", MAXLINE - strlen(userid) - 1);
user_length = strlen(userid);
strncat(userid, password, MAXLINE - strlen(userid) - 1);
FILE *fp = fopen(PASSWORD_FILE, "r");
if(!fp) {
perror("fopen");
exit(1);
}
char line[MAXLINE];
while(fgets(line, sizeof(line) - 1, fp)) {
line[strlen(line) - 1] = '\0';
if (strcmp(userid, line) == 0) {
fclose(fp);
exit(0); // found match
}
else if(strncmp(userid, line, user_length) == 0) {
fclose(fp);
exit (2); // invalid password
}
}
fclose(fp);
exit(3); // no such user
}
#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
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72 | 2023-03-08T19:49:59.789Z | 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
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72 | 2023-03-08T19:53:56.374Z | 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
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int p = fork();
char *arg_Ptr[2];
arg_Ptr[] = {validate.c, user_id, password, NULL};
return 0;
}
| null | null |
63c648c25f004bf4898cff72 | 2023-03-08T20:02:26.231Z | General Question | v1 | how to get the path of a c file | null | null | null |
63c648c25f004bf4898cff72 | 2023-03-08T20:06:39.243Z | 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"
#define PASSWORD_FILE "pass.txt"
/* Reads two chunks from stdin, and checks if they match a user id
* and password pair from a password file. The first chunk (MAX_PASSWORD bytes)
* will contain a user id, and the second chunk (MAX_PASSWORD bytes) will
* contain a password.
*
* The program exits with a value of
* 0 if the user id and password match,
* 1 if there is an error,
* 2 if the user id is found but the password does not match, and
* 3 if the user id is not found in the password file.
*/
/* Note that read will read at most MAX_PASSWORD bytes. This means that the
* longest password we can support is MAX_PASSWORD. We can't count on the
* caller to send the null termination character, so we have to ensure
* that we terminate the string that is sent anyway.
*
* If the input is coming from the console/keyboard, then the write calls
* are managed by the shell which will process each piece of input
* separately.
* However, if the input is coming from a pipe, there is no guarantee that
* two writes generated by checkpasswd will send the data such that it
* will be read by the two reads below. The means that it is important for
* checkpasswd to send exactly as many bytes that validate is expecting.
* Otherwise the behaviour may be unpredicatable.
*
* This also means that checkpasswd should report an invalid password
* or no user_id if the user input is longer than MAX_PASSWORD.
*/
int main(void){
int n, user_length;
char userid[MAXLINE];
char password[MAXLINE];
if((n = read(STDIN_FILENO, userid, MAX_PASSWORD)) == -1) {
perror("read");
exit(1);
} else if(n == 0) {
fprintf(stderr, "Error: could not read from stdin");
exit(1);
}
// Make sure user id is null-terminated
if(n <= MAX_PASSWORD) {
userid[n] ='\0';
}
// Remove newline character if it exists
char *newline;
if((newline = strchr(userid, '\n')) != NULL) {
*newline = '\0';
}
if((n = read(STDIN_FILENO, password, MAX_PASSWORD)) == -1) {
perror("read");
exit(1);
} else if(n == 0) {
fprintf(stderr, "Error: could not read from stdin");
exit(1);
}
// Make sure password is null-terminated
if(n <= MAX_PASSWORD) {
password[n] ='\0';
}
// Remove newline character if it exists
if((newline = strchr(password, '\n')) != NULL) {
*newline = '\0';
}
// Prepare the userid:password string to compare to password file entries.
// We expect userid to have enough space to concatenate ":" + password
// but we will play it safe and use strncat
strncat(userid, ":", MAXLINE - strlen(userid) - 1);
user_length = strlen(userid);
strncat(userid, password, MAXLINE - strlen(userid) - 1);
FILE *fp = fopen(PASSWORD_FILE, "r");
if(!fp) {
perror("fopen");
exit(1);
}
char line[MAXLINE];
while(fgets(line, sizeof(line) - 1, fp)) {
line[strlen(line) - 1] = '\0';
if (strcmp(userid, line) == 0) {
fclose(fp);
exit(0); // found match
}
else if(strncmp(userid, line, user_length) == 0) {
fclose(fp);
exit (2); // invalid password
}
}
fclose(fp);
exit(3); // no such user
}
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
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int p = fork();
char *arg_Ptr[2];
arg_Ptr[] = {validate.c, user_id, password, NULL};
execv(__FILE__, arg_Ptr);
return 0;
}
| checkpasswd.c creates a new process to run the validate program | null |
63c648c25f004bf4898cff72 | 2023-03-08T20:09:16.479Z | Question from Code | v1 | how to get the path of another file, validate.c | #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"
#define PASSWORD_FILE "pass.txt"
/* Reads two chunks from stdin, and checks if they match a user id
* and password pair from a password file. The first chunk (MAX_PASSWORD bytes)
* will contain a user id, and the second chunk (MAX_PASSWORD bytes) will
* contain a password.
*
* The program exits with a value of
* 0 if the user id and password match,
* 1 if there is an error,
* 2 if the user id is found but the password does not match, and
* 3 if the user id is not found in the password file.
*/
/* Note that read will read at most MAX_PASSWORD bytes. This means that the
* longest password we can support is MAX_PASSWORD. We can't count on the
* caller to send the null termination character, so we have to ensure
* that we terminate the string that is sent anyway.
*
* If the input is coming from the console/keyboard, then the write calls
* are managed by the shell which will process each piece of input
* separately.
* However, if the input is coming from a pipe, there is no guarantee that
* two writes generated by checkpasswd will send the data such that it
* will be read by the two reads below. The means that it is important for
* checkpasswd to send exactly as many bytes that validate is expecting.
* Otherwise the behaviour may be unpredicatable.
*
* This also means that checkpasswd should report an invalid password
* or no user_id if the user input is longer than MAX_PASSWORD.
*/
int main(void){
int n, user_length;
char userid[MAXLINE];
char password[MAXLINE];
if((n = read(STDIN_FILENO, userid, MAX_PASSWORD)) == -1) {
perror("read");
exit(1);
} else if(n == 0) {
fprintf(stderr, "Error: could not read from stdin");
exit(1);
}
// Make sure user id is null-terminated
if(n <= MAX_PASSWORD) {
userid[n] ='\0';
}
// Remove newline character if it exists
char *newline;
if((newline = strchr(userid, '\n')) != NULL) {
*newline = '\0';
}
if((n = read(STDIN_FILENO, password, MAX_PASSWORD)) == -1) {
perror("read");
exit(1);
} else if(n == 0) {
fprintf(stderr, "Error: could not read from stdin");
exit(1);
}
// Make sure password is null-terminated
if(n <= MAX_PASSWORD) {
password[n] ='\0';
}
// Remove newline character if it exists
if((newline = strchr(password, '\n')) != NULL) {
*newline = '\0';
}
// Prepare the userid:password string to compare to password file entries.
// We expect userid to have enough space to concatenate ":" + password
// but we will play it safe and use strncat
strncat(userid, ":", MAXLINE - strlen(userid) - 1);
user_length = strlen(userid);
strncat(userid, password, MAXLINE - strlen(userid) - 1);
FILE *fp = fopen(PASSWORD_FILE, "r");
if(!fp) {
perror("fopen");
exit(1);
}
char line[MAXLINE];
while(fgets(line, sizeof(line) - 1, fp)) {
line[strlen(line) - 1] = '\0';
if (strcmp(userid, line) == 0) {
fclose(fp);
exit(0); // found match
}
else if(strncmp(userid, line, user_length) == 0) {
fclose(fp);
exit (2); // invalid password
}
}
fclose(fp);
exit(3); // no such user
}
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
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int p = fork();
char *arg_Ptr[2];
arg_Ptr[] = {validate.c, user_id, password, NULL};
execv(__FILE__, arg_Ptr);
return 0;
}
| null | null |
63c648c25f004bf4898cff72 | 2023-03-08T20:13:26.666Z | 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"
#define PASSWORD_FILE "pass.txt"
/* Reads two chunks from stdin, and checks if they match a user id
* and password pair from a password file. The first chunk (MAX_PASSWORD bytes)
* will contain a user id, and the second chunk (MAX_PASSWORD bytes) will
* contain a password.
*
* The program exits with a value of
* 0 if the user id and password match,
* 1 if there is an error,
* 2 if the user id is found but the password does not match, and
* 3 if the user id is not found in the password file.
*/
/* Note that read will read at most MAX_PASSWORD bytes. This means that the
* longest password we can support is MAX_PASSWORD. We can't count on the
* caller to send the null termination character, so we have to ensure
* that we terminate the string that is sent anyway.
*
* If the input is coming from the console/keyboard, then the write calls
* are managed by the shell which will process each piece of input
* separately.
* However, if the input is coming from a pipe, there is no guarantee that
* two writes generated by checkpasswd will send the data such that it
* will be read by the two reads below. The means that it is important for
* checkpasswd to send exactly as many bytes that validate is expecting.
* Otherwise the behaviour may be unpredicatable.
*
* This also means that checkpasswd should report an invalid password
* or no user_id if the user input is longer than MAX_PASSWORD.
*/
int main(void){
int n, user_length;
char userid[MAXLINE];
char password[MAXLINE];
if((n = read(STDIN_FILENO, userid, MAX_PASSWORD)) == -1) {
perror("read");
exit(1);
} else if(n == 0) {
fprintf(stderr, "Error: could not read from stdin");
exit(1);
}
// Make sure user id is null-terminated
if(n <= MAX_PASSWORD) {
userid[n] ='\0';
}
// Remove newline character if it exists
char *newline;
if((newline = strchr(userid, '\n')) != NULL) {
*newline = '\0';
}
if((n = read(STDIN_FILENO, password, MAX_PASSWORD)) == -1) {
perror("read");
exit(1);
} else if(n == 0) {
fprintf(stderr, "Error: could not read from stdin");
exit(1);
}
// Make sure password is null-terminated
if(n <= MAX_PASSWORD) {
password[n] ='\0';
}
// Remove newline character if it exists
if((newline = strchr(password, '\n')) != NULL) {
*newline = '\0';
}
// Prepare the userid:password string to compare to password file entries.
// We expect userid to have enough space to concatenate ":" + password
// but we will play it safe and use strncat
strncat(userid, ":", MAXLINE - strlen(userid) - 1);
user_length = strlen(userid);
strncat(userid, password, MAXLINE - strlen(userid) - 1);
FILE *fp = fopen(PASSWORD_FILE, "r");
if(!fp) {
perror("fopen");
exit(1);
}
char line[MAXLINE];
while(fgets(line, sizeof(line) - 1, fp)) {
line[strlen(line) - 1] = '\0';
if (strcmp(userid, line) == 0) {
fclose(fp);
exit(0); // found match
}
else if(strncmp(userid, line, user_length) == 0) {
fclose(fp);
exit (2); // invalid password
}
}
fclose(fp);
exit(3); // no such user
}
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
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int p = fork();
char *arg_Ptr[2];
arg_Ptr[] = {validate.c, user_id, password, NULL};
execv(realpath(), arg_Ptr);
return 0;
}
| checkpasswd.c creates a new process to run the validate program | null |
63c648c25f004bf4898cff72 | 2023-03-08T20:14:28.862Z | 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"
#define PASSWORD_FILE "pass.txt"
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
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int p = fork();
char *arg_Ptr[2];
arg_Ptr[] = {validate.c, user_id, password, NULL};
execv(realpath(), arg_Ptr);
return 0;
}
| checkpasswd.c creates a new process to run the validate program | null |
63c648c25f004bf4898cff72 | 2023-03-08T20:19:51.566Z | 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
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int pid = fork();
if(pid == 0){
char *arg_Ptr[2];
arg_Ptr[] = {validate.c, &user_id, &password, NULL};
execv(__FILE__, arg_Ptr);
}
return 0;
}
| checkpasswd.c creates a new process to run the validate program | null |
63c648c25f004bf4898cff72 | 2023-03-08T20:21:24.232Z | 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
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int pid = fork();
if(pid == 0){
char *arg_Ptr[2];
arg_Ptr[] = {validate.c, user_id, password, NULL};
execv(./validate, arg_Ptr);
}
return 0;
}
| checkpasswd.c creates a new process to run the validate program | null |
63c648c25f004bf4898cff72 | 2023-03-08T20:23:01.895Z | 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
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int pid = fork();
if(pid == 0){
char *arg_Ptr[2];
// arg_Ptr[0] = "validate.c";
arg_Ptr[] = {"validate.c", user_id, password, NULL};
execv(./validate, arg_Ptr);
}
return 0;
}
| checkpasswd.c creates a new process to run the validate program | null |
63c648c25f004bf4898cff72 | 2023-03-08T20:25:19.751Z | 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
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int pid = fork();
if(pid == 0){
char *arg_Ptr[2];
arg_Ptr[0] = "validate.c";
arg_Ptr[1] = user_id;
arg_Ptr[2] = password;
arg_Ptr[3] = NULL;
execv(./validate.c, arg_Ptr);
}
return 0;
}
| checkpasswd.c creates a new process to run the validate program | null |
63c648c25f004bf4898cff72 | 2023-03-08T20:34:55.200Z | 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
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int pid = fork();
if(pid == 0){
char *arg_Ptr[2];
arg_Ptr[0] = "validate.c";
arg_Ptr[1] = user_id;
arg_Ptr[2] = password;
arg_Ptr[3] = NULL;
execv(./validate.c, arg_Ptr);
}
wait();
return 0;
}
| checkpasswd.c creates a new process to run the validate program | null |
63c648c25f004bf4898cff72 | 2023-03-08T20:42:59.254Z | 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
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int pid = fork();
if(pid == 0){
char *arg_Ptr[2];
arg_Ptr[0] = "validate.c";
arg_Ptr[1] = user_id;
arg_Ptr[2] = password;
arg_Ptr[3] = NULL;
execv(./validate.c, arg_Ptr);
}
int status;
if (wait(&status) >= 0){
if (WEXITED(status)){
exit(WEXITSTATUS(status));
}
}
return 0;
}
| checkpasswd.c creates a new process to run the validate program | null |
63c648c25f004bf4898cff72 | 2023-03-08T20:45:11.619Z | 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
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int pid = fork();
if(pid == 0){
char *arg_Ptr[5];
arg_Ptr[0] = "validate.c";
arg_Ptr[1] = user_id;
arg_Ptr[2] = password;
arg_Ptr[3] = NULL;
execv(./validate.c, arg_Ptr);
}
int status;
if (wait(&status) >= 0){
if (WEXITED(status)){
exit(WEXITSTATUS(status));
}
}
return 0;
}
| checkpasswd.c creates a new process to run the validate program | null |