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
63c648635f004bf4898cfc07
2023-03-10T22:52:18.657Z
General Question
v2
how to traverse through strtok token?
null
null
null
63c648635f004bf4898cfc07
2023-03-11T00:11:34.479Z
General Question
v2
how to know if strdup is working? i cant print the string returned by strdup?
null
null
null
63c648635f004bf4898cfc07
2023-03-11T00:20:02.997Z
Question from Code
v2
Why can't I print the head->target?
head = malloc(sizeof(Rule)); head->target = strdup(token); printf("%s\n", head->target);
null
null
63c648635f004bf4898cfc07
2023-03-11T00:21:14.735Z
Question from Code
v2
why is this not printing the head->target?
while (fgets(line, MAXLINE, fp) != NULL) { line[MAXLINE - 1] = '\n'; if (is_comment_or_empty(line) != 1) { char *token; token = strtok(line, " "); while (token != NULL) { if (token[0] != '\t' && token[0] != '\n') { if (head == NULL) { head = malloc(sizeof(Rule)); head->target = strdup(token); head->dependencies = NULL; head->actions = NULL; head->next_rule = NULL; } } token = strtok(NULL, " "); } } } printf("%s\n", head->target); }
null
null
63c648635f004bf4898cfc07
2023-03-11T00:30:11.386Z
General Question
v2
Why does the token print a bunch of blank lines at the beginning?
null
null
null
63c648635f004bf4898cfc07
2023-03-11T04:06:43.490Z
Question from Code
v2
why does this while loop never end?
while (token != NULL) { if (token[0] != '\t' && (isspace(token[0]) == 0) && (strcmp(token, ":") != 0)) { if (head == NULL) { r = malloc(sizeof(Rule)); r->target = strdup(token); r->dependencies = NULL; r->actions = NULL; r->next_rule = NULL; head = r; curr = head; } if (curr->dependencies == NULL) { dep = malloc(sizeof(Dependency)); if (strcmp(curr->target, token) != 0) { r = malloc(sizeof(Rule)); r->target = strdup(token); r->dependencies = NULL; r->actions = NULL; r->next_rule = NULL; dep->next_dep = NULL; dep->rule = r; printf("%s\n", dep->rule->target); curr->dependencies = dep; } } else { while (curr->dependencies->next_dep != NULL) { curr->dependencies = curr->dependencies->next_dep; } r = malloc(sizeof(Rule)); r->target = strdup(token); r->actions = NULL; r->next_rule = NULL; dep->next_dep = NULL; dep->rule = r; curr->dependencies->next_dep = dep; printf("%s\n", curr->dependencies->next_dep->rule->target); } } token = strtok(NULL, " "); } }
null
null
63c648635f004bf4898cfc07
2023-03-11T04:09:05.928Z
Question from Code
v2
is this the correct way to loop to the last element?
while (curr->dependencies->next_dep != NULL) { curr->dependencies = curr->dependencies->next_dep; }
null
null
63c648635f004bf4898cfc07
2023-03-11T04:10:47.719Z
Question from Code
v2
why does this give me segmentation fault?
while (curr->dependencies != NULL) { curr->dependencies = curr->dependencies->next_dep; } r = malloc(sizeof(Rule)); r->target = strdup(token); r->actions = NULL; r->next_rule = NULL; dep->next_dep = NULL; dep->rule = r; curr->dependencies = dep; printf("%s\n", curr->dependencies->next_dep->rule->target); } }
null
null
63c648635f004bf4898cfc07
2023-03-11T04:11:39.138Z
Question from Code
v2
why is this giving me segmentation fault?
if (curr->dependencies == NULL) { dep = malloc(sizeof(Dependency)); if (strcmp(curr->target, token) != 0) { r = malloc(sizeof(Rule)); r->target = strdup(token); r->dependencies = NULL; r->actions = NULL; r->next_rule = NULL; dep->next_dep = NULL; dep->rule = r; printf("%s\n", dep->rule->target); curr->dependencies = dep; } } else { while (curr->dependencies != NULL) { curr->dependencies = curr->dependencies->next_dep; } r = malloc(sizeof(Rule)); r->target = strdup(token); r->actions = NULL; r->next_rule = NULL; dep->next_dep = NULL; dep->rule = r; curr->dependencies = dep; printf("%s\n", curr->dependencies->next_dep->rule->target); } }
null
null
63c648635f004bf4898cfc07
2023-03-11T04:33:26.682Z
Question from Code
v2
why is this printing false twice?
while (fgets(line, MAXLINE, fp) != NULL) { line[MAXLINE - 1] = '\0'; int i = MAXLINE - 1; while (isspace(line[i]) != 0) { line[i] = '\0'; i--; } if (is_comment_or_empty(line) != 1) { char *token; token = strtok(line, " "); while (token != NULL) { if (token[0] != '\t' && (isspace(token[0]) == 0) && (strcmp(token, ":") != 0)) { if (head == NULL) { printf("true\n"); r = malloc(sizeof(Rule)); r->target = strdup(token); r->dependencies = NULL; r->actions = NULL; r->next_rule = NULL; head = r; curr = head; } if (curr->dependencies == NULL) { printf("false\n"); dep = malloc(sizeof(Dependency)); if (strcmp(curr->target, token) != 0) { printf("nani?\n"); r = malloc(sizeof(Rule)); r->target = strdup(token); r->dependencies = NULL; r->actions = NULL; r->next_rule = NULL; dep->next_dep = NULL; dep->rule = r; curr->dependencies = dep; printf("%s\n", curr->dependencies->rule->target); if (curr->dependencies == NULL) { printf("what?\n"); } } } else { } } token = strtok(NULL, " "); } } }
null
null
63c648635f004bf4898cfc07
2023-03-11T04:45:31.260Z
Question from Code
v2
does this loop to the second element when there is only one element in the linked list?
while (curr->dependencies != NULL) { curr->dependencies = curr->dependencies->next_dep; }
null
null
63c648635f004bf4898cfc07
2023-03-11T16:04:11.004Z
Question from Code
v2
is this causing a segmentation fault?
while (curr->dependencies->next_dep != NULL) { curr->dependencies = curr->dependencies->next_dep; } curr->dependencies->next_dep = dep;
null
null
63c648635f004bf4898cfc07
2023-03-11T16:05:31.355Z
Question from Code
v2
will this cause segmentation fault?
while (curr->dependencies->next_dep != NULL) { curr->dependencies = curr->dependencies->next_dep; } if (curr->dependencies != NULL) { curr->dependencies->next_dep = dep; }
null
null
63c648635f004bf4898cfc07
2023-03-11T18:43:32.087Z
Question from Code
v2
why is this giving me segmentation fault?
if (token[0] == '\t') { printf("true\n"); printf("%s\n", token); token = strtok(NULL, " "); a = malloc(sizeof(Action)); char** arg = NULL; int i = 0; while (token != NULL) { arg[i] = strdup(token); token = strtok(NULL, " "); i++; }
null
null
63c648635f004bf4898cfc07
2023-03-11T18:45:13.784Z
Question from Code
v2
will this give me segmentation fault?
if (token[0] == '\t') { printf("true\n"); printf("%s\n", token); token = strtok(NULL, " "); a = malloc(sizeof(Action)); char** arg = a->args; int i = 0; while (token != NULL) { arg[i] = strdup(token); token = strtok(NULL, " "); i++; }
null
null
63c648635f004bf4898cfc07
2023-03-11T18:47:31.879Z
Question from Code
v2
why is this giving me segmentation fault?
if (token[0] == '\t') { printf("true\n"); printf("%s\n", token); a = malloc(sizeof(Action)); char** arg = a->args; int i = 0; while (token != NULL) { arg[i] = strdup(token); token = strtok(NULL, " "); i++; }
null
null
63c648635f004bf4898cfc07
2023-03-11T18:51:19.770Z
Question from Code
v2
do i need to malloc for args?
typedef struct action_node { char **args; struct action_node *next_act; } Action;
null
null
63c648635f004bf4898cfc07
2023-03-11T18:57:43.986Z
Question from Code
v2
how would you malloc for args without knowing how many char * you will be storing?
typedef struct action_node { char **args; struct action_node *next_act; } Action;
null
null
63c648635f004bf4898cfc07
2023-03-11T19:08:40.822Z
Question from Code
v2
why is this giving me segmentation
char *token; token = strtok(line, " "); if (token != NULL && token[0] == '\t') { a = malloc(sizeof(Action)); char **arg = malloc(sizeof(char *) * MAXLINE); int i = 0; while (token != NULL) { arg[i] = strdup(token); token = strtok(NULL, " "); i++; } a->args = arg; a->next_act = NULL; int j = 0; while (arg != NULL) { printf("%s\n", arg[j]); j++; } }
null
null
63c648635f004bf4898cfc07
2023-03-11T20:02:46.946Z
Question from Code
v2
why is this giving me segmentation fault?
while (curr->dependencies->next_dep != NULL) { curr->dependencies = curr->dependencies->next_dep; } curr->dependencies->next_dep->rule = r2; printf("%s\n", curr->dependencies->rule->target);
null
null
63c648635f004bf4898cfc07
2023-03-11T20:15:22.865Z
Question from Code
v2
why is this giving me segmentation fault?
Rule *curr2 = head; Rule *r = NULL; while (curr2 != NULL) { if (curr2->target != NULL && strcmp(curr2->target, token) == 0) { r = curr2; } curr2 = curr2->next_rule; }
null
null
63c648635f004bf4898cfc07
2023-03-11T20:45:32.364Z
Question from Code
v2
does this loop to the last element?
while (curr->next_rule != NULL) { curr->next_rule; }
null
null
63c648635f004bf4898cfc07
2023-03-11T20:46:03.580Z
Question from Code
v2
does this loop to the last element?
while (curr->next_rule != NULL) { curr = curr->next_rule; }
null
null
63c648635f004bf4898cfc07
2023-03-11T20:53:18.279Z
Question from Code
v2
why is this giving me infinite while loop
while (curr->next_rule != NULL) { curr = curr->next_rule; }
null
null
63c648635f004bf4898cfc07
2023-03-11T20:55:08.440Z
General Question
v2
does this loop to the last element?
null
null
null
63c648635f004bf4898cfc07
2023-03-11T20:55:25.302Z
Question from Code
v2
does this loop to the last element?
while (curr != NULL) { curr = curr->next_rule; }
null
null
63c648635f004bf4898cfc07
2023-03-11T20:56:11.112Z
Question from Code
v2
if i want to assign the curr->next value at the very last element will this loop take me to the right element to append at the end?
while (curr != NULL) { curr = curr->next_rule; }
null
null
63c648635f004bf4898cfc07
2023-03-11T21:03:38.151Z
Question from Code
v2
why is this causing infinite while loop
Rule *curr2 = head; Rule *r2 = NULL; while (curr2 != NULL) { if (strcmp(curr2->target, token) == 0) { printf("yay\n"); r2 = curr2; } curr2 = curr2->next_rule; } if (r2 == NULL) { r2 = malloc(sizeof(Rule)); r2->target = strdup(token); r2->dependencies = NULL; r2->actions = a; r2->next_rule = NULL; } while (curr->next_rule != NULL) { printf("%s\n", curr->target); curr = curr->next_rule; } curr->next_rule = r2;
null
null
63c648635f004bf4898cfc07
2023-03-12T05:48:27.368Z
Help Write Code
v2
null
null
null
to append to the last node of the linked list how should i do it?
63c648635f004bf4898cfc07
2023-03-12T05:52:43.254Z
Question from Code
v2
why is this giving me infinite while loop?
while (fgets(line, MAXLINE, fp) != NULL) { line[MAXLINE - 1] = '\0'; int i = MAXLINE - 1; while (isspace(line[i]) != 0) { line[i] = '\0'; i--; } if (is_comment_or_empty(line) != 1) { char *token; token = strtok(line, " "); Rule *r2 = NULL; Rule *curr2 = head; while (token != NULL) { while (curr2 != NULL) { if (strcmp(curr2->target, token) == 0) { r2 = curr2; } curr2 = curr2->next_rule; } if (r2 == NULL) { r = malloc(sizeof(Rule)); r->target = strdup(token); r->actions = NULL; r->dependencies = NULL; r->next_rule = NULL; } if (head == NULL) { head = r; } else { Rule *curr = head; while (curr->next_rule != NULL) { curr = curr->next_rule; } curr->next_rule = r; } token = strtok(NULL, " "); } } }
null
null
63c648635f004bf4898cfc07
2023-03-12T06:13:56.602Z
Help Fix Code
v2
why is tis giving me infinite while loop?
if(is_comment_or_empty(line) != 1){ // printf("%s\n", line[0]); char *token; token = strtok(line, " "); while(token != NULL){ if((isspace(token[0]) == 0 )&& (strcmp(token, ":") != 0) && (token[0] != '\t')){ Rule *r = NULL; Rule *curr2 = head; // Check if rule already exists while(curr2 != NULL){ printf("%s\n", curr2->target); if(strcmp(curr2->target, token) == 0){ r = curr2; break; } curr2 = curr2->next_rule; } // Rule doesn't exists if(r == NULL){ r = malloc(sizeof(Rule)); r->target = strdup(token); r->actions = NULL; r->dependencies = NULL; r->next_rule = NULL; } // Now append to head if(head == NULL){ head = r; }else{ Rule *curr = head; while(curr-> next_rule!= NULL){ curr = curr->next_rule; } curr->next_rule = r; } } token = strtok(NULL, " "); } } } return head; }
null
null
63c648635f004bf4898cfc07
2023-03-12T06:16:03.749Z
Help Fix Code
v2
make rules nodes like a make file, check if the rule node already exists, if it doesnt don't add a new one. If it doesnt create a new rule node and add to the end of the linked list
/* 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) { //Initialize variables char line[MAXLINE]; Rule *head = NULL; Rule *r; // Dependency *dep; // Action *a; // Read the first line while(fgets(line, MAXLINE, fp) != NULL){ line[MAXLINE - 1] = '\0'; int i = MAXLINE - 1; while(isspace(line[i]) != 0){ line[i] = '\0'; i --; } if(is_comment_or_empty(line) != 1){ // printf("%s\n", line[0]); char *token; token = strtok(line, " "); while(token != NULL){ if((isspace(token[0]) == 0 )&& (strcmp(token, ":") != 0) && (token[0] != '\t')){ Rule *r = NULL; Rule *curr2 = head; // Check if rule already exists while(curr2 != NULL){ printf("%s\n", curr2->target); if(strcmp(curr2->target, token) == 0){ r = curr2; break; } curr2 = curr2->next_rule; } // Rule doesn't exists if(r == NULL){ r = malloc(sizeof(Rule)); r->target = strdup(token); r->actions = NULL; r->dependencies = NULL; r->next_rule = NULL; } // Now append to head if(head == NULL){ head = r; }else{ Rule *curr = head; while(curr-> next_rule!= NULL){ curr = curr->next_rule; } curr->next_rule = r; } } token = strtok(NULL, " "); } } } return head; }
null
null
63c648635f004bf4898cfc07
2023-03-12T06:20:42.193Z
Question from Code
v2
why is this code giving finite while loop?
Rule *parse_file(FILE *fp) { char line[MAXLINE]; Rule *head = NULL; Rule *r; while (fgets(line, MAXLINE, fp) != NULL) { line[MAXLINE - 1] = '\0'; int i = MAXLINE - 1; while (isspace(line[i]) != 0) { line[i] = '\0'; i--; } if (is_comment_or_empty(line) != 1) { char *token; token = strtok(line, " "); while (token != NULL) { if ((isspace(token[0]) == 0) && (strcmp(token, ":") != 0) && (token[0] != '\t')) { Rule *r = NULL; Rule *curr2 = head; while (curr2 != NULL) { printf("%s\n", curr2->target); if (strcmp(curr2->target, token) == 0) { r = curr2; break; } curr2 = curr2->next_rule; } if (r == NULL) { r = malloc(sizeof(Rule)); r->target = strdup(token); r->actions = NULL; r->dependencies = NULL; r->next_rule = NULL; } if (head == NULL) { head = r; } else { Rule *curr = head; while (curr->next_rule != NULL) { curr = curr->next_rule; } curr->next_rule = r; } } token = strtok(NULL, " "); } } } return head; }
null
null
63c648635f004bf4898cfc07
2023-03-12T07:00:23.881Z
Question from Code
v2
does this sucessfully set all the trailing spaces to null?
int i = MAXLINE; while (isspace(line[i]) != 0) { line[i] = '\0'; i--; }
null
null
63c648635f004bf4898cfc07
2023-03-13T22:45:26.229Z
Help Write Code
v2
null
null
null
/* Evaluate the rule in rules corresponding to the given target. If target is NULL, evaluate the first rule instead. If pflag is 0, evaluate each dependency in sequence. If pflag is 1, then evaluate each dependency in parallel (by creating one new process per dependency). In this case, the parent process will wait until all child processes have terminated before checking dependency modified times to decide whether to execute the actions. */
63c648635f004bf4898cfc07
2023-03-14T04:37:08.211Z
Question from Code
v2
why is this recursion stopping after it executes action?
void evaluate_dep(char *target, Rule *rule) { Rule *r = find_rule(target, rule, 0); if (r == NULL) { } if (r != NULL) { Dependency *dep = r->dependencies; if (dep == NULL) { execute_action(r->actions); } if (dep != NULL) { struct stat t; if (stat(target, &t) != 0) { printf("f\n"); execute_action(r->actions); } else { int res = 2; res = compare_time(r->target, dep->rule->target); printf("%d\n", res); if (res == 0) { execute_action(r->actions); } } while (dep != NULL) { evaluate_dep(dep->rule->target, rule); struct stat t; if (stat(target, &t) != 0) { printf("f\n"); execute_action(r->actions); } else { int res = 2; res = compare_time(r->target, dep->rule->target); printf("%d\n", res); if (res == 0) { execute_action(r->actions); } } dep = dep->next_dep; } } } }
null
null
63c648635f004bf4898cfc07
2023-03-14T04:52:12.712Z
Question from Code
v2
why does this recursion stop after executing one action only?
void evaluate_dep(char *target, Rule *rule) { Rule *r = find_rule(target, rule, 0); if (r == NULL) { } if (r != NULL) { Dependency *dep = r->dependencies; if (dep == NULL) { execute_action(r->actions); } if (dep != NULL) { struct stat t; if (stat(target, &t) != 0) { printf("f\n"); execute_action(r->actions); } else { int res = 2; res = compare_time(r->target, dep->rule->target); printf("%d\n", res); if (res == 0) { execute_action(r->actions); } } while (dep != NULL) { evaluate_dep(dep->rule->target, rule); struct stat t; if (stat(target, &t) != 0) { printf("f\n"); execute_action(r->actions); } else { int res = 2; res = compare_time(r->target, dep->rule->target); printf("%d\n", res); if (res == 0) { execute_action(r->actions); } } dep = dep->next_dep; } } } }
null
null
63c648635f004bf4898cfc07
2023-03-14T04:56:33.898Z
Question from Code
v2
why is this recursion not working?
void evaluate_dep(char *target, Rule *rule) { Rule *r = find_rule(target, rule, 0); if (r == NULL) { } if (r != NULL) { Dependency *dep = r->dependencies; if (dep == NULL) { execute_action(r->actions); } if (dep != NULL) { struct stat t; if (stat(target, &t) != 0) { printf("f\n"); execute_action(r->actions); } else { int res = 2; res = compare_time(r->target, dep->rule->target); printf("%d\n", res); if (res == 0) { execute_action(r->actions); } } while (dep != NULL) { evaluate_dep(dep->rule->target, rule); struct stat t; if (stat(target, &t) != 0) { printf("f\n"); execute_action(r->actions); } else { int res = 2; res = compare_time(r->target, dep->rule->target); printf("%d\n", res); if (res == 0) { execute_action(r->actions); } } dep = dep->next_dep; } } } }
null
null
63c648635f004bf4898cfc07
2023-03-14T05:00:33.480Z
Question from Code
v2
why does this code stop right after doing one action?
void evaluate_dep(char *target, Rule *rule) { Rule *r = find_rule(target, rule, 0); if (r == NULL) { } if (r != NULL) { Dependency *dep = r->dependencies; if (dep == NULL) { execute_action(r->actions); } if (dep != NULL) { struct stat t; if (stat(target, &t) != 0) { printf("f\n"); execute_action(r->actions); } else { int res = 2; res = compare_time(r->target, dep->rule->target); printf("%d\n", res); if (res == 0) { execute_action(r->actions); } } while (dep != NULL) { evaluate_dep(dep->rule->target, rule); struct stat t; if (stat(target, &t) != 0) { printf("f\n"); execute_action(r->actions); } else { int res = 2; res = compare_time(r->target, dep->rule->target); printf("%d\n", res); if (res == 0) { execute_action(r->actions); } } dep = dep->next_dep; } } } }
null
null
63c648635f004bf4898cfc07
2023-03-14T05:01:34.414Z
Question from Code
v2
why is this recursion stopping right after one action is executed?
void evaluate_dep(char *target, Rule *rule) { Rule *r = find_rule(target, rule, 0); if (r == NULL) { } if (r != NULL) { Dependency *dep = r->dependencies; if (dep == NULL) { execute_action(r->actions); } while (dep != NULL) { evaluate_dep(dep->rule->target, rule); struct stat t; if (stat(target, &t) != 0) { printf("f\n"); execute_action(r->actions); } else { int res = 2; res = compare_time(r->target, dep->rule->target); printf("%d\n", res); if (res == 0) { execute_action(r->actions); } } dep = dep->next_dep; } } }
null
null
63c648635f004bf4898cfc07
2023-03-14T05:07:41.371Z
Question from Code
v2
Why is this only executing one action then stopping the loop?
void execute_action(Action *a) { Action *curr = a; char buffer[MAXLINE]; while (curr != NULL) { args_to_string(curr->args, buffer, sizeof(buffer)); printf("%s\n", buffer); curr = curr->next_act; } Action *curr2 = a; while (curr2 != NULL) { execvp(curr2->args[0], curr2->args); curr2 = curr2->next_act; } } void evaluate_dep(char *target, Rule *rule) { Rule *r = find_rule(target, rule, 0); if (r == NULL) { } if (r != NULL) { Dependency *dep = r->dependencies; if (dep == NULL) { execute_action(r->actions); } while (dep != NULL) { evaluate_dep(dep->rule->target, rule); struct stat t; if (stat(target, &t) != 0) { printf("f\n"); execute_action(r->actions); } else { int res = 2; res = compare_time(r->target, dep->rule->target); printf("%d\n", res); if (res == 0) { execute_action(r->actions); } } dep = dep->next_dep; } } }
null
null
63c648635f004bf4898cfc07
2023-03-14T20:33:18.947Z
Question from Code
v2
how come this can't execute multiple actions?
void execute_action(Action *a) { Action *curr = a; char buffer[MAXLINE]; while (curr != NULL) { args_to_string(curr->args, buffer, sizeof(buffer)); printf("%s\n", buffer); curr = curr->next_act; } Action *curr2 = a; while (curr2 != NULL) { execvp(curr2->args[0], curr2->args); curr2 = curr2->next_act; } }
null
null
63c648635f004bf4898cfc07
2023-03-14T21:19:28.115Z
Question from Code
v2
why is this wait not working
void execute_action(Action *a) { Action *curr = a; char buffer[MAXLINE]; while (curr != NULL) { args_to_string(curr->args, buffer, sizeof(buffer)); printf("%s\n", buffer); curr = curr->next_act; } Action *curr2 = a; while (curr2 != NULL) { int n = fork(); if (n == 0) { execvp(curr2->args[0], curr2->args); } else { int status; if (!(WIFEXITED(status))) { perror("wait"); exit(1); } } curr2 = curr2->next_act; } }
null
null
63c648635f004bf4898cfc07
2023-03-14T21:22:32.732Z
Question from Code
v2
why is this executing the actions multiple times?
void execute_action(Action *a) { Action *curr = a; char buffer[MAXLINE]; while (curr != NULL) { args_to_string(curr->args, buffer, sizeof(buffer)); printf("%s\n", buffer); curr = curr->next_act; } Action *curr2 = a; while (curr2 != NULL) { int n = fork(); if (n == 0) { execvp(curr2->args[0], curr2->args); exit(0); } else { int status; wait(&status); if (!(WIFEXITED(status))) { perror("wait"); exit(1); } } curr2 = curr2->next_act; } }
null
null
63c648635f004bf4898cfc07
2023-03-14T21:27:07.084Z
Question from Code
v2
is this working properly
void execute_action(Action *a) { Action *curr = a; char buffer[MAXLINE]; while (curr != NULL) { args_to_string(curr->args, buffer, sizeof(buffer)); printf("%s\n", buffer); curr = curr->next_act; } Action *curr2 = a; while (curr2 != NULL) { int n = fork(); if (n == 0) { execvp(curr2->args[0], curr2->args); exit(0); } else { int status; wait(&status); if (!(WIFEXITED(status))) { perror("wait"); exit(1); } } curr2 = curr2->next_act; } }
null
null
63c648635f004bf4898cfc07
2023-03-14T21:35:52.780Z
Question from Code
v2
is this doimg parallel properly?
void evaluate_parallel(Rule *r, Rule *rule) { int num_child = 0; if (r != NULL) { Dependency *dep = r->dependencies; if (dep == NULL) { execute_action(r->actions); } while (dep != NULL) { int n = fork(); if (n == 0) { evaluate_parallel(dep->rule, rule); exit(0); } else { num_child += 1; struct stat t; if (stat(r->target, &t) != 0) { execute_action(r->actions); } else { int res = 2; res = compare_time(r->target, dep->rule->target); if (res == 0) { execute_action(r->actions); } } } dep = dep->next_dep; } } for (int i = 0; i < num_child; i++) { int status; wait(&status); if (!(WIFEXITED(status))) { perror("wait"); exit(1); } } }
null
null
63c648635f004bf4898cfc07
2023-03-14T21:38:49.802Z
Question from Code
v2
how to make this run in parallel
void evaluate_parallel(Rule *r, Rule *rule) { int num_child = 0; if (r != NULL) { Dependency *dep = r->dependencies; if (dep == NULL) { execute_action(r->actions); } while (dep != NULL) { int n = fork(); if (n == 0) { evaluate_parallel(dep->rule, rule); exit(0); } else { num_child += 1; struct stat t; if (stat(r->target, &t) != 0) { execute_action(r->actions); } else { int res = 2; res = compare_time(r->target, dep->rule->target); if (res == 0) { execute_action(r->actions); } } } dep = dep->next_dep; } } for (int i = 0; i < num_child; i++) { int status; wait(&status); if (!(WIFEXITED(status))) { perror("wait"); exit(1); } } }
null
null
63c648635f004bf4898cfc07
2023-03-25T23:20:07.558Z
General Question
v2
Why is my lab11 saying chat_client connection refused?
null
null
null
63c648635f004bf4898cfc07
2023-03-25T23:20:27.624Z
General Question
v2
what are the causes of connect refused on a server?
null
null
null
63c648635f004bf4898cfc07
2023-03-25T23:21:50.567Z
Question from Code
v2
why is this causing connecting errors?
nt accept_connection(int fd, struct sockname *users) { int user_index = 0; while (user_index < MAX_CONNECTIONS && users[user_index].sock_fd != -1) { user_index++; } if (user_index == MAX_CONNECTIONS) { fprintf(stderr, "server: max concurrent connections\n"); return -1; } int client_fd = accept(fd, NULL, NULL); if (client_fd < 0) { perror("server: accept"); close(fd); exit(1); } char name[BUF_SIZE + 1]; int num_read; if ((num_read = read(client_fd, &name, BUF_SIZE)) == -1) { perror("read"); exit(1); } name[num_read] = '\0'; users[user_index].sock_fd = client_fd; if ((users[user_index].username = malloc(strlen(name) * sizeof(char))) == NULL) { perror("malloc"); exit(1); } strcpy(users[user_index].username, name); return client_fd; }
null
null
63c648635f004bf4898cfc07
2023-04-03T01:50:17.057Z
Question from Code
v2
what is causing this error?
savannapan@Savannas-MacBook-Pro a4 % make gcc -DPORT=\59208 -g -std=gnu99 -Wall -Werror -o friend_server friend_server.c Undefined symbols for architecture x86_64: "_accept_connection", referenced from: _main in friend_server-0cc6cb.o "_init_server_addr", referenced from: _main in friend_server-0cc6cb.o "_set_up_server_socket", referenced from: _main in friend_server-0cc6cb.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [friend_server] Error 1
null
null
63c648635f004bf4898cfc07
2023-04-03T02:00:20.544Z
Question from Code
v2
what may this causing when i have already included the #define _GNU_SOURCE
friends.c : 193 : 5 : error : implicit declaration of function ‘asprintf’; did you mean ‘vsprintf’ ? [-Werror = implicit - function - declaration] 193 | asprintf(&time, "%s", asctime(localtime(post->date))); | ^~~~~~~~ | vsprintf
null
null
63c648635f004bf4898cfc07
2023-04-03T02:08:46.253Z
Question from Code
v2
is asprintf not supported on ssh?
friends.c : 193 : 5 : error : implicit declaration of function ‘asprintf’; did you mean ‘vsprintf’ ? [-Werror = implicit - function - declaration] 193 | asprintf(&time, "%s", asctime(localtime(post->date))); | ^~~~~~~~ | vsprintf
null
null
63c648635f004bf4898cfc07
2023-04-03T02:10:30.188Z
General Question
v2
what is the difference between asprintf and vsprrintf?
null
null
null
63c648635f004bf4898cfc07
2023-04-03T02:26:19.662Z
Question from Code
v2
why is printing random garbage?
------------------------------------------ > list_users �[�Z�david karen michelle bogdan > profile david �[�Z�Name: david ------------------------------------------ Friends: bogdan
null
null
63c648635f004bf4898cfc07
2023-04-03T02:27:28.376Z
Question from Code
v2
why is this printing garbage sometimes on ssh?
char *list_users(const User *curr) { int size = 0; const User *curr2 = (User *)curr; while (curr2->next != NULL) { size += strlen(curr2->name) + 2; curr2 = curr2->next; } size += strlen(curr2->name); char *buffer = malloc(sizeof(char) * (size + 1)); if (buffer == NULL) { free(buffer); exit(1); } User *curr3 = (User *)curr; while (curr3->next != NULL) { strcat(buffer, curr3->name); strcat(buffer, "\r\n"); curr3 = curr3->next; } strcat(buffer, curr3->name); strcat(buffer, "\0"); strcat(buffer, "\r\n"); printf("%s", buffer); return buffer; }
null
null
63c648635f004bf4898cfc07
2023-04-03T03:14:53.064Z
Help Write Code
v2
null
null
null
how to approach this?
63c648635f004bf4898cfc07
2023-04-03T03:15:18.502Z
Help Write Code
v2
null
null
null
please write code from this description?
63c648635f004bf4898cfc07
2023-04-03T03:15:48.726Z
Question from Code
v2
please write pseudo code with this description?
Write the friend_server.c code so that you can have one client writing friends commands to the server. When you run the executable it should not take any command-line arguments. You will want some pieces from friendme.c (either your own solution or the new one we have provided), but you should remove sections of code that are no longer needed. Take advantage of example socket code that you have been given (including source code from the PCRS videos, lectures, and labs.) When your client connects to the server, ask for and store their name. Then allow them to enter commands. Of course the commands aren't very interesting at this point without a second user for them to befriend or message. But before adding a second user, you should sort out how to handle partial reads. Calling read on a socket is not guaranteed to return a full line typed in by the client, so you will need to keep track of how much is read each time and check to see if you have received a network newline which indicates the end of a command.
null
null
63c648635f004bf4898cfc07
2023-04-03T14:49:56.714Z
Question from Code
v2
why is this not printing the what is your username?
int main() { setbuf(stdout, NULL); fprintf(stdout, "Welcome to FriendMe! (Local version)\nPlease type a command:\n> "); 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; } printf("What is your usename?"); close(fd); } free(self); close(listenfd); return 0; }
null
null
63c648635f004bf4898cfc07
2023-04-03T15:04:09.720Z
Question from Code
v2
What does this mean?
^~~friend_server.c : 159 : 7 : error : a function declaration without a prototype is deprecated in all versions of C and is not supported in C2x[-Werror, -Wdeprecated - non - prototype] char * partial_read(fd) {
null
null
63c648635f004bf4898cfc07
2023-04-03T20:54:30.447Z
Question from Code
v2
will this return the full line read before the bufsize is filled?
char *partial_read(int fd, int op) { char *buf = malloc(sizeof(char) * BUFSIZE); buf[BUFSIZE - 1] = '\0'; int inbuf = 0; int room = sizeof(buf); char *after = buf; int nbytes; while ((nbytes = read(fd, after, room)) > 0) { inbuf += nbytes; int where; while ((where = find_network_newline(buf, inbuf)) > 0) { buf[where - 2] = '\0'; printf("hello%s", buf); inbuf -= where; memmove(buf, buf + where, inbuf); } after = buf + inbuf; room = sizeof(buf) - inbuf; } return buf; }
null
null
63c648635f004bf4898cfc07
2023-04-03T21:16:52.834Z
Question from Code
v2
why is this only returning the buffer once over 7 character have been typed, but not immediately when a newline characer is detected?
char *partial_read(int fd) { char *buf = malloc(sizeof(char) * BUFSIZE); buf[BUFSIZE - 1] = '\0'; int inbuf = 0; int room = sizeof(buf); char *after = buf; int nbytes; while ((nbytes = read(fd, after, room)) > 0) { inbuf += nbytes; int where; while ((where = find_network_newline(buf, inbuf)) > 0) { buf[where - 2] = '\0'; return buf; } if (find_network_newline(buf, inbuf) == 0) { buf[inbuf] = '\0'; return buf; } after = buf + inbuf; room = sizeof(buf) - inbuf; } return buf; }
null
null
63c648635f004bf4898cfc07
2023-04-03T23:32:02.940Z
Question from Code
v2
what is this code doing with the cmd_agrv
int main(int argc, char *argv[]) { int batch_mode = (argc == 2); char input[INPUT_BUFFER_SIZE]; FILE *input_stream; User *user_list = NULL; if (batch_mode) { input_stream = fopen(argv[1], "r"); if (input_stream == NULL) { perror("Error opening file"); exit(1); } } else { input_stream = stdin; } printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> "); while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) { if (batch_mode) { printf("%s", input); } char *cmd_argv[INPUT_ARG_MAX_NUM]; int cmd_argc = tokenize(input, cmd_argv); if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { break; } printf("> "); } if (batch_mode) { fclose(input_stream); } return 0; }
null
null
63c648635f004bf4898cfc07
2023-04-04T04:20:56.737Z
Question from Code
v2
why is this returning from the outside loop when more than 8 characters are read?
char *partial_read(int fd) { char *buf = malloc(2 + ((sizeof(char)) * BUFSIZE)); buf[BUFSIZE - 1] = '\0'; int inbuf = 0; int room = sizeof(buf); char *after = buf; int nbytes; while ((nbytes = read(fd, after, room)) > 0) { inbuf += nbytes; int where; if ((where = find_network_newline(buf, inbuf)) > 0) { printf("returned here!\n"); buf[where - 2] = '\0'; inbuf -= where; memmove(buf, buf + where, inbuf); return buf; } after = buf + inbuf; room = sizeof(buf) - inbuf; } printf("returned outside loop!\n"); return buf; }
null
null
63c648635f004bf4898cfc07
2023-04-04T04:26:02.934Z
Question from Code
v2
how many characters does this read before it will return from outside loop?
char *partial_read(int fd) { char *buf = malloc(sizeof(char) * BUFSIZE); buf[BUFSIZE - 1] = '\0'; int inbuf = 0; int room = sizeof(buf); char *after = buf; int nbytes; while ((nbytes = read(fd, after, room)) > 0) { inbuf += nbytes; int where; if ((where = find_network_newline(buf, inbuf)) > 0) { printf("returned here!\n"); buf[where - 2] = '\0'; inbuf -= where; memmove(buf, buf + where, inbuf); return buf; } after = buf + inbuf; room = sizeof(buf) - inbuf; } printf("returned outside loop!\n"); return buf; }
null
null
63c648635f004bf4898cfc07
2023-04-04T04:26:47.185Z
Question from Code
v2
how many characters does this loop read?
char *partial_read(int fd) { char *buf = malloc(sizeof(char) * BUFSIZE); buf[BUFSIZE - 1] = '\0'; int inbuf = 0; int room = sizeof(buf); char *after = buf; int nbytes; while ((nbytes = read(fd, after, room)) > 0) { inbuf += nbytes; int where; if ((where = find_network_newline(buf, inbuf)) > 0) { printf("returned here!\n"); buf[where - 2] = '\0'; inbuf -= where; memmove(buf, buf + where, inbuf); return buf; } after = buf + inbuf; room = sizeof(buf) - inbuf; } printf("returned outside loop!\n"); return buf; }
null
null
63c648635f004bf4898cfc07
2023-04-04T14:26:54.815Z
Question from Code
v2
why is this only calling process_args on the second input that i read, and not the first one?
int read_from(int client_fd, struct sockname *users, User *user_list) { int fd = client_fd; char *input = partial_read(fd); int num_read = strlen(input); char *cmd_argv[INPUT_ARG_MAX_NUM]; int cmd_argc = tokenize(input, cmd_argv); printf("returned input!\n"); printf("%d\n", cmd_argc); if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) { printf("hello"); } if (num_read == 0) { client_fd = -1; return fd; } return 0; }
null
null
63c648635f004bf4898cfc07
2023-04-04T14:31:53.936Z
Question from Code
v2
why is this only going to else case and prints incorrect syntax after the second time it is called?
int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) { User *user_list = *user_list_ptr; printf("function called\n"); if (cmd_argc <= 0) { return 0; } else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) { return -1; } else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) { switch (create_user(cmd_argv[1], user_list_ptr)) { case 1: error("user by this name already exists"); break; case 2: error("username is too long"); break; } } else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) { list_users(user_list); } else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) { switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) { case 1: break; } } else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) { int space_needed = 0; for (int i = 3; i < cmd_argc; i++) { space_needed += strlen(cmd_argv[i]) + 1; } char *contents = malloc(space_needed); if (contents == NULL) { perror("malloc"); exit(1); } strcpy(contents, cmd_argv[3]); for (int i = 4; i < cmd_argc; i++) { strcat(contents, " "); strcat(contents, cmd_argv[i]); } User *author = find_user(cmd_argv[1], user_list); User *target = find_user(cmd_argv[2], user_list); switch (make_post(author, target, contents)) { case 1: error("the users are not friends"); break; case 2: error("at least one user you entered does not exist"); break; } } else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) { User *user = find_user(cmd_argv[1], user_list); print_user(user); } else { printf("Incorrect syntax"); } return 0; }
null
null
63c648635f004bf4898cfc07
2023-04-04T16:56:06.162Z
Question from Code
v2
how to fix this issue?
error : The following untracked working tree files would be overwritten by merge : a4 / friend_server Please move or remove them before you merge. }
null
null
63c648635f004bf4898cfc07
2023-04-04T19:31:34.559Z
Question from Code
v2
is it correctly copying the username from partial read return input?
client_fd = accept_connection(sock_fd, users, &user_list); Client *c = malloc(sizeof(Client)); c->fd = client_fd; c->username = NULL; char *s1 = "What is your username?\r\n"; if (write(c->fd, s1, strlen(s1)) < 0) { perror("write"); exit(1); } char username[MAX_NAME + 1]; int num_read = 0; char *input = partial_read(c->fd); printf("%s\n", input); num_read = strlen(input); if (num_read < 0) { perror("server: read username"); exit(1); } username[num_read] = '\0'; if ((c->username = malloc(strlen(input) * sizeof(char))) == NULL) { perror("malloc at server"); exit(1); } strcpy(username, input); strcpy(c->username, username); user = process_name(&user_list, client_fd, c->username); printf("%s", user->name); printf("returned name\r\n"); if (client_fd > max_fd) { max_fd = client_fd; } FD_SET(client_fd, &all_fds); // printf("Accepted connection\n");
null
null
63c648635f004bf4898cfc07
2023-04-04T19:34:40.194Z
Question from Code
v2
why is this code not printing the username? is it being copied corretly?
if (FD_ISSET(sock_fd, &listen_fds)) { client_fd = accept_connection(sock_fd, users, &user_list); Client *c = malloc(sizeof(Client)); c->fd = client_fd; c->username = NULL; char *s1 = "What is your username?\r\n"; if (write(c->fd, s1, strlen(s1)) < 0) { perror("write"); exit(1); } char username[MAX_NAME + 1]; int num_read = 0; char *input = partial_read(c->fd); printf("%s\n", input); num_read = strlen(input); if (num_read < 0) { perror("server: read username"); exit(1); } if ((c->username = malloc((strlen(input) + 1) * sizeof(char))) == NULL) { perror("malloc at server"); exit(1); } strcpy(username, input); username[num_read - 1] = '\0'; strcpy(c->username, username); user = process_name(&user_list, client_fd, c->username); printf("%s", user->name); printf("returned name\r\n"); if (client_fd > max_fd) { max_fd = client_fd; } FD_SET(client_fd, &all_fds); char *s = "Go ahead and enter user commands>\r\n"; if (write(client_fd, s, strlen(s)) < 0) { perror("write"); exit(1); } }
null
null
63c648635f004bf4898cfc07
2023-04-04T22:43:38.674Z
Question from Code
v2
wha is causing this error?
malloc() : corrupted top size
null
null
63c648635f004bf4898cfc07
2023-04-05T03:47:46.629Z
Question from Code
v2
why is this giving segmentation fault?
strcpy(contents, cmd_argv[3]); for (int i = 3; i < cmd_argc; i++) { strcat(contents, " "); strcat(contents, cmd_argv[i]); }
null
null
63c648635f004bf4898cfc07
2023-04-05T03:48:36.753Z
Question from Code
v2
why is this giving segmentation fault?
int space_needed = 0; for (int i = 3; i < cmd_argc; i++) { space_needed += strlen(cmd_argv[i]) + 1; } printf("before space!\n"); char *contents = malloc(space_needed); if (contents == NULL) { perror("malloc"); exit(1); } printf("after malloc!\n"); strcpy(contents, cmd_argv[3]); for (int i = 3; i < cmd_argc; i++) { strcat(contents, " "); strcat(contents, cmd_argv[i]); }
null
null
63c648635f004bf4898cfc07
2023-04-05T04:04:43.821Z
Question from Code
v2
why is this giving segmentation fault?
} else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 3) { printf("first stop!\n"); int space_needed = 1; for (int i = 2; i < cmd_argc; i++) { space_needed += strlen(cmd_argv[i]) + 1; } printf("before space!\n"); char *contents = malloc(space_needed); if (contents == NULL) { perror("malloc"); exit(1); } printf("after malloc!\n"); strcpy(contents, cmd_argv[3]); for (int i = 2; i < cmd_argc; i++) { strcat(contents, " "); strcat(contents, cmd_argv[i]); }
null
null
63c648635f004bf4898cfc07
2023-04-05T19:11:33.352Z
General Question
v2
why do i get a segmentation fault after my program terminates?
null
null
null
63c648635f004bf4898cfc07
2023-04-05T19:12:16.366Z
General Question
v2
why do i get a segmentation fault after my server is killed?
null
null
null
63c648635f004bf4898cfc07
2023-04-06T01:52:25.482Z
General Question
v2
how to check on a file on valgrind?
null
null
null
63c648635f004bf4898cfc07
2023-04-06T02:30:26.845Z
General Question
v2
i am getting a segmentation fault after a client disconnects from a server, Think about what happens in the select command when a file descriptor in the read set corresponds to a client program that has died. Then think about what read returns if you call it on a socket where the process on the read end of the socket has died? how to fix this
null
null
null
63c648635f004bf4898cfc07
2023-04-06T17:04:56.770Z
Question from Code
v2
why is this giving segmentation fault?
int read_from(User *users, Client *c, User **head) { int fd = c->fd; char *input = partial_read(c); int num_read = 0; if (input == NULL) { return fd; } num_read = strlen(input); input[num_read] = '\0'; if (strlen(input) > 0 && (strcmp(input, "read") != 0)) { if (c->username == NULL) { int create = process_name(c, input, head); if (create != 0) { free(input); return create; } } else { char *cmd_argv[INPUT_ARG_MAX_NUM]; for (int i = 0; i < INPUT_ARG_MAX_NUM; i++) { cmd_argv[i] = NULL; } int cmd_argc = tokenize2(input, cmd_argv); char *res = process_args(cmd_argc, cmd_argv, &users, c->user_ptr, fd); if (res != NULL) { if (strcmp(res, "quit") == 0) { return fd; } } else if (input == NULL) { return fd; } free(input); } } return 0; }
null
null
63c648635f004bf4898cfc07
2023-04-06T19:16:55.670Z
Question from Code
v2
what does this mean?
Invalid write of size 2
null
null
63c648635f004bf4898cfc07
2023-04-06T19:25:00.340Z
General Question
v2
does snprinf malloc anything?
null
null
null
63c648635f004bf4898cfc07
2023-04-06T21:52:14.955Z
Question from Code
v2
what does this mean?
free() : double free detected in tcache 2
null
null
63c648635f004bf4898cfc07
2023-04-06T21:58:15.235Z
Question from Code
v2
is there double free error for this?
void free_client(int fd, Client **clients) { if (*clients != NULL) { if ((*clients)->fd == fd) { Client *pass = *clients; *clients = (*clients)->next; free(pass->username); free(pass->buffer); free(pass); } else { Client *prev = *clients; Client *curr = (*clients)->next; while (curr != NULL && curr->fd != fd) { prev = curr; curr = curr->next; } if (curr != NULL) { prev->next = curr->next; free(curr->username); free(curr->buffer); free(curr); } } } }
null
null
63c648655f004bf4898cfc16
2023-02-07T05:44:36.320Z
General Question
v1
What is the keyword that indicates that a word is a legal target but isn't an actual file?
null
null
null
63c648655f004bf4898cfc16
2023-02-15T22:12:21.190Z
General Question
v1
how do you printf a formatted date of type time_t?
null
null
null
63c648655f004bf4898cfc16
2023-02-15T23:26:38.392Z
Question from Code
v1
Can you partially initialize a struct inline, that is, from the code, can you exclude some struct attributes in the inline definition?
*new_user = (User){.name="", .profile_pic="", .first_post=NULL, .next=NULL};
null
null
63c648655f004bf4898cfc16
2023-03-31T21:15:09.424Z
General Question
v2
the OSI model treats the network and protocol layer as separate. That is, IP and TCP are two separate things. In that case, what is TCP/IP and why does it mingle the layers?
null
null
null
63c648655f004bf4898cfc16
2023-03-31T21:18:28.256Z
General Question
v2
you did not show the examples of other protocols. Please show me them
null
null
null
63c648655f004bf4898cfc16
2023-04-04T04:32:17.926Z
General Question
v2
subject=CSC I am told to write a shell command to print the following message "Last week of CSC209", using the variable subject. I am not sure how to do it. Could you show me how or guide me in the right direction?
null
null
null
63c648655f004bf4898cfc1b
2023-01-21T22:01:18.322Z
General Question
v1
can you write me a c program that prints back a user's input?
null
null
null
63c648655f004bf4898cfc1b
2023-01-30T17:37:57.959Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> // TODO: Implement a helper named check_permissions that matches the prototype below. int check_permissions(char *check_perms, char *req_perms) { int i; for (i = 0; i < 9; i++) { if (req_perms[i] != check_perms[i] && req_perms[i] != '-') { return 1; } } return 0; } int main(int argc, char** argv) { if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1; } // TODO: Process command line arguments. int cutoff = strtol(argv[1], NULL, 10); int total = 0; int success; int current_size; scanf("%*[^\n]\n");//Get past & ignore the Total (first) line if (argc == 3) { //Check permissions version char *permissions = malloc(sizeof(char) * 9); permissions = argv[2]; char *current_perm = malloc(sizeof(char) * 9); success = scanf("%*1s%9s %*d %*s %*s %d %*[^\n]\n", current_perm, &current_size); while (success > -1) { if (current_size > cutoff && check_permissions(current_perm, permissions) == 0) { total++; } success = scanf("%*s %*d %*s %*s %d %*[^\n]\n", &current_size); } free(permissions); free(current_perm); } else {//Regular version //Do one check first, so that they are synced properly with EOF check success = scanf("%*s %*d %*s %*s %d %*[^\n]\n", &current_size); while (success > -1) { if (current_size > cutoff) { total++; } success = scanf("%*s %*d %*s %*s %d %*[^\n]\n", &current_size); } } printf("%d\n", total); return 0; }
the intended behavior is to avoid segfaults
null
63c648665f004bf4898cfc20
2023-01-25T17:00:01.152Z
Question from Code
v1
Why is there error
#include <stdlib.h> #include <stdio.h> /* * Define a function void fib(...) below. This function takes parameter n * and generates the first n values in the Fibonacci sequence. Recall that this * sequence is defined as: * 0, 1, 1, 2, 3, 5, ... , fib[n] = fib[n-2] + fib[n-1], ... * The values should be stored in a dynamically-allocated array composed of * exactly the correct number of integers. The values should be returned * through a pointer parameter passed in as the first argument. * * See the main function for an example call to fib. * Pay attention to the expected type of fib's parameters. */ /* Write your solution here */ void fib(int **fib_ptr, int count) { fib_ptr = malloc(sizeof(int)*count); for (int i = 0; i <= count; i++) { if (i == 0) { fib_ptr[i] = 0; } else if (i == 1) { fib_ptr[i] = 1; } else { fib_ptr[i] = fib_ptr[i - 1] + fib_ptr[i - 2]; } } } int main(int argc, char **argv) { /* do not change this main function */ int count = strtol(argv[1], NULL, 10); int *fib_sequence; fib(&fib_sequence, count); for (int i = 0; i < count; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
null
null
63c648665f004bf4898cfc20
2023-01-25T17:02:51.306Z
Question from Code
v1
why is there segmentation error
#include <stdlib.h> #include <stdio.h> /* * Define a function void fib(...) below. This function takes parameter n * and generates the first n values in the Fibonacci sequence. Recall that this * sequence is defined as: * 0, 1, 1, 2, 3, 5, ... , fib[n] = fib[n-2] + fib[n-1], ... * The values should be stored in a dynamically-allocated array composed of * exactly the correct number of integers. The values should be returned * through a pointer parameter passed in as the first argument. * * See the main function for an example call to fib. * Pay attention to the expected type of fib's parameters. */ /* Write your solution here */ void fib(int **fib_ptr, int count) { fib_ptr = malloc(sizeof(int)*count); for (int i = 0; i <= count; i++) { if (i == 0) { fib_ptr[i] = 0; } else if (i == 1) { fib_ptr[i] = 1; } else { fib_ptr[i] = fib_ptr[i - 1] + fib_ptr[i - 2]; } } } int main(int argc, char **argv) { /* do not change this main function */ int count = strtol(argv[1], NULL, 10); int *fib_sequence; fib(&fib_sequence, count); for (int i = 0; i < count; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
null
null
63c648665f004bf4898cfc20
2023-01-25T17:07:56.467Z
Question from Code
v1
Why is there segmentation error?
#include <stdlib.h> #include <stdio.h> /* * Define a function void fib(...) below. This function takes parameter n * and generates the first n values in the Fibonacci sequence. Recall that this * sequence is defined as: * 0, 1, 1, 2, 3, 5, ... , fib[n] = fib[n-2] + fib[n-1], ... * The values should be stored in a dynamically-allocated array composed of * exactly the correct number of integers. The values should be returned * through a pointer parameter passed in as the first argument. * * See the main function for an example call to fib. * Pay attention to the expected type of fib's parameters. */ /* Write your solution here */ void fib(int **fib_ptr, int count) { fib_ptr = malloc(sizeof(int)*count); for (int i = 0; i < count; i++) { if (i == 0) { *fib_ptr[i] = 0; } else if (i == 1) { *fib_ptr[i] = 1; } else { *fib_ptr[i] = *fib_ptr[i - 1] + *fib_ptr[i - 2]; } } } int main(int argc, char **argv) { /* do not change this main function */ int count = strtol(argv[1], NULL, 10); int *fib_sequence; fib(&fib_sequence, count); for (int i = 0; i < count; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
null
null
63c648665f004bf4898cfc20
2023-01-25T17:09:38.908Z
Question from Code
v1
why is there segmentation error?
#include <stdlib.h> #include <stdio.h> /* * Define a function void fib(...) below. This function takes parameter n * and generates the first n values in the Fibonacci sequence. Recall that this * sequence is defined as: * 0, 1, 1, 2, 3, 5, ... , fib[n] = fib[n-2] + fib[n-1], ... * The values should be stored in a dynamically-allocated array composed of * exactly the correct number of integers. The values should be returned * through a pointer parameter passed in as the first argument. * * See the main function for an example call to fib. * Pay attention to the expected type of fib's parameters. */ /* Write your solution here */ void fib(int **fib_ptr, int count) { *fib_ptr = malloc(sizeof(int)*count); for (int i = 0; i < count; i++) { if (i == 0) { *fib_ptr[i] = 0; } else if (i == 1) { *fib_ptr[i] = 1; } else { *fib_ptr[i] = *fib_ptr[i - 1] + *fib_ptr[i - 2]; } } } int main(int argc, char **argv) { /* do not change this main function */ int count = strtol(argv[1], NULL, 10); int *fib_sequence; fib(&fib_sequence, count); for (int i = 0; i < count; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
null
null
63c648665f004bf4898cfc20
2023-01-25T17:11:43.589Z
Help Fix Code
v1
null
#include <stdlib.h> #include <stdio.h> /* * Define a function void fib(...) below. This function takes parameter n * and generates the first n values in the Fibonacci sequence. Recall that this * sequence is defined as: * 0, 1, 1, 2, 3, 5, ... , fib[n] = fib[n-2] + fib[n-1], ... * The values should be stored in a dynamically-allocated array composed of * exactly the correct number of integers. The values should be returned * through a pointer parameter passed in as the first argument. * * See the main function for an example call to fib. * Pay attention to the expected type of fib's parameters. */ /* Write your solution here */ void fib(int **fib_ptr, int count) { *fib_ptr = malloc(sizeof(int)*count); for (int i = 0; i < count; i++) { if (i == 0) { *fib_ptr[i] = 0; } else if (i == 1) { *fib_ptr[i] = 1; } else { *fib_ptr[i] = *fib_ptr[i - 1] + *fib_ptr[i - 2]; } } } int main(int argc, char **argv) { /* do not change this main function */ int count = strtol(argv[1], NULL, 10); int *fib_sequence; fib(&fib_sequence, count); for (int i = 0; i < count; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
when the function is called with an int argument, print the fibonacci sequence up to the argument
null
63c648665f004bf4898cfc20
2023-01-25T17:15:33.734Z
Explain Code
v1
null
#include <stdlib.h> #include <stdio.h> /* * Define a function void fib(...) below. This function takes parameter n * and generates the first n values in the Fibonacci sequence. Recall that this * sequence is defined as: * 0, 1, 1, 2, 3, 5, ... , fib[n] = fib[n-2] + fib[n-1], ... * The values should be stored in a dynamically-allocated array composed of * exactly the correct number of integers. The values should be returned * through a pointer parameter passed in as the first argument. * * See the main function for an example call to fib. * Pay attention to the expected type of fib's parameters. */ /* Write your solution here */ void fib(int **fib_ptr, int count) { *fib_ptr = malloc(sizeof(int)*count); for (int i = 0; i < count; i++) { if (i == 0) { *fib_ptr[i] = 0; } else if (i == 1) { *fib_ptr[i] = 1; } else { *fib_ptr[i] = *fib_ptr[i - 1] + *fib_ptr[i - 2]; } } } int main(int argc, char **argv) { /* do not change this main function */ int count = strtol(argv[1], NULL, 10); int *fib_sequence; fib(&fib_sequence, count); for (int i = 0; i < count; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
null
null
63c648665f004bf4898cfc20
2023-03-09T17:41:00.513Z
Help Fix Code
v2
This code gives me error, which is Error: could not read from stdin. Please fix that
#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); } printf("HELLO00"); // TODO int ret; int fd[2]; ret = fork(); if (ret < 0) { perror("pipe"); } if (pipe(fd) == -1) { perror("pipe"); exit(1); } printf("HELLO1"); if (ret == 0) { // child process close(fd[1]); dup2(fd[0], STDIN_FILENO); execl("./validate", "validate", NULL); close(fd[0]); } else { // parent process printf("HELLO2"); close(fd[0]); dup2(fd[1], STDIN_FILENO); write(fd[1], user_id, 10); write(fd[1], password, 10); close(fd[1]); int status; pid_t child_pid = wait(&status); if (child_pid == -1) { perror("wait"); return -1; } if (WIFEXITED(status)) { int exit_status = WEXITSTATUS(status); if(exit_status == 0) { printf("%s", SUCCESS); } else if(exit_status == 2) { printf("%s", INVALID); } else { printf("%s", NO_USER); } } } return 0; }
null
null