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
⌀ |
---|---|---|---|---|---|---|---|
63c647dc5f004bf4898cf752 | 2023-03-02T02:46:05.301Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
break;
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
} | Only the original parent calls fork and the new children do not create additional processes. | null |
63c647dc5f004bf4898cf752 | 2023-03-02T02:47:27.471Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| each process creates exactly one a new process. Keep the printf call for all processes. | null |
63c647dc5f004bf4898cf752 | 2023-03-02T02:48:12.770Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| Each fork process creates exactly one new fork process. | null |
63c647dc5f004bf4898cf752 | 2023-03-02T02:53:32.723Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
if (n > 0){
break;
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| The original parent calls fork and new children do not create additional processes | null |
63c647dc5f004bf4898cf752 | 2023-03-02T03:10:10.287Z | Help Fix Code | v1 | null | int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| Modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. The resulting diagram will look something like the following when childcreates 3 is called:
414 -> 416 -> 417 -> 420 | null |
63c647dc5f004bf4898cf752 | 2023-03-02T03:10:54.147Z | Help Fix Code | v1 | null | int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
break;
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| Modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. The resulting diagram will look something like the following when childcreates 3 is called: 414 -> 416 -> 417 -> 420 | null |
63c647dc5f004bf4898cf752 | 2023-03-02T03:11:42.674Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
if (n == 0){
break;
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| Modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. The resulting diagram will look something like the following when childcreates 3 is called: 414 -> 416 -> 417 -> 420 | null |
63c647dc5f004bf4898cf752 | 2023-03-02T03:12:32.903Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
if (n == 0){
break;
}
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
return 0;
}
| Modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. The resulting diagram will look something like the following when childcreates 3 is called: 414 -> 416 -> 417 -> 420 | null |
63c647dc5f004bf4898cf752 | 2023-03-02T18:46:27.667Z | General Question | v1 | What is the syntax for wait in a parent call that waits for the child fork call to complete? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-02T18:58:46.152Z | Question from Code | v1 | Why are all pid the same? | int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
printf("ppid = %d, pid = %d\n", getppid(), getpid());
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
if (n == 0){
break;
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-02T19:07:24.872Z | Help Fix Code | v1 | null | int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
printf("ppid = %d, pid = %d\n", getppid(), getpid());
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
if (n > 0){
break;
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| have the parent wait for all children forks to finish using the wait call. | null |
63c647dc5f004bf4898cf752 | 2023-03-02T19:08:09.273Z | Help Fix Code | v1 | null | int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
printf("ppid = %d, pid = %d\n", getppid(), getpid());
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
if (n > 0){
wait(NULL);
break;
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| have the parent wait for all children forks to finish using the wait call. | null |
63c647dc5f004bf4898cf752 | 2023-03-02T19:08:55.885Z | Help Fix Code | v1 | null | int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
printf("ppid = %d, pid = %d\n", getppid(), getpid());
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
if (n > 0){
break;
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
wait(NULL);
return 0;
}
| have the parent wait for all children forks to finish using the wait call. | null |
63c647dc5f004bf4898cf752 | 2023-03-02T19:13:01.995Z | Question from Code | v1 | Why does all children fork have the same pid? | int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
printf("ppid = %d, pid = %d\n", getppid(), getpid());
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
if (n == 0){
break;
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
wait(NULL);
return 0;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-02T19:15:42.677Z | Help Fix Code | v1 | null | int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
printf("ppid = %d, pid = %d\n", getppid(), getpid());
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
if (n == 0){
break;
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
wait(NULL);
return 0;
}
| the command line argument is the number of forks done by this program. Have it such that all the forks have different process ids but are forked from the same parent (have the same ppid). | null |
63c647dc5f004bf4898cf752 | 2023-03-02T19:32:05.736Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
printf("ppid = %d, pid = %d\n", getppid(), getpid());
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
if (n > 0){
exit(0);
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| In the new file, modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. Add the appropriate wait calls to ensure that each parent does not terminate before its children. | null |
63c647dc5f004bf4898cf752 | 2023-03-02T19:34:05.971Z | Help Fix Code | v1 | null | #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: forkloop <iterations>\n");
exit(1);
}
int iterations = strtol(argv[1], NULL, 10);
printf("ppid = %d, pid = %d\n", getppid(), getpid());
for (int i = 0; i < iterations; i++) {
int n = fork();
if (n < 0) {
perror("fork");
exit(1);
}
if (n > 0){
wait(NULL);
exit(0);
}
printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i);
}
return 0;
}
| In the new file, modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. Add the appropriate wait calls to ensure that each parent does not terminate before its children. | null |
63c647dc5f004bf4898cf752 | 2023-03-08T18:13:32.328Z | General Question | v1 | What is the default standard input? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-08T18:30:50.901Z | General Question | v1 | how do I run a c file validate.c and have it return the return code to stdout? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-08T18:37:11.436Z | General Question | v1 | when running a compiled file using the ./ command, how can I have the exit code in stdout? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-08T20:34:36.926Z | General Question | v1 | How to fork into another file in c? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-09T20:12:49.404Z | Help Fix Code | v2 | This program should take in two arguments from stdin, then write them to a pipe. Then, have fork the program and have the child process call the program ./validate with the two arguments from the pipe. Depending on the exit code of ./validate, the program should output SUCCESS, INVALID or NO_USER. |
int fd[2];
if(pipe(fd) = -1){
perror("pipe");
}
int status;
int n = fork();
if(n < 0){
perror("fork");
exit(1);
} else if (n > 0){
close(fd[0]);
if (write(fd[1], user_id, MAXLINE) == -1){
perror("write to pipe");
}
if (write(fd[1], password, MAXLINE) == -1){
perror("write to pipe");
}
close(fd[1]);
if (wait(&status) == -1){
perror("wait");
exit(1);
}
if (WIFEXITED(status)){
int child_status = WEXITSTATUS(status);
if (child_status == 0){
printf(SUCCESS);
} else if (child_status == 2){
printf(INVALID);
} else {
printf(NO_USER);
}
}
} else if (n == 0){
close(fd[1]);
execl("./validate", "validate", NULL);
read(fd[0], STDIN_FILENO, MAXLINE);
read(fd[0], STDIN_FILENO, MAXLINE);
execl("echo $?", "echo", "$?", NULL);
close(fd[0]);
exit(0);
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-09T20:22:54.029Z | Help Fix Code | v2 | The program should use the exit status of the validate program to determine which of the three following messages to print:
"Password verified" the exit code is 0.
"Invalid password" if the exit code is 2.
NO_USER otherwise.
Currently, the problem is that my program returns NO_
USER even if the exit code should be 0. | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define MAX_PASSWORD 10
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
//TODO
if((strlen(user_id) > MAX_PASSWORD) || strlen(password) > MAX_PASSWORD){
printf(NO_USER);
}
int fd[2];
if(pipe(fd) == -1){
perror("pipe");
}
int status;
int n = fork();
if(n < 0){
perror("fork");
exit(1);
} else if (n > 0){
close(fd[0]);
if (write(fd[1], user_id, MAXLINE) == -1){
perror("write to pipe");
}
if (write(fd[1], password, MAXLINE) == -1){
perror("write to pipe");
}
close(fd[1]);
if (wait(&status) == -1){
perror("wait");
exit(1);
}
if (WIFEXITED(status)){
int child_status = WEXITSTATUS(status);
if (child_status == 0){
printf(SUCCESS);
} else if (child_status == 2){
printf(INVALID);
} else {
printf(NO_USER);
}
}
} else if (n == 0){
close(fd[1]);
execl("./validate", "validate", NULL);
dup2(fd[0], STDIN_FILENO);
dup2(fd[0], STDIN_FILENO);
execl("echo $?", "echo", "$?", NULL);
close(fd[0]);
exit(0);
}
return 0;
}
1,18 Top
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-09T20:30:21.136Z | Question from Code | v2 | Why is the code asking me for 4 stdin inputs when there should only be 2 stdin inputs? | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define MAX_PASSWORD 10
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
//TODO
if((strlen(user_id) > MAX_PASSWORD) || strlen(password) > MAX_PASSWORD){
printf(NO_USER);
}
int fd[2];
if(pipe(fd) == -1){
perror("pipe");
}
int status;
int n = fork();
if(n < 0){
perror("fork");
exit(1);
} else if (n > 0){
close(fd[0]);
if (write(fd[1], user_id, MAXLINE) == -1){
perror("write to pipe");
}
if (write(fd[1], password, MAXLINE) == -1){
perror("write to pipe");
}
close(fd[1]);
if (wait(&status) == -1){
perror("wait");
exit(1);
}
if (WIFEXITED(status)){
int child_status = WEXITSTATUS(status);
if (child_status == 0){
printf(SUCCESS);
} else if (child_status == 2){
printf(INVALID);
} else {
printf(NO_USER);
}
}
} else if (n == 0){
close(fd[1]);
execl("./validate", "validate", NULL);
dup2(fd[0], STDIN_FILENO);
dup2(fd[0], STDIN_FILENO);
execl("echo $?", "echo", "$?", NULL);
close(fd[0]);
exit(0);
}
return 0;
}
1,1 Top
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-09T20:49:11.928Z | General Question | v2 | How to stop fork from rerunning fgets | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-09T22:43:59.049Z | Help Fix Code | v2 | The code is returning invalid even when the exit code of validate is 0 | int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
//TODO
if((strlen(user_id) > MAX_PASSWORD) || strlen(password) > MAX_PASSWORD){
printf(NO_USER);
exit(1);
}
int fd[2];
if(pipe(fd) == -1){
perror("pipe");
}
int status;
int n = fork();
if(n < 0){
perror("fork");
exit(1);
} else if (n > 0){
close(fd[0]);
if (write(fd[1], user_id, MAXLINE) == -1){
perror("write to pipe");
}
if (write(fd[1], password, MAXLINE) == -1){
perror("write to pipe");
}
close(fd[1]);
if (wait(&status) == -1){
perror("wait");
exit(1);
}
if (WIFEXITED(status)){
int child_status = WEXITSTATUS(status);
printf("%d", child_status);
if (child_status == 0){
printf(SUCCESS);
} else if (child_status == 2){
printf(INVALID);
} else {
printf(NO_USER);
}
}
} else if (n == 0){
close(fd[1]);
// execl("./validate", "validate", NULL);
dup2(fd[0], STDIN_FILENO);
dup2(fd[0], STDIN_FILENO);
execl("./validate", "validate", NULL);
execl("echo $?", "echo", "$?", NULL);
close(fd[0]);
exit(0);
}
return 0;
}
14,0-1 21%
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-10T00:08:29.940Z | Explain Code | v2 | null | int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
//TODO
if((strlen(user_id) > MAX_PASSWORD) || strlen(password) > MAX_PASSWORD){
printf(NO_USER);
exit(1);
}
int fd[2];
if(pipe(fd) == -1){
perror("pipe");
}
int status;
int n = fork();
if(n < 0){
perror("fork");
exit(1);
} else if (n > 0){
close(fd[0]);
if (write(fd[1], user_id, MAXLINE) == -1){
perror("write to pipe");
}
if (write(fd[1], password, MAXLINE) == -1){
perror("write to pipe");
}
close(fd[1]);
if (wait(&status) == -1){
perror("wait");
exit(1);
}
if (WIFEXITED(status)){
int child_status = WEXITSTATUS(status);
printf("%d", child_status);
if (child_status == 0){
printf(SUCCESS);
} else if (child_status == 2){
printf(INVALID);
} else {
printf(NO_USER);
}
}
} else if (n == 0){
close(fd[1]);
// execl("./validate", "validate", NULL);
dup2(fd[0], STDIN_FILENO);
dup2(fd[0], STDIN_FILENO);
execl("./validate", "validate", NULL);
execl("echo $?", "echo", "$?", NULL);
close(fd[0]);
exit(0);
}
return 0;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-10T19:44:19.579Z | General Question | v2 | If there are two things in the pipe do you use dup2 once or twice? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-10T22:25:17.693Z | Help Fix Code | v2 | Initialize memory for fd[1], however, the code is giving me an error as expected ‘void *’ but argument is of type ‘int’
| int fd[2];
if(pipe(fd) == -1){
perror("pipe");
}
memset(fd[1], MAX_PASSWORD * 2, sizeof(char)); | null | null |
63c647dc5f004bf4898cf752 | 2023-03-10T22:29:35.815Z | Help Write Code | v2 | null | null | null | How to deference fd and get the first element of it? |
63c647dc5f004bf4898cf752 | 2023-03-10T22:30:24.880Z | General Question | v2 | *memset (void *__s, int __c, size_t __n) __THROW __nonnull ((1)); what does this mean? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-10T22:31:36.554Z | Help Write Code | v2 | null | null | null | int fd[2];
if(pipe(fd) == -1){
perror("pipe");
}
Given the above code, use memset to allocate space of 2 *sizeof(char) for the first element in fd. |
63c647dc5f004bf4898cf752 | 2023-03-10T22:32:32.173Z | Help Write Code | v2 | null | null | null | int fd[2];
if(pipe(fd) == -1){
perror("pipe");
}
Given the above code, use memset to allocate space of 2*MAX_PASSWORD *sizeof(char) for the second element in fd.
|
63c647dc5f004bf4898cf752 | 2023-03-10T22:36:02.138Z | General Question | v2 | What does this error mean? /usr/include/string.h:61:14: note: declared here | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T14:11:21.448Z | Help Write Code | v2 | null | null | null | How to parse through a line like "main : linked_list.o main.o" to get all the dependencies and target? |
63c647dc5f004bf4898cf752 | 2023-03-13T14:31:57.680Z | Help Write Code | v2 | null | null | null | How can I name a variable with the counter I? For example, if I want string1, string2 and string3 when i=1,2,3 respectively? |
63c647dc5f004bf4898cf752 | 2023-03-13T14:34:50.451Z | General Question | v2 | error: variable-sized object may not be initialized
30 | Dependency *dep[i] = malloc(sizeof(Dependency));
Why is the compiler giving me this error? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T14:41:27.484Z | Help Write Code | v2 | null | null | null | How to parse through a line like "main : linked_list.o main.o" to get all the dependencies and target (where the number of dependencies is unknown), while mallocing space for each dependency with a data type dependency, where dependency->name be the name of the dependency? |
63c647dc5f004bf4898cf752 | 2023-03-13T14:43:42.402Z | Help Fix Code | v2 | I want a Dependency and a Rule for every i value | int i = 1;
while (token != NULL){
Dependency *dep[i] = malloc(sizeof(Dependency));
if (dep[i] == NULL){
perror("malloc");
exit(1);
}
Rule *rule[i] = malloc(sizeof(Rule));
if (rule[i] == NULL){
perror("malloc");
exit(1);
}
dep[i]->rule_node = rule[i];
i++;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T15:29:54.634Z | Help Fix Code | v2 | If I have multiple tokens, how can I fix this code such that I can easily access the previous dep and rule spaces? | token = strtok(line, " ");
while (token != NULL){
Dependency *dep = malloc(sizeof(Dependency));
if (dep == NULL){
perror("malloc");
exit(1);
}
Rule *rule = malloc(sizeof(Rule));
if (rule == NULL){
perror("malloc");
exit(1);
}
rule->target = token;
dep->rule = rule;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T15:30:38.529Z | Help Write Code | v2 | null | null | null | How to add code to go to the next token? |
63c647dc5f004bf4898cf752 | 2023-03-13T16:18:59.054Z | General Question | v2 | How to check if the first character in a string is a tab? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T16:35:50.906Z | General Question | v2 | What are arguements in make files? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T16:39:03.415Z | Question from Code | v2 | WHat would the output of this code be if
args = "gcc -Wall -g -std=gnu99 -o main linked_list.o main.o" | char *args_to_string(char **args, char *buffer, int size) {
buffer[0] = '\0';
int i = 0;
while (args[i] != NULL) {
strncat(buffer, args[i], size - strlen(buffer));
strncat(buffer, " ", size - strlen(buffer));
i++;
}
return buffer;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T16:41:47.665Z | Question from Code | v2 | WHat would the output of this code be if args = "main : linked_list.o main.o" | char *args_to_string(char **args, char *buffer, int size) {
buffer[0] = '\0';
int i = 0;
while (args[i] != NULL) {
strncat(buffer, args[i], size - strlen(buffer));
strncat(buffer, " ", size - strlen(buffer));
i++;
}
return buffer;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T16:56:11.999Z | General Question | v2 | How to fix this code
error: lvalue required as unary ‘&’ operand
60 | char **argument = &strtok(line, " "); | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T16:58:20.250Z | Question from Code | v2 | How to fix the error
error: lvalue required as unary ‘&’ operand
60 | char **argument = &(strtok(line, " ")); | fgets(line, MAXLINE, fp);
if (strstr(line, "-o") != NULL) {
strtok(line, " ");
Action *action = malloc(sizeof(Action));
char **argument = &(strtok(line, " "));
action->args = argument;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T17:04:13.030Z | General Question | v2 | How to fix this error
parse.c:24:22: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [-Wint-conversion]
24 | if (strchr(line, "\t") != line){
| ^~~~
| |
| char * | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T17:33:02.344Z | Help Fix Code | v2 | Read from the open file fp, and create the linked data structure that creates a Dependency node, Rule node and Action node for each dependency, rule and action.
The code is currently giving me a no stack, malloc error when I try to run it in gdb, how do I fix that?
| Rule *parse_file(FILE *fp) {
// Implement this function and remove the stubbed return statement below.
Rule *head = malloc(sizeof(Rule));
if (head == NULL){
perror("malloc");
exit(1);
}
char line[MAXLINE];
while(fgets(line, MAXLINE, fp) != NULL){
// checking if the first character is a tab to determine whether it is a target line or an action line
if (strchr(line, "\t") != line){
if (is_comment_or_empty(line) == 0){
head->target = strtok(line, " : ");
Rule *prev_rule = head;
Dependency *prev_dep;
char *token;
token = strtok(line, " ");
while (token != NULL){
Dependency *dep = malloc(sizeof(Dependency));
if (dep == NULL){
perror("malloc");
exit(1);
}
Rule *rule = malloc(sizeof(Rule));
if (rule == NULL){
perror("malloc");
exit(1);
}
prev_rule->dependencies = dep;
prev_rule->next_rule = rule;
rule->target = token;
dep->rule = rule;
// change prev_rule and prev_dep to current dep and rule
prev_rule = rule;
prev_dep = dep;
token = strtok(line, " ");
}
}
} else { // the first character is a tab
if (is_comment_or_empty(line) == 0){
if((strstr(line, "-o") != NULL) || (strstr(line, "-c") != NULL)){
strtok(line, " ");
Action *action = malloc(sizeof(Action));
char *argument = strtok(line, " ");
action->args = &argument;
}
}
}
}
return NULL;
}
typedef struct action_node {
char **args; // An array of strings suitable to be passed to execvp
struct action_node *next_act;
} Action;
typedef struct dep_node {
struct rule_node *rule;
struct dep_node *next_dep;
} Dependency;
typedef struct rule_node {
char *target;
Dependency *dependencies;
Action *actions;
struct rule_node *next_rule;
} Rule;
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T17:36:41.158Z | Help Fix Code | v2 | This code is giving me a malloc: Cannot allocate memory error | Rule *parse_file(FILE *fp) {
// Implement this function and remove the stubbed return statement below.
Rule *head = malloc(sizeof(Rule));
if (head == NULL){
perror("malloc");
exit(1);
}
char line[MAXLINE];
while(fgets(line, MAXLINE, fp) != NULL){
// checking if the first character is a tab to determine whether it is a target line or an action line
if (strchr(line, "\t") != line){
if (is_comment_or_empty(line) == 0){
head->target = strtok(line, " : ");
Rule *prev_rule = head;
Dependency *prev_dep;
char *token;
token = strtok(line, " ");
while (token != NULL){
Dependency *dep = malloc(sizeof(Dependency));
if (dep == NULL){
perror("malloc");
exit(1);
}
Rule *rule = malloc(sizeof(Rule));
if (rule == NULL){
perror("malloc");
exit(1);
}
prev_rule->dependencies = dep;
prev_rule->next_rule = rule;
rule->target = token;
dep->rule = rule;
// change prev_rule and prev_dep to current dep and rule
prev_rule = rule;
prev_dep = dep;
token = strtok(line, " ");
}
}
} else { // the first character is a tab
if (is_comment_or_empty(line) == 0){
if((strstr(line, "-o") != NULL) || (strstr(line, "-c") != NULL)){
strtok(line, " ");
Action *action = malloc(sizeof(Action));
char *argument = strtok(line, " ");
action->args = &argument;
}
}
}
}
return head;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T17:40:01.691Z | General Question | v2 | What is the code to check where the memory error happened with valgrind | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T17:47:01.178Z | General Question | v2 | What does memset do and how do I use it? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T18:13:29.405Z | General Question | v2 | How to fix this warning?
warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [-Wint-conversion]
8 | FILE *fp = fopen(argv[1], "r");
| ~~~~^~~
| null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-13T20:27:04.592Z | General Question | v2 | How to set action->args[1] to be arg_name with open and close quotation marks ""? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-14T11:55:31.270Z | Question from Code | v2 |
What does the program print to stderr when it runs without interruption? | int x = 5;
void handler(int sig) {
x += 3;
fprintf(stderr, "inside %d ", x);
}
int main() {
fprintf(stderr, "start ");
struct sigaction act;
act.sa_handler = handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGINT, &act, NULL);
x += 2;
fprintf(stderr, "outside %d", x);
return 0;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-14T15:36:26.810Z | General Question | v2 | How do I fix this error?
Invalid read of size 4
==13752== at 0x4900657: fgets (iofgets.c:47)
==13752== by 0x1094B2: parse_file (parse.c:17)
==13752== by 0x10984B: main (parse.c:197)
==13752== Address 0x0 is not stack'd, malloc'd or (recently) free'd
This is line 17: while(fgets(line, MAXLINE, fp) != NULL){ | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-14T19:10:53.550Z | Help Fix Code | v2 | Why is this chuck of code giving me a segmentation fault and how do I fix it? | int found = 0;
Rule *dep_rule = head;
while(dep_rule != NULL){
if(strcmp(dep_rule->target, dep_name)==0){
dep->rule = dep_rule;
found = 1;
break;
}
dep_rule = dep_rule->next_rule;
}
if(found == 0){
dep_rule = malloc(sizeof(Rule));
if(dep_rule == NULL){
perror("malloc");
exit(1);
}
dep_rule->target = malloc(sizeof(char) * strlen(dep_name));
if(dep_rule->target == NULL){
perror("malloc");
exit(1);
}
strncpy(dep_rule->target, dep_name, strlen(dep_name));
dep_rule->dependencies = NULL;
dep_rule->actions = NULL;
dep_rule->next_rule = NULL;
} | null | null |
63c647dc5f004bf4898cf752 | 2023-03-14T19:13:35.988Z | Help Fix Code | v2 | Why is this code giving me a segmentation fault at the following line:
if(dep->rule->target == NULL) { | int found = 0;
Rule *dep_rule = head;
while(dep_rule != NULL){
if(strcmp(dep_rule->target, dep_name)==0){
dep->rule = dep_rule;
found = 1;
break;
}
dep_rule = dep_rule->next_rule;
}
if(found == 0){
dep_rule = malloc(sizeof(Rule));
if(dep_rule == NULL){
perror("malloc");
exit(1);
}
dep_rule->target = malloc(sizeof(char) * strlen(dep_name));
if(dep_rule->target == NULL){
perror("malloc");
exit(1);
}
strncpy(dep_rule->target, dep_name, strlen(dep_name));
dep_rule->dependencies = NULL;
dep_rule->actions = NULL;
dep_rule->next_rule = NULL;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-15T00:26:59.944Z | Help Write Code | v2 | null | null | null | Make sure that the last character of a string is not \n, if it is \n, delete \n. |
63c647dc5f004bf4898cf752 | 2023-03-15T00:34:38.397Z | Question from Code | v2 | what might be the reasons of my output missing one line after clearn : | main : linked_list.o main.o
gcc -
Wall - g - std = gnu99 -
o main linked_list.o main
.o
main.o : linked_list.h
gcc -
Wall - g - std = gnu99 -
c main
.c
linked_list.o
: linked_list.c linked_list.h
gcc - Wall - g - std = gnu99 -
c linked_list.c
clean : rm -
f linked_list.o
rm -
f main.o
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-15T13:28:34.149Z | Help Write Code | v2 | null | null | null | Use stat to get the last modified time (mtime) of a file. Read the man page (using man 2 stat) to learn how to call stat. The field of the stat struct that you want is called st_mtim on Linux.
The struct timespec st_mtim; has two fields, one for seconds (tv_sec) and one for nanoseconds (tv_nsec). You will need both of these values to find out which time is more recent. |
63c647dc5f004bf4898cf752 | 2023-03-15T13:38:18.559Z | Help Fix Code | v2 | go through target_rule's nodes of dependencies and save the last modified time present in any dependency | struct stat st;
st.st_mtim.tv_sec = 0;
struct stat last_mod_time;
last_mod_time.st_mtim.tv_sec = 0;
while (target_rule->dependencies != NULL){
if(stat(dep->rule->target, &st) == 0){
if(last_mod_time.st_mtim.tv_sec == 0){
last_mod_time = st;
} else {
if(difftime(st, last_mod_time) > 0){
last_mod_time = st;
}
}
}
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-15T15:14:28.859Z | General Question | v2 | How to fix st_mtim: No such file or directory error | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-15T15:25:26.944Z | General Question | v2 | What header files to include for st_mtim? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-15T15:34:46.406Z | Question from Code | v2 | How to fix warning: ‘st’ may be used uninitialize, same with 'last_mod_time' and 'target_mod_time' | struct stat *st;
53 st->st_mtim.tv_sec = 0;
54 struct stat *last_mod_time;
55 last_mod_time->st_mtim.tv_sec = 0;
56 while (target_rule->dependencies != NULL) {
57 if (stat(dep->rule->target, st) == 0) {
58 if (last_mod_time->st_mtim.tv_sec == 0) {
59 last_mod_time = st;
60
}
else {
61 if (last_mod_time->st_mtim.tv_sec > st->st_mtim.tv_sec) {
62 last_mod_time = st;
63
}
else if (last_mod_time->st_mtim.tv_sec == st->st_mtim.tv_sec) {
64 if (last_mod_time->st_mtim.tv_nsec > st->st_mtim.tv_nsec) {
65 last_mod_time = st;
66
}
67
}
68
}
69
}
else if (stat(dep->rule->target, st) == -1) {
70 perror("st_mtim");
71 exit(1);
72
}
73 74
}
75 struct stat *target_mod_time;
76 if (stat(target, target_mod_time) == 0) {
77 printf("successfully got target time");
78
}
else if (stat(target, target_mod_time) == -1) {
79 perror("st_mtim");
80 exit(1);
81
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-15T16:10:34.141Z | Help Fix Code | v2 | I am receiving a st_mtim: No such file or directory
error |
#include <sys/stat.h>
struct stat target_mod_time;
// struct timespec target_time = target_mod_time->st_mtim;
if(stat(target, &target_mod_time) == 0){
printf("successfully got target time");
} else if(stat(target, &target_mod_time) == -1){
perror("st_mtim");
exit(1);
}
struct stat st;
while (target_rule->dependencies != NULL){
if(stat(dep->rule->target, &st) == 0){
// comparing the last modified time for the dependency to that of the target
if(target_mod_time.st_mtim.tv_sec > st.st_mtim.tv_sec){
//execute the rule's actions
execute_actions(target_rule);
} else if (target_mod_time.st_mtim.tv_sec == st.st_mtim.tv_sec){
if (target_mod_time.st_mtim.tv_nsec > st.st_mtim.tv_nsec){
//execute the rule's actions
execute_actions(target_rule);
}
}
} else if(stat(dep->rule->target, &st) == -1){
perror("st_mtim");
exit(1);
}
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-15T17:03:44.865Z | Help Fix Code | v2 | Trying to check if target exit with stat | struct stat target_exist;
stat(target, &target_exist);
if(stat(target, &target_exist) != 0){
perror("target non-existent");
exit(1);
} | null | null |
63c647dc5f004bf4898cf752 | 2023-03-17T15:31:47.293Z | Help Fix Code | v2 | Change this code such that it prints out random numbers from 0 to 99 instead of in that order, use the function random | for (int i = 0; i < 100; i++){
fprintf(fp, "%d\n", i);
} | null | null |
63c647dc5f004bf4898cf752 | 2023-03-17T15:47:35.406Z | Help Fix Code | v2 | It is giving me an error that buffer may not be initialized. | int *buffer;
41 for (;;) {
42 fseek(fp, (rand()%99)*sizeof(int), SEEK_SET);
43 fread(buffer, sizeof(int), 1, fp);
44
45 } | null | null |
63c647dc5f004bf4898cf752 | 2023-03-17T15:48:52.568Z | Help Fix Code | v2 | How to fix the code giving me an error that buffer may not be initialized. | 24 int main(int argc, char **argv) {
25 if (argc != 3) {
26 fprintf(stderr, "Usage: time_reads s filename\n");
27 exit(1);
28 }
29 seconds = strtol(argv[1], NULL, 10);
30
31 FILE *fp;
32 if ((fp = fopen(argv[2], "r")) == NULL) {
33 perror("fopen");
34 exit(1);
35 }
36
37 /* In an infinite loop, read an int from a random location in the file,
38 * and print it to stderr.
39 */
40 int *buffer;
41 for (;;) {
42 fseek(fp, (rand()%99)*sizeof(int), SEEK_SET);
43 fread(buffer, sizeof(int), 1, fp);
44
45 }
46 return 1; // something is wrong if we ever get here!
47 }
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-17T16:05:10.417Z | Help Write Code | v2 | null | null | null | Now, use sigactionLinks to an external site. to add a signal handler to your program. Start with something simple that just prints a message to standard out and exits (with termination code 0) when it receives a SIGPROF. |
63c647dc5f004bf4898cf752 | 2023-03-17T16:25:36.708Z | Help Fix Code | v2 | Fix the code such that the integers printed out are from 0 to 99. Currectly, the integers printed out are 9 digits | 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);
}
/* In an infinite loop, read an int from a random location in the file,
* and print it to stderr.
*/
int buffer = 0;
for (;;) {
fseek(fp, (rand()%99)*sizeof(int), SEEK_SET);
fread(&buffer, sizeof(int), 1, fp);
fprintf(stderr, "%d\n", buffer);
}
return 1; // something is wrong if we ever get here!
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-23T21:55:42.883Z | Help Fix Code | v2 | THis code only successfully prints out the first line input into the server, and not any lines after that. | int main() {
setbuf(stdout, NULL);
struct sockaddr_in *self = init_server_addr(PORT);
int listenfd = set_up_server_socket(self, 5);
while (1) {
int fd = accept_connection(listenfd);
if (fd < 0) {
continue;
}
// Receive messages
char buf[BUFSIZE] = {'\0'};
int inbuf = 0; // How many bytes currently in buffer?
int room = sizeof(buf); // How many bytes remaining in buffer?
char *after = buf; // Pointer to position after the data in buf
int nbytes;
while ((nbytes = read(fd, after, room)) > 0) {
// Step 1: update inbuf (how many bytes were just added?)
inbuf = nbytes;
int where;
while ((where = find_network_newline(buf, inbuf)) > 0) {
buf[where-1] = '\0';
printf("Next message: %s\n", buf);
// Note that we could have also used write to avoid having to
// put the '\0' in the buffer. Try using write later!
// Step 4: update inbuf and remove the full line from the buffer
// There might be stuff after the line, so don't just do inbuf = 0.
inbuf = inbuf-where;
memmove(buf, &buf[where], BUFSIZE-where);
// You want to move the stuff after the full line to the beginning
// of the buffer. A loop can do it, or you can use memmove.
// memmove(destination, source, number_of_bytes)
}
// Step 5: update after and room, in preparation for the next read.
after = &buf[inbuf];
room = BUFSIZE - inbuf;
}
close(fd);
printf("The connection is now closed ...\n");
}
free(self);
close(listenfd);
return 0;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-27T19:45:04.588Z | Explain Code | v2 | null | int main(void) {
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd < 0) {
perror("client: socket");
exit(1);
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
if (inet_pton(AF_INET, "127.0.0.1", &server.sin_addr) < 1) {
perror("client: inet_pton");
close(sock_fd);
exit(1);
}
if (connect(sock_fd, (struct sockaddr *)&server, sizeof(server)) == -1) {
perror("client: connect");
close(sock_fd);
exit(1);
}
char buf[2 * BUF_SIZE + 2];
printf("Please enter a username: ");
fflush(stdout);
int num_read = read(STDIN_FILENO, buf, BUF_SIZE);
if (num_read == 0) {
close(sock_fd);
exit(0);
}
buf[num_read] = '\0';
if (write(sock_fd, buf, num_read) != num_read) {
perror("client: write");
close(sock_fd);
exit(1);
}
while (1) {
num_read = read(STDIN_FILENO, buf, BUF_SIZE);
if (num_read == 0) {
break;
}
}
buf[num_read] = '\0';
if (write(sock_fd, buf, num_read) != num_read) {
perror("client: write");
close(sock_fd);
exit(1);
}
num_read = read(sock_fd, buf, sizeof(buf) - 1);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
printf("[Server] %s", buf);
}
close(sock_fd);
return 0;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-27T20:53:48.454Z | Question from Code | v2 | There are now two different types of input that we could receive: a username, or a message. How could we tell if we have if we have received a username yet? | 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;
}
return 0;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-27T21:09:31.810Z | Help Fix Code | v2 | your task is to modify read_from to see if we have received a username yet (how can we tell?), and allocate memory for the username field in the sockname struct and copy the name to the field for that client. After the username has been set, all new incoming messages from that connection will be messages to echo. | int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
/* In Lab 10, you focused on handling partial reads. For this lab, you do
* not need handle partial reads. Because of that, this server program
* does not check for "\r\n" when it reads from the client.
*/
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;
}
return 0;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-27T21:16:29.733Z | Explain Code | v2 | null | 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;
}
return 0;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-27T21:44:40.247Z | Help Write Code | v2 | null | null | null | Write read_from to see if we have received a username yet (how can we tell?), and allocate memory for the username field in the sockname struct and copy the name to the field for that client. After the username has been set, all new incoming messages from that connection will be messages to echo.
|
63c647dc5f004bf4898cf752 | 2023-03-27T21:56:33.609Z | Help Write Code | v2 | null | null | null | I am writing code in the server. How do I broadcast to all clients that are connected to this server? |
63c647dc5f004bf4898cf752 | 2023-03-27T22:10:54.622Z | Help Fix Code | v2 | How to add to this code such that it will print buf to all of the clients connected to this server? | int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
/* In Lab 10, you focused on handling partial reads. For this lab, you do
* not need handle partial reads. Because of that, this server program
* does not check for "\r\n" when it reads from the client.
*/
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;
}
return 0;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-27T22:12:33.706Z | Question from Code | v2 | How to loop through the users and check if each one is connected? | 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;
}
return 0;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-27T22:14:21.568Z | Help Fix Code | v2 | broadcasts any message received to all connected clients | int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int first_read = 1;
/* In Lab 10, you focused on handling partial reads. For this lab, you do
* not need handle partial reads. Because of that, this server program
* does not check for "\r\n" when it reads from the client.
*/
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 (first_read == 1){ // it is a username
users[client_index].username = malloc(sizeof(char) * num_read);
strcpy(users[client_index].username, buf);
first_read = 0;
// add to the table of users?
} else { // it is not a username
for(int i = 0; i < client_index + 1; i++){
if(users[i].sock_fd > 0){
printf("%s\n", buf);
}
}
}
return 0;
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-28T01:53:28.487Z | Explain Code | v2 | null | while (1) {
fd_set listen_fds = all_fds;
if (select(max_fd + 1, &listen_fds, NULL, NULL, NULL) == -1) {
perror("server: select");
exit(1);
}
if (FD_ISSET(sock_fd, &listen_fds)) {
int client_fd = accept_connection(sock_fd, users);
if (client_fd > max_fd) {
max_fd = client_fd;
}
FD_SET(client_fd, &all_fds);
printf("Accepted connection\n");
}
for (int index = 0; index < MAX_CONNECTIONS; index++) {
if (users[index].sock_fd > -1 && FD_ISSET(users[index].sock_fd, &listen_fds)) {
int client_closed = read_from(index, users);
if (client_closed > 0) {
FD_CLR(client_closed, &all_fds);
printf("Client %d disconnected\n", client_closed);
} else {
printf("Echoing message from client %d\n", users[index].sock_fd);
}
}
}
| null | null |
63c647dc5f004bf4898cf752 | 2023-03-31T16:40:05.713Z | General Question | v2 | Why does printf("[Server] %s", buf); prints successfully but printf("[Server]"); not print anything? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-03-31T16:40:43.454Z | General Question | v2 | How to fix printf("[Server]"); such that only [server] is printed? | null | null | null |
63c647dc5f004bf4898cf752 | 2023-04-03T20:38:19.905Z | Help Fix Code | v2 | Why does it say ‘temp2’ may be used uninitialized in this function line 227? | char *temp2;
226 for (int i = 0; i < MAX_FRIENDS && user->friends[i] != NULL; i++) {
227 snprintf(temp2, strlen(user->friends[i]->name + 2), "%s\r\n", user->friends[i]->name);
} | null | null |
63c647dc5f004bf4898cf752 | 2023-04-03T20:48:01.725Z | Question from Code | v2 | Why is it printing 0? | char *user_string = calloc(sizeof(char) * 171, 1);
printf("%ld\n", strlen(user_string)); | null | null |
63c647dc5f004bf4898cf752 | 2023-04-03T20:58:50.604Z | Help Fix Code | v2 | Why is this code not copying name, friends and posts into user_string? | snprintf(user_string, sizeof(user), "%s%s%s", name, friends, posts); | null | null |
63c647dc5f004bf4898cf752 | 2023-04-03T22:05:28.302Z | Explain Code | v2 | null | int main(int argc, char *argv[]) {
145 int batch_mode = (argc == 2);
146 char input[INPUT_BUFFER_SIZE];
147 FILE *input_stream;
148 149 150 User *user_list = NULL;
151 152 if (batch_mode) {
153 input_stream = fopen(argv[1], "r");
154 if (input_stream == NULL) {
155 perror("Error opening file");
156 exit(1);
157
}
158
}
else {
159 160 input_stream = stdin;
161
}
162 163 printf("Welcome to FriendMe! (Local version)\nPlease type a command:\n> ");
164 165 while (fgets(input, INPUT_BUFFER_SIZE, input_stream) != NULL) {
166 167 168 if (batch_mode) {
169 printf("%s", input);
170
}
171 172 char *cmd_argv[INPUT_ARG_MAX_NUM];
173 int cmd_argc = tokenize(input, cmd_argv);
174 175 if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list) == -1) {
176 break;
177
}
178 179 printf("> ");
180
}
181 182 if (batch_mode) {
183 fclose(input_stream);
184
}
185 186 return 0;
187
} | null | null |
63c647dc5f004bf4898cf752 | 2023-04-03T22:06:55.090Z | Help Write Code | v2 | null | null | null | How can I add code to make this a server where other clients can connect to it? |
63c647dc5f004bf4898cf752 | 2023-04-03T22:11:50.131Z | Explain Code | v2 | null | int main() {
setbuf(stdout, NULL);
struct sockaddr_in *self = init_server_addr(PORT);
int listenfd = set_up_server_socket(self, 5);
while (1) {
int fd = accept_connection(listenfd);
if (fd < 0) {
continue;
}
char buf[BUFSIZE] = {'\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("Next message: %s\n", buf);
inbuf = inbuf - where;
for (int i = 0; i < where; i++) {
buf[i] = '\0';
}
memmove(buf, buf + where, inbuf);
for (int i = where; i < BUFSIZE; i++) {
buf[i] = '\0';
}
}
after = buf + inbuf;
room = BUFSIZE - inbuf;
}
close(fd);
printf("The connection is now closed ...\n");
}
free(self);
close(listenfd);
return 0;
} | null | null |
63c647dc5f004bf4898cf752 | 2023-04-04T00:55:33.095Z | Help Fix Code | v2 | How do I change (what do I add, where should I add them, and what should I remove) from this code if I want to change it from local host to hosting on a server? | main{
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);
}
return 0;
}
} | null | null |
63c647dc5f004bf4898cf752 | 2023-04-04T01:06:54.317Z | General Question | v2 | How to solve this two errors? friend_server.c:22:25: error: expected expression before ‘[’ token
22 | int status = setsockopt([sock_fd], SOL_SOCKET, SO_REUSEADDR,
| ^
friend_server.c:22:26: error: ‘sock_fd’ undeclared here (not in a function)
22 | int status = setsockopt([sock_fd], SOL_SOCKET, SO_REUSEADDR,
| null | null | null |
63c647dc5f004bf4898cf752 | 2023-04-04T01:17:23.790Z | Question from Code | v2 | what is the sock fd in this code? | int listen_soc = socket(AF_INET, SOCK_STREAM, 0);
if (listen_soc == -1) {
perror("server: socket");
exit(1);
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(54321);
memset(&server.sin_zero, 0, 8);
server.sin_addr.s_addr = INADDR_ANY;
if (bind(listen_soc, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) == -1) {
perror("server: bind");
close(listen_soc);
exit(1);
}
if (listen(listen_soc, 5) < 0) {
perror("listen");
exit(1);
}
struct sockaddr_in client_addr;
unsigned int client_len = sizeof(struct sockaddr_in);
client_addr.sin_family = AF_INET;
int client_socket = accept(listen_soc, (struct sockaddr *)&client_addr, &client_len);
if (client_socket == -1) {
perror("accept");
return -1;
}
write(client_socket, "Welcome to FriendMe!\nPlease type a command:\n> ", 49);
char line[INPUT_BUFFER_SIZE];
read(client_socket, line, INPUT_BUFFER_SIZE);
line[INPUT_BUFFER_SIZE - 1] = '\0';
printf("reading line");
| null | null |
63c647dc5f004bf4898cf752 | 2023-04-04T01:31:54.841Z | Help Fix Code | v2 | How to fix this error error: ‘g’ undeclared (first use in this function)
friend_server.c:160:29: note: in expansion of macro ‘PORT’
160 | server.sin_port = htons(PORT);
| ^~~~
<command-line>: note: each undeclared identifier is reported only once for each function it appears in
friend_server.c:160:29: note: in expansion of macro ‘PORT’
160 | server.sin_port = htons(PORT);
| ^~~~ | // Create socket
int listen_soc = socket(AF_INET, SOCK_STREAM, 0);
if (listen_soc == -1) {
perror("server: socket");
exit(1);
}
//initialize server address
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
memset(&server.sin_zero, 0, 8);
server.sin_addr.s_addr = INADDR_ANY;
// bind socket to an address
if (bind(listen_soc, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) == -1) {
perror("server: bind");
close(listen_soc);
exit(1);
}
// Set up a queue in the kernel to hold pending connections.
if (listen(listen_soc, 5) < 0) {
// listen failed
perror("listen");
exit(1);
}
struct sockaddr_in client_addr;
unsigned int client_len = sizeof(struct sockaddr_in);
client_addr.sin_family = AF_INET;
int client_socket = accept(listen_soc, (struct sockaddr *)&client_addr, &client_len);
if (client_socket == -1) {
perror("accept");
return -1;
}
write(client_socket, "Welcome to FriendMe!\nPlease type a command:\n> ", 49);
char line[INPUT_BUFFER_SIZE];
read(client_socket, line, INPUT_BUFFER_SIZE);
line[INPUT_BUFFER_SIZE - 1] = '\0';
printf("reading line");
| null | null |
63c647dc5f004bf4898cf752 | 2023-04-04T02:07:14.859Z | Explain Code | v2 | null | 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;
}
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);
} | null | null |
63c647dc5f004bf4898cf752 | 2023-04-04T19:11:10.709Z | Help Write Code | v2 | null | null | null | every time a client connects to a server, increment the client_count variable and change curr (which is a specific node in a linked list) to the new client. |
63c647dc5f004bf4898cf752 | 2023-04-04T19:29:35.502Z | Help Fix Code | v2 | Edit this code to read from the client's standard input. check if the client's input is in a linked list called user. If it is, return "already in list", if not, create a node that is the name. | char buf[INPUT_BUFFER_SIZE] = {'\0'};
int inbuf = 0; // How many bytes currently in buffer?
int room = sizeof(buf); // How many bytes remaining in buffer?
char *after = buf; // Pointer to position after the data in 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';
curr->name = calloc(sizeof(char) * (MAXNAME + 2), 1);
strcpy(curr->name, buf);
strcat(curr->name, '\r\n');
inbuf = inbuf-where;
for (int i = 0; i < where; i++){
buf[i] = '\0';
}
memmove(buf, buf+where, inbuf);
for (int i = where; i < INPUT_BUFFER_SIZE; i++){
buf[i] = '\0';
}
}
after = buf+inbuf;
room = BUFSIZE - inbuf;
}
close(fd);
printf("The connection is now closed ...\n"); | null | null |
63c647dc5f004bf4898cf752 | 2023-04-04T20:07:42.402Z | General Question | v2 | How to fix this error: assignment to expression with array type
212 | curr->name = calloc(sizeof(char) * (MAX_NAME + 2), 1); | null | null | null |
63c647dc5f004bf4898cf752 | 2023-04-04T21:53:41.191Z | Explain Code | v2 | null | 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;
} | 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.