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
63c647ae5f004bf4898cf5c7
2023-04-25T16:43:51.265Z
General Question
v2
Could I dereference out-of-bound index?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T16:46:07.496Z
General Question
v2
How to check if a binary representation is a negative number?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T16:47:08.421Z
General Question
v2
Where is the sign bit in binary representation?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T16:51:15.112Z
General Question
v2
How to copy an array of characters?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T18:46:09.642Z
General Question
v2
redir in shell programming?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T20:40:40.188Z
General Question
v2
When to update max_fd in socket programming?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T20:42:35.780Z
General Question
v2
What happens if I initialized the members of a struct with values in a wrong way?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T20:49:48.361Z
General Question
v2
Name the three (3) categories that most preprocessing directives fall into
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T20:51:23.117Z
General Question
v2
What are the three categories most of C preprocessors are categorized as?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T20:52:26.538Z
General Question
v2
What is the point of calling fclose() when program will end anyway?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T20:53:28.246Z
General Question
v2
What are 2 similarities between pipes and files?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T20:58:34.899Z
General Question
v2
How do I initialize an array of char?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T21:08:01.342Z
General Question
v2
How to identify if a process is a zombie?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T21:09:20.324Z
General Question
v2
What does ps aux do?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T21:09:58.879Z
General Question
v2
How does ps differ from ps aux?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T21:12:22.305Z
General Question
v2
Are System calls C programs' interface to the operating system and the computer’s hardware?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T21:13:16.832Z
General Question
v2
Is it true that a server may read and write to multiple clients on the same socket?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T21:15:12.207Z
General Question
v2
Is it true that Makefile will exit with an error if the target of a rule is not a file?
null
null
null
63c647ae5f004bf4898cf5c7
2023-04-25T21:15:57.853Z
General Question
v2
Are all dependencies in a Makefile files?
null
null
null
63c647b05f004bf4898cf5d6
2023-01-19T02:45:07.072Z
Help Fix Code
v1
null
char phone[11]; int num; scanf("%s%d", phone, num);
phone should save the first 11 char from the input, which is followed by a number
null
63c647b05f004bf4898cf5d6
2023-01-22T23:54:23.456Z
Explain Code
v1
null
int length = 0; int num = 298475 for (int i = 10; i > 10; i*10) { num -= (num % i); length ++ }
null
null
63c647b05f004bf4898cf5d6
2023-01-23T00:06:33.860Z
Explain Code
v1
null
int c = 1; int a = c;
null
null
63c647b05f004bf4898cf5d6
2023-01-23T00:11:51.745Z
Explain Code
v1
null
int x = 123; if (x / 10 == 12){ printf("yay"); }
null
null
63c647b05f004bf4898cf5d6
2023-01-23T00:12:41.562Z
Explain Code
v1
null
int x = 128; if (x / 10 == 12){ printf("yay"); }
null
null
63c647b05f004bf4898cf5d6
2023-01-28T02:23:10.331Z
Help Fix Code
v1
null
int copy = 123456789; int length = 1; for (int i = 10; copy > 10; i * 10){ copy -= copy % i; // turning every digit to 0 length += 1; }
count the number of digits in copy
null
63c647b05f004bf4898cf5d6
2023-01-28T03:03:19.327Z
Explain Code
v1
null
printf("check: %d \n", sin % (10^1));
null
null
63c647b05f004bf4898cf5d6
2023-01-28T03:03:46.784Z
Explain Code
v1
null
printf("check: %d \n", 123456789 % (10^1));
null
null
63c647b05f004bf4898cf5d6
2023-01-28T03:05:38.961Z
Explain Code
v1
null
int sin = 123456789; for (int i = 0; i < 9; i ++) { printf("sin: %d \n", sin); int digit = (sin % (10^(1+i))); printf("digit: %d \n", digit); sin -= digit * 10^i; sin_array[8 - i] = digit // dividing by 10 drops the decimals printf("array: %d \n", sin_array[8 - i]); }
null
null
63c647b05f004bf4898cf5d6
2023-01-28T03:10:24.036Z
Explain Code
v1
null
int sin = 123456789; for (int i = 0; i < 9; i ++) { printf("sin: %d \n", sin); int digit = (sin % (10^(1+i))); printf("digit: %d \n", digit); sin -= digit * 10^i; sin_array[8 - i] = digit; // dividing by 10 drops the decimals printf("array: %d \n", sin_array[8 - i]); }
null
null
63c647b05f004bf4898cf5d6
2023-01-28T03:17:52.627Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> int main() { int sin = 123456789; int sin_array[9]; int digit; for (int i = 0; i < 9; i ++) { digit = sin % (10^(1+i)); printf("digit: %d \n", digit); sin -= digit * 10^i; sin = sin / 10; sin_array[8 - i] = digit; } }
should take each digit in sin and place it in sin_array at the corresponding index
null
63c647b05f004bf4898cf5d6
2023-01-31T20:09:08.521Z
Help Fix Code
v1
null
if (strlen(str) > amount) { // truncate str at amount characters str[amount] = '\0'; return (strlen(str) - amount); } return 0;
if length of the string is greater than the amount given, then truncate the string to be of length amount and return the number of chars removed
null
63c647b05f004bf4898cf5d6
2023-02-09T20:05:16.989Z
Help Fix Code
v1
null
struct *current; int length = sizeof(*(current->friends))/sizeof(*(current->friends)[0]);
save length of an array called friends that's saved in struct called current
null
63c647b05f004bf4898cf5d6
2023-02-11T05:55:45.518Z
Question from Code
v1
What is the line to compile it into an executable named a.out that it will print the value 5 to stdout?
#include <stdio.h> int main() { #ifdef MACRO printf("%d\n", MACRO); #endif return 0; }
null
null
63c647b05f004bf4898cf5d6
2023-02-11T05:57:09.215Z
Question from Code
v1
in the program. What will this declaration line become after the program has passed through the C pre-processor?
#define MAXNAME = 32; char name[MAXNAME];
null
null
63c647b05f004bf4898cf5d6
2023-02-27T16:13:32.016Z
Question from Code
v1
why does the third line make w[1] = "ello"?
char **w = malloc(2 * sizeof(char *)); w[0] = "hello"; *(w + 1) = w[0] + 1; printf("%s, %s", w[0], w[1]);
null
null
63c647b05f004bf4898cf5d6
2023-02-27T16:17:11.200Z
General Question
v1
how can you tell the difference between compiling and run time error
null
null
null
63c647b05f004bf4898cf5d6
2023-02-27T16:20:29.923Z
Explain Code
v1
null
char *names[2];
null
null
63c647b05f004bf4898cf5d6
2023-02-27T16:21:39.230Z
Question from Code
v1
which takes precedence, * or [2]
char *names[2];
null
null
63c647b05f004bf4898cf5d6
2023-02-27T16:23:19.240Z
General Question
v1
fscanf vs fread?
null
null
null
63c647b05f004bf4898cf5d6
2023-02-27T16:27:14.577Z
Question from Code
v1
is this run time or compiling error
char *film = "Moonlight";  NO ERROR char x; x = film;  RUN-TIME ERROR x[0] = ’L’;
null
null
63c647b05f004bf4898cf5d6
2023-02-27T16:27:40.912Z
Question from Code
v1
is this run time or compiling error
char *film = "Moonlight"; char x; x = film; x[0] = ’L’;
null
null
63c647b05f004bf4898cf5d6
2023-02-27T16:35:14.609Z
Question from Code
v1
why is this a run time error
int *mkpoint(int x, int y) { int pt[2] = {x, y}; return pt; } int *x; x = mkpoint(3, 4);
null
null
63c647b05f004bf4898cf5d6
2023-02-27T20:11:33.357Z
Question from Code
v1
how do we make sure the new children don't create additional processes
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); }
null
null
63c647b05f004bf4898cf5d6
2023-02-27T21:34:30.308Z
Question from Code
v1
how do we make sure that when process only creates one child and control the total number of processes
for (int i = 0; i < iterations; i++) { if (created == 0){ int created = fork(); if (created < 0) { perror("fork"); exit(1); } else if (created > 0) { wait(&waitstatus); } } printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); }
null
null
63c647b05f004bf4898cf5d6
2023-02-27T22:52:24.060Z
Question from Code
v1
why does the parent process fork 2 processes?
int main() { int iterations = 2; int created = 0; int n = 0; for (int i = 0; i < iterations; i++) { if (created == 0){ int created = fork(); if (created < 0) { perror("fork"); exit(1); } else if(created > 0) { wait(&n); } } printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); } return 0; }
null
null
63c647b05f004bf4898cf5d6
2023-02-27T22:57:10.932Z
Question from Code
v1
why does the parent process fork 2 processes when created changes to a non 0 value
int main() { int iterations = 2; int created = 0; int n = 0; for (int i = 0; i < iterations; i++) { if (created == 0){ int created = fork(); if (created < 0) { perror("fork"); exit(1); } else if(created > 0) { wait(&n); } } printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); } return 0; }
null
null
63c647b05f004bf4898cf5d6
2023-03-07T00:07:57.878Z
Question from Code
v1
this program uses a different file called validate, which validates the inputed password and username. The input (from standard input) is in this form: file_name \n username \n password. But when I run my program (which forks a child and pipes the input to the child), stdin looks like this: file_name \n username \n password \n \n. Why?
int fds[2]; if (pipe(fds) == -1) { return -1; } int n = fork(); if (n == -1) { return -1; } else if (n == 0) { // child process dup2(fds[0], STDOUT_FILENO); close(fds[1]); // closed unused end execl("./validate", "validate", NULL); } else { // parent process close(fds[0]); write(fds[1], user_id, 10); // hardcoded MAXPASSWORD write(fds[1], password, 10); int status; if(wait(&status) == -1) { return -1; } else if (WIFEXITED(status)) { // checks id process exits normally int validation = WEXITSTATUS(status); if (validation == 0) { printf(SUCCESS); } else if (validation == 1) { // error return -1; } else if (validation == 2) { printf(INVALID); } else if (validation == 3) { printf(NO_USER); } else if (validation == 0) { printf(SUCCESS); } } close(fds[1]); }
null
null
63c647b05f004bf4898cf5d6
2023-03-07T00:12:39.831Z
Question from Code
v1
this program uses a different file called validate, which validates the inputed password and username. The input (from standard input) is in this form: file_name \n username \n password. But when I run my program (which forks a child and pipes the input to the child), stdin looks like this: file_name \n username \n password \n \n. Why?
int fds[2]; pipe(fds) == -1; int n = fork(); if (n == 0) { // child process dup2(fds[0], STDOUT_FILENO); close(fds[1]); // closed unused end execl("./validate", "validate", NULL); } else { // parent process close(fds[0]); write(fds[1], user_id, 10); // hardcoded MAXPASSWORD write(fds[1], password, 10); int status; wait(&status); if (WIFEXITED(status)) { // checks id process exits normally int validation = WEXITSTATUS(status); if (validation == 0) { printf(SUCCESS); } } close(fds[1]); }
null
null
63c647b05f004bf4898cf5d6
2023-03-07T00:39:01.946Z
Question from Code
v1
When the parent process exits, why does the shell print the prompt again, causing the shell to read from stdin?
int fds[2]; pipe(fds) == -1; int n = fork(); if (n == 0) { // child process dup2(fds[0], STDOUT_FILENO); close(fds[1]); // closed unused end execl("./validate", "validate", NULL); } else { // parent process close(fds[0]); write(fds[1], user_id, 10); // hardcoded MAXPASSWORD write(fds[1], password, 10); int status; wait(&status); if (WIFEXITED(status)) { // checks id process exits normally int validation = WEXITSTATUS(status); if (validation == 0) { printf(SUCCESS); } } close(fds[1]); }
null
null
63c647b05f004bf4898cf5d6
2023-03-07T00:40:03.380Z
Question from Code
v1
When the parent process exits, why does the shell print the prompt again, causing the shell to read from stdin?
int fds[2]; if (pipe(fds) == -1) { return -1; } int n = fork(); if (n == -1) { return -1; } else if (n == 0) { // child process close(fds[1]); // closed unused end dup2(fds[0], STDOUT_FILENO); execl("./validate", "validate", NULL); close(fds[0]); } else { // parent process close(fds[0]); write(fds[1], user_id, 10); // hardcoded MAXPASSWORD write(fds[1], password, 10); close(fds[1]); int status; if(wait(&status) == -1) { return -1; } else if (WIFEXITED(status)) { // checks id process exits normally int validation = WEXITSTATUS(status); if (validation == 0) { printf(SUCCESS); } } } return 0;
null
null
63c647b05f004bf4898cf5d6
2023-03-07T00:41:42.585Z
Question from Code
v1
When the parent process exits, how do you keep shell from printing the prompt again, causing the shell to read from stdin?
int fds[2]; if (pipe(fds) == -1) { return -1; } int n = fork(); if (n == -1) { return -1; } else if (n == 0) { // child process close(fds[1]); // closed unused end dup2(fds[0], STDOUT_FILENO); execl("./validate", "validate", NULL); close(fds[0]); } else { // parent process close(fds[0]); write(fds[1], user_id, 10); // hardcoded MAXPASSWORD write(fds[1], password, 10); close(fds[1]); int status; if(wait(&status) == -1) { return -1; } else if (WIFEXITED(status)) { // checks id process exits normally int validation = WEXITSTATUS(status); if (validation == 0) { printf(SUCCESS); } } } return 0;
null
null
63c647b05f004bf4898cf5d6
2023-03-07T00:43:50.283Z
Question from Code
v1
this program uses a different file called validate, which validates the inputed password and username. The input (from standard input) is in this form: file_name \n username \n password. But when I run my program (which forks a child and pipes the input to the child), stdin looks like this: file_name \n username \n password \n \n. Why does this happen, and how do I mitigate it?
int fds[2]; if (pipe(fds) == -1) { return -1; } int n = fork(); if (n == -1) { return -1; } else if (n == 0) { // child process close(fds[1]); // closed unused end dup2(fds[0], STDOUT_FILENO); execl("./validate", "validate", NULL); close(fds[0]); } else { // parent process close(fds[0]); write(fds[1], user_id, 10); // hardcoded MAXPASSWORD write(fds[1], password, 10); close(fds[1]); int status; if(wait(&status) == -1) { return -1; } else if (WIFEXITED(status)) { // checks id process exits normally int validation = WEXITSTATUS(status); if (validation == 0) { printf(SUCCESS); } } } return 0;
null
null
63c647b05f004bf4898cf5d6
2023-03-07T03:33:29.203Z
Question from Code
v1
why is it the validate file asks for input from stdin in the form: <username> \n <password> \n but this code asks for input in the form: <username> \n <password> \n \n \n \n? How do I fix this?
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> // NOTE: MAX_PASSWORD must be less than MAXLINE/2 #define MAXLINE 32 #define MAX_PASSWORD 10 #define PASSWORD_FILE "pass.txt" /* Reads two chunks from stdin, and checks if they match a user id * and password pair from a password file. The first chunk (MAX_PASSWORD bytes) * will contain a user id, and the second chunk (MAX_PASSWORD bytes) will * contain a password. * * The program exits with a value of * 0 if the user id and password match, * 1 if there is an error, * 2 if the user id is found but the password does not match, and * 3 if the user id is not found in the password file. */ /* Note that read will read at most MAX_PASSWORD bytes. This means that the * longest password we can support is MAX_PASSWORD. We can't count on the * caller to send the null termination character, so we have to ensure * that we terminate the string that is sent anyway. * * If the input is coming from the console/keyboard, then the write calls * are managed by the shell which will process each piece of input * separately. * However, if the input is coming from a pipe, there is no guarantee that * two writes generated by checkpasswd will send the data such that it * will be read by the two reads below. The means that it is important for * checkpasswd to send exactly as many bytes that validate is expecting. * Otherwise the behaviour may be unpredicatable. * * This also means that checkpasswd should report an invalid password * or no user_id if the user input is longer than MAX_PASSWORD. */ int main(void){ int n, user_length; char userid[MAXLINE]; char password[MAXLINE]; if((n = read(STDIN_FILENO, userid, MAX_PASSWORD)) == -1) { perror("read"); exit(1); } else if(n == 0) { fprintf(stderr, "Error: could not read from stdin"); exit(1); } // Make sure user id is null-terminated if(n <= MAX_PASSWORD) { userid[n] ='\0'; } // Remove newline character if it exists char *newline; if((newline = strchr(userid, '\n')) != NULL) { *newline = '\0'; } if((n = read(STDIN_FILENO, password, MAX_PASSWORD)) == -1) { perror("read"); exit(1); } else if(n == 0) { fprintf(stderr, "Error: could not read from stdin"); exit(1); } // Make sure password is null-terminated if(n <= MAX_PASSWORD) { password[n] ='\0'; } // Remove newline character if it exists if((newline = strchr(password, '\n')) != NULL) { *newline = '\0'; } // Prepare the userid:password string to compare to password file entries. // We expect userid to have enough space to concatenate ":" + password // but we will play it safe and use strncat strncat(userid, ":", MAXLINE - strlen(userid) - 1); user_length = strlen(userid); strncat(userid, password, MAXLINE - strlen(userid) - 1); FILE *fp = fopen(PASSWORD_FILE, "r"); if(!fp) { perror("fopen"); exit(1); } char line[MAXLINE]; while(fgets(line, sizeof(line) - 1, fp)) { line[strlen(line) - 1] = '\0'; if (strcmp(userid, line) == 0) { fclose(fp); exit(0); // found match } else if(strncmp(userid, line, user_length) == 0) { fclose(fp); exit (2); // invalid password } } fclose(fp); exit(3); // no such user }
null
null
63c647b05f004bf4898cf5d6
2023-03-07T03:36:23.083Z
Question from Code
v1
why is it the validate file asks for input from stdin in the form: <username> \n <password> \n but this code asks for input in the form: <username> \n <password> \n \n \n \n? How do I fix this?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; /* The user will type in a user name on one line followed by a password on the next. DO NOT add any prompts. The only output of this program will be one of the messages defined above. Please read the comments in validate carefully */ if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if(fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } // TODO // checking length if (strlen(user_id) > 10) { printf(NO_USER); exit(1); } if (strlen(password) > 10) { printf(INVALID); exit(1); } // the hard shit int fds[2]; if (pipe(fds) == -1) { exit(1); } int n = fork(); if (n < 0) { exit(1); } else if (n == 0) { // child process dup2(fds[0], STDOUT_FILENO); close(fds[0]); close(fds[1]); execl("./validate", "validate", NULL); exit(1); } else { // parent process close(fds[0]); write(fds[1], user_id, 10); // hardcoded MAXPASSWORD write(fds[1], password, 10); close(fds[1]); int status; if(wait(&status) == -1){ exit(1); } if (WIFEXITED(status)) { // checks id process exits normally int validation = WEXITSTATUS(status); if (validation == 0) { printf("%s", SUCCESS); } else if (validation == 1) { // error exit(1); } else if (validation == 2) { printf("%s", INVALID); } else if (validation == 3) { printf("%s", NO_USER); } } else { exit(1); } } return 0; }
null
null
63c647b05f004bf4898cf5d6
2023-03-07T03:40:05.260Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; /* The user will type in a user name on one line followed by a password on the next. DO NOT add any prompts. The only output of this program will be one of the messages defined above. Please read the comments in validate carefully */ if(fgets(user_id, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } if(fgets(password, MAXLINE, stdin) == NULL) { perror("fgets"); exit(1); } // TODO // checking length if (strlen(user_id) > 10) { printf(NO_USER); exit(1); } if (strlen(password) > 10) { printf(INVALID); exit(1); } // the hard shit int fds[2]; if (pipe(fds) == -1) { exit(1); } int n = fork(); if (n < 0) { exit(1); } else if (n == 0) { // child process dup2(fds[0], STDOUT_FILENO); close(fds[0]); close(fds[1]); execl("./validate", "validate", NULL); exit(1); } else { // parent process close(fds[0]); write(fds[1], user_id, 10); // hardcoded MAXPASSWORD write(fds[1], password, 10); close(fds[1]); int status; if(wait(&status) == -1){ exit(1); } if (WIFEXITED(status)) { // checks id process exits normally int validation = WEXITSTATUS(status); if (validation == 0) { printf("%s", SUCCESS); } else if (validation == 1) { // error exit(1); } else if (validation == 2) { printf("%s", INVALID); } else if (validation == 3) { printf("%s", NO_USER); } } else { exit(1); } } return 0; }
why is it the validate file asks for input from stdin in the form: <username> \n <password> \n but this code asks for input in the form: <username> \n <password> \n \n \n \n, which causes the program to take in the last 2 lines (which are blank) as inputs. How do I fix this?
null
63c647b05f004bf4898cf5d6
2023-03-07T03:41:22.290Z
Help Fix Code
v1
null
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #define MAXLINE 256 #define SUCCESS "Password verified\n" #define INVALID "Invalid password\n" #define NO_USER "No such user\n" int main(void) { char user_id[MAXLINE]; char password[MAXLINE]; /* The user will type in a user name on one line followed by a password on the next. */ if (strlen(user_id) > 10) { printf(NO_USER); exit(1); } if (strlen(password) > 10) { printf(INVALID); exit(1); } int fds[2]; if (pipe(fds) == -1) { exit(1); } int n = fork(); if (n < 0) { exit(1); } else if (n == 0) { // child process dup2(fds[0], STDOUT_FILENO); close(fds[0]); close(fds[1]); execl("./validate", "validate", NULL); exit(1); } else { // parent process close(fds[0]); write(fds[1], user_id, 10); // hardcoded MAXPASSWORD write(fds[1], password, 10); close(fds[1]); int status; if(wait(&status) == -1){ exit(1); } if (WIFEXITED(status)) { // checks id process exits normally int validation = WEXITSTATUS(status); if (validation == 0) { printf("%s", SUCCESS); } else if (validation == 1) { // error exit(1); } else if (validation == 2) { printf("%s", INVALID); } else if (validation == 3) { printf("%s", NO_USER); } } else { exit(1); } } return 0; }
why is it the validate file asks for input from stdin in the form: <username> \n <password> \n but this code asks for input in the form: <username> \n <password> \n \n \n \n, which causes the program to take in the last 2 lines (which are blank) as inputs. How do I fix this?
null
63c647b05f004bf4898cf5d6
2023-03-09T19:11:11.358Z
Question from Code
v2
what would this print
int main() { char *str; strncat(str, "hello", 3); printf("%s\n", str); return 0; }
null
null
63c647b05f004bf4898cf5d6
2023-03-09T19:12:26.773Z
Question from Code
v2
what would this print
int main() { char *str; strncpy(str, "hello", 3); printf("%s\n", str); return 0; }
null
null
63c647b05f004bf4898cf5d6
2023-03-09T19:35:51.956Z
General Question
v2
how to use strtok()
null
null
null
63c647b05f004bf4898cf5d6
2023-03-10T00:14:17.612Z
Question from Code
v2
is the tab ('\t') included in the first token? If so, how do I get rid of it?
int main () { char str[100] = "\tgcc -Wall -g -std=gnu99 -c shit.c"; const char s[2] = " "; char *token; /* get the first token */ token = strtok(str, s); /* walk through other tokens */ while(token != NULL ) { printf( "\n%s", token ); token = strtok(NULL, s); } return(0); }
null
null
63c647b05f004bf4898cf5d6
2023-03-10T00:17:13.711Z
General Question
v2
if you tokenize a str, what is strlen(token)?
null
null
null
63c647b05f004bf4898cf5d6
2023-03-10T00:21:31.867Z
Help Write Code
v2
null
null
null
how to get number of tokens from strtok
63c647b05f004bf4898cf5d6
2023-03-10T00:29:30.833Z
Question from Code
v2
does this give me the str removing '\t'?
strchr(tokens, '\t') + 1;
null
null
63c647b05f004bf4898cf5d6
2023-03-10T01:18:52.676Z
General Question
v2
if you have array = char[][], what does strlen(array) give you
null
null
null
63c647b05f004bf4898cf5d6
2023-03-10T01:19:39.492Z
Question from Code
v2
If you have double array = char[x][y], how do you find the x number?
strchr(tokens, '\t') + 1;
null
null
63c647b05f004bf4898cf5d6
2023-03-10T01:20:06.333Z
General Question
v2
If you have double array = char[x][y], how do you find the x number?
null
null
null
63c647b05f004bf4898cf5d6
2023-03-10T01:23:46.128Z
Help Write Code
v2
null
null
null
c how to print 2d array when you dont know the dimensions
63c647b05f004bf4898cf5d6
2023-03-10T01:28:26.739Z
Help Write Code
v2
null
null
null
how do I while loop through a 2d char array to print each word in the array
63c647b05f004bf4898cf5d6
2023-03-10T01:42:15.102Z
Help Write Code
v2
null
null
null
how do I assign space for 2d array using malloc
63c647b05f004bf4898cf5d6
2023-03-10T01:43:36.468Z
Question from Code
v2
why does the *char give error
char **args = malloc(sizeof(*char) * len);
null
null
63c647b05f004bf4898cf5d6
2023-03-10T01:53:17.449Z
Question from Code
v2
why does this give error and how do I fix it?
fread(line, MAXLINE, sizeof(char), fp); while (line != EOF) {
null
null
63c647b05f004bf4898cf5d6
2023-03-10T03:50:19.156Z
Question from Code
v2
why does this line of code give error
char *tokens = strtok(rest_of_line, ' ');
null
null
63c647b05f004bf4898cf5d6
2023-03-10T04:08:32.288Z
Question from Code
v2
why does this give error
char *line; fgets(line, MAXLINE, fp);
null
null
63c647b05f004bf4898cf5d6
2023-03-10T04:33:49.233Z
General Question
v2
how to run gdb for a file that doesn't have a main function? And how do I use gdb with input files (the code I want to debug needs an input file)?
null
null
null
63c647b05f004bf4898cf5d6
2023-03-10T04:39:16.872Z
General Question
v2
wen I use gdb: "gdb pmake.c < handout_example/handout.mk" I get there error: "not in executable format: file format not recognized".
null
null
null
63c647b05f004bf4898cf5d6
2023-03-10T04:41:23.948Z
General Question
v2
when I use gdb: "gdb pmake < handout_example/handout.mk" I get there error: "not in executable format: file format not recognized".
null
null
null
63c647b05f004bf4898cf5d6
2023-03-10T04:41:52.639Z
General Question
v2
when I use gdb: "gdb pmake < handout_example/handout.mk" I get there error: "not in executable format: file format not recognized". pmake has already been compiled by gcc
null
null
null
63c647b05f004bf4898cf5d6
2023-03-10T04:42:15.622Z
General Question
v2
when I use gdb: "gdb pmake < handout_example/handout.mk" I get there error: "not in executable format: file format not recognized". pmake has already been compiled by gcc -g
null
null
null
63c647b05f004bf4898cf5d6
2023-03-11T01:25:41.063Z
Question from Code
v2
should strncpy's last argument be strlen(token) or strlen(token) + 1 if strlen(token) = number of letters I want to copy (not include null terminator)
char **args = mallo(sizeof(char *) * len); for (int i = 0; i < len; i++) { args[i] = malloc(sizeof(char) * strlen(token) + 1); strncpy(args[i], token, strlen(token)); }
null
null
63c647b05f004bf4898cf5d6
2023-03-11T01:39:13.392Z
General Question
v2
how do I check if 2 chars are equal (I want to check if the first char in a string is a tab)
null
null
null
63c647b05f004bf4898cf5d6
2023-03-11T01:54:31.075Z
Question from Code
v2
Assume all function calls from nonstandard c libraries are helpers that work perfectly. This program is meant to create Node structures to organize make files. There's a function that prints the structure in a format to mimic a make file on standard output. But when I call on the corresponding print function in my terminal, it just keeps asking for input and I can't exit out (I have to kill terminal). What am I doing wrong?
Rule *parse_file(FILE *fp) { Rule *target_cur; Rule *target_head = NULL; Rule *other_head = NULL; char line[MAXLINE]; fgets(line, MAXLINE, fp); while (line != NULL) { if (is_comment_or_empty(line) == 1) { // line is empty or is a comment } else if (strchr(line, ':') != NULL) { // line contains start of rule (target & prerequisites) // target: target_cur = check_target(target_head, other_head, line); // dependencies check_dependencies(target_cur, target_head, other_head, strchr(line, ':') + 1); // curr is still pointed to the same rule (as it should) } else if (strncmp(line, "\t", sizeof(char)) == 0) { // line starts with tab, AKA contains actions (should mitigate the \r\n issues, I had 'else' before) char *tokens = strtok(line, " "); int len = 0; while (tokens != NULL) { len++; strtok(NULL, " "); } check_actions(target_cur, line, len - 1); // len - 1 cause of the tab = first token } } return target_head; }
null
null
63c647b05f004bf4898cf5d6
2023-03-11T03:26:37.243Z
Question from Code
v2
why does this code cause a segmentation fault?
void check_dependencies(Rule *curr, Rule *target_head, Rule *other_head, char *line) { // line passed in starts at space right after ':' char *token = strtok(line, " "); while (token != NULL) { Dependency *new = malloc(sizeof(Dependency)); Dependency *curr_dep; if (target_contains(target_head, token) != NULL) { new->rule = target_contains(target_head, token); } else if (other_contains(other_head, token) != NULL) { new->rule = other_contains(other_head, token); } else { // this target doesn't exist yet, create an 'other' Rule and attach to end of other lst Rule *new_rule = malloc(sizeof(Rule)); Rule *curr_other = other_head; new_rule->actions = NULL; new_rule->dependencies = NULL; new_rule->next_rule = NULL; new_rule->target = malloc(sizeof(char) * (strlen(token) + 1)); strncpy(new_rule->target, token, strlen(token) + 1); // attach to end of 'other' list while (curr_other->next_rule != NULL) { curr_other = curr_other->next_rule; } curr_other->next_rule = new_rule; } if (curr->dependencies == NULL) { curr->dependencies = new; curr_dep = new; } else { curr_dep->next_dep = new; curr_dep = new; } token = strtok(NULL, " "); } }
null
null
63c647b05f004bf4898cf5d6
2023-03-11T03:37:54.478Z
Question from Code
v2
Assuming curr cannot be NULL, why does this code cause a segmentation fault?
void check_dependencies(Rule *curr, Rule *target_head, Rule *other_head, char *line) { // line passed in starts at space right after ':' char *token = strtok(line, " "); while (token != NULL) { Dependency *new = malloc(sizeof(Dependency)); Dependency *curr_dep; if (target_contains(target_head, token) != NULL) { new->rule = target_contains(target_head, token); } else if (other_contains(other_head, token) != NULL) { new->rule = other_contains(other_head, token); } else { // this target doesn't exist yet, create an 'other' Rule and attach to end of other lst Rule *new_rule = malloc(sizeof(Rule)); Rule *curr_other = other_head; new_rule->actions = NULL; new_rule->dependencies = NULL; new_rule->next_rule = NULL; new_rule->target = malloc(sizeof(char) * (strlen(token) + 1)); strncpy(new_rule->target, token, strlen(token) + 1); // attach to end of 'other' list while (curr_other->next_rule != NULL) { curr_other = curr_other->next_rule; } curr_other->next_rule = new_rule; } if (curr->dependencies == NULL) { curr->dependencies = new; curr_dep = new; } else { curr_dep->next_dep = new; curr_dep = new; } token = strtok(NULL, " "); } }
null
null
63c647b05f004bf4898cf5d6
2023-03-11T03:47:39.425Z
Question from Code
v2
Assuming curr cannot be NULL, why does this code cause a segmentation fault?
void check_dependencies(Rule *curr, Rule *target_head, Rule *other_head, char *line) { // line passed in starts at space right after ':' char *token = strtok(line, " "); while (token != NULL) { Dependency *new = malloc(sizeof(Dependency)); Dependency *curr_dep; if (target_contains(target_head, token) != NULL) { new->rule = target_contains(target_head, token); } else if (other_contains(other_head, token) != NULL) { new->rule = other_contains(other_head, token); } else { // this target doesn't exist yet, create an 'other' Rule and attach to end of other lst Rule *new_rule = malloc(sizeof(Rule)); Rule *curr_other = other_head; new_rule->actions = NULL; new_rule->dependencies = NULL; new_rule->next_rule = NULL; new_rule->target = malloc(sizeof(char) * (strlen(token) + 1)); strncpy(new_rule->target, token, strlen(token) + 1); if (curr_other == NULL) { other_head = new_rule; } else { while (curr_other->next_rule != NULL) { curr_other = curr_other->next_rule; } curr_other->next_rule = new_rule; } } if (curr->dependencies == NULL) { curr->dependencies = new; curr_dep = new; } else { curr_dep->next_dep = new; curr_dep = new; } token = strtok(NULL, " "); } }
null
null
63c647b05f004bf4898cf5d6
2023-03-11T03:56:20.780Z
General Question
v2
when I call strtok twice in a row on the same line, it gives me segmentation fault the second time around. How to I mitigate this so I can call strtok again?
null
null
null
63c647b05f004bf4898cf5d6
2023-03-11T04:10:33.746Z
Question from Code
v2
the first print statement prints but the following print statements don't instead, the terminal screen goes blank. What causes this error?
if (strncmp(line, "\t", sizeof(char)) == 0) { printf("enter into action line \n"); // line starts with tab, AKA contains actions (should mitigate the \r\n issues, I had 'else' before) char *copy = strdup(line); char *tokens = strtok(copy, " "); int len = 0; while (tokens != NULL) { printf("token: %s\n", tokens); len++; strtok(NULL, " "); } printf("number of arguments: %d \n", len); check_actions(target_cur, line, len - 1); // len - 1 cause of the tab = first token printf("first argument: %s \n", target_cur->actions->args[0]); }
null
null
63c647b05f004bf4898cf5d6
2023-03-11T04:12:15.018Z
General Question
v2
what function can I use to count the number of tokens there are after calling strtok
null
null
null
63c647b05f004bf4898cf5d6
2023-03-11T04:13:25.396Z
General Question
v2
is there a function that returns the number occurences of a char within a string?
null
null
null
63c647b05f004bf4898cf5d6
2023-03-11T04:20:28.490Z
Question from Code
v2
Assuming curr cannot be null, what might be causing a segmentation error?
void check_actions(Rule *curr, char *line, int len) { Action *new_action = malloc(sizeof(Action)); new_action->next_act = NULL; Action *cur_action = NULL; if (curr->dependencies == NULL) { curr->actions = new_action; cur_action = new_action; } else { cur_action->next_act = new_action; cur_action = new_action; } char *copy = strdup(line); char *token = strtok(copy, " "); char **a = malloc(sizeof(char *) * len); int i = 0; while (token != NULL) { if (strcmp(token, "\t") != 0) { // AKA not a tab, update args a[i] = malloc(sizeof(char) * (strlen(token) + 1)); strncpy(a[i], token, strlen(token) + 1); } i++; strtok(NULL, " "); } cur_action->args = a; }
null
null
63c647b05f004bf4898cf5d6
2023-03-11T04:24:38.691Z
Question from Code
v2
Assuming curr cannot be null, what might be causing a segmentation error?
void check_actions(Rule *curr, char *line, int len) { Action *new_action = malloc(sizeof(Action)); new_action->next_act = NULL; Action *cur_action = NULL; if (curr->actions == NULL) { curr->actions = new_action; cur_action = new_action; } else { cur_action->next_act = new_action; cur_action = new_action; } char *copy = strdup(line); char *token = strtok(copy, " "); char **a = malloc(sizeof(char *) * len); int i = 0; while (token != NULL) { if (strcmp(token, "\t") != 0) { // AKA not a tab, update args a[i] = malloc(sizeof(char) * (strlen(token) + 1)); strncpy(a[i], token, strlen(token) + 1); } i++; token = strtok(NULL, " "); } cur_action->args = a; }
null
null
63c647b05f004bf4898cf5d6
2023-03-11T04:37:27.649Z
General Question
v2
I run a file with terminal command: ./pmake -f handout.mk -o, where -f and -o are defined in pmake.c, handout.mk is a file in the same folder, and pmake is compiled from pmake.c How do I run gdb to test this exact input?
null
null
null
63c647b05f004bf4898cf5d6
2023-03-11T04:51:57.749Z
General Question
v2
im running gdb and when I write display <variable> where variable is a string from a file, gdb displays it in binary. Is there a way to have gdb display the variable in human readable text?
null
null
null
63c647b05f004bf4898cf5d6
2023-03-11T05:06:46.532Z
General Question
v2
in gdb, how do I go back a line?
null
null
null
63c647b05f004bf4898cf5d6
2023-03-11T05:44:22.585Z
Question from Code
v2
I'm trying to remove the rest of the string line starting from '\r', but this gives me warnings when I compile. Why, and how do I fix this?
char *ptr = strchr(line, '\r'); ptr = '\0';
null
null
63c647b05f004bf4898cf5d6
2023-03-11T05:47:20.198Z
Question from Code
v2
when I run the code this is the warning that pops up in gcc warning: null argument where non-null required (argument 2) [-Wnonnull] 206 | strcpy(ptr, '\0'); what does it mean and how do i fix this?
char *ptr = strchr(line, '\r'); strcpy(ptr, '\0');
null
null
63c647b05f004bf4898cf5d6
2023-03-11T06:13:04.654Z
Question from Code
v2
why doesn't this code print "outside 7" to standard error?
int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); // POSITION A struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); // POSITION B x += 2; // POSITION C fprintf(stderr, "outside %d", x); return 0; }
null
null
63c647b05f004bf4898cf5d6
2023-03-11T15:33:17.753Z
General Question
v2
how do I remove whitespace from a string
null
null
null
63c647b05f004bf4898cf5d6
2023-03-11T15:33:55.273Z
General Question
v2
how do I remove whitespace from a string without strtok
null
null
null
63c647b05f004bf4898cf5d6
2023-03-12T05:24:59.290Z
General Question
v2
how to use struct stat to compare the last modified times of two files
null
null
null
63c647b05f004bf4898cf5d6
2023-03-12T20:45:39.270Z
Question from Code
v2
line 3 gives "warning: passing argument 1 of ‘stat’ from incompatible pointer type [-Wincompatible-pointer-types]" error when I compile it on gcc. What causes this problem and how do I fix it?
Dependency *dep_cur = rule->dependencies; struct stat check_stat; stat(dep_cur, &check_stat);
null
null