Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions chapters/chapter01/play-1-1-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>

void sig_handler (int signo) {
fprintf(stderr, "Failed to exec!\n");
exit(1);
}

int main (int argc, char **argv) {

// Setting up child process
// Fork - Exec - Wait

int pipefd[2];

if (pipe(pipefd)) {
fprintf(stderr, "Failed to setup pipe!\n");
return 1;
}

pid_t parent = getpid();
pid_t child = fork();

signal(SIGINT, sig_handler);

if (child == -1) { // Error!
fprintf(stderr, "Failed to fork!\n");
return 1;
} else if (child == 0) { // Child
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[1]);
execl(argv[1], argv[1], NULL); // Add command line options here
kill(parent, SIGINT);
return 1;
}

close(pipefd[1]);

int status = 0;
char buffer[1024];
memset(buffer, 0, 1024);

waitpid(child, &status, 0);
read(pipefd[0], buffer, 1024);

int retval = WEXITSTATUS(status);

buffer[1023] = '\0'; // Prevent issues with this

// Time to analyze results

printf("Program returned: %d\n", retval);
if (retval != 0) {
printf("Use a return value of 0 to indicate that your program completed successfully\n");
}

if (buffer[0] == '\0') {
printf("You didn't write anything to stdout! Check your write statement.\n");
} else {
printf("You printed out:\n\n%s\nVery Cool!\n", buffer);
}

return 0;
}
80 changes: 80 additions & 0 deletions chapters/chapter01/play-2-1-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>

const char answer[] = "I think\nI thin\nI thi\nI th\nI t\nI \nI\n";

void sig_handler (int signo) {
fprintf(stderr, "Failed to exec!\n");
exit(1);
}

int main (int argc, char **argv) {

// Setting up child process
// Fork - Exec - Wait

int pipefd[2];

if (pipe(pipefd)) {
fprintf(stderr, "Failed to setup pipe!\n");
return 1;
}

pid_t parent = getpid();
pid_t child = fork();

signal(SIGINT, sig_handler);

if (child == -1) { // Error!
fprintf(stderr, "Failed to fork!\n");
return 1;
} else if (child == 0) { // Child
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[1]);
execl(argv[1], argv[1], NULL); // Add command line options here
kill(parent, SIGINT);
return 1;
}

close(pipefd[1]);

int status = 0;
char buffer[1024];
memset(buffer, 0, 1024);

read(pipefd[0], buffer, 1024);
waitpid(child, &status, 0);

int retval = WEXITSTATUS(status);

buffer[1023] = '\0'; // Prevent issues with this

// Time to analyze results

printf("Program returned: %d\n", retval);
if (retval == 7) {
printf("Nice! Now try returning 1023\n");
} else if (retval == 255) {
printf("Interesting :)\n");
} else {
printf("Try returning 7 or 1023\n");
}

if (buffer[0] == '\0') {
printf("You didn't write anything to stdout! Check your write statement.\n");
} else {
printf("You printed out:\n\n%s\n", buffer);
if (strcmp(buffer, answer) == 0) {
printf("Nice work!\n");
} else {
printf("Looks like something isn't quite right with your write statements\n");
}
}

return 0;
}
89 changes: 89 additions & 0 deletions chapters/chapter01/play-3-1-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/stat.h>

const char sing[] = "hello!";
const char singnl[] = "hello!\n";
const char doub[] = "hello!hello!";
const char doubnl[] = "hello!hello!\n";

void sig_handler (int signo) {
fprintf(stderr, "Failed to exec!\n");
exit(1);
}

