diff options
author | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-10-19 11:31:50 +0200 |
---|---|---|
committer | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-10-19 11:32:08 +0200 |
commit | 104dabefd62952f2d892a2dcdfb5700d9379ac00 (patch) | |
tree | 39b62d01627e67177e7b156e5eb0be1c3163a1ca | |
parent | 261b81eeaada702eb254e502462ac3a4d1249ec0 (diff) |
add shared code in common.c
-rwxr-xr-x | build.sh | 5 | ||||
-rw-r--r-- | common.c | 20 | ||||
-rw-r--r-- | config.h | 16 |
3 files changed, 24 insertions, 17 deletions
@@ -1,3 +1,4 @@ #!/bin/sh -gcc -g -Wall -pedantic -std=c99 -o server server.c -gcc -g -Wall -pedantic -std=c99 -o chatty client.c +set -x +gcc -g -Wall -pedantic -std=c99 -o chatty client.c common.c +gcc -g -Wall -pedantic -std=c99 -o server server.c common.c diff --git a/common.c b/common.c new file mode 100644 index 0000000..b177987 --- /dev/null +++ b/common.c @@ -0,0 +1,20 @@ +#include "config.h" +#include <stdarg.h> +#include <strings.h> +#include <unistd.h> + +// wrapper for write +void writef(char *format, ...) +{ + va_list args; + char buf[BUF_MAX + 1]; + va_start(args, format); + + vsnprintf(buf, sizeof(buf), format, args); + va_end(args); + + int n = 0; + while (*(buf + n) != 0) + n++; + write(0, buf, n); +} @@ -10,21 +10,7 @@ // current user's name #define USERNAME "unrtdqttr" -// wrapper for write -void writef(char *format, ...) -{ - va_list args; - char buf[BUF_MAX +1]; - va_start(args, format); - - vsnprintf(buf, sizeof(buf), format, args); - va_end(args); - - int n = 0; - while (*(buf + n) != 0) - n++; - write(0, buf, n); -} +void writef(char* format, ...); struct message { char buf[MSG_MAX]; |