commit 1b746a5e7e167289b406307286bf08fe103c32d6
parent 8a2470e7311119b1388df4c115811357f4f781c0
Author: Christos Margiolis <christos@margiolis.net>
Date: Wed, 26 May 2021 19:03:35 +0300
changed mail
Diffstat:
12 files changed, 78 insertions(+), 146 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -1,6 +1,6 @@
MIT License
-(c) 2019-Present Christos Margiolis <christos@christosmarg.xyz>
+(c) 2019-Present Christos Margiolis <christos@margiolis.net>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
diff --git a/c-os2/ex1/ex2.c b/c-os2/ex1/ex2.c
@@ -1,5 +1,6 @@
#include <sys/wait.h>
+#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -12,8 +13,6 @@
* Τρόπος μεταγλώττισης: `cc ex2.c -o ex2`
*/
-static char *argv0; /* Program name */
-
/*
* Print a process' info. `n` indicates which process is being
* printed -- for example `printproc(2)` will print P2.
@@ -24,15 +23,6 @@ printproc(int n)
printf("p: %d\tpid: %d\tppid: %d\n", n, getpid(), getppid());
}
-/* Die. */
-static void
-die(const char *str)
-{
- fprintf(stderr, "%s: ", argv0);
- perror(str);
- exit(EXIT_FAILURE);
-}
-
int
main(int argc, char *argv[])
{
@@ -41,68 +31,66 @@ main(int argc, char *argv[])
int n; /* Bytes returned from read(2) */
int i = 3; /* P2 will create 3 child procs */
- argv0 = *argv;
-
/* Create pipe */
if (pipe(fd) < 0)
- die("pipe");
+ err(1, "pipe");
printproc(0);
/* Create P1 */
switch (fork()) {
case -1:
- die("fork");
+ err(1, "fork");
case 0:
printproc(1);
(void)strcpy(buf, "Hello from your first child\n");
/* Close the read fd and send message to P0 */
(void)close(fd[0]);
if (write(fd[1], buf, sizeof(buf)) != sizeof(buf))
- die("write");
+ err(1, "write");
exit(EXIT_SUCCESS);
default:
/* Close the write fd and receive message from P1 */
(void)close(fd[1]);
if ((n = read(fd[0], buf, sizeof(buf))) != sizeof(buf))
- die("read");
+ err(1, "read");
/* Print the message to stdout */
if (write(STDOUT_FILENO, buf, n) != n)
- die("write");
+ err(1, "write");
if (wait(NULL) < 0)
- die("wait");
+ err(1, "wait");
/* Create P2 */
switch (fork()) {
case -1:
- die("fork");
+ err(1, "fork");
case 0:
printproc(2);
/* create P3, P4 and P5 */
while (i--) {
switch (fork()) {
case -1:
- die("fork");
+ err(1, "fork");
case 0:
printproc(2 + i + 1);
exit(EXIT_SUCCESS);
default:
/* Wait for all children to exit first */
if (wait(NULL) < 0)
- die("wait");
+ err(1, "wait");
}
}
exit(EXIT_SUCCESS);
default:
/* wait for P2 to exit */
if (wait(NULL) < 0)
- die("wait");
+ err(1, "wait");
}
/*
* Finally, the parent process executes ps(1) after
* everything else has exited
*/
if (execl("/bin/ps", "ps", NULL) < 0)
- die("execl");
+ err(1, "execl");
}
return 0;
diff --git a/c-os2/ex1/ex3.c b/c-os2/ex1/ex3.c
@@ -1,3 +1,4 @@
+#include <err.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
@@ -25,10 +26,6 @@ struct foo {
/* Function declarations */
static void *calc(void *);
static void *emalloc(size_t);
-static void die(const char *);
-
-/* Global variable */
-static char *argv0; /* Program name */
/*
* Each thread calculates the sum of the squares of each element in a specified
@@ -72,10 +69,10 @@ calc(void *foo)
* to f->sums at the same time.
*/
if (pthread_mutex_lock(&f->mutex) != 0)
- die("pthread_mutex_lock");
+ err(1, "pthread_mutex_lock");
f->sums[tid] = localsum;
if (pthread_mutex_unlock(&f->mutex) != 0)
- die("pthread_mutex_unlock");
+ err(1, "pthread_mutex_unlock");
return NULL;
}
@@ -87,19 +84,10 @@ emalloc(size_t nb)
void *p;
if ((p = malloc(nb)) == NULL)
- die("malloc");
+ err(1, "malloc");
return p;
}
-/* Die. */
-static void
-die(const char *str)
-{
- fprintf(stderr, "%s: ", argv0);
- perror(str);
- exit(EXIT_FAILURE);
-}
-
int
main(int argc, char *argv[])
{
@@ -109,7 +97,6 @@ main(int argc, char *argv[])
int totalsum; /* What its name says */
int i; /* Counter */
- argv0 = *argv;
f = emalloc(sizeof(struct foo));
/*
* We do error checks for `n` and `ntd` but in case we read from a
@@ -140,7 +127,7 @@ main(int argc, char *argv[])
f->arr[i] = rand() % (ULIM - LLIM) + LLIM;
if (pthread_mutex_init(&f->mutex, NULL) != 0)
- die("pthread_mutex_init");
+ err(1, "pthread_mutex_init");
/*
* Start multithreading. For each thread we assign `calc`
* to be the callback function that each thread will call
@@ -150,9 +137,9 @@ main(int argc, char *argv[])
for (i = 0; i < f->ntd; i++) {
f->tid = i;
if (pthread_create(&tds[i], NULL, calc, (void *)f) != 0)
- die("pthread_create");
+ err(1, "pthread_create");
if (pthread_join(tds[i], NULL) != 0)
- die("pthread_join");
+ err(1, "pthread_join");
}
totalsum = 0;
while (f->ntd--)
diff --git a/c-os2/ex2/ex1_1.c b/c-os2/ex2/ex1_1.c
@@ -21,10 +21,8 @@ struct foo {
/* Function declarations */
static void *tdprint(void *);
static void *emalloc(size_t);
-static void die(const char *);
/* Global variables */
-static char *argv0;
static const char *nums[] = { /* Each thread will print one of these */
"<one>",
"<two>",
@@ -38,12 +36,12 @@ tdprint(void *foo)
f = (struct foo *)foo;
if (sem_wait(&f->mutex) < 0)
- die("sem_wait");
+ err(1, "sem_wait");
printf("%s", f->str);
/* Prevent memory leak from strdup(2). */
free(f->str);
if (sem_post(&f->mutex) < 0)
- die("sem_post");
+ err(1, "sem_post");
return NULL;
}
@@ -54,19 +52,11 @@ emalloc(size_t nb)
void *p;
if ((p = malloc(nb)) == NULL)
- die("malloc");
+ err(1, "malloc");
return p;
}
-static void
-die(const char *str)
-{
- fprintf(stderr, "%s: ", argv0);
- perror(str);
- exit(1);
-}
-
int
main(int argc, char *argv[])
{
@@ -74,7 +64,6 @@ main(int argc, char *argv[])
pthread_t *tds;
int i, len, n = 5;
- argv0 = *argv;
len = LEN(nums);
f = emalloc(sizeof(struct foo));
/*
@@ -86,15 +75,15 @@ main(int argc, char *argv[])
tds = emalloc(len * sizeof(pthread_t));
if (sem_init(&f->mutex, 0, 1) < 0)
- die("sem_init");
+ err(1, "sem_init");
while (n--) {
for (i = 0; i < len; i++) {
if ((f->str = strdup(nums[i])) == NULL)
- die("strdup");
+ err(1, "strdup");
if (pthread_create(&tds[i], NULL, tdprint, (void *)f) != 0)
- die("pthread_create");
+ err(1, "pthread_create");
if (pthread_join(tds[i], NULL) != 0)
- die("pthread_join");
+ err(1, "pthread_join");
}
}
printf("\n");
diff --git a/c-os2/ex2/ex3_client.c b/c-os2/ex2/ex3_client.c
@@ -2,6 +2,7 @@
#include <sys/types.h>
#include <sys/un.h>
+#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -20,9 +21,6 @@ struct pack_res {
float avg;
};
-static void *emalloc(size_t);
-static void die(const char *);
-
static char *argv0;
static void *
@@ -31,19 +29,10 @@ emalloc(size_t nb)
void *p;
if ((p = malloc(nb)) == NULL)
- die("malloc");
-
+ err(1, "malloc");
return p;
}
-static void
-die(const char *str)
-{
- fprintf(stderr, "%s: ", argv0);
- perror(str);
- exit(1);
-}
-
int
main(int argc, char *argv[])
{
@@ -71,12 +60,12 @@ main(int argc, char *argv[])
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
- die("socket");
+ err(1, "socket");
(void)memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
(void)strncpy(sun.sun_path, sockfile, sizeof(sun.sun_path) - 1);
if (connect(fd, (struct sockaddr *)&sun, sizeof(struct sockaddr_un)) < 0)
- die("connect");
+ err(1, "connect");
res = emalloc(sizeof(struct pack_res));
@@ -96,17 +85,17 @@ main(int argc, char *argv[])
}
(void)getchar();
if (send(fd, &n, sizeof(int), 0) < 0)
- die("send");
+ err(1, "send");
if (send(fd, arr, n * sizeof(int), 0) < 0)
- die("send");
+ err(1, "send");
if (recv(fd, res, sizeof(struct pack_res), 0) < 0)
- die("recv");
+ err(1, "recv");
printf("response: %s\tavg: %.2f\n", res->str, res->avg);
printf("%s> continue (y/n)? ", argv0);
ch = getchar();
if (send(fd, &ch, 1, 0) < 0)
- die("send");
+ err(1, "send");
if (ch == 'n')
break;
}
diff --git a/c-os2/ex2/ex3_server.c b/c-os2/ex2/ex3_server.c
@@ -2,6 +2,7 @@
#include <sys/types.h>
#include <sys/un.h>
+#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@@ -30,7 +31,6 @@ struct foo {
static int srv(struct foo *);
static void sighandler(int);
static void *emalloc(size_t);
-static void die(const char *);
static char *argv0;
/* Only gets set if a termination signal is caught. */
@@ -44,7 +44,7 @@ srv(struct foo *foo)
int *arr;
int i, n, sum;
int rc;
- char cont = 'y';
+ char cont;
/*
* The return code is -1 (error) by default so that we don't
@@ -54,7 +54,7 @@ srv(struct foo *foo)
*/
rc = -1;
f = (struct foo *)foo;
- while (cont != 'n') {
+ for (;;) {
if (recv(f->cfd, &n, sizeof(int), 0) < 0)
goto fail;
arr = emalloc(n * sizeof(int));
@@ -66,6 +66,13 @@ srv(struct foo *foo)
printf("cfd: %d\tarr[%d]: %d\n", f->cfd, i, arr[i]);
sum += arr[i];
}
+ free(arr);
+ /*
+ * If we go to `fail:`, `gcc` for some reason double frees if
+ * we don't manually set it to NULL, even though we explicitly
+ * check for NULL first...
+ */
+ arr = NULL;
if ((res->avg = sum / (float)n) > 10.0f) {
(void)strncpy(res->str, "sequence: ok",
@@ -82,7 +89,8 @@ srv(struct foo *foo)
if (recv(f->cfd, &cont, 1, 0) < 0)
goto fail;
- free(arr);
+ if (cont == 'n')
+ break;
}
rc = 0;
@@ -109,20 +117,10 @@ emalloc(size_t nb)
void *p;
if ((p = malloc(nb)) == NULL)
- die("malloc");
-
+ err(1, "malloc");
return p;
}
-/* FIXME: clean up resources as well. */
-static void
-die(const char *str)
-{
- fprintf(stderr, "%s: ", argv0);
- perror(str);
- exit(1);
-}
-
static void
usage(void)
{
@@ -151,7 +149,7 @@ main(int argc, char *argv[])
* listen(2)'s FreeBSD man page), but it's better to
* not allow it in case the user passes a negative
* value accidentally. Also a value of 0 doesn't make
- * any sense, so we don't allow it either.
+ * sense, so we don't allow it either.
*/
if ((backlog = atoi(optarg)) < 1)
usage();
@@ -176,22 +174,22 @@ main(int argc, char *argv[])
sa.sa_handler = sighandler;
sa.sa_flags = SA_RESTART;
/*if (sigaction(SIGHUP, &sa, NULL) < 0)*/
- /*die("sigaction: SIGHUP");*/
+ /*err(1, "sigaction: SIGHUP");*/
/*if (sigaction(SIGINT, &sa, NULL) < 0)*/
- /*die("sigaction: SIGINT");*/
+ /*err(1, "sigaction: SIGINT");*/
/*if (sigaction(SIGTERM, &sa, NULL) < 0)*/
- /*die("sigaction: SIGTERM");*/
+ /*err(1, "sigaction: SIGTERM");*/
if ((sfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
- die("socket");
+ err(1, "socket");
(void)memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
(void)strncpy(sun.sun_path, sockfile, sizeof(sun.sun_path) - 1);
if (bind(sfd, (struct sockaddr *)&sun, sizeof(sun)) < 0)
- die("bind");
+ err(1, "bind");
if (listen(sfd, backlog) < 0)
- die("listen");
+ err(1, "listen");
f = emalloc(sizeof(struct foo));
f->nsucc = 0;
@@ -210,10 +208,10 @@ main(int argc, char *argv[])
printf("[%s] accepted client: %d\n", argv0, f->cfd);
switch (fork()) {
case -1:
- die("fork");
+ err(1, "fork");
case 0:
if (srv(f) < 0)
- perror("srv");
+ warnx("srv failed");
_exit(0);
default:
(void)close(f->cfd);
diff --git a/c-os2/lab2_fifo.c b/c-os2/lab2_fifo.c
@@ -2,18 +2,12 @@
#include <sys/stat.h>
#include <sys/wait.h>
+#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-static void
-die(const char *str)
-{
- perror(str);
- exit(1);
-}
-
int
main(int argc, char *argv[])
{
@@ -23,21 +17,21 @@ main(int argc, char *argv[])
int ret, val;
if ((ret = mkfifo(coolfifo, 0600)) < 0)
- die("mkfifo");
+ err(1, "mkfifo");
switch (pid = fork()) {
case -1:
- die("fork");
+ err(1, "fork");
case 0:
if ((cfp = fopen(coolfifo, "w")) == NULL)
- die("fopen");
+ err(1, "fopen");
ret = fprintf(cfp, "%d", 1000);
fflush(cfp);
exit(0);
default:
if ((pfp = fopen(coolfifo, "r")) == NULL)
- die("fopen");
+ err(1, "fopen");
if ((ret = fscanf(pfp, "%d", &val)) < 0)
- die("fscanf");
+ err(1, "fscanf");
fclose(pfp);
printf("parent: recv: %d\n", val);
unlink(coolfifo);
diff --git a/c-os2/lab2_pipe.c b/c-os2/lab2_pipe.c
@@ -11,14 +11,6 @@
static char *argv0;
-static void
-die(const char *str)
-{
- fprintf(stderr, "%s: ", argv0);
- perror(str);
- exit(1);
-}
-
int
main(int argc, char *argv[])
{
@@ -33,18 +25,18 @@ main(int argc, char *argv[])
return 1;
}
if ((fp = fopen(argv[1], "r")) == NULL)
- die("fopen");
+ err(1, "fopen");
if (pipe(fd) < 0)
- die("pipe");
+ err(1, "pipe");
switch (pid = fork()) {
case -1:
- die("fork");
+ err(1, "fork");
case 0:
close(fd[1]);
if (fd[0] != STDIN_FILENO) {
if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO)
- die("dup2");
+ err(1, "dup2");
close(fd[0]);
}
if ((pager = getenv("PAGER")) == NULL)
@@ -54,19 +46,19 @@ main(int argc, char *argv[])
else
argv0 = pager;
if (execlp(pager, argv0, NULL) < 0)
- die("execlp");
+ err(1, "execlp");
default:
close(fd[0]);
while (fgets(buf, BUFSIZ, fp) != NULL) {
n = strlen(buf);
if (write(fd[1], buf, n) != n)
- die("write");
+ err(1, "write");
}
if (ferror(fp))
- die("fgets");
+ err(1, "fgets");
close(fd[1]);
if (waitpid(pid, NULL, 0) < 0)
- die("waitpid");
+ err(1, "waitpid");
exit(0);
}
diff --git a/cpp-oop/game/Engine.cc b/cpp-oop/game/Engine.cc
@@ -142,11 +142,6 @@ Engine::init_entities()
srand(time(nullptr));
calc_pos(&x, &y);
- /*
- * Passing the parameters in reverse order (i.e `y, x` instead of
- * `x, y`) so that mvwaddch(3) doesn't print the entity on the
- * wrong coordinates.
- */
entities.push_back(new Potter(x, y, Movable::Direction::DOWN, 'P'));
for (i = 0; i < nenemies; i++) {
calc_pos(&x, &y);
@@ -267,7 +262,7 @@ Engine::enemies_move()
{
std::map<int, int> dists;
int ex, ey;
- auto mindist = [](const std::pair<int, int>& a, const std::pair<int, int>& b) {
+ auto distcmp = [](const std::pair<int, int>& a, const std::pair<int, int>& b) {
return a.first < b.second;
};
@@ -292,7 +287,7 @@ Engine::enemies_move()
if (!dists.empty()) {
auto min = std::min_element(dists.begin(),
- dists.end(), mindist);
+ dists.end(), distcmp);
e->set_newpos(min->second, wxmax, wymax);
}
}
diff --git a/cpp-oop/game/Score.cc b/cpp-oop/game/Score.cc
@@ -1,9 +1,8 @@
-#include <cstring>
#include "Score.hpp"
Score::Score(const char *scorefile)
{
- (void)memset(&hiscores, 0, sizeof(hiscores));
+ curscore = 0;
}
Score::~Score()
diff --git a/cpp-oop/game/Score.hpp b/cpp-oop/game/Score.hpp
@@ -2,6 +2,7 @@
#define _SCORE_HPP_
#include <fstream>
+#include <vector>
class Score {
private:
@@ -10,6 +11,9 @@ private:
int score;
} hiscores[5];
+ std::fstream sf;
+ int curscore;
+
public:
Score(const char *scorefile);
~Score();
diff --git a/cpp-oop/game/main.cc b/cpp-oop/game/main.cc
@@ -2,9 +2,6 @@
#include "Engine.hpp"
-/* Function declarations */
-static void die(const std::string&);
-
/* Program name */
static char *argv0;