int main (int argc, char **argv) {

// Setting up child process
// Fork - Exec - Wait

signal(SIGINT, sig_handler);

pid_t parent = getpid();
pid_t child = fork();

if (child == -1) { // Error!
fprintf(stderr, "Failed to fork!\n");
return 1;
} else if (child == 0) { // Child
execl(argv[1], argv[1], NULL); // Add command line options here
kill(parent, SIGINT);
return 1;
}


int status = 0;
waitpid(child, &status, 0);

child = fork();

if (child == -1) { // Error!
fprintf(stderr, "Failed to fork!\n");
return 1;
} else if (child == 0) { // Child
execl(argv[1], argv[1], NULL); // Add command line options here
kill(parent, SIGINT);
return 1;
}

waitpid(child, &status, 0);

// Time to analyze results

struct stat statbuf;
if (stat("./message.txt", &statbuf)) {
printf("Error analyzing message.txt\n");
return 1;
}

if ((statbuf.st_mode & S_IRGRP) || (statbuf.st_mode & S_IROTH)) {
printf("Other people can read your file!\n");
} else {
printf("Only you can read your file. Nice!\n");
}

char buffer[1024];
memset(buffer, 0, 1024);

int fd = open("./message.txt", O_RDONLY);
read(fd, buffer, 1024);

buffer[1023] = '\0';

printf("After two runs, message.txt looks like: \n\n%s\n", buffer);

if (strcmp(buffer, sing) == 0 || strcmp(buffer, singnl) == 0) {
printf("Looks good!\n");
} else if (strcmp(buffer, doub) == 0 || strcmp(buffer, doubnl) == 0) {
printf("Looks like you are appending to the file instead of truncating it. Check your flags!\n");
} else {
printf("Something looks off, try checking your flags and your write statement\n");
}

return 0;
}
91 changes: 91 additions & 0 deletions chapters/chapter01/play-3-2-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/stat.h>

const char sing[] = "hello!";
const char singnl[] = "hello!\n";
const char doub[] = "hello!hello!";
const char doubnl[] = "hello!hello!\n";

void sig_handler (int signo) {
fprintf(stderr, "Failed to exec!\n");
exit(1);
}

int main (int argc, char **argv) {

// Setting up child process
// Fork - Exec - Wait

system("mv ./message.txt ./message.txt.old");

signal(SIGINT, sig_handler);

pid_t parent = getpid();
pid_t child = fork();

if (child == -1) { // Error!
fprintf(stderr, "Failed to fork!\n");
return 1;
} else if (child == 0) { // Child
execl(argv[1], argv[1], NULL); // Add command line options here
kill(parent, SIGINT);
return 1;
}


int status = 0;
waitpid(child, &status, 0);

child = fork();

if (child == -1) { // Error!
fprintf(stderr, "Failed to fork!\n");
return 1;
} else if (child == 0) { // Child
execl(argv[1], argv[1], NULL); // Add command line options here
kill(parent, SIGINT);
return 1;
}

waitpid(child, &status, 0);

// Time to analyze results

struct stat statbuf;
if (stat("./message.txt", &statbuf)) {
printf("Error analyzing message.txt\n");
return 1;
}

if (statbuf.st_mode & S_IROTH) {
printf("Everyone can read your file. Nice!\n");
} else {
printf("Other people can't read your file! Check your flags!\n");
}

char buffer[1024];
memset(buffer, 0, 1024);

int fd = open("./message.txt", O_RDONLY);
read(fd, buffer, 1024);

buffer[1023] = '\0';

printf("After two runs, message.txt looks like: \n\n%s\n", buffer);

if (strcmp(buffer, sing) == 0 || strcmp(buffer, singnl) == 0) {
printf("Make sure you append to the file this time!\n");
} else if (strcmp(buffer, doub) == 0 || strcmp(buffer, doubnl) == 0) {
printf("Looks good!\n");
} else {
printf("Something looks off, try checking your flags and your write statement\n");
}

return 0;
}
44 changes: 44 additions & 0 deletions chapters/chapter01/play-4-1-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>

void sig_handler (int signo) {
fprintf(stderr, "Failed to exec!\n");
exit(1);
}

int main (int argc, char **argv) {

// Setting up child process
// Fork - Exec - Wait

pid_t parent = getpid();
pid_t child = fork();

signal(SIGINT, sig_handler);

if (child == -1) { // Error!
fprintf(stderr, "Failed to fork!\n");
return 1;
} else if (child == 0) { // Child
execl(argv[1], argv[1], NULL); // Add command line options here
kill(parent, SIGINT);
return 1;
}

int status = 0;
waitpid(child, &status, 0);


buffer[1023] = '\0'; // Prevent issues with this

// Time to analyze results

printf("Here is the contents of the error.txt\n");
system("cat errors.txt");

return 0;
}
Loading