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
⌀ |
---|---|---|---|---|---|---|---|
63c648775f004bf4898cfcc0 | 2023-01-25T03:28:32.762Z | General Question | v1 | How do I initialize an array of 100 pointers to strings?
| null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-25T03:29:19.570Z | General Question | v1 | generate a detailed documentation of `strdup` with usage examples and explanations | null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-25T03:34:29.693Z | General Question | v1 | Can I initialize an empty array and later add strings to these spots? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-25T03:36:05.020Z | Question from Code | v1 | What does this code do? | char array[10][100] = {""}; | null | null |
63c648775f004bf4898cfcc0 | 2023-01-25T03:39:10.924Z | Question from Code | v1 | With this code I get the error "error: assignment to expression with array type". What is the issue? | perm_list[i] = permissions; | null | null |
63c648775f004bf4898cfcc0 | 2023-01-25T03:39:32.751Z | General Question | v1 | What is an lvalue?
| null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-25T03:43:47.960Z | General Question | v1 | If perm is an array of strings, what is perm[0]? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-25T03:46:10.437Z | General Question | v1 | what does the error message "lvalue required as left operand of assignment" mean? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-25T03:47:17.125Z | General Question | v1 | How do I add an element to an array if the spot is already filled? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-25T03:48:17.986Z | General Question | v1 | How to create an array of pointers to strings? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-25T03:49:24.473Z | Question from Code | v1 | What type of object is this? | char *array[10] | null | null |
63c648775f004bf4898cfcc0 | 2023-01-25T03:51:18.727Z | Question from Code | v1 | Does this code work? If not, why? | char *array[10];
char *word = "hello";
array[0] = word; | null | null |
63c648775f004bf4898cfcc0 | 2023-01-25T03:51:49.212Z | Question from Code | v1 | Does this code work? If not, why? | char *array[10];
char *word = "hello";
array[0] = &word; | null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T16:27:48.762Z | Question from Code | v1 | What does this code do? | malloc(100*sizeof(char*)) | null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T16:28:20.403Z | General Question | v1 | How do I allocate space for 100 pointers to strings? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T16:41:07.901Z | Explain Code | v1 | null | while (-1 != scanf("%c%s %*s %*s %*s %s %*s %*s %*s %*s", &directory, permissions, mem)) {
if (directory == '-') {
perm_list[i] = permissions;
mem_list[i] = strtol(mem, NULL, 10);
i ++;
size ++;
}
} | null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T16:41:53.167Z | General Question | v1 | generate a detailed documentation of `ps` with usage examples and explanations | null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T16:44:37.005Z | Question from Code | v1 | When I run this program with the input 700 and then give standard input :
total 329
-rwx------ 1 reid staff 1734 Jun 22 14:52 prog
-rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt
-rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz
-rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c
I expect to see 3, as there are three files with size greater than 700. However, the program returns 0. What is the issue? |
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.
// allocating memory and skipping the first line of ls input
char **perm_list = malloc(100 * sizeof(char*));
int mem_list[100];
scanf("%*s %*s ");
char directory;
char *permissions = malloc(11*sizeof(char));
char mem[9];
// iterating over the stdin, and reading the required values. Turning the size into
int size = 0;
int i = 0;
while (-1 != scanf("%c%s %*s %*s %*s %s %*s %*s %*s %*s", &directory, permissions, mem)) {
if (directory == '-') {
perm_list[i] = permissions;
mem_list[i] = strtol(mem, NULL, 10);
i ++;
size ++;
}
}
// TODO: Call check_permissions as part of your solution to count the files to
// compute and print the correct value.
int count = 0;
int lower = strtol(argv[1], NULL, 10);
if (argc == 2) {
int j;
for (j = 0; j < size; j ++) {
if (mem_list[i] > lower) {
count ++;
}
}
}
} | null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T16:45:39.958Z | Question from Code | v1 | When I run this program with the input 700 and then give standard input : total 329 -rwx------ 1 reid staff 1734 Jun 22 14:52 prog -rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt -rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz -rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c I expect to see 3, as there are three files with size greater than 700. However, the program returns 0. What is the issue? |
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.
// allocating memory and skipping the first line of ls input
char **perm_list = malloc(100 * sizeof(char*));
int mem_list[100];
scanf("%*s %*s ");
char directory;
char *permissions = malloc(11*sizeof(char));
char mem[9];
// iterating over the stdin, and reading the required values. Turning the size into
int size = 0;
int i = 0;
while (-1 != scanf("%c%s %*s %*s %*s %s %*s %*s %*s %*s", &directory, permissions, mem)) {
if (directory == '-') {
perm_list[i] = permissions;
mem_list[i] = strtol(mem, NULL, 10);
i ++;
size ++;
}
}
int count = 0;
int lower = strtol(argv[1], NULL, 10);
if (argc == 2) {
int j;
for (j = 0; j < size; j ++) {
if (mem_list[i] > lower) {
count ++;
}
}
}
} | null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T16:46:53.299Z | Question from Code | v1 | When I run this program with the input 700 and then give standard input :
total 329
-rwx------ 1 reid staff 1734 Jun 22 14:52 prog
-rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt
-rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz
-rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c
I expect to see 3, as there are three files with size greater than 700. However, the program returns 0. What is the issue? Also I intend to skip over the first line of the input |
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.
// allocating memory and skipping the first line of ls input
char **perm_list = malloc(100 * sizeof(char*));
int mem_list[100];
scanf("%*s %*s ");
char directory;
char *permissions = malloc(11*sizeof(char));
char mem[9];
// iterating over the stdin, and reading the required values. Turning the size into
int size = 0;
int i = 0;
while (-1 != scanf("%c%s %*s %*s %*s %s %*s %*s %*s %*s", &directory, permissions, mem)) {
if (directory == '-') {
perm_list[i] = permissions;
mem_list[i] = strtol(mem, NULL, 10);
i ++;
size ++;
}
}
int count = 0;
int lower = strtol(argv[1], NULL, 10);
if (argc == 2) {
int j;
for (j = 0; j < size; j ++) {
if (mem_list[i] > lower) {
count ++;
}
}
}
} | null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T16:47:50.517Z | General Question | v1 | How do I skip a line of stdin? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T16:49:00.881Z | General Question | v1 | Does scanf "skip" over lines of stdin if I read the input but do nothing with it? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T16:50:30.756Z | General Question | v1 | How can I use scanf to "skip" over a line of stdin?
| null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T23:34:47.800Z | Question from Code | v1 | This code is supposed to remove the first line from stdin, then add the numbers from a line if the lines do not begin with a 'd'. However, it does not do this. What is wrong? | int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
int count = 0;
char *required_permissions;
int lower_bound = strtol(argv[1], NULL, 10);
if (argc == 3){
required_permissions = argv[2];
}
// Skipping the first line
scanf("%*[^\n]");
char permissions[11];
char str_size[6];
char directory;
while (-1 != scanf("%c%s %*s %*s %*s %s %*s %*s %*s %*s", &directory, permissions, str_size)) {
if (directory != 'd') {
int size = strtol(str_size, NULL, 10);
if (size > lower_bound) {
count ++;
}
}
}
printf("%d", count);
return 0;
}
| null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T23:35:25.184Z | Question from Code | v1 | This code is supposed to add the numbers from a line if the lines do not begin with a 'd'. However, it does not do this. What is wrong? | int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
int count = 0;
char *required_permissions;
int lower_bound = strtol(argv[1], NULL, 10);
if (argc == 3){
required_permissions = argv[2];
}
// Skipping the first line
scanf("%*[^\n]");
char permissions[11];
char str_size[6];
char directory;
while (-1 != scanf("%c%s %*s %*s %*s %s %*s %*s %*s %*s", &directory, permissions, str_size)) {
if (directory != 'd') {
int size = strtol(str_size, NULL, 10);
if (size > lower_bound) {
count ++;
}
}
}
printf("%d", count);
return 0;
}
| null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T23:36:05.587Z | Question from Code | v1 | This code is supposed to add the numbers from a line if the lines do not begin with a 'd'. However, it does not do this. What is wrong?
It does not need to check the required permissions | int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
int count = 0;
char *required_permissions;
int lower_bound = strtol(argv[1], NULL, 10);
if (argc == 3){
required_permissions = argv[2];
}
// Skipping the first line
scanf("%*[^\n]");
char permissions[11];
char str_size[6];
char directory;
while (-1 != scanf("%c%s %*s %*s %*s %s %*s %*s %*s %*s", &directory, permissions, str_size)) {
if (directory != 'd') {
int size = strtol(str_size, NULL, 10);
if (size > lower_bound) {
count ++;
}
}
}
printf("%d", count);
return 0;
}
| null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T23:36:39.936Z | Question from Code | v1 | This code is supposed to add the numbers from a line if the lines do not begin with a 'd'. However, it does not do this. What is wrong? | int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
int count = 0;
int lower_bound = strtol(argv[1], NULL, 10);
if (argc == 3){
required_permissions = argv[2];
}
// Skipping the first line
scanf("%*[^\n]");
char permissions[11];
char str_size[6];
char directory;
while (-1 != scanf("%c%s %*s %*s %*s %s %*s %*s %*s %*s", &directory, permissions, str_size)) {
if (directory != 'd') {
int size = strtol(str_size, NULL, 10);
if (size > lower_bound) {
count ++;
}
}
}
printf("%d", count);
return 0;
}
| null | null |
63c648775f004bf4898cfcc0 | 2023-01-26T23:38:22.952Z | Explain Code | v1 | null | int check_permissions(char *file_permissions, char *required_permissions) {
int correct = 0;
int i = 0;
while (!correct && i < 9) {
if (required_permissions[i] != '-') {
if (file_permissions[i] != required_permissions[i]) {
correct = 1;
} else {
i ++;
}
}
}
return correct;
}
int main(int argc, char** argv) {
if (!(argc == 2 || argc == 3)) {
fprintf(stderr, "USAGE: count_large size [permissions]\n");
return 1;
}
int count = 0;
char *required_permissions;
int lower_bound = strtol(argv[1], NULL, 10);
if (argc == 3){
required_permissions = argv[2];
}
// Skipping the first line
scanf("%*[^\n]");
char permissions[11];
char str_size[6];
char directory;
while (-1 != scanf("%c%s %*s %*s %*s %s %*s %*s %*s %*s", &directory, permissions, str_size)) {
if (directory != 'd') {
int size = strtol(str_size, NULL, 10);
if (argc == 3) {
if (!check_permissions(permissions, required_permissions) && size > lower_bound) {
count ++;
}
} else if (size > lower_bound) {
count ++;
}
}
}
printf("%d", count);
return 0;
} | null | null |
63c648775f004bf4898cfcc0 | 2023-01-27T19:28:43.440Z | Question from Code | v1 | What does this code do? | scanf("%*[^\n]"); | null | null |
63c648775f004bf4898cfcc0 | 2023-01-27T19:29:06.091Z | Question from Code | v1 | Does this code skip the first line of stdin? | scanf("%*[^\n]"); | null | null |
63c648775f004bf4898cfcc0 | 2023-01-27T19:38:42.378Z | Question from Code | v1 | What does this code do? | if (!) {
printf("bruh");
} else {
printf("not bruh");
} | null | null |
63c648775f004bf4898cfcc0 | 2023-01-27T19:39:08.164Z | Question from Code | v1 | what does this code do? | if (!0) {
printf("bruh");
} else {
printf("not bruh");
} | null | null |
63c648775f004bf4898cfcc0 | 2023-01-27T19:39:48.902Z | Question from Code | v1 | what does this code do? | if (!3) {
printf("bruh");
} else {
printf("not bruh");
} | null | null |
63c648775f004bf4898cfcc0 | 2023-01-27T19:43:14.421Z | General Question | v1 | How do i set up gdb to debug my code?
| null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-27T19:55:46.008Z | General Question | v1 | What does the char '\r' represent? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-01-27T19:57:53.226Z | Question from Code | v1 | What does this code do? | scanf("%c%s %*s %*s %*s %s %*s %*s %*s %*s\r*", &directory, permissions, str_size) | null | null |
63c648775f004bf4898cfcc0 | 2023-03-12T20:03:33.812Z | General Question | v2 | How can I use a call to stat() to check if a specific file exits, without using errno? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-12T20:18:33.298Z | General Question | v2 | How can I generate a struct timespec with the current time? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-12T21:01:28.481Z | Help Fix Code | v2 | When I am running this code, I am trying to get it to perform the same as a makefile. Assuming that I already am giving it the required data files and structures for it to work, and that args_to_string turns the given array into a string. When I run the code in the shell, it stops after one call to gcc and prints "stat: No such file or directory". But if I run it in gdb and walk through it, it works. Why is this? |
struct timespec parallel_evaluate(Rule *rule) {
int len = dep_length(rule);
int i = 0;
struct timespec timestamps[len];
Dependency *curr = rule->dependencies;
while (curr != NULL) {
timestamps[i] = parallel_evaluate(curr->rule);
curr = curr->next_dep;
i ++;
}
struct stat stats;
char path[strlen(rule->target) + 3];
strcpy(path, "./");
strcat(path, rule->target);
int err = stat(path, &stats);
if (err == -1) {
if (rule->actions != NULL) {
// run the actions for this rule
run_actions_parallel(rule->actions);
// call stat again and return the last modified time
if (stat(path, &stats) == -1) {
perror("stat");
exit(1);
}
} else {
perror("stat");
exit(1);
}
} else {
// compare the timestamp from stat to all other timestamps
i = 0;
int update = 0;
while (i < len && update == 0) {
if (timestamps[i].tv_nsec > stats.st_mtim.tv_nsec) {
update = 1;
}
i ++;
}
if (update) {
run_actions_parallel(rule->actions);
if (stat(path, &stats) == -1) {
perror("stat");
exit(1);
}
}
}
return stats.st_mtim;
}
int dep_length(Rule *rule) {
int size = 0;
Dependency *curr = rule->dependencies;
while (curr != NULL) {
size ++;
curr = curr->next_dep;
}
return size;
}
int arr_length(char **arr) {
int i = 0;
int len = 0;
while (arr[i] != NULL) {
len = len + strlen(arr[i]) + 1;
i ++;
}
return len;
}
| null | null |
63c648775f004bf4898cfcc0 | 2023-03-12T21:05:03.234Z | Help Fix Code | v2 | This code should basically perform the actions of a makefile | struct timespec parallel_evaluate(Rule *rule) {
int len = dep_length(rule);
int i = 0;
struct timespec timestamps[len];
Dependency *curr = rule->dependencies;
while (curr != NULL) {
timestamps[i] = parallel_evaluate(curr->rule);
curr = curr->next_dep;
i ++;
}
struct stat stats;
char path[strlen(rule->target) + 3];
strcpy(path, "./");
strcat(path, rule->target);
int err = stat(path, &stats);
if (err == -1) {
if (rule->actions != NULL) {
// run the actions for this rule
run_actions_parallel(rule->actions);
// call stat again and return the last modified time
if (stat(path, &stats) == -1) {
perror("stat");
exit(1);
}
} else {
perror("stat");
exit(1);
}
} else {
// compare the timestamp from stat to all other timestamps
i = 0;
int update = 0;
while (i < len && update == 0) {
if (timestamps[i].tv_nsec > stats.st_mtim.tv_nsec) {
update = 1;
}
i ++;
}
if (update) {
run_actions_parallel(rule->actions);
if (stat(path, &stats) == -1) {
perror("stat");
exit(1);
}
}
}
return stats.st_mtim;
}
int dep_length(Rule *rule) {
int size = 0;
Dependency *curr = rule->dependencies;
while (curr != NULL) {
size ++;
curr = curr->next_dep;
}
return size;
}
int arr_length(char **arr) {
int i = 0;
int len = 0;
while (arr[i] != NULL) {
len = len + strlen(arr[i]) + 1;
i ++;
}
return len;
}
void run_actions_parallel(Action *actions) {
Action *curr = actions;
char *buffer;
while (curr != NULL) {
int len = arr_length(curr->args);
buffer = malloc(len);
if (buffer == NULL) {
perror("malloc");
exit(1);
}
args_to_string(curr->args, buffer, len);
printf("%s\n", buffer);
int r = fork();
if (r == 0) {
// child process, run the command
execvp(curr->args[0], curr->args);
perror("execvp");
exit(1);
} else if (r > 0) {
// parent process, continue iterating
curr = curr->next_act;
} else {
perror("fork");
exit(1);
}
}
}
| null | null |
63c648775f004bf4898cfcc0 | 2023-03-12T22:53:42.454Z | General Question | v2 | If I am forking a program and I want to consider the return value of a child in the parent, how can I do this? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-12T23:07:13.903Z | General Question | v2 | What is a struct timespec? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-13T00:17:08.954Z | General Question | v2 | What happens if I call wait() in a parent process when all child processes have already returned? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-13T00:19:53.503Z | General Question | v2 | How do I get the return value of a child process with wait? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-13T18:52:35.846Z | General Question | v2 | How do I get return values of a child process in the parent process? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-13T19:12:44.170Z | General Question | v2 | How can I get the return value of a child process that returns a struct in the parent process? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-13T19:16:11.312Z | General Question | v2 | How does waitpid work? Can I use it to return a struct from a child to a parent process? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-13T19:21:34.174Z | General Question | v2 | How can I access the return value of a child process in a parent process? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-14T22:49:24.706Z | General Question | v2 | If I call strtok on a string in an outer scope, then can I still access it within the scope of another function and continue from where I started? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-14T23:41:13.795Z | General Question | v2 | Does wait() set errno? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-15T00:00:38.796Z | Question from Code | v2 | What does this error mean? | gcc : error : linked_list.o : No such file or directory | null | null |
63c648775f004bf4898cfcc0 | 2023-03-15T00:24:36.556Z | Question from Code | v2 | What does this error mean? | gcc : fatal error : no input files | null | null |
63c648775f004bf4898cfcc0 | 2023-03-15T00:26:53.877Z | General Question | v2 | Explain how to use waitpid
| null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-15T22:33:52.080Z | General Question | v2 | How to set RAND_MAX when using random? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-15T22:42:54.865Z | General Question | v2 | How does srandom work? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-16T02:10:32.612Z | General Question | v2 | How to use the random() function? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-16T02:17:00.798Z | General Question | v2 | Can you briefly explain how to read from a binary file? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-16T02:25:06.746Z | General Question | v2 | How can I find the pid of a process that is currently running? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-16T02:30:07.981Z | General Question | v2 | Can you explain how sigaction works? In particular, the arguments it takes? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-16T02:36:37.091Z | General Question | v2 | Does sigaction set perror? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-16T02:36:57.310Z | General Question | v2 | Does sigaction set errno? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-16T02:40:12.146Z | General Question | v2 | How do I print to stdout with fprintf? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-16T02:41:41.821Z | Help Fix Code | v2 | My code should be replacing the initial signal handler with the function handler(), but this isn't happening | // function for signal handling
int handler(int signum) {
fprintf(stdout, "Bruh Moment");
exit(0);
}
/* The first command-line argument is the number of seconds to set a timer to run.
* The second argument is the name of a binary file containing 100 ints.
* Assume both of these arguments are correct.
*/
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: time_reads s filename\n");
exit(1);
}
seconds = strtol(argv[1], NULL, 10);
FILE *fp;
if ((fp = fopen(argv[2], "r")) == NULL) {
perror("fopen");
exit(1);
}
int max = 99;
int min = 0;
// create a struct sigaction
struct sigaction act;
act.sa_handler = (__sighandler_t) handler;
if (sigaction(SIGINT, &act, NULL) == -1) {
perror("sigaction");
exit(1);
}
/* In an infinite loop, read an int from a random location in the file,
* and print it to stderr.
*/
for (;;) {
int index = random() % (max - min + 1) + min;
if (fseek(fp, index * sizeof(int), SEEK_SET) == -1) {
perror("fseek");
exit(1);
}
int k;
if (fread(&k, sizeof(int), 1, fp) == -1) {
perror("fread");
exit(1);
}
printf("%d", k);
}
return 1; // something is wrong if we ever get here!
} | null | null |
63c648775f004bf4898cfcc0 | 2023-03-16T02:49:23.460Z | Question from Code | v2 | This code is supposed to replace the signal handler but its not working. Why? | // function for signal handling
void handler(int signum) {
fprintf(stdout, "Bruh Moment");
exit(0);
}
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: time_reads s filename\n");
exit(1);
}
seconds = strtol(argv[1], NULL, 10);
FILE *fp;
if ((fp = fopen(argv[2], "r")) == NULL) {
perror("fopen");
exit(1);
}
int max = 99;
int min = 0;
// create a struct sigaction
struct sigaction act;
act.sa_handler = handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
if (sigaction(SIGINT, &act, NULL) == -1) {
perror("sigaction");
exit(1);
}
for (;;) {
int index = random() % (max - min + 1) + min;
if (fseek(fp, index * sizeof(int), SEEK_SET) == -1) {
perror("fseek");
exit(1);
}
int k;
if (fread(&k, sizeof(int), 1, fp) == -1) {
perror("fread");
exit(1);
}
fprintf(stdout, "%d", k);
}
return 1; // something is wrong if we ever get here!
} | null | null |
63c648775f004bf4898cfcc0 | 2023-03-16T02:50:47.243Z | Question from Code | v2 | This code doesn't compile. Why? | // function for signal handling
void handler(int signum) {
fprintf(stdout, "Bruh Moment");
exit(0);
}
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: time_reads s filename\n");
exit(1);
}
seconds = strtol(argv[1], NULL, 10);
FILE *fp;
if ((fp = fopen(argv[2], "r")) == NULL) {
perror("fopen");
exit(1);
}
int max = 99;
int min = 0;
// create a struct sigaction
struct sigaction act;
act.sa_handler = handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
if (sigaction(SIGINT, act, NULL) == -1) {
perror("sigaction");
exit(1);
}
for (;;) {
int index = random() % (max - min + 1) + min;
if (fseek(fp, index * sizeof(int), SEEK_SET) == -1) {
perror("fseek");
exit(1);
}
int k;
if (fread(&k, sizeof(int), 1, fp) == -1) {
perror("fread");
exit(1);
}
fprintf(stdout, "%d", k);
}
return 1; // something is wrong if we ever get here!
} | null | null |
63c648775f004bf4898cfcc0 | 2023-03-16T02:54:53.211Z | General Question | v2 | Why does my handler work when a kill signal is sent with Ctrl+C but not when i use kill in a different window? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-16T03:01:09.586Z | Question from Code | v2 | What does this error mean from kill? | - bash:kill:SIGPROF:arguments must be process or job IDs | null | null |
63c648775f004bf4898cfcc0 | 2023-03-21T22:24:19.491Z | General Question | v2 | Is there an easy way to add int to a string
| null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-27T03:33:32.638Z | General Question | v2 | Variables width and height have been initialized. Calculate the area of a rectangle using width and height, and assign the resulting value to a variable named area, in a shell command | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-27T22:45:57.372Z | General Question | v2 | When select() blocks, what makes it return? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-27T23:06:35.416Z | General Question | v2 | Would a call to connect() from a client cause select to stop blocking? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-27T23:18:18.143Z | General Question | v2 | I am attempting to set up a connection between a client and a server, where the client reads from stdin, then writes to the server, then reads from the server and prints output, and the server reads from the client and then writes it back to them, but there is a one-write delay. Do you know why this may be? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-03-27T23:19:36.540Z | Question from Code | v2 | I am attempting to set up a connection between a client and a server, where the client reads from stdin, then writes to the server, then reads from the server and prints output, and the server reads from the client and then writes it back to them, but there is a one-write delay. This is my client code. what is the issue? | int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
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;
}
if (users[client_index].username == NULL) {
// username hasn't been set yet
users[client_index].username = malloc(num_read);
if (users[client_index].username == NULL) {
perror("malloc");
exit(1);
}
strcpy(users[client_index].username, buf);
}
return 0;
} | null | null |
63c648775f004bf4898cfcc0 | 2023-03-27T23:21:04.372Z | Question from Code | v2 | Question: I am attempting to set up a connection between a client and a server, where the client reads from stdin, then writes to the server, then reads from the server and prints output, and the server reads from the client and then writes it back to them, but there is a one-write delay. This is my server code. what is the issue? | int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
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;
}
if (users[client_index].username == NULL) {
// username hasn't been set yet
users[client_index].username = malloc(num_read);
if (users[client_index].username == NULL) {
perror("malloc");
exit(1);
}
strcpy(users[client_index].username, buf);
}
return 0;
} | null | null |
63c648775f004bf4898cfcc0 | 2023-03-27T23:23:06.887Z | Question from Code | v2 | I am attempting to set up a connection between a client and a server, where the client reads from stdin, then writes to the server, then reads from the server and prints output, and the server reads from the client and then writes it back to them, but there is a one-write delay. This is my client code. what is the issue? | num_read = read(STDIN_FILENO, buf, BUF_SIZE);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
if (write(sock_fd, buf, num_read) != num_read) {
perror("client: write");
close(sock_fd);
exit(1);
}
num_read = read(sock_fd, buf, sizeof(buf) - 1);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
printf("[Server] %s", buf);
} | null | null |
63c648775f004bf4898cfcc0 | 2023-04-03T23:48:11.142Z | General Question | v2 | How do partial reads work when reading on a server? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-04-04T00:30:33.099Z | General Question | v2 | Does read add a null terminator? | null | null | null |
63c648775f004bf4898cfcc0 | 2023-04-04T01:50:13.618Z | General Question | v2 | What does this error mean:
netcat: write failed (0/2): Broken pipe | null | null | null |
63c648775f004bf4898cfcc0 | 2023-04-04T01:57:37.297Z | General Question | v2 | Does select block?
| null | null | null |
63c648775f004bf4898cfcc0 | 2023-04-04T01:59:19.335Z | General Question | v2 | Is there a way to set select so it checks if any fds are ready when it is called but never blocks? | null | null | null |
63c648785f004bf4898cfcc5 | 2023-02-22T04:26:53.548Z | General Question | v1 | What is the difference between fgets and fscanf
| null | null | null |
63c648785f004bf4898cfcc5 | 2023-02-22T04:28:18.851Z | General Question | v1 | write me a short program using fscanf | null | null | null |
63c648785f004bf4898cfcc5 | 2023-02-22T04:29:35.740Z | Explain Code | v1 | null | #include <stdio.h>
int main(int argc, char **argv) {
if (argc == 1) {
printf("Usage: %s ARG\n", argv[0]);
return 1;
}
printf("Your command-line argument: %s\n", argv[1]);
return 0;
} | null | null |
63c648795f004bf4898cfcca | 2023-02-15T22:38:55.113Z | Question from Code | v1 | why segmentation fault | int delete_user(const char *name, User **user_ptr_del) {
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
User *curr = *user_ptr_del;
while(strcmp(curr->next->name, name) == 0) {
curr = curr->next;
}
curr->next = curr->next->next;
for (int i = 0; i < MAX_FRIENDS; i++) {
User *curr_friend = user->friends[i];
int index = 0;
for(int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(curr_friend->friends[j]->name, name) == 0) {
index = j;
}
}
for (int k = index; k + 1 < MAX_FRIENDS; k++) {
curr_friend->friends[k] = curr_friend->friends[k+1];
}
if (index == MAX_FRIENDS - 1) {
curr_friend->friends[index] = NULL;
}
}
Post *curr_post = user->first_post;
while(curr_post != NULL) {
Post *p = curr_post;
curr_post = curr_post->next;
free(p->date);
free(p->contents);
free(p);
}
free(user);
return 0;
} | null | null |
63c648795f004bf4898cfcca | 2023-02-15T22:55:33.614Z | Question from Code | v1 | why segmentation fault | int delete_user(const char *name, User **user_ptr_del) {
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
User *curr = *user_ptr_del;
if((curr->next == NULL) && (strcmp(curr->name, name) == 0)) {
curr = NULL;
}
while(strcmp(curr->next->name, name) == 0) {
curr = curr->next;
}
curr->next = curr->next->next;
for (int i = 0; i < MAX_FRIENDS; i++) {
User *curr_friend = user->friends[i];
int index = 0;
for(int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(curr_friend->friends[j]->name, name) == 0) {
index = j;
}
}
for (int k = index; k + 1 < MAX_FRIENDS; k++) {
curr_friend->friends[k] = curr_friend->friends[k+1];
}
if (index == MAX_FRIENDS - 1) {
curr_friend->friends[index] = NULL;
}
}
Post *curr_post = user->first_post;
while(curr_post != NULL) {
Post *p = curr_post;
curr_post = curr_post->next;
free(p->date);
free(p->contents);
free(p);
}
free(user);
return 0;
} | null | null |
63c648795f004bf4898cfcca | 2023-02-15T23:01:58.236Z | Question from Code | v1 | why segmentation fault | int delete_user(const char *name, User **user_ptr_del) {
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
User *curr = *user_ptr_del;
if((curr->next == NULL) && (strcmp(curr->name, name) == 0)) {
curr = NULL;
}
while(strcmp(curr->next->name, name) != 0) {
curr = curr->next;
}
curr->next = curr->next->next;
for (int i = 0; i < MAX_FRIENDS; i++) {
User *curr_friend = user->friends[i];
int index = 0;
for(int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(curr_friend->friends[j]->name, name) == 0) {
index = j;
}
}
for (int k = index; k + 1 < MAX_FRIENDS; k++) {
curr_friend->friends[k] = curr_friend->friends[k+1];
}
if (index == MAX_FRIENDS - 1) {
curr_friend->friends[index] = NULL;
}
}
Post *curr_post = user->first_post;
while(curr_post != NULL) {
Post *p = curr_post;
curr_post = curr_post->next;
free(p->date);
free(p->contents);
free(p);
}
free(user);
return 0;
} | null | null |
63c648795f004bf4898cfcca | 2023-02-15T23:04:23.519Z | Question from Code | v1 | why segmentation fault
| int delete_user(const char *name, User **user_ptr_del) {
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
User *curr = *user_ptr_del;
if((curr->next == NULL) && (strcmp(curr->name, name) == 0)) {
curr = NULL;
}
else {
while(strcmp(curr->next->name, name) != 0) {
curr = curr->next;
}
curr->next = curr->next->next;
}
for (int i = 0; i < MAX_FRIENDS; i++) {
User *curr_friend = user->friends[i];
int index = 0;
for(int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(curr_friend->friends[j]->name, name) == 0) {
index = j;
}
}
for (int k = index; k + 1 < MAX_FRIENDS; k++) {
curr_friend->friends[k] = curr_friend->friends[k+1];
}
if (index == MAX_FRIENDS - 1) {
curr_friend->friends[index] = NULL;
}
}
Post *curr_post = user->first_post;
while(curr_post != NULL) {
Post *p = curr_post;
curr_post = curr_post->next;
free(p->date);
free(p->contents);
free(p);
}
free(user);
return 0;
} | null | null |
63c648795f004bf4898cfcca | 2023-02-15T23:20:58.065Z | Question from Code | v1 | why segmentation fault | int delete_user(const char *name, User **user_ptr_del) {
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
User *curr = *user_ptr_del;
if((curr->next == NULL) || (strcmp(curr->name, name) == 0)) {
*user_ptr_del = curr->next;
}
else {
while(strcmp(curr->next->name, name) != 0) {
curr = curr->next;
}
curr->next = curr->next->next;
}
for (int i = 0; i < MAX_FRIENDS; i++) {
User *curr_friend = user->friends[i];
if (curr_friend == NULL) {
break;
}
int index = 0;
for(int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(curr_friend->friends[j]->name, name) == 0) {
index = j;
}
}
for (int k = index; k + 1 < MAX_FRIENDS; k++) {
curr_friend->friends[k] = curr_friend->friends[k+1];
}
if (index == MAX_FRIENDS - 1) {
curr_friend->friends[index] = NULL;
}
}
Post *curr_post = user->first_post;
while(curr_post != NULL) {
Post *p = curr_post;
curr_post = curr_post->next;
free(p->date);
free(p->contents);
free(p);
}
free(user);
return 0;
} | null | null |
63c648795f004bf4898cfcca | 2023-02-15T23:21:53.564Z | Question from Code | v1 | why segmentation fault | int delete_user(const char *name, User **user_ptr_del) {
User *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
User *curr = *user_ptr_del;
if((curr->next == NULL) || (strcmp(curr->name, name) == 0)) {
*user_ptr_del = curr->next;
}
else {
while(strcmp(curr->next->name, name) != 0) {
curr = curr->next;
}
curr->next = curr->next->next;
}
for (int i = 0; i < MAX_FRIENDS; i++) {
User *curr_friend = user->friends[i];
if (curr_friend == NULL) {
break;
}
int index = 0;
for(int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(curr_friend->friends[j]->name, name) == 0) {
index = j;
}
}
for (int k = index; k + 1 < MAX_FRIENDS; k++) {
curr_friend->friends[k] = curr_friend->friends[k+1];
}
if (index == MAX_FRIENDS - 1) {
curr_friend->friends[index] = NULL;
}
}
Post *curr_post = user->first_post;
while(curr_post != NULL) {
Post *p = curr_post;
curr_post = curr_post->next;
free(p->date);
free(p->contents);
free(p);
}
free(user);
return 0;
}
User *find_user(const char *name, const User *head) {
User *curr = (User *) head;
if (curr != NULL) {
while((strcmp(curr->name, name) != 0) && (curr != NULL)) {
curr = curr->next;
}
if (strcmp(curr->name, name) == 0) {
return curr;
}
}
return NULL;
} | null | null |
63c648795f004bf4898cfcca | 2023-02-16T00:14:48.728Z | Question from Code | v1 | why segmentation fault | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* 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) {
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("malloc");
exit(-1);
}
if (strlen(name) + 1 > MAX_NAME) {
return 2;
}
strncpy(new_user->name, name, MAX_NAME);
if(*user_ptr_add == NULL) {
*user_ptr_add = new_user;
}
else{
User *curr = *user_ptr_add;
while(curr->next != NULL) {
if (strcmp(curr->name, name) == 0) {
return 1;
}
curr = curr->next;
}
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;
if (curr != NULL) {
while((strcmp(curr->name, name) != 0) && (curr != NULL)) {
curr = curr->next;
}
if (strcmp(curr->name, name) == 0) {
return curr;
}
}
return NULL;
}
/*
* 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) {
User *new_curr = (User *) curr;
printf("User List\n");
while(new_curr != NULL) {
printf("\t%s\n", new_curr->name);
new_curr = new_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) + 1 > MAX_NAME) {
return 2;
}
FILE *file = fopen(filename, "r");
if (file == NULL) {
return 1;
}
strncpy(user->profile_pic, filename, MAX_NAME);
fclose(file);
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 *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
if ((friend1 == NULL) || friend2 == NULL) {
return 4;
}
if (strcmp(name1, name2) == 0) {
return 3;
}
int len_f1 = 0;
int len_f2 = 0;
for(int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == NULL) {
break;
}
if (strcmp(friend1->friends[i]->name, name2) == 0) {
return 1;
}
len_f1 += 1;
}
for(int i = 0; i < MAX_FRIENDS; i++) {
if (friend2->friends[i] == NULL) {
break;
}
len_f2 += 1;
}
if ((len_f1 == MAX_FRIENDS) || (len_f2 == MAX_FRIENDS)) {
return 2;
}
friend1->friends[len_f1] = friend2;
friend2->friends[len_f2] = friend1;
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) {
// FILE *file = fopen(user->profile_pic, "r");
// char *ch = malloc(sizeof(char));
// while(fread(ch, sizeof(char), 1, file) != 0) {
// printf("%s", ch);
// }
printf("Name: %s\n", user->name);
printf("------------------------------------------\n");
printf("Friends:\n");
for(int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] == NULL) {
break;
}
printf("%s\n", user->friends[i]->name);
}
printf("------------------------------------------\n");
printf("Posts:\n");
Post *curr = user->first_post;
while(curr != NULL) {
printf("From: %s\n", curr->author);
printf("Date: %s\n", ctime(curr->date));
printf("\n");
printf("%s\n", curr->contents);
if (curr->next == NULL) {
break;
}
printf("\n");
printf("===\n");
printf("\n");
curr = curr->next;
}
printf("------------------------------------------\n");
return 0;
}
return 1;
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if ((author == NULL) || (target == NULL)) {
return 2;
}
int found = 0;
for(int i = 0; i < MAX_FRIENDS; i++) {
if (target->friends[i] == NULL) {
break;
}
if (strcmp(target->friends[i]->name, author->name) == 0) {
found = 1;
break;
}
}
if (found == 0) {
return 1;
}
Post *new_post = malloc(sizeof(Post));
if (new_post == NULL) {
perror("malloc");
exit(-1);
}
strncpy(new_post->author, author->name, MAX_NAME);
new_post->contents = contents;
time_t *curr_datetime = malloc(sizeof(time_t));
if (curr_datetime == NULL) {
perror("malloc");
exit(-1);
}
*curr_datetime = time(NULL);
new_post->date = curr_datetime;
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 *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
User *curr = *user_ptr_del;
if((curr->next == NULL) || (strcmp(curr->name, name) == 0)) {
*user_ptr_del = curr->next;
}
else {
while(strcmp(curr->next->name, name) != 0) {
curr = curr->next;
}
curr->next = curr->next->next;
}
for (int i = 0; i < MAX_FRIENDS; i++) {
User *curr_friend = user->friends[i];
if (curr_friend == NULL) {
break;
}
int index = 0;
for(int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(curr_friend->friends[j]->name, name) == 0) {
index = j;
break;
}
}
for (int k = index; k + 1 < MAX_FRIENDS; k++) {
curr_friend->friends[k] = curr_friend->friends[k+1];
}
if (index == MAX_FRIENDS - 1) {
curr_friend->friends[index] = NULL;
}
}
Post *curr_post = user->first_post;
while(curr_post != NULL) {
Post *p = curr_post;
curr_post = curr_post->next;
free(p->date);
free(p->contents);
free(p);
}
free(user);
return 0;
}
| null | null |
63c648795f004bf4898cfcca | 2023-02-16T00:18:43.279Z | Question from Code | v1 | why segmentation fault | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* 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) {
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("malloc");
exit(-1);
}
if (strlen(name) + 1 > MAX_NAME) {
return 2;
}
strncpy(new_user->name, name, MAX_NAME);
if(*user_ptr_add == NULL) {
*user_ptr_add = new_user;
}
else{
User *curr = *user_ptr_add;
while(curr->next != NULL) {
if (strcmp(curr->name, name) == 0) {
return 1;
}
curr = curr->next;
}
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;
if (curr != NULL) {
while((strcmp(curr->name, name) != 0) && (curr != NULL)) {
curr = curr->next;
}
if (strcmp(curr->name, name) == 0) {
return curr;
}
}
return NULL;
}
/*
* 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) {
User *new_curr = (User *) curr;
printf("User List\n");
while(new_curr != NULL) {
printf("\t%s\n", new_curr->name);
new_curr = new_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) + 1 > MAX_NAME) {
return 2;
}
FILE *file = fopen(filename, "r");
if (file == NULL) {
return 1;
}
strncpy(user->profile_pic, filename, MAX_NAME);
fclose(file);
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 *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
if ((friend1 == NULL) || friend2 == NULL) {
return 4;
}
if (strcmp(name1, name2) == 0) {
return 3;
}
int len_f1 = 0;
int len_f2 = 0;
for(int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == NULL) {
break;
}
if (strcmp(friend1->friends[i]->name, name2) == 0) {
return 1;
}
len_f1 += 1;
}
for(int i = 0; i < MAX_FRIENDS; i++) {
if (friend2->friends[i] == NULL) {
break;
}
len_f2 += 1;
}
if ((len_f1 == MAX_FRIENDS) || (len_f2 == MAX_FRIENDS)) {
return 2;
}
friend1->friends[len_f1] = friend2;
friend2->friends[len_f2] = friend1;
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) {
// FILE *file = fopen(user->profile_pic, "r");
// char *ch = malloc(sizeof(char));
// while(fread(ch, sizeof(char), 1, file) != 0) {
// printf("%s", ch);
// }
printf("Name: %s\n", user->name);
printf("------------------------------------------\n");
printf("Friends:\n");
for(int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] == NULL) {
break;
}
printf("%s\n", user->friends[i]->name);
}
printf("------------------------------------------\n");
printf("Posts:\n");
Post *curr = user->first_post;
while(curr != NULL) {
printf("From: %s\n", curr->author);
printf("Date: %s\n", ctime(curr->date));
printf("\n");
printf("%s\n", curr->contents);
if (curr->next == NULL) {
break;
}
printf("\n");
printf("===\n");
printf("\n");
curr = curr->next;
}
printf("------------------------------------------\n");
return 0;
}
return 1;
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if ((author == NULL) || (target == NULL)) {
return 2;
}
int found = 0;
for(int i = 0; i < MAX_FRIENDS; i++) {
if (target->friends[i] == NULL) {
break;
}
if (strcmp(target->friends[i]->name, author->name) == 0) {
found = 1;
break;
}
}
if (found == 0) {
return 1;
}
Post *new_post = malloc(sizeof(Post));
if (new_post == NULL) {
perror("malloc");
exit(-1);
}
strncpy(new_post->author, author->name, MAX_NAME);
new_post->contents = contents;
time_t *curr_datetime = malloc(sizeof(time_t));
if (curr_datetime == NULL) {
perror("malloc");
exit(-1);
}
*curr_datetime = time(NULL);
new_post->date = curr_datetime;
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 *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
User *curr = *user_ptr_del;
if((curr->next == NULL) || (strcmp(curr->name, name) == 0)) {
*user_ptr_del = curr->next;
}
else {
while(strcmp(curr->next->name, name) != 0) {
curr = curr->next;
}
curr->next = curr->next->next;
}
for (int i = 0; i < MAX_FRIENDS; i++) {
User *curr_friend = user->friends[i];
if (curr_friend == NULL) {
break;
}
int index = 0;
for(int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(curr_friend->friends[j]->name, name) == 0) {
index = j;
break;
}
}
for (int k = index; k + 1 < MAX_FRIENDS; k++) {
curr_friend->friends[k] = curr_friend->friends[k+1];
}
if (index == MAX_FRIENDS - 1) {
curr_friend->friends[index] = NULL;
}
}
Post *curr_post = user->first_post;
while(curr_post != NULL) {
Post *p = curr_post;
curr_post = curr_post->next;
free(p->date);
free(p->contents);
free(p);
}
free(user);
return 0;
}
#define INPUT_BUFFER_SIZE 256
#define INPUT_ARG_MAX_NUM 12
#define DELIM " \n"
/*
* Print a formatted error message to stderr.
*/
void error(char *msg) {
fprintf(stderr, "Error: %s\n", msg);
}
/*
* Read and process commands
* Return: -1 for quit command
* 0 otherwise
*/
int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) {
User *user_list = *user_list_ptr;
if (cmd_argc <= 0) {
return 0;
} else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) {
return -1;
} else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) {
switch (create_user(cmd_argv[1], user_list_ptr)) {
case 1:
error("user by this name already exists");
break;
case 2:
error("username is too long");
break;
}
} else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) {
list_users(user_list);
} else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) {
User *user = find_user(cmd_argv[1], user_list);
if (user == NULL) {
error("user not found");
} else {
switch (update_pic(user, cmd_argv[2])) {
case 1:
error("file not found");
break;
case 2:
error("filename too long");
break;
}
}
} else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) {
if (delete_user(cmd_argv[1], user_list_ptr) == 1) {
error("user by this name does not exist");
}
} else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) {
switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) {
case 1:
error("users are already friends");
break;
case 2:
error("at least one user you entered has the max number of friends");
break;
case 3:
error("you must enter two different users");
break;
case 4:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) {
// first determine how long a string we need
int space_needed = 0;
for (int i = 3; i < cmd_argc; i++) {
space_needed += strlen(cmd_argv[i]) + 1;
}
// allocate the space
char *contents = malloc(space_needed);
// copy in the bits to make a single string
strcpy(contents, cmd_argv[3]);
for (int i = 4; i < cmd_argc; i++) {
strcat(contents, " ");
strcat(contents, cmd_argv[i]);
}
User *author = find_user(cmd_argv[1], user_list);
User *target = find_user(cmd_argv[2], user_list);
switch (make_post(author, target, contents)) {
case 1:
error("the users are not friends");
break;
case 2:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) {
User *user = find_user(cmd_argv[1], user_list);
if (print_user(user) == 1) {
error("user not found");
}
} else {
error("Incorrect syntax");
}
return 0;
}
/*
* Tokenize the string stored in cmd.
* Return the number of tokens, and store the tokens in cmd_argv.
*/
int tokenize(char *cmd, char **cmd_argv) {
int cmd_argc = 0;
char *next_token = strtok(cmd, DELIM);
while (next_token != NULL) {
if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) {
error("too many arguments!");
cmd_argc = 0;
break;
}
cmd_argv[cmd_argc] = next_token;
cmd_argc++;
next_token = strtok(NULL, DELIM);
}
return cmd_argc;
}
int main(int argc, char* argv[]) {
int batch_mode = (argc == 2);
char input[INPUT_BUFFER_SIZE];
FILE *input_stream;
// Create the heads of the empty data structure
User *user_list = NULL;
if (batch_mode) {
input_stream = fopen(argv[1], "r");
if (input_stream == NULL) {
perror("Error opening file");
exit(1);
}
} else {
// interactive mode
input_stream = stdin;
}
printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> ");
while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) {
// only echo the line in batch mode since in interactive mode the user
// just typed the line
if (batch_mode) {
printf("%s", input);
}
char *cmd_argv[INPUT_ARG_MAX_NUM];
int cmd_argc = tokenize(input, cmd_argv);
if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) {
break; // can only reach if quit command was entered
}
printf("> ");
}
if (batch_mode) {
fclose(input_stream);
}
// Delete all users. This should free all heap memory that was allocated
// during the run of the program. Uncomment after you've implemented
// deleting users.
printf("Freeing all heap space.\n");
while (user_list != NULL) {
delete_user(user_list->name, &user_list);
}
return 0;
}
| null | null |
63c648795f004bf4898cfcca | 2023-02-16T00:19:37.686Z | Question from Code | v1 | which line causes segmentation fault | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* 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) {
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("malloc");
exit(-1);
}
if (strlen(name) + 1 > MAX_NAME) {
return 2;
}
strncpy(new_user->name, name, MAX_NAME);
if(*user_ptr_add == NULL) {
*user_ptr_add = new_user;
}
else{
User *curr = *user_ptr_add;
while(curr->next != NULL) {
if (strcmp(curr->name, name) == 0) {
return 1;
}
curr = curr->next;
}
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;
if (curr != NULL) {
while((strcmp(curr->name, name) != 0) && (curr != NULL)) {
curr = curr->next;
}
if (strcmp(curr->name, name) == 0) {
return curr;
}
}
return NULL;
}
/*
* 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) {
User *new_curr = (User *) curr;
printf("User List\n");
while(new_curr != NULL) {
printf("\t%s\n", new_curr->name);
new_curr = new_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) + 1 > MAX_NAME) {
return 2;
}
FILE *file = fopen(filename, "r");
if (file == NULL) {
return 1;
}
strncpy(user->profile_pic, filename, MAX_NAME);
fclose(file);
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 *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
if ((friend1 == NULL) || friend2 == NULL) {
return 4;
}
if (strcmp(name1, name2) == 0) {
return 3;
}
int len_f1 = 0;
int len_f2 = 0;
for(int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == NULL) {
break;
}
if (strcmp(friend1->friends[i]->name, name2) == 0) {
return 1;
}
len_f1 += 1;
}
for(int i = 0; i < MAX_FRIENDS; i++) {
if (friend2->friends[i] == NULL) {
break;
}
len_f2 += 1;
}
if ((len_f1 == MAX_FRIENDS) || (len_f2 == MAX_FRIENDS)) {
return 2;
}
friend1->friends[len_f1] = friend2;
friend2->friends[len_f2] = friend1;
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) {
// FILE *file = fopen(user->profile_pic, "r");
// char *ch = malloc(sizeof(char));
// while(fread(ch, sizeof(char), 1, file) != 0) {
// printf("%s", ch);
// }
printf("Name: %s\n", user->name);
printf("------------------------------------------\n");
printf("Friends:\n");
for(int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] == NULL) {
break;
}
printf("%s\n", user->friends[i]->name);
}
printf("------------------------------------------\n");
printf("Posts:\n");
Post *curr = user->first_post;
while(curr != NULL) {
printf("From: %s\n", curr->author);
printf("Date: %s\n", ctime(curr->date));
printf("\n");
printf("%s\n", curr->contents);
if (curr->next == NULL) {
break;
}
printf("\n");
printf("===\n");
printf("\n");
curr = curr->next;
}
printf("------------------------------------------\n");
return 0;
}
return 1;
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if ((author == NULL) || (target == NULL)) {
return 2;
}
int found = 0;
for(int i = 0; i < MAX_FRIENDS; i++) {
if (target->friends[i] == NULL) {
break;
}
if (strcmp(target->friends[i]->name, author->name) == 0) {
found = 1;
break;
}
}
if (found == 0) {
return 1;
}
Post *new_post = malloc(sizeof(Post));
if (new_post == NULL) {
perror("malloc");
exit(-1);
}
strncpy(new_post->author, author->name, MAX_NAME);
new_post->contents = contents;
time_t *curr_datetime = malloc(sizeof(time_t));
if (curr_datetime == NULL) {
perror("malloc");
exit(-1);
}
*curr_datetime = time(NULL);
new_post->date = curr_datetime;
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 *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
User *curr = *user_ptr_del;
if((curr->next == NULL) || (strcmp(curr->name, name) == 0)) {
*user_ptr_del = curr->next;
}
else {
while(strcmp(curr->next->name, name) != 0) {
curr = curr->next;
}
curr->next = curr->next->next;
}
for (int i = 0; i < MAX_FRIENDS; i++) {
User *curr_friend = user->friends[i];
if (curr_friend == NULL) {
break;
}
int index = 0;
for(int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(curr_friend->friends[j]->name, name) == 0) {
index = j;
break;
}
}
for (int k = index; k + 1 < MAX_FRIENDS; k++) {
curr_friend->friends[k] = curr_friend->friends[k+1];
}
if (index == MAX_FRIENDS - 1) {
curr_friend->friends[index] = NULL;
}
}
Post *curr_post = user->first_post;
while(curr_post != NULL) {
Post *p = curr_post;
curr_post = curr_post->next;
free(p->date);
free(p->contents);
free(p);
}
free(user);
return 0;
}
#define INPUT_BUFFER_SIZE 256
#define INPUT_ARG_MAX_NUM 12
#define DELIM " \n"
/*
* Print a formatted error message to stderr.
*/
void error(char *msg) {
fprintf(stderr, "Error: %s\n", msg);
}
/*
* Read and process commands
* Return: -1 for quit command
* 0 otherwise
*/
int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) {
User *user_list = *user_list_ptr;
if (cmd_argc <= 0) {
return 0;
} else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) {
return -1;
} else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) {
switch (create_user(cmd_argv[1], user_list_ptr)) {
case 1:
error("user by this name already exists");
break;
case 2:
error("username is too long");
break;
}
} else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) {
list_users(user_list);
} else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) {
User *user = find_user(cmd_argv[1], user_list);
if (user == NULL) {
error("user not found");
} else {
switch (update_pic(user, cmd_argv[2])) {
case 1:
error("file not found");
break;
case 2:
error("filename too long");
break;
}
}
} else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) {
if (delete_user(cmd_argv[1], user_list_ptr) == 1) {
error("user by this name does not exist");
}
} else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) {
switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) {
case 1:
error("users are already friends");
break;
case 2:
error("at least one user you entered has the max number of friends");
break;
case 3:
error("you must enter two different users");
break;
case 4:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) {
// first determine how long a string we need
int space_needed = 0;
for (int i = 3; i < cmd_argc; i++) {
space_needed += strlen(cmd_argv[i]) + 1;
}
// allocate the space
char *contents = malloc(space_needed);
// copy in the bits to make a single string
strcpy(contents, cmd_argv[3]);
for (int i = 4; i < cmd_argc; i++) {
strcat(contents, " ");
strcat(contents, cmd_argv[i]);
}
User *author = find_user(cmd_argv[1], user_list);
User *target = find_user(cmd_argv[2], user_list);
switch (make_post(author, target, contents)) {
case 1:
error("the users are not friends");
break;
case 2:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) {
User *user = find_user(cmd_argv[1], user_list);
if (print_user(user) == 1) {
error("user not found");
}
} else {
error("Incorrect syntax");
}
return 0;
}
/*
* Tokenize the string stored in cmd.
* Return the number of tokens, and store the tokens in cmd_argv.
*/
int tokenize(char *cmd, char **cmd_argv) {
int cmd_argc = 0;
char *next_token = strtok(cmd, DELIM);
while (next_token != NULL) {
if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) {
error("too many arguments!");
cmd_argc = 0;
break;
}
cmd_argv[cmd_argc] = next_token;
cmd_argc++;
next_token = strtok(NULL, DELIM);
}
return cmd_argc;
}
int main(int argc, char* argv[]) {
int batch_mode = (argc == 2);
char input[INPUT_BUFFER_SIZE];
FILE *input_stream;
// Create the heads of the empty data structure
User *user_list = NULL;
if (batch_mode) {
input_stream = fopen(argv[1], "r");
if (input_stream == NULL) {
perror("Error opening file");
exit(1);
}
} else {
// interactive mode
input_stream = stdin;
}
printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> ");
while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) {
// only echo the line in batch mode since in interactive mode the user
// just typed the line
if (batch_mode) {
printf("%s", input);
}
char *cmd_argv[INPUT_ARG_MAX_NUM];
int cmd_argc = tokenize(input, cmd_argv);
if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) {
break; // can only reach if quit command was entered
}
printf("> ");
}
if (batch_mode) {
fclose(input_stream);
}
// Delete all users. This should free all heap memory that was allocated
// during the run of the program. Uncomment after you've implemented
// deleting users.
printf("Freeing all heap space.\n");
while (user_list != NULL) {
delete_user(user_list->name, &user_list);
}
return 0;
}
| null | null |
63c648795f004bf4898cfcca | 2023-02-16T00:21:16.389Z | Question from Code | v1 | which line causes segmentation fault | #include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* 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) {
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("malloc");
exit(-1);
}
if (strlen(name) + 1 > MAX_NAME) {
return 2;
}
strncpy(new_user->name, name, MAX_NAME);
if(*user_ptr_add == NULL) {
*user_ptr_add = new_user;
}
else{
User *curr = *user_ptr_add;
while(curr->next != NULL) {
if (strcmp(curr->name, name) == 0) {
return 1;
}
curr = curr->next;
}
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;
if (curr != NULL) {
while((strcmp(curr->name, name) != 0) && (curr != NULL)) {
curr = curr->next;
}
if (strcmp(curr->name, name) == 0) {
return curr;
}
}
return NULL;
}
/*
* 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) {
User *new_curr = (User *) curr;
printf("User List\n");
while(new_curr != NULL) {
printf("\t%s\n", new_curr->name);
new_curr = new_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) + 1 > MAX_NAME) {
return 2;
}
FILE *file = fopen(filename, "r");
if (file == NULL) {
return 1;
}
strncpy(user->profile_pic, filename, MAX_NAME);
fclose(file);
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 *friend1 = find_user(name1, head);
User *friend2 = find_user(name2, head);
if ((friend1 == NULL) || friend2 == NULL) {
return 4;
}
if (strcmp(name1, name2) == 0) {
return 3;
}
int len_f1 = 0;
int len_f2 = 0;
for(int i = 0; i < MAX_FRIENDS; i++) {
if (friend1->friends[i] == NULL) {
break;
}
if (strcmp(friend1->friends[i]->name, name2) == 0) {
return 1;
}
len_f1 += 1;
}
for(int i = 0; i < MAX_FRIENDS; i++) {
if (friend2->friends[i] == NULL) {
break;
}
len_f2 += 1;
}
if ((len_f1 == MAX_FRIENDS) || (len_f2 == MAX_FRIENDS)) {
return 2;
}
friend1->friends[len_f1] = friend2;
friend2->friends[len_f2] = friend1;
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) {
// FILE *file = fopen(user->profile_pic, "r");
// char *ch = malloc(sizeof(char));
// while(fread(ch, sizeof(char), 1, file) != 0) {
// printf("%s", ch);
// }
printf("Name: %s\n", user->name);
printf("------------------------------------------\n");
printf("Friends:\n");
for(int i = 0; i < MAX_FRIENDS; i++) {
if (user->friends[i] == NULL) {
break;
}
printf("%s\n", user->friends[i]->name);
}
printf("------------------------------------------\n");
printf("Posts:\n");
Post *curr = user->first_post;
while(curr != NULL) {
printf("From: %s\n", curr->author);
printf("Date: %s\n", ctime(curr->date));
printf("\n");
printf("%s\n", curr->contents);
if (curr->next == NULL) {
break;
}
printf("\n");
printf("===\n");
printf("\n");
curr = curr->next;
}
printf("------------------------------------------\n");
return 0;
}
return 1;
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if ((author == NULL) || (target == NULL)) {
return 2;
}
int found = 0;
for(int i = 0; i < MAX_FRIENDS; i++) {
if (target->friends[i] == NULL) {
break;
}
if (strcmp(target->friends[i]->name, author->name) == 0) {
found = 1;
break;
}
}
if (found == 0) {
return 1;
}
Post *new_post = malloc(sizeof(Post));
if (new_post == NULL) {
perror("malloc");
exit(-1);
}
strncpy(new_post->author, author->name, MAX_NAME);
new_post->contents = contents;
time_t *curr_datetime = malloc(sizeof(time_t));
if (curr_datetime == NULL) {
perror("malloc");
exit(-1);
}
*curr_datetime = time(NULL);
new_post->date = curr_datetime;
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 *user = find_user(name, *user_ptr_del);
if (user == NULL) {
return 1;
}
User *curr = *user_ptr_del;
if((curr->next == NULL) || (strcmp(curr->name, name) == 0)) {
*user_ptr_del = curr->next;
}
else {
while(strcmp(curr->next->name, name) != 0) {
curr = curr->next;
}
curr->next = curr->next->next;
}
for (int i = 0; i < MAX_FRIENDS; i++) {
User *curr_friend = user->friends[i];
if (curr_friend == NULL) {
break;
}
int index = 0;
for(int j = 0; j < MAX_FRIENDS; j++) {
if (strcmp(curr_friend->friends[j]->name, name) == 0) {
index = j;
break;
}
}
for (int k = index; k + 1 < MAX_FRIENDS; k++) {
curr_friend->friends[k] = curr_friend->friends[k+1];
}
if (index == MAX_FRIENDS - 1) {
curr_friend->friends[index] = NULL;
}
}
Post *curr_post = user->first_post;
while(curr_post != NULL) {
Post *p = curr_post;
curr_post = curr_post->next;
free(p->date);
// free(p->contents);
free(p);
}
free(user);
return 0;
}
#define INPUT_BUFFER_SIZE 256
#define INPUT_ARG_MAX_NUM 12
#define DELIM " \n"
/*
* Print a formatted error message to stderr.
*/
void error(char *msg) {
fprintf(stderr, "Error: %s\n", msg);
}
/*
* Read and process commands
* Return: -1 for quit command
* 0 otherwise
*/
int process_args(int cmd_argc, char **cmd_argv, User **user_list_ptr) {
User *user_list = *user_list_ptr;
if (cmd_argc <= 0) {
return 0;
} else if (strcmp(cmd_argv[0], "quit") == 0 && cmd_argc == 1) {
return -1;
} else if (strcmp(cmd_argv[0], "add_user") == 0 && cmd_argc == 2) {
switch (create_user(cmd_argv[1], user_list_ptr)) {
case 1:
error("user by this name already exists");
break;
case 2:
error("username is too long");
break;
}
} else if (strcmp(cmd_argv[0], "list_users") == 0 && cmd_argc == 1) {
list_users(user_list);
} else if (strcmp(cmd_argv[0], "update_pic") == 0 && cmd_argc == 3) {
User *user = find_user(cmd_argv[1], user_list);
if (user == NULL) {
error("user not found");
} else {
switch (update_pic(user, cmd_argv[2])) {
case 1:
error("file not found");
break;
case 2:
error("filename too long");
break;
}
}
} else if (strcmp(cmd_argv[0], "delete_user") == 0 && cmd_argc == 2) {
if (delete_user(cmd_argv[1], user_list_ptr) == 1) {
error("user by this name does not exist");
}
} else if (strcmp(cmd_argv[0], "make_friends") == 0 && cmd_argc == 3) {
switch (make_friends(cmd_argv[1], cmd_argv[2], user_list)) {
case 1:
error("users are already friends");
break;
case 2:
error("at least one user you entered has the max number of friends");
break;
case 3:
error("you must enter two different users");
break;
case 4:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "post") == 0 && cmd_argc >= 4) {
// first determine how long a string we need
int space_needed = 0;
for (int i = 3; i < cmd_argc; i++) {
space_needed += strlen(cmd_argv[i]) + 1;
}
// allocate the space
char *contents = malloc(space_needed);
// copy in the bits to make a single string
strcpy(contents, cmd_argv[3]);
for (int i = 4; i < cmd_argc; i++) {
strcat(contents, " ");
strcat(contents, cmd_argv[i]);
}
User *author = find_user(cmd_argv[1], user_list);
User *target = find_user(cmd_argv[2], user_list);
switch (make_post(author, target, contents)) {
case 1:
error("the users are not friends");
break;
case 2:
error("at least one user you entered does not exist");
break;
}
} else if (strcmp(cmd_argv[0], "profile") == 0 && cmd_argc == 2) {
User *user = find_user(cmd_argv[1], user_list);
if (print_user(user) == 1) {
error("user not found");
}
} else {
error("Incorrect syntax");
}
return 0;
}
/*
* Tokenize the string stored in cmd.
* Return the number of tokens, and store the tokens in cmd_argv.
*/
int tokenize(char *cmd, char **cmd_argv) {
int cmd_argc = 0;
char *next_token = strtok(cmd, DELIM);
while (next_token != NULL) {
if (cmd_argc >= INPUT_ARG_MAX_NUM - 1) {
error("too many arguments!");
cmd_argc = 0;
break;
}
cmd_argv[cmd_argc] = next_token;
cmd_argc++;
next_token = strtok(NULL, DELIM);
}
return cmd_argc;
}
int main(int argc, char* argv[]) {
int batch_mode = (argc == 2);
char input[INPUT_BUFFER_SIZE];
FILE *input_stream;
// Create the heads of the empty data structure
User *user_list = NULL;
if (batch_mode) {
input_stream = fopen(argv[1], "r");
if (input_stream == NULL) {
perror("Error opening file");
exit(1);
}
} else {
// interactive mode
input_stream = stdin;
}
printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> ");
while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) {
// only echo the line in batch mode since in interactive mode the user
// just typed the line
if (batch_mode) {
printf("%s", input);
}
char *cmd_argv[INPUT_ARG_MAX_NUM];
int cmd_argc = tokenize(input, cmd_argv);
if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) {
break; // can only reach if quit command was entered
}
printf("> ");
}
if (batch_mode) {
fclose(input_stream);
}
// Delete all users. This should free all heap memory that was allocated
// during the run of the program. Uncomment after you've implemented
// deleting users.
printf("Freeing all heap space.\n");
while (user_list != NULL) {
delete_user(user_list->name, &user_list);
}
return 0;
}
| null | null |
63c648795f004bf4898cfcca | 2023-03-15T20:56:58.249Z | Question from Code | v2 | why segmentation fault? | Rule *parse_file(FILE *fp) {
Rule *ds = malloc(sizeof(Rule));
char *line = malloc(sizeof(char) * MAXLINE);
while (fgets(line, MAXLINE, fp)) {
if (!is_comment_or_empty(line) && line[0] != '\n') {
char *line_cpy = malloc(sizeof(char) * MAXLINE);
strcpy(line_cpy, line);
char *ch = " \t";
// Array length
char *curr_token1 = strtok(line_cpy, ch);
int len = 1;
while (curr_token1 != NULL) {
curr_token1 = strtok(NULL, ch);
len += 1;
}
char **arr = malloc(sizeof(char *) * len);
// Array
char *curr_token2 = strtok(line, ch);
arr[0] = curr_token2;
int i = 1;
while (curr_token2 != NULL) {
curr_token2 = strtok(NULL, ch);
arr[i] = curr_token2;
i += 1;
}
// Rules
else {
Rule *curr_rule = malloc(sizeof(Rule));
curr_rule->target = malloc(sizeof(char) * MAXLINE);
strcpy(curr_rule->target, arr[0]);
if (ds == NULL) {
ds = curr_rule;
} else {
Rule *prev_rule = ds;
while (prev_rule->next_rule != NULL) {
prev_rule = prev_rule->next_rule;
}
prev_rule->next_rule = curr_rule;
}
}
}
}
return ds;
}
int is_comment_or_empty(const char *line) {
for (int i = 0; i < strlen(line); i++) {
if (line[i] == '#') {
return 1;
}
if (line[i] != '\t' && line[i] != ' ') {
return 0;
}
}
return 1;
} | null | null |
63c648795f004bf4898cfcca | 2023-03-15T20:59:41.610Z | Question from Code | v2 | which line gives segmentation fault? | Rule *parse_file(FILE *fp) {
Rule *ds = malloc(sizeof(Rule));
char *line = malloc(sizeof(char) * MAXLINE);
while (fgets(line, MAXLINE, fp)) {
if (!is_comment_or_empty(line) && line[0] != '\n') {
char *line_cpy = malloc(sizeof(char) * MAXLINE);
strcpy(line_cpy, line);
char *ch = " \t";
// Array length
char *curr_token1 = strtok(line_cpy, ch);
int len = 1;
while (curr_token1 != NULL) {
curr_token1 = strtok(NULL, ch);
len += 1;
}
char **arr = malloc(sizeof(char *) * len);
// Array
char *curr_token2 = strtok(line, ch);
arr[0] = curr_token2;
int i = 1;
while (curr_token2 != NULL) {
curr_token2 = strtok(NULL, ch);
arr[i] = curr_token2;
i += 1;
}
// Rules
else {
Rule *curr_rule = malloc(sizeof(Rule));
curr_rule->target = malloc(sizeof(char) * MAXLINE);
strcpy(curr_rule->target, arr[0]);
if (ds == NULL) {
ds = curr_rule;
} else {
Rule *prev_rule = ds;
while (prev_rule->next_rule != NULL) {
prev_rule = prev_rule->next_rule;
}
prev_rule->next_rule = curr_rule;
}
}
}
}
return ds;
}
int is_comment_or_empty(const char *line) {
for (int i = 0; i < strlen(line); i++) {
if (line[i] == '#') {
return 1;
}
if (line[i] != '\t' && line[i] != ' ') {
return 0;
}
}
return 1;
} | null | null |
63c648795f004bf4898cfccf | 2023-01-25T22:19:13.016Z | General Question | v1 | What is the type of the address of a pointer | null | null | null |
63c648795f004bf4898cfccf | 2023-01-25T22:20:45.370Z | General Question | v1 | what is malloc used for? | null | null | null |
63c648795f004bf4898cfccf | 2023-01-25T22:29:13.316Z | Question from Code | v1 | Explain why this code gives segmentation fault | void fib(int **seq, int n) {
*seq = malloc(sizeof(int *) * n);
*seq[0] = 0;
*seq[1] = 1;
for(int i=2;i<n;i++) {
*seq[i] = *seq[i-1] + *seq[i-2];
}
}
| null | null |
63c648795f004bf4898cfccf | 2023-01-25T22:32:48.767Z | Question from Code | v1 | How to fix this code? | void fib(int **seq, int n) {
*seq = malloc(sizeof(int *) * n);
*seq[0] = 0;
*seq[1] = 1;
for(int i=2;i<n;i++) {
*seq[i] = *seq[i-1] + *seq[i-2];
}
}
| null | null |
63c648795f004bf4898cfccf | 2023-01-26T01:18:45.500Z | Question from Code | v1 | Why does this give segmentation fault? | #include <stdio.h>
#include <stdlib.h>
int **split_array(const int *s, int length) {
int **pt=NULL;
*pt = malloc(sizeof(int *) * 2);
pt[0] = malloc(sizeof(int) * (length - (length / 2)) );
pt[1] = malloc(sizeof(int) * (length / 2));
int j=0;
int k=0;
for(int i=0;i<length;i++){
if(i % 2 ==0) {
pt[0][j] = s[i];
j +=1;
} else {
pt[1][k] = s[i];
k +=1;
}
}
return pt;
}
int *build_array(char **strs, int size) {
int arr[size];
for(int i=0;i<size;i++){
arr[i] = strtol(strs[i], NULL, 10);
}
int *pt = arr;
return pt;
}
int main(int argc, char **argv) {
int *full_array = build_array(argv,argc-1);
int **result = split_array(full_array, argc-1);
printf("Original array:\n");
for (int i = 0; i < argc - 1; i++) {
printf("%d ", full_array[i]);
}
printf("\n");
printf("result[0]:\n");
for (int i = 0; i < argc / 2; i++) {
printf("%d ", result[0][i]);
}
printf("\n");
printf("result[1]:\n");
for (int i = 0; i < (argc - 1) / 2; i++) {
printf("%d ", result[1][i]);
}
printf("\n");
free(full_array);
free(result[0]);
free(result[1]);
free(result);
return 0;
}
| null | null |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.