Skip to content
Snippets Groups Projects
Commit f60c95d2 authored by Nagy Tibor's avatar Nagy Tibor
Browse files

tests: Work on more thorough error handling

parent d1a424c0
No related branches found
No related tags found
No related merge requests found
...@@ -2,8 +2,7 @@ ...@@ -2,8 +2,7 @@
#include <unistd.h> #include <unistd.h>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int i; for(int i = 0; i < argc; i++) {
for(i = 0; i < argc; i++) {
write(STDOUT_FILENO, argv[i], strlen(argv[i])); write(STDOUT_FILENO, argv[i], strlen(argv[i]));
write(STDOUT_FILENO, " ", 1); write(STDOUT_FILENO, " ", 1);
} }
......
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h> #include <sys/resource.h>
void ptimeval(struct timeval* val) { void ptimeval(struct timeval* val) {
...@@ -8,14 +9,18 @@ void ptimeval(struct timeval* val) { ...@@ -8,14 +9,18 @@ void ptimeval(struct timeval* val) {
int main(void) { int main(void) {
struct rusage r_usage; struct rusage r_usage;
if (getrusage(RUSAGE_SELF, &r_usage) < 0) {
if (getrusage(RUSAGE_SELF, &r_usage) == -1) {
perror("getrusage"); perror("getrusage");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("ru_utime:"); printf("ru_utime:");
ptimeval(&r_usage.ru_utime); ptimeval(&r_usage.ru_utime);
printf("ru_stime:"); printf("ru_stime:");
ptimeval(&r_usage.ru_utime); ptimeval(&r_usage.ru_utime);
printf("ru_maxrss: %ld\n", r_usage.ru_maxrss); printf("ru_maxrss: %ld\n", r_usage.ru_maxrss);
printf("ru_ixrss: %ld\n", r_usage.ru_ixrss); printf("ru_ixrss: %ld\n", r_usage.ru_ixrss);
printf("ru_idrss: %ld\n", r_usage.ru_idrss); printf("ru_idrss: %ld\n", r_usage.ru_idrss);
......
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
int main(void) { int main(void) {
int status = brk((void*)100); int status = brk((void*)100);
printf("brk exited with status code %d\n", status);
if (status == -1) {
perror("brk");
exit(EXIT_FAILURE);
} else if (status != 0) {
printf("brk returned %d, unexpected result\n", status);
exit(EXIT_FAILURE);
}
} }
#include <limits.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
int main(void) { int main(void) {
char* cwd1 = malloc(4096*sizeof(char));//(char*) calloc(4096 + 1, sizeof(char)); char cwd[PATH_MAX] = { 0 };
getcwd(cwd1, 4096); char *cwd_result = NULL;
printf("initial cwd: %s\n", cwd1);
free(cwd1); cwd_result = getcwd(cwd, PATH_MAX);
chdir(".."); if (cwd_result == NULL) {
char* cwd2 = malloc(4096*sizeof(char));//(char*) calloc(4096 + 1, sizeof(char)); perror("getcwd");
getcwd(cwd2, 4096); exit(EXIT_FAILURE);
printf("final cwd: %s\n", cwd2); } else if (cwd_result != cwd) {
free(cwd2); puts("getcwd returned something else than the buf argument");
exit(EXIT_FAILURE);
}
printf("getcwd before chdir: %s\n", cwd);
int status = chdir("..");
if (status == -1) {
perror("chdir");
exit(EXIT_FAILURE);
} else if (status != 0) {
printf("chdir returned %d, unexpected result\n", status);
exit(EXIT_FAILURE);
}
cwd_result = getcwd(cwd, PATH_MAX);
if (cwd_result == NULL) {
perror("getcwd");
exit(EXIT_FAILURE);
} else if (cwd_result != cwd) {
puts("getcwd returned something else than the buf argument");
exit(EXIT_FAILURE);
}
printf("getcwd after chdir: %s\n", cwd);
} }
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
int main(void) { int main(void) {
char* args[] = {"sh", "-c", "echo 'exec works :D'", NULL}; char* args[] = {"sh", "-c", "echo 'exec works :D'", NULL};
execv("/bin/sh", args);
perror("execv"); int status = execv("/bin/sh", args);
if (status == -1) {
perror("execv");
exit(EXIT_FAILURE);
}
} }
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
int main(void) { int main(void) {
int fd = open("..", 0, 0); int fd = open("..", 0, 0);
int status; if (fd == -1) {
status = fchdir(fd); perror("open");
printf("fchdir exited with status code %d\n", status); exit(EXIT_FAILURE);
close(fd); } else if (fd < 0) {
printf("open returned %d, unexpected result\n", fd);
exit(EXIT_FAILURE);
}
int status = fchdir(fd);
if (status == -1) {
perror("fchdir");
exit(EXIT_FAILURE);
} else if (status != 0) {
printf("fchdir returned %d, unexpected result\n", status);
exit(EXIT_FAILURE);
}
int c = close(fd);
if (c == -1) {
perror("close");
exit(EXIT_FAILURE);
} else if (c != 0) {
printf("close returned %d, unexpected result\n", c);
exit(EXIT_FAILURE);
}
} }
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
int main(void) { int main(void) {
int fd = open(".", 0, 0); int fd = open(".", 0, 0);
int status; if (fd == -1) {
status = fsync(fd); perror("open");
printf("fsync exited with status code %d\n", status); exit(EXIT_FAILURE);
close(fd); } else if (fd < 0) {
printf("open returned %d, unexpected result\n", fd);
exit(EXIT_FAILURE);
}
int status = fsync(fd);
if (status == -1) {
perror("fsync");
exit(EXIT_FAILURE);
} else if (status != 0) {
printf("fsync returned %d, unexpected result\n", status);
exit(EXIT_FAILURE);
}
int c = close(fd);
if (c == -1) {
perror("close");
exit(EXIT_FAILURE);
} else if (c != 0) {
printf("close returned %d, unexpected result\n", c);
exit(EXIT_FAILURE);
}
} }
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
int main(void) { int main(void) {
int fd = creat("ftruncate.out", 0777); int fd = creat("ftruncate.out", 0777);
int status; if (fd == -1) {
status = ftruncate(fd, 100); perror("creat");
printf("ftruncate exited with status code %d\n", status); exit(EXIT_FAILURE);
close(fd); } else if (fd < 0) {
printf("creat returned %d, unexpected result\n", fd);
exit(EXIT_FAILURE);
}
int status = ftruncate(fd, 100);
if (status == -1) {
perror("ftruncate");
exit(EXIT_FAILURE);
} else if (status != 0) {
printf("ftruncate returned %d, unexpected result\n", status);
exit(EXIT_FAILURE);
}
int c = close(fd);
if (c == -1) {
perror("close");
exit(EXIT_FAILURE);
} else if (c != 0) {
printf("close returned %d, unexpected result\n", c);
exit(EXIT_FAILURE);
}
} }
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include <unistd.h> #include <unistd.h>
int main(void) { int main(void) {
char first[PATH_MAX]; char first[PATH_MAX] = { 0 };
getcwd(first, PATH_MAX); getcwd(first, PATH_MAX);
puts(first); puts(first);
......
...@@ -3,10 +3,16 @@ ...@@ -3,10 +3,16 @@
#include <unistd.h> #include <unistd.h>
int main(void) { int main(void) {
char* hostname = malloc(256); char hostname[256] = { 0 };
if (gethostname(hostname, 256) == 0) {
int status = gethostname(hostname, 256);
if (status == 0) {
printf("Hostname: %s\n", hostname); printf("Hostname: %s\n", hostname);
} else if (status == -1) {
perror("gethostname");
exit(EXIT_FAILURE);
} else { } else {
puts("error getting hostname"); printf("gethostname returned %d, unexpected result\n", status);
exit(EXIT_FAILURE);
} }
} }
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
int main(void) { int main(void) {
// 1 is stdout int status = isatty(STDOUT_FILENO);
if (isatty(1)) {
if (status == 1) {
puts("'Tis a tty :D"); puts("'Tis a tty :D");
} else if (status == 0) {
if (errno == ENOTTY) {
// I wouldn't consider stdout not being a TTY an error
// (CI runners, etc.)
puts("Whatever a tty is, it's not me");
} else {
perror("isatty");
exit(EXIT_FAILURE);
}
} else { } else {
puts("Whatever a tty is, it's not me"); printf("isatty returned %d, unexpected result\n", status);
exit(EXIT_FAILURE);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment