From b033314bf1901d436dc71d41d5e1f37dda47e511 Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Thu, 12 Dec 2024 16:03:08 +0100 Subject: formatting: Use clang-format on all projects This commit also add braces to all `if` statements. --- userland/libc/Makefile | 1 + userland/libc/arpa/inet/ntohl.c | 4 +++- userland/libc/arpa/inet/ntohs.c | 2 +- userland/libc/ctype/isalnum.c | 2 +- userland/libc/ctype/isalpha.c | 4 +++- userland/libc/ctype/isascii.c | 4 +++- userland/libc/ctype/isblank.c | 2 +- userland/libc/ctype/iscntrl.c | 5 ++--- userland/libc/ctype/isprint.c | 4 +++- userland/libc/ctype/ispunct.c | 2 +- userland/libc/ctype/tolower.c | 3 ++- userland/libc/ctype/toupper.c | 3 ++- userland/libc/dirent/readdir.c | 6 +++-- userland/libc/dirent/scandir.c | 6 +++-- userland/libc/fcntl/open.c | 2 +- userland/libc/include/assert.h | 4 ++-- userland/libc/include/math.h | 12 +++++----- userland/libc/include/pty.h | 4 ++-- userland/libc/include/setjmp.h | 6 ++--- userland/libc/include/sys/sendfile.h | 7 +++--- userland/libc/include/tb/sb.h | 3 +-- userland/libc/isspace.c | 2 +- userland/libc/libgen/basename.c | 12 ++++++---- userland/libc/libgen/dirname.c | 18 ++++++++++----- userland/libc/malloc/malloc.h | 2 +- userland/libc/memset.c | 3 ++- userland/libc/pty.c | 20 ++++++++--------- userland/libc/signal/sigaction.c | 5 ++--- userland/libc/stdio/feof.c | 2 +- userland/libc/stdio/ferror.c | 2 +- userland/libc/stdio/fflush.c | 2 +- userland/libc/stdio/fgetc.c | 9 +++++--- userland/libc/stdio/fgets.c | 9 +++++--- userland/libc/stdio/fileno.c | 2 +- userland/libc/stdio/fputc.c | 3 ++- userland/libc/stdio/fputs.c | 6 +++-- userland/libc/stdio/fscanf.c | 2 +- userland/libc/stdio/getchar.c | 4 +++- userland/libc/stdio/open_memstream.c | 9 +++++--- userland/libc/stdio/putc.c | 4 +++- userland/libc/stdio/remove.c | 4 ++-- userland/libc/stdio/rename.c | 2 +- userland/libc/stdio/stdin.c | 6 +++-- userland/libc/stdio/tmpfile.c | 10 ++++----- userland/libc/stdio/ungetc.c | 3 ++- userland/libc/stdio/vfprintf.c | 42 +++++++++++++++++++++++------------ userland/libc/stdio/vprintf.c | 4 +++- userland/libc/stdlib/abort.c | 2 +- userland/libc/stdlib/abs.c | 4 +++- userland/libc/stdlib/atexit.c | 4 ++-- userland/libc/stdlib/atof.c | 2 +- userland/libc/stdlib/atoi.c | 4 +++- userland/libc/stdlib/getenv.c | 6 ++--- userland/libc/stdlib/mkstemp.c | 7 ++++-- userland/libc/stdlib/qsort.c | 5 +++-- userland/libc/stdlib/strtod.c | 6 +++-- userland/libc/stdlib/strtol.c | 9 +++++--- userland/libc/stdlib/strtoll.c | 11 +++++---- userland/libc/stdlib/strtoul.c | 9 +++++--- userland/libc/stdlib/system.c | 5 +++-- userland/libc/string/memchr.c | 2 +- userland/libc/string/memcmp.c | 6 +++-- userland/libc/string/memcpy.c | 12 ++++++---- userland/libc/string/sscanf.c | 12 ++++++---- userland/libc/string/strcasecmp.c | 6 +++-- userland/libc/string/strchr.c | 8 ++++--- userland/libc/string/strcmp.c | 6 +++-- userland/libc/string/strcspn.c | 2 +- userland/libc/string/strdup.c | 3 ++- userland/libc/string/strlcpy.c | 6 +++-- userland/libc/string/strncasecmp.c | 8 ++++--- userland/libc/string/strncmp.c | 6 +++-- userland/libc/string/strncpy.c | 6 +++-- userland/libc/string/strndup.c | 5 +++-- userland/libc/string/strnlen.c | 6 +++-- userland/libc/string/strpbrk.c | 2 +- userland/libc/string/strrchr.c | 8 ++++--- userland/libc/string/strspn.c | 3 ++- userland/libc/string/strstr.c | 10 +++++---- userland/libc/string/strtok.c | 12 +++++----- userland/libc/sys/mman.h | 2 +- userland/libc/sys/mman/mmap.c | 4 ++-- userland/libc/sys/mman/munmap.c | 2 +- userland/libc/sys/sendfile.c | 2 +- userland/libc/sys/socket/listen.c | 6 ++--- userland/libc/sys/socket/sendto.c | 2 +- userland/libc/sys/time/gettimeofday.c | 6 +++-- userland/libc/sys/types.h | 4 ++-- userland/libc/tb/sha1.c | 37 +++++++++++++++++++----------- userland/libc/time/gmtime.c | 2 +- userland/libc/time/localtime.c | 28 +++++++++++------------ userland/libc/time/strftime.c | 8 +++---- userland/libc/time/time.c | 3 ++- userland/libc/ubsan.c | 4 ++-- userland/libc/unistd/_exit.c | 4 +++- userland/libc/unistd/chdir.c | 6 +++-- userland/libc/unistd/getpid.c | 6 ++--- userland/libc/unistd/isatty.c | 5 +++-- userland/libc/unistd/msleep.c | 6 +++-- userland/libc/unistd/unlink.c | 2 +- userland/libc/unistd/uptime.c | 4 +++- userland/libc/unistd/write.c | 2 +- 102 files changed, 378 insertions(+), 243 deletions(-) (limited to 'userland/libc') diff --git a/userland/libc/Makefile b/userland/libc/Makefile index e5aeff1..19dabe6 100644 --- a/userland/libc/Makefile +++ b/userland/libc/Makefile @@ -6,6 +6,7 @@ OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.o sys/mman/munmap.o memset all: libc.a %.o: %.c + clang-format -i $< $(CC) $(CFLAGS) -I. -o $@ -c $< %.o: %.s diff --git a/userland/libc/arpa/inet/ntohl.c b/userland/libc/arpa/inet/ntohl.c index 456819d..4cf3d8c 100644 --- a/userland/libc/arpa/inet/ntohl.c +++ b/userland/libc/arpa/inet/ntohl.c @@ -2,4 +2,6 @@ #include #include -uint32_t ntohl(uint32_t nl) { return htonl(nl); } +uint32_t ntohl(uint32_t nl) { + return htonl(nl); +} diff --git a/userland/libc/arpa/inet/ntohs.c b/userland/libc/arpa/inet/ntohs.c index e0324ff..0a97b50 100644 --- a/userland/libc/arpa/inet/ntohs.c +++ b/userland/libc/arpa/inet/ntohs.c @@ -3,5 +3,5 @@ #include uint16_t ntohs(uint16_t nl) { - return htons(nl); + return htons(nl); } diff --git a/userland/libc/ctype/isalnum.c b/userland/libc/ctype/isalnum.c index 870728d..073b8bb 100644 --- a/userland/libc/ctype/isalnum.c +++ b/userland/libc/ctype/isalnum.c @@ -2,5 +2,5 @@ // This is probably the most useless libc function I have seen so far. int isalnum(int c) { - return isalpha(c) || isdigit(c); + return isalpha(c) || isdigit(c); } diff --git a/userland/libc/ctype/isalpha.c b/userland/libc/ctype/isalpha.c index 130f493..38c3cb4 100644 --- a/userland/libc/ctype/isalpha.c +++ b/userland/libc/ctype/isalpha.c @@ -1,4 +1,6 @@ #include // https://pubs.opengroup.org/onlinepubs/9699919799/functions/isalpha.html -int isalpha(int c) { return (('A' <= toupper(c)) && ('Z' >= toupper(c))); } +int isalpha(int c) { + return (('A' <= toupper(c)) && ('Z' >= toupper(c))); +} diff --git a/userland/libc/ctype/isascii.c b/userland/libc/ctype/isascii.c index 660c8bb..b598dde 100644 --- a/userland/libc/ctype/isascii.c +++ b/userland/libc/ctype/isascii.c @@ -1,4 +1,6 @@ #include // https://pubs.opengroup.org/onlinepubs/9699919799/functions/isascii.html -int isascii(int c) { return (c < 128); } +int isascii(int c) { + return (c < 128); +} diff --git a/userland/libc/ctype/isblank.c b/userland/libc/ctype/isblank.c index 488fa88..88ffa0b 100644 --- a/userland/libc/ctype/isblank.c +++ b/userland/libc/ctype/isblank.c @@ -1,5 +1,5 @@ #include int isblank(int c) { - return (c == ' ' || c == '\t'); + return (c == ' ' || c == '\t'); } diff --git a/userland/libc/ctype/iscntrl.c b/userland/libc/ctype/iscntrl.c index 92ed7f0..4fead0d 100644 --- a/userland/libc/ctype/iscntrl.c +++ b/userland/libc/ctype/iscntrl.c @@ -1,6 +1,5 @@ #include -int iscntrl(int c) -{ - return (unsigned)c < 0x20 || c == 0x7f; +int iscntrl(int c) { + return (unsigned)c < 0x20 || c == 0x7f; } diff --git a/userland/libc/ctype/isprint.c b/userland/libc/ctype/isprint.c index e6a3d0e..5504ebc 100644 --- a/userland/libc/ctype/isprint.c +++ b/userland/libc/ctype/isprint.c @@ -1,3 +1,5 @@ #include -int isprint(int c) { return c > 0x20 && 0x7F != c; } +int isprint(int c) { + return c > 0x20 && 0x7F != c; +} diff --git a/userland/libc/ctype/ispunct.c b/userland/libc/ctype/ispunct.c index 18dc7d8..abef66d 100644 --- a/userland/libc/ctype/ispunct.c +++ b/userland/libc/ctype/ispunct.c @@ -2,5 +2,5 @@ // https://pubs.opengroup.org/onlinepubs/9699919799/functions/ispunct.html int ispunct(int c) { - return (c == '.'); + return (c == '.'); } diff --git a/userland/libc/ctype/tolower.c b/userland/libc/ctype/tolower.c index f1bb163..c14ea54 100644 --- a/userland/libc/ctype/tolower.c +++ b/userland/libc/ctype/tolower.c @@ -1,7 +1,8 @@ #include int tolower(int c) { - if (c >= 'A' && c <= 'Z') + if (c >= 'A' && c <= 'Z') { return c - 'A' + 'a'; + } return c; } diff --git a/userland/libc/ctype/toupper.c b/userland/libc/ctype/toupper.c index 0f33886..599a0ff 100644 --- a/userland/libc/ctype/toupper.c +++ b/userland/libc/ctype/toupper.c @@ -1,7 +1,8 @@ #include int toupper(int c) { - if (c >= 'a' && c <= 'z') + if (c >= 'a' && c <= 'z') { return c - 'a' + 'A'; + } return c; } diff --git a/userland/libc/dirent/readdir.c b/userland/libc/dirent/readdir.c index 9a9f8fa..a7f0d66 100644 --- a/userland/libc/dirent/readdir.c +++ b/userland/libc/dirent/readdir.c @@ -18,10 +18,12 @@ struct dirent *readdir(DIR *dir) { size_t offset = dir->dir_num * sizeof(struct dirent); int rc; if (-1 == (rc = pread(dir->fd, &dir->internal_direntry, sizeof(struct dirent), - offset))) + offset))) { return NULL; - if (rc < (int)sizeof(struct dirent)) + } + if (rc < (int)sizeof(struct dirent)) { return NULL; + } dir->dir_num++; return &(dir->internal_direntry); diff --git a/userland/libc/dirent/scandir.c b/userland/libc/dirent/scandir.c index 3ac040a..11f23a9 100644 --- a/userland/libc/dirent/scandir.c +++ b/userland/libc/dirent/scandir.c @@ -70,12 +70,14 @@ void scandir_sane_free(struct dirent **namelist) { int scandir(const char *dir, struct dirent ***namelist, int (*sel)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) { - if (!compar) + if (!compar) { compar = nop_compar; + } DIR *d = opendir(dir); - if (!d) + if (!d) { return -1; + } struct dirent **list = NULL; struct dirent *e; int rc = 0; diff --git a/userland/libc/fcntl/open.c b/userland/libc/fcntl/open.c index 357d826..1e8781a 100644 --- a/userland/libc/fcntl/open.c +++ b/userland/libc/fcntl/open.c @@ -1,6 +1,6 @@ #include -#include #include +#include int open(const char *file, int flags, ...) { mode_t mode = 0; diff --git a/userland/libc/include/assert.h b/userland/libc/include/assert.h index a009e5f..c35845e 100644 --- a/userland/libc/include/assert.h +++ b/userland/libc/include/assert.h @@ -1,10 +1,10 @@ #ifndef ASSERT_H #define ASSERT_H -#define assert(expr) \ +#define assert(expr) \ { \ if (!(expr)) \ - aFailed(__FILE__, __LINE__); \ + aFailed(__FILE__, __LINE__); \ } void aFailed(char *f, int l); #endif diff --git a/userland/libc/include/math.h b/userland/libc/include/math.h index 7168a5d..3590caa 100644 --- a/userland/libc/include/math.h +++ b/userland/libc/include/math.h @@ -1,16 +1,16 @@ #define max(_a, _b) ((_a) > (_b) ? (_a) : (_b)) #define min(_a, _b) ((_a) < (_b) ? (_a) : (_b)) -#if 100*__GNUC__+__GNUC_MINOR__ >= 303 -#define NAN __builtin_nanf("") -#define INFINITY __builtin_inff() +#if 100 * __GNUC__ + __GNUC_MINOR__ >= 303 +#define NAN __builtin_nanf("") +#define INFINITY __builtin_inff() #else -#define NAN (0.0f/0.0f) -#define INFINITY 1e40f +#define NAN (0.0f / 0.0f) +#define INFINITY 1e40f #endif #define HUGE_VALF INFINITY -#define HUGE_VAL ((double)INFINITY) +#define HUGE_VAL ((double)INFINITY) #define HUGE_VALL ((long double)INFINITY) double ldexp(double x, int exp); diff --git a/userland/libc/include/pty.h b/userland/libc/include/pty.h index b8ce978..e5de5a3 100644 --- a/userland/libc/include/pty.h +++ b/userland/libc/include/pty.h @@ -1,6 +1,6 @@ #ifndef PTY_H #define PTY_H int openpty(int *amaster, int *aslave, char *name, - /*const struct termios*/ void *termp, - /*const struct winsize*/ void *winp); + /*const struct termios*/ void *termp, + /*const struct winsize*/ void *winp); #endif diff --git a/userland/libc/include/setjmp.h b/userland/libc/include/setjmp.h index ea15cf3..ec7e23c 100644 --- a/userland/libc/include/setjmp.h +++ b/userland/libc/include/setjmp.h @@ -2,9 +2,9 @@ #define SETJMP_H typedef unsigned long __jmp_buf[6]; typedef struct __jmp_buf_tag { - __jmp_buf __jb; - unsigned long __fl; - unsigned long __ss[128/sizeof(long)]; + __jmp_buf __jb; + unsigned long __fl; + unsigned long __ss[128 / sizeof(long)]; } jmp_buf[1]; typedef jmp_buf sigjmp_buf; diff --git a/userland/libc/include/sys/sendfile.h b/userland/libc/include/sys/sendfile.h index 5e83999..6fdd21b 100644 --- a/userland/libc/include/sys/sendfile.h +++ b/userland/libc/include/sys/sendfile.h @@ -1,7 +1,6 @@ -#include -#include #include +#include #include +#include -u32 sendfile(int out_fd, int in_fd, off_t *offset, size_t count, - int *error_rc); +u32 sendfile(int out_fd, int in_fd, off_t *offset, size_t count, int *error_rc); diff --git a/userland/libc/include/tb/sb.h b/userland/libc/include/tb/sb.h index 8493997..7832015 100644 --- a/userland/libc/include/tb/sb.h +++ b/userland/libc/include/tb/sb.h @@ -22,8 +22,7 @@ int sb_isempty(const struct sb *ctx); int sb_append_char(struct sb *ctx, char c); int sb_delete_right(struct sb *ctx, size_t n); int sb_append(struct sb *ctx, const char *s); -int sb_append_buffer(struct sb *ctx, const char *buffer, - size_t length); +int sb_append_buffer(struct sb *ctx, const char *buffer, size_t length); int sb_append_sv(struct sb *ctx, struct sv sv); int sb_prepend_sv(struct sb *ctx, struct sv sv); int sb_prepend_buffer(struct sb *ctx, const char *buffer, size_t length); diff --git a/userland/libc/isspace.c b/userland/libc/isspace.c index 9ba2766..a922d91 100644 --- a/userland/libc/isspace.c +++ b/userland/libc/isspace.c @@ -1,5 +1,5 @@ #include int isspace(int c) { - return c == ' ' || (unsigned)c-'\t' < 5; + return c == ' ' || (unsigned)c - '\t' < 5; } diff --git a/userland/libc/libgen/basename.c b/userland/libc/libgen/basename.c index a0a8adb..64a533d 100644 --- a/userland/libc/libgen/basename.c +++ b/userland/libc/libgen/basename.c @@ -19,18 +19,21 @@ char *basename_slash_return_value = "/"; char *basename(char *path) { // If path is a null pointer or points to an empty string, basename() // shall return a pointer to the string ".". - if (NULL == path || '\0' == *path) + if (NULL == path || '\0' == *path) { return basename_empty_return_value; + } char *start = path; // Move the string to the end for (; *path; path++) ; - if (start == path) + if (start == path) { return start; + } path--; - if ('/' == *path) // Trailing slash + if ('/' == *path) { // Trailing slash *path = '\0'; + } // Loop until next slash is found for (; path != start && '/' != *path; path--) ; @@ -39,7 +42,8 @@ char *basename(char *path) { // pointed to by path is exactly "//", it is implementation-defined whether //'/' or "//" is returned. path++; - if ('\0' == *path) + if ('\0' == *path) { return basename_slash_return_value; + } return path; } diff --git a/userland/libc/libgen/dirname.c b/userland/libc/libgen/dirname.c index fb3c813..feaf784 100644 --- a/userland/libc/libgen/dirname.c +++ b/userland/libc/libgen/dirname.c @@ -5,18 +5,21 @@ char *dirname_slash_return_value = "/"; char *dirname(char *path) { // If path is a null pointer or points to an empty string, // dirname() shall return a pointer to the string "." - if (!path) + if (!path) { return dirname_empty_return_value; - if ('\0' == *path) + } + if ('\0' == *path) { return dirname_empty_return_value; + } char *start = path; for (; *path; path++) ; path--; if ('/' == *path) { - if (start == path) + if (start == path) { return path; + } path--; } @@ -24,11 +27,13 @@ char *dirname(char *path) { ; // If path does not contain a '/', then dirname() shall return a pointer to // the string ".". - if ('/' != *path) + if ('/' != *path) { return dirname_empty_return_value; + } - if (path == start) + if (path == start) { return dirname_slash_return_value; + } *path = '\0'; path--; @@ -36,8 +41,9 @@ char *dirname(char *path) { for (; path != start && '/' != *path; path--) ; - if ('/' != *path) + if ('/' != *path) { return dirname_empty_return_value; + } path++; return path; diff --git a/userland/libc/malloc/malloc.h b/userland/libc/malloc/malloc.h index 596dbe5..d6a6ab7 100644 --- a/userland/libc/malloc/malloc.h +++ b/userland/libc/malloc/malloc.h @@ -1,7 +1,7 @@ #ifndef MALLOC_H #define MALLOC_H -#include #include +#include void *malloc(size_t s); void *calloc(size_t nelem, size_t elsize); diff --git a/userland/libc/memset.c b/userland/libc/memset.c index 51910e9..6baa3ca 100644 --- a/userland/libc/memset.c +++ b/userland/libc/memset.c @@ -7,8 +7,9 @@ void *memset(void *s, int c, size_t n) { // s. unsigned char *p = s; - for (; n > 0; n--, p++) + for (; n > 0; n--, p++) { *p = (unsigned char)c; + } // The memset() function shall return s return s; diff --git a/userland/libc/pty.c b/userland/libc/pty.c index 17b91de..d64e212 100644 --- a/userland/libc/pty.c +++ b/userland/libc/pty.c @@ -2,14 +2,14 @@ #include "syscall.h" int openpty(int *amaster, int *aslave, char *name, - /*const struct termios*/ void *termp, - /*const struct winsize*/ void *winp) { - SYS_OPENPTY_PARAMS args = { - .amaster = amaster, - .aslave = aslave, - .name = name, - .termp = termp, - .winp = winp, - }; - return syscall(SYS_OPENPTY, &args, 0, 0, 0, 0); + /*const struct termios*/ void *termp, + /*const struct winsize*/ void *winp) { + SYS_OPENPTY_PARAMS args = { + .amaster = amaster, + .aslave = aslave, + .name = name, + .termp = termp, + .winp = winp, + }; + return syscall(SYS_OPENPTY, &args, 0, 0, 0, 0); } diff --git a/userland/libc/signal/sigaction.c b/userland/libc/signal/sigaction.c index 1ff22a7..97ba6ed 100644 --- a/userland/libc/signal/sigaction.c +++ b/userland/libc/signal/sigaction.c @@ -1,7 +1,6 @@ #include #include -int sigaction(int sig, const struct sigaction *act, - struct sigaction *oact) { - RC_ERRNO(syscall(SYS_SIGACTION, sig, act, oact, 0,0)) +int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) { + RC_ERRNO(syscall(SYS_SIGACTION, sig, act, oact, 0, 0)) } diff --git a/userland/libc/stdio/feof.c b/userland/libc/stdio/feof.c index 7f46301..91e7e56 100644 --- a/userland/libc/stdio/feof.c +++ b/userland/libc/stdio/feof.c @@ -1,5 +1,5 @@ #include int feof(FILE *stream) { - return stream->is_eof; + return stream->is_eof; } diff --git a/userland/libc/stdio/ferror.c b/userland/libc/stdio/ferror.c index 8cb46cf..5049db8 100644 --- a/userland/libc/stdio/ferror.c +++ b/userland/libc/stdio/ferror.c @@ -1,5 +1,5 @@ #include int ferror(FILE *stream) { - return stream->has_error; + return stream->has_error; } diff --git a/userland/libc/stdio/fflush.c b/userland/libc/stdio/fflush.c index 1cadc72..8bfd16d 100644 --- a/userland/libc/stdio/fflush.c +++ b/userland/libc/stdio/fflush.c @@ -1,6 +1,6 @@ +#include #include #include -#include // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fflush.html int fflush(FILE *stream) { diff --git a/userland/libc/stdio/fgetc.c b/userland/libc/stdio/fgetc.c index afa299f..dc7af77 100644 --- a/userland/libc/stdio/fgetc.c +++ b/userland/libc/stdio/fgetc.c @@ -8,13 +8,16 @@ int fgetc(FILE *stream) { return stream->buffered_char; } char c; - if (1 == fread(&c, 1, 1, stream)) + if (1 == fread(&c, 1, 1, stream)) { return (int)c; + } // FIXME: Should use feof and ferror - if (stream->has_error) + if (stream->has_error) { return EOF; - if (stream->is_eof) + } + if (stream->is_eof) { return EOF; + } // How did we get here? assert(0); return EOF; diff --git a/userland/libc/stdio/fgets.c b/userland/libc/stdio/fgets.c index 8e21501..acc89a0 100644 --- a/userland/libc/stdio/fgets.c +++ b/userland/libc/stdio/fgets.c @@ -4,13 +4,16 @@ char *fgets(char *s, int n, FILE *stream) { for (int i = 0; i < n; i++) { char c; fread(&c, 1, 1, stream); - if (stream->has_error) + if (stream->has_error) { return NULL; - if (stream->is_eof) + } + if (stream->is_eof) { return NULL; + } s[i] = c; - if (c == '\n') + if (c == '\n') { break; + } } return s; } diff --git a/userland/libc/stdio/fileno.c b/userland/libc/stdio/fileno.c index 246cc51..bdf9332 100644 --- a/userland/libc/stdio/fileno.c +++ b/userland/libc/stdio/fileno.c @@ -1,5 +1,5 @@ -#include #include +#include // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fileno.html // The fileno() function shall return the integer file descriptor associated diff --git a/userland/libc/stdio/fputc.c b/userland/libc/stdio/fputc.c index 7c8fa7c..c41ffa4 100644 --- a/userland/libc/stdio/fputc.c +++ b/userland/libc/stdio/fputc.c @@ -1,7 +1,8 @@ #include int fputc(int c, FILE *stream) { - if (fwrite(&c, 1, 1, stream) > 0) + if (fwrite(&c, 1, 1, stream) > 0) { return c; + } return EOF; } diff --git a/userland/libc/stdio/fputs.c b/userland/libc/stdio/fputs.c index 1b70c66..5570693 100644 --- a/userland/libc/stdio/fputs.c +++ b/userland/libc/stdio/fputs.c @@ -2,8 +2,10 @@ int fputs(const char *s, FILE *stream) { const char *b = s; - for (; *s; s++) - if (0 == fwrite(s, 1, 1, stream)) + for (; *s; s++) { + if (0 == fwrite(s, 1, 1, stream)) { return EOF; + } + } return s - b; } diff --git a/userland/libc/stdio/fscanf.c b/userland/libc/stdio/fscanf.c index 4af79f9..f8803fc 100644 --- a/userland/libc/stdio/fscanf.c +++ b/userland/libc/stdio/fscanf.c @@ -1,5 +1,5 @@ -#include #include +#include int fscanf(FILE *stream, const char *format, ...) { va_list ap; diff --git a/userland/libc/stdio/getchar.c b/userland/libc/stdio/getchar.c index dad2263..e7991cd 100644 --- a/userland/libc/stdio/getchar.c +++ b/userland/libc/stdio/getchar.c @@ -1,4 +1,6 @@ #include // The getchar() function shall be equivalent to getc(stdin). -int getchar(void) { return fgetc(stdin); } +int getchar(void) { + return fgetc(stdin); +} diff --git a/userland/libc/stdio/open_memstream.c b/userland/libc/stdio/open_memstream.c index b129cb6..b7ea70b 100644 --- a/userland/libc/stdio/open_memstream.c +++ b/userland/libc/stdio/open_memstream.c @@ -35,8 +35,9 @@ size_t memstream_write(FILE *fp, const unsigned char *buf, size_t n) { memcpy(c->buffer + fp->offset_in_file, buf, n); fp->offset_in_file += n; - if (fp->offset_in_file > (long)c->buffer_usage) + if (fp->offset_in_file > (long)c->buffer_usage) { c->buffer_usage = fp->offset_in_file; + } return n; } @@ -46,8 +47,9 @@ size_t memstream_read(FILE *fp, unsigned char *buf, size_t n) { n = min(length_left, n); memcpy(buf, c->buffer + fp->offset_in_file, n); fp->offset_in_file += n; - if (0 == n) + if (0 == n) { fp->is_eof = 1; + } return n; } @@ -73,8 +75,9 @@ int memstream_seek(FILE *stream, long offset, int whence) { FILE *open_memstream(char **bufp, size_t *sizep) { struct Memstream *c = NULL; FILE *fp = malloc(sizeof(FILE)); - if (!fp) + if (!fp) { return NULL; + } fp->offset_in_file = 0; fp->buffered_char = 0; diff --git a/userland/libc/stdio/putc.c b/userland/libc/stdio/putc.c index a468a95..cb89ed9 100644 --- a/userland/libc/stdio/putc.c +++ b/userland/libc/stdio/putc.c @@ -1,3 +1,5 @@ #include -int putc(int c, FILE *stream) { return fputc(c, stream);} +int putc(int c, FILE *stream) { + return fputc(c, stream); +} diff --git a/userland/libc/stdio/remove.c b/userland/libc/stdio/remove.c index 39f8af2..374be58 100644 --- a/userland/libc/stdio/remove.c +++ b/userland/libc/stdio/remove.c @@ -1,9 +1,9 @@ -#include #include +#include extern int errno; int remove(const char *path) { - (void)path; + (void)path; // FIXME errno = ENAMETOOLONG; return -1; diff --git a/userland/libc/stdio/rename.c b/userland/libc/stdio/rename.c index a02cacb..676fd8c 100644 --- a/userland/libc/stdio/rename.c +++ b/userland/libc/stdio/rename.c @@ -5,5 +5,5 @@ int rename(const char *old, const char *new) { (void)old; (void)new; assert(0); // TODO: Implement - return 0; + return 0; } diff --git a/userland/libc/stdio/stdin.c b/userland/libc/stdio/stdin.c index e497167..4620df3 100644 --- a/userland/libc/stdio/stdin.c +++ b/userland/libc/stdio/stdin.c @@ -36,8 +36,9 @@ size_t write_fd(FILE *f, const unsigned char *s, size_t l) { size_t non_cache_read_fd(FILE *f, unsigned char *s, size_t l) { int rc = read(f->fd, s, l); - if (rc == 0) + if (rc == 0) { f->is_eof = 1; + } if (rc == -1) { f->has_error = 1; return 0; @@ -46,8 +47,9 @@ size_t non_cache_read_fd(FILE *f, unsigned char *s, size_t l) { } size_t read_fd(FILE *f, unsigned char *s, size_t l) { - if (0 == l) + if (0 == l) { return 0; + } // Skip using cache if the length being requested if longer than or // equal to the cache block size. This avoids doing a bunch of extra diff --git a/userland/libc/stdio/tmpfile.c b/userland/libc/stdio/tmpfile.c index cee6e0a..9d3fb92 100644 --- a/userland/libc/stdio/tmpfile.c +++ b/userland/libc/stdio/tmpfile.c @@ -1,9 +1,9 @@ -#include #include +#include FILE *tmpfile(void) { - // TODO - printf("TODO: Implement tmpfile()\n"); - assert(0); - return NULL; + // TODO + printf("TODO: Implement tmpfile()\n"); + assert(0); + return NULL; } diff --git a/userland/libc/stdio/ungetc.c b/userland/libc/stdio/ungetc.c index 3b93b70..18e5a21 100644 --- a/userland/libc/stdio/ungetc.c +++ b/userland/libc/stdio/ungetc.c @@ -1,8 +1,9 @@ #include int ungetc(int c, FILE *stream) { - if (stream->has_buffered_char) + if (stream->has_buffered_char) { return EOF; + } stream->buffered_char = c; stream->has_buffered_char = 1; fseek(stream, -1, SEEK_CUR); diff --git a/userland/libc/stdio/vfprintf.c b/userland/libc/stdio/vfprintf.c index d384c1d..35a7c9a 100644 --- a/userland/libc/stdio/vfprintf.c +++ b/userland/libc/stdio/vfprintf.c @@ -32,8 +32,9 @@ int fprint_num(FILE *f, long long n, int base, char *char_set, int prefix, str[i] = char_set[0]; i++; } else { - for (; n != 0 && i < 32; i++, n /= base) + for (; n != 0 && i < 32; i++, n /= base) { str[i] = char_set[(n % base)]; + } } if (is_signed) { @@ -45,16 +46,19 @@ int fprint_num(FILE *f, long long n, int base, char *char_set, int prefix, int orig_i = i; if (!right_padding) { - for (; prefix - orig_i > 0; prefix--) + for (; prefix - orig_i > 0; prefix--) { FILE_WRITE(f, &t, 1, &c); + } } - for (i--; i >= 0; i--) + for (i--; i >= 0; i--) { FILE_WRITE(f, &(str[i]), 1, &c); + } if (right_padding) { - for (; prefix - orig_i > 0; prefix--) + for (; prefix - orig_i > 0; prefix--) { FILE_WRITE(f, &t, 1, &c); + } } return c; } @@ -82,16 +86,19 @@ int print_string(FILE *f, const char *s, int *rc, int prefix, int right_padding, char t = ' '; int c = 0; if (!right_padding) { - if (prefix) + if (prefix) { assert(-1 == precision); // FIXME: Is this correct? - for (; prefix - l > 0; prefix--) + } + for (; prefix - l > 0; prefix--) { FILE_WRITE(f, &t, 1, &c); + } } int bl = precision; for (; *s; s++, (*rc)++) { if (precision != -1) { - if (0 == bl) + if (0 == bl) { break; + } bl--; } int r = 0; @@ -99,8 +106,9 @@ int print_string(FILE *f, const char *s, int *rc, int prefix, int right_padding, } if (right_padding) { assert(-1 == precision); // FIXME: Is this correct? - for (; prefix - l > 0; prefix--) + for (; prefix - l > 0; prefix--) { FILE_WRITE(f, &t, 1, &c); + } } (*rc) += c; return 0; @@ -110,8 +118,9 @@ int parse_precision(const char **fmt) { const char *s = *fmt; int rc = 0; for (int i = 0;; i++, s++) { - if ('\0' == *s) + if ('\0' == *s) { break; + } const char c = *s; if ('*' == c) { assert(i == 0); @@ -149,8 +158,9 @@ int vfprintf(FILE *f, const char *fmt, va_list ap) { continue; } - if ('\0' == *s) + if ('\0' == *s) { break; + } switch (*s) { case '.': @@ -158,14 +168,16 @@ int vfprintf(FILE *f, const char *fmt, va_list ap) { assert('\0' != *s); precision = parse_precision(&s); assert('\0' != *s); - if (-1 == precision) + if (-1 == precision) { precision = va_arg(ap, int); + } cont = 1; break; case '0': prefix *= 10; - if (0 == prefix) + if (0 == prefix) { zero_padding = 1; + } cont = 1; break; case '-': @@ -221,13 +233,15 @@ int vfprintf(FILE *f, const char *fmt, va_list ap) { char *a = va_arg(ap, char *); if (!a) { if (-1 == - print_string(f, "(NULL)", &rc, prefix, right_padding, precision)) + print_string(f, "(NULL)", &rc, prefix, right_padding, precision)) { return -1; + } cont = 0; break; } - if (-1 == print_string(f, a, &rc, prefix, right_padding, precision)) + if (-1 == print_string(f, a, &rc, prefix, right_padding, precision)) { return -1; + } cont = 0; break; } diff --git a/userland/libc/stdio/vprintf.c b/userland/libc/stdio/vprintf.c index 8a8dc33..c06e9f9 100644 --- a/userland/libc/stdio/vprintf.c +++ b/userland/libc/stdio/vprintf.c @@ -1,3 +1,5 @@ #include -int vprintf(const char *format, va_list ap) { return vdprintf(1, format, ap); } +int vprintf(const char *format, va_list ap) { + return vdprintf(1, format, ap); +} diff --git a/userland/libc/stdlib/abort.c b/userland/libc/stdlib/abort.c index 90368a9..5279676 100644 --- a/userland/libc/stdlib/abort.c +++ b/userland/libc/stdlib/abort.c @@ -1,6 +1,6 @@ #include -#include #include +#include // https://pubs.opengroup.org/onlinepubs/9699919799/functions/abort.html void abort(void) { diff --git a/userland/libc/stdlib/abs.c b/userland/libc/stdlib/abs.c index 1079beb..9e50b0a 100644 --- a/userland/libc/stdlib/abs.c +++ b/userland/libc/stdlib/abs.c @@ -1,3 +1,5 @@ #include -int abs(int i) { return (i < 0) ? (-i) : (i); } +int abs(int i) { + return (i < 0) ? (-i) : (i); +} diff --git a/userland/libc/stdlib/atexit.c b/userland/libc/stdlib/atexit.c index 0e401ff..1dc34cc 100644 --- a/userland/libc/stdlib/atexit.c +++ b/userland/libc/stdlib/atexit.c @@ -1,6 +1,6 @@ #include int atexit(void (*func)(void)) { - //TODO - return 0; + // TODO + return 0; } diff --git a/userland/libc/stdlib/atof.c b/userland/libc/stdlib/atof.c index 8524f8b..f008852 100644 --- a/userland/libc/stdlib/atof.c +++ b/userland/libc/stdlib/atof.c @@ -1,5 +1,5 @@ #include double atof(const char *str) { - return strtod(str,(char **)NULL); + return strtod(str, (char **)NULL); } diff --git a/userland/libc/stdlib/atoi.c b/userland/libc/stdlib/atoi.c index 2183306..876f69a 100644 --- a/userland/libc/stdlib/atoi.c +++ b/userland/libc/stdlib/atoi.c @@ -1,4 +1,6 @@ #include // https://pubs.opengroup.org/onlinepubs/9699919799/ -int atoi(const char *str) { return (int)strtol(str, (char **)NULL, 10); } +int atoi(const char *str) { + return (int)strtol(str, (char **)NULL, 10); +} diff --git a/userland/libc/stdlib/getenv.c b/userland/libc/stdlib/getenv.c index 13d871c..0240d66 100644 --- a/userland/libc/stdlib/getenv.c +++ b/userland/libc/stdlib/getenv.c @@ -1,7 +1,7 @@ #include char *getenv(const char *name) { - (void)name; - // FIXME - return NULL; + (void)name; + // FIXME + return NULL; } diff --git a/userland/libc/stdlib/mkstemp.c b/userland/libc/stdlib/mkstemp.c index 1ea8790..0dec8e9 100644 --- a/userland/libc/stdlib/mkstemp.c +++ b/userland/libc/stdlib/mkstemp.c @@ -1,14 +1,17 @@ #include #include -char rand_char(void) { return 'A' + (rand() % 10); } +char rand_char(void) { + return 'A' + (rand() % 10); +} int mkstemp(char *template) { // FIXME: Incomplete const char *s = template; for (; *template; template ++) { - if ('X' == *template) + if ('X' == *template) { *template = rand_char(); + } } return open(s, O_RDWR, O_CREAT); } diff --git a/userland/libc/stdlib/qsort.c b/userland/libc/stdlib/qsort.c index 3f87db5..f60fa5d 100644 --- a/userland/libc/stdlib/qsort.c +++ b/userland/libc/stdlib/qsort.c @@ -6,9 +6,10 @@ void qsort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *)) { // If the nel argument has the value zero, the comparison function pointed to // by compar shall not be called and no rearrangement shall take place. - if (0 == nel) + if (0 == nel) { return; - + } + // AB // Results in negative // BA diff --git a/userland/libc/stdlib/strtod.c b/userland/libc/stdlib/strtod.c index 2c83879..01119bb 100644 --- a/userland/libc/stdlib/strtod.c +++ b/userland/libc/stdlib/strtod.c @@ -1,8 +1,10 @@ +#include #include #include -#include -int ctoi(char c) { return c - '0'; } +int ctoi(char c) { + return c - '0'; +} double strtod(const char *restrict nptr, char **restrict endptr) { double r = 0; diff --git a/userland/libc/stdlib/strtol.c b/userland/libc/stdlib/strtol.c index 460dedd..0b1bbad 100644 --- a/userland/libc/stdlib/strtol.c +++ b/userland/libc/stdlib/strtol.c @@ -9,13 +9,15 @@ int get_value(char c, long base); // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html long strtol(const char *str, char **restrict endptr, int base) { long ret_value = 0; - if (endptr) + if (endptr) { *endptr = (char *)str; + } // Ignore inital white-space sequence for (; *str && isspace(*str); str++) ; - if (!*str) + if (!*str) { return ret_value; + } // int sign = 0; if ('-' == *str) { @@ -63,7 +65,8 @@ long strtol(const char *str, char **restrict endptr, int base) { errno = EINVAL; return 0; } - if (endptr) + if (endptr) { *endptr = (char *)str; + } return ret_value; } diff --git a/userland/libc/stdlib/strtoll.c b/userland/libc/stdlib/strtoll.c index 0e397ff..9a2a624 100644 --- a/userland/libc/stdlib/strtoll.c +++ b/userland/libc/stdlib/strtoll.c @@ -9,13 +9,15 @@ int get_value(char c, long base); // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtoll.html long long strtoll(const char *str, char **restrict endptr, int base) { long long ret_value = 0; - if (endptr) + if (endptr) { *endptr = (char *)str; + } // Ignore inital white-space sequence for (; *str && isspace(*str); str++) ; - if (!*str) + if (!*str) { return ret_value; + } // int sign = 0; if ('-' == *str) { @@ -45,7 +47,7 @@ long long strtoll(const char *str, char **restrict endptr, int base) { if (2 <= base && 36 >= base) { for (; *str; str++) { int val = get_value(*str, base); - if(-1 == val) { + if (-1 == val) { break; } if (ret_value > LLONG_MAX / base) { @@ -63,7 +65,8 @@ long long strtoll(const char *str, char **restrict endptr, int base) { errno = EINVAL; return 0; } - if (endptr) + if (endptr) { *endptr = (char *)str; + } return ret_value; } diff --git a/userland/libc/stdlib/strtoul.c b/userland/libc/stdlib/strtoul.c index 39dc8eb..0f2c71b 100644 --- a/userland/libc/stdlib/strtoul.c +++ b/userland/libc/stdlib/strtoul.c @@ -25,13 +25,15 @@ int get_value(char c, long base) { unsigned long strtoul(const char *restrict str, char **restrict endptr, int base) { unsigned long ret_value = 0; - if (endptr) + if (endptr) { *endptr = (char *)str; + } // Ignore inital white-space sequence for (; *str && isspace(*str); str++) ; - if (!*str) + if (!*str) { return ret_value; + } // int sign = 0; if ('-' == *str) { @@ -79,7 +81,8 @@ unsigned long strtoul(const char *restrict str, char **restrict endptr, errno = EINVAL; return 0; } - if (endptr) + if (endptr) { *endptr = (char *)str; + } return ret_value; } diff --git a/userland/libc/stdlib/system.c b/userland/libc/stdlib/system.c index 1cba966..c35bee5 100644 --- a/userland/libc/stdlib/system.c +++ b/userland/libc/stdlib/system.c @@ -1,10 +1,11 @@ #include -#include #include +#include int system(const char *command) { - if (!command) + if (!command) { return NULL; + } int pid = fork(); if (0 == pid) { char *argv[2]; diff --git a/userland/libc/string/memchr.c b/userland/libc/string/memchr.c index cef3bc2..0d581f4 100644 --- a/userland/libc/string/memchr.c +++ b/userland/libc/string/memchr.c @@ -4,7 +4,7 @@ void *memchr(const void *s, int c, size_t n) { const char *p = s; for (; n > 0; n--, p++) { if (*p == c) { - return (void*)p; + return (void *)p; } } return NULL; diff --git a/userland/libc/string/memcmp.c b/userland/libc/string/memcmp.c index 01109b8..157189f 100644 --- a/userland/libc/string/memcmp.c +++ b/userland/libc/string/memcmp.c @@ -3,9 +3,11 @@ int memcmp(const void *s1, const void *s2, size_t n) { int return_value = 0; - for (uint32_t i = 0; i < n; i++) - if (((unsigned char *)(s1))[i] != ((unsigned char *)(s2))[i]) + for (uint32_t i = 0; i < n; i++) { + if (((unsigned char *)(s1))[i] != ((unsigned char *)(s2))[i]) { return_value++; + } + } return return_value; } diff --git a/userland/libc/string/memcpy.c b/userland/libc/string/memcpy.c index 7a9f01e..65f646d 100644 --- a/userland/libc/string/memcpy.c +++ b/userland/libc/string/memcpy.c @@ -4,15 +4,19 @@ void *memcpy(void *dest, const void *src, uint32_t n) { unsigned char *d = dest; const unsigned char *s = src; - for (; n >= 8; n -= 8, d += 8, s += 8) + for (; n >= 8; n -= 8, d += 8, s += 8) { *(uint64_t *)d = *(uint64_t *)s; + } - for (; n >= 4; n -= 4, d += 4, s += 4) + for (; n >= 4; n -= 4, d += 4, s += 4) { *(uint32_t *)d = *(uint32_t *)s; + } - for (; n >= 2; n -= 2, d += 2, s += 2) + for (; n >= 2; n -= 2, d += 2, s += 2) { *(uint16_t *)d = *(uint16_t *)s; - for (; n; n--) + } + for (; n; n--) { *d++ = *s++; + } return dest; } diff --git a/userland/libc/string/sscanf.c b/userland/libc/string/sscanf.c index e1ede63..8a3ed0e 100644 --- a/userland/libc/string/sscanf.c +++ b/userland/libc/string/sscanf.c @@ -38,8 +38,9 @@ long ftnum(FILE *stream, int base, int *error) { return 0; } for (;;) { - if (EOF == (c = fgetc(stream))) + if (EOF == (c = fgetc(stream))) { break; + } if (c == '\0') { ungetc(c, stream); break; @@ -69,13 +70,15 @@ int vfscanf(FILE *stream, const char *format, va_list ap) { for (; *format; format++) { if (*format != '%' && !cont) { char c; - if (isspace(*format)) + if (isspace(*format)) { continue; + } if (EOF == (c = fgetc(stream))) { break; } - if (*format == c) // TODO: Make sure this is the correct behaviour + if (*format == c) { // TODO: Make sure this is the correct behaviour continue; + } // TODO: Make sure this is the correct behaviour errno = EINVAL; assert(0); @@ -164,8 +167,9 @@ size_t sscanf_read(FILE *f, unsigned char *s, size_t l) { s++; r++; } - if (!(*(c->s + c->offset))) + if (!(*(c->s + c->offset))) { f->is_eof = 1; + } /* memcpy(s, c->s, l); c->s += l;*/ diff --git a/userland/libc/string/strcasecmp.c b/userland/libc/string/strcasecmp.c index 6420dbf..399ebc2 100644 --- a/userland/libc/string/strcasecmp.c +++ b/userland/libc/string/strcasecmp.c @@ -7,8 +7,9 @@ int strcasecmp(const char *s1, const char *s2) { int l1, l2, rc; l1 = l2 = rc = 0; for (; (*s1 || *s2);) { - if (tolower(*s1) != tolower(*s2)) + if (tolower(*s1) != tolower(*s2)) { rc++; + } if (*s1) { l1++; s1++; @@ -23,7 +24,8 @@ int strcasecmp(const char *s1, const char *s2) { // equal to, or less than 0, if the string pointed to by s1 is // greater than, equal to, or less than the string pointed to by // s2, respectively. - if (l2 > l1) + if (l2 > l1) { return -rc; + } return rc; } diff --git a/userland/libc/string/strchr.c b/userland/libc/string/strchr.c index 1995547..179e47c 100644 --- a/userland/libc/string/strchr.c +++ b/userland/libc/string/strchr.c @@ -2,10 +2,12 @@ char *strchr(const char *s, int c) { for (; *s; s++) { - if (*s == (char)c) - return (char*)s; + if (*s == (char)c) { + return (char *)s; + } } - if ((char)c == '\0') + if ((char)c == '\0') { return (char *)s; + } return NULL; } diff --git a/userland/libc/string/strcmp.c b/userland/libc/string/strcmp.c index 368b8fb..b07f023 100644 --- a/userland/libc/string/strcmp.c +++ b/userland/libc/string/strcmp.c @@ -7,8 +7,9 @@ int strcmp(const char *s1, const char *s2) { int l1, l2, rc; l1 = l2 = rc = 0; for (; *s1 || *s2;) { - if (*s1 != *s2) + if (*s1 != *s2) { rc++; + } if (*s1) { l1++; s1++; @@ -23,7 +24,8 @@ int strcmp(const char *s1, const char *s2) { // equal to, or less than 0, if the string pointed to by s1 is // greater than, equal to, or less than the string pointed to by // s2, respectively. - if (l2 > l1) + if (l2 > l1) { return -rc; + } return rc; } diff --git a/userland/libc/string/strcspn.c b/userland/libc/string/strcspn.c index 2ad38d5..fb5fe50 100644 --- a/userland/libc/string/strcspn.c +++ b/userland/libc/string/strcspn.c @@ -6,7 +6,7 @@ size_t strcspn(const char *s1, const char *s2) { for (const char *t = s2; *t; t++) { if (*s1 == *s2) { r++; - break; + break; } } } diff --git a/userland/libc/string/strdup.c b/userland/libc/string/strdup.c index 9e22f94..ead535a 100644 --- a/userland/libc/string/strdup.c +++ b/userland/libc/string/strdup.c @@ -8,8 +8,9 @@ char *strdup(const char *s) { size_t l = strlen(s); char *r = malloc(l + 1); - if (!r) + if (!r) { return NULL; + } strcpy(r, s); return r; } diff --git a/userland/libc/string/strlcpy.c b/userland/libc/string/strlcpy.c index a2d3dd9..041522b 100644 --- a/userland/libc/string/strlcpy.c +++ b/userland/libc/string/strlcpy.c @@ -7,12 +7,14 @@ size_t *strlcpy(char *s1, const char *s2, size_t n) { size_t tmp_n = n; const char *os2 = s2; for (; tmp_n; tmp_n--) { - if ((*s1++ = *s2++) == '\0') + if ((*s1++ = *s2++) == '\0') { break; + } } if (tmp_n == 0) { - if (n != 0) + if (n != 0) { *s1 = '\0'; /* NUL-terminate s1 */ + } while (*s2++) ; } diff --git a/userland/libc/string/strncasecmp.c b/userland/libc/string/strncasecmp.c index c635a03..8a7768c 100644 --- a/userland/libc/string/strncasecmp.c +++ b/userland/libc/string/strncasecmp.c @@ -1,6 +1,6 @@ -#include #include #include +#include int strncasecmp(const char *s1, const char *s2, size_t n) { // The strcmp() function shall compare the string pointed to by s1 @@ -8,8 +8,9 @@ int strncasecmp(const char *s1, const char *s2, size_t n) { int l1, l2, rc; l1 = l2 = rc = 0; for (; (*s1 || *s2) && n > 0; n--) { - if (tolower(*s1) != tolower(*s2)) + if (tolower(*s1) != tolower(*s2)) { rc++; + } if (*s1) { l1++; s1++; @@ -24,7 +25,8 @@ int strncasecmp(const char *s1, const char *s2, size_t n) { // equal to, or less than 0, if the string pointed to by s1 is // greater than, equal to, or less than the string pointed to by // s2, respectively. - if (l2 > l1) + if (l2 > l1) { return -rc; + } return rc; } diff --git a/userland/libc/string/strncmp.c b/userland/libc/string/strncmp.c index fd46189..3d6383b 100644 --- a/userland/libc/string/strncmp.c +++ b/userland/libc/string/strncmp.c @@ -6,8 +6,9 @@ int strncmp(const char *s1, const char *s2, size_t n) { int l1, l2, rc; l1 = l2 = rc = 0; for (; (*s1 || *s2) && n > 0; n--) { - if (*s1 != *s2) + if (*s1 != *s2) { rc++; + } if (*s1) { l1++; s1++; @@ -22,7 +23,8 @@ int strncmp(const char *s1, const char *s2, size_t n) { // equal to, or less than 0, if the string pointed to by s1 is // greater than, equal to, or less than the string pointed to by // s2, respectively. - if (l2 > l1) + if (l2 > l1) { return -rc; + } return rc; } diff --git a/userland/libc/string/strncpy.c b/userland/libc/string/strncpy.c index 0e88c63..fb89d38 100644 --- a/userland/libc/string/strncpy.c +++ b/userland/libc/string/strncpy.c @@ -4,10 +4,12 @@ char *strncpy(char *s1, const char *s2, size_t n) { char *rc = s1; for (; n > 0; s1++, s2++, n--) { *s1 = *s2; - if (!*s2) + if (!*s2) { break; + } } - for (; n > 0; n--,s1++) + for (; n > 0; n--, s1++) { *s1 = '\0'; + } return rc; } diff --git a/userland/libc/string/strndup.c b/userland/libc/string/strndup.c index 22d6303..3240d97 100644 --- a/userland/libc/string/strndup.c +++ b/userland/libc/string/strndup.c @@ -1,7 +1,7 @@ +#include #include #include #include -#include // The strndup() function shall be equivalent to the strdup() function, // duplicating the provided s in a new block of memory allocated as if @@ -16,8 +16,9 @@ char *strndup(const char *s, size_t size) { size_t l = strlen(s); size_t real_l = min(l, size); char *r = malloc(real_l + 1); - if (!r) + if (!r) { return NULL; + } strlcpy(r, s, real_l); return r; } diff --git a/userland/libc/string/strnlen.c b/userland/libc/string/strnlen.c index 86df42e..50f907e 100644 --- a/userland/libc/string/strnlen.c +++ b/userland/libc/string/strnlen.c @@ -2,8 +2,10 @@ size_t strnlen(const char *s, size_t maxlen) { size_t i; - for (i = 0; i < maxlen; i++) - if (s[i] == 0) + for (i = 0; i < maxlen; i++) { + if (s[i] == 0) { break; + } + } return i; } diff --git a/userland/libc/string/strpbrk.c b/userland/libc/string/strpbrk.c index 3bb058b..cbe51de 100644 --- a/userland/libc/string/strpbrk.c +++ b/userland/libc/string/strpbrk.c @@ -4,7 +4,7 @@ char *strpbrk(const char *s1, const char *s2) { for (; *s1; s1++) { for (const char *t = s2; *t; t++) { if (*s1 == *t) { - return (char*)s1; + return (char *)s1; } } } diff --git a/userland/libc/string/strrchr.c b/userland/libc/string/strrchr.c index a44199a..30b625d 100644 --- a/userland/libc/string/strrchr.c +++ b/userland/libc/string/strrchr.c @@ -3,10 +3,12 @@ char *strrchr(const char *s, int c) { char *last = NULL; for (; *s; s++) { - if (*s == (char)c) + if (*s == (char)c) { last = (char *)s; + } + } + if ((char)c == '\0') { + last = (char *)s; } - if ((char)c == '\0') - last = (char*)s; return last; } diff --git a/userland/libc/string/strspn.c b/userland/libc/string/strspn.c index 2a7d3ae..d6b1595 100644 --- a/userland/libc/string/strspn.c +++ b/userland/libc/string/strspn.c @@ -10,8 +10,9 @@ size_t strspn(const char *s1, const char *s2) { break; } } - if (!e) + if (!e) { break; + } r++; } return r; diff --git a/userland/libc/string/strstr.c b/userland/libc/string/strstr.c index 9de0954..4300f24 100644 --- a/userland/libc/string/strstr.c +++ b/userland/libc/string/strstr.c @@ -2,8 +2,9 @@ char *strstr(const char *s1, const char *s2) { // If s2 points to a string with zero length, the function shall return s1. - if ('\0' == *s2) - return (char*)s1; + if ('\0' == *s2) { + return (char *)s1; + } for (; *s1; s1++) { const char *t1 = s1; const char *t2 = s2; @@ -14,8 +15,9 @@ char *strstr(const char *s1, const char *s2) { break; } } - if (!is_dif) - return (char*)s1; + if (!is_dif) { + return (char *)s1; + } } return NULL; } diff --git a/userland/libc/string/strtok.c b/userland/libc/string/strtok.c index 7b8d0e3..a4a9d0f 100644 --- a/userland/libc/string/strtok.c +++ b/userland/libc/string/strtok.c @@ -7,18 +7,20 @@ char *strtok(char *restrict s, const char *restrict sep) { strtok_s = s; return strtok(NULL, sep); } - if(!strtok_s) - return NULL; + if (!strtok_s) { + return NULL; + } char *e = strpbrk(strtok_s, sep); if (!e) { - char *r = strtok_s; - strtok_s = NULL; + char *r = strtok_s; + strtok_s = NULL; return r; } *e = '\0'; e--; - for (; *sep; sep++) + for (; *sep; sep++) { e++; + } e++; char *r = strtok_s; strtok_s = e; diff --git a/userland/libc/sys/mman.h b/userland/libc/sys/mman.h index e3ff734..a915291 100644 --- a/userland/libc/sys/mman.h +++ b/userland/libc/sys/mman.h @@ -1,7 +1,7 @@ #ifndef MMAP_H #define MMAP_H -#include #include +#include void *mmap(void *addr, size_t length, int prot, int flags, int fd, size_t offset); diff --git a/userland/libc/sys/mman/mmap.c b/userland/libc/sys/mman/mmap.c index b9ad3a2..53c6f77 100644 --- a/userland/libc/sys/mman/mmap.c +++ b/userland/libc/sys/mman/mmap.c @@ -1,6 +1,6 @@ -#include #include #include +#include extern int errno; @@ -14,6 +14,6 @@ void *mmap(void *addr, size_t length, int prot, int flags, int fd, .fd = fd, .offset = offset, }; -// return (void*)syscall(SYS_MMAP, &args, 0, 0, 0, 0); + // return (void*)syscall(SYS_MMAP, &args, 0, 0, 0, 0); RC_ERRNO(syscall(SYS_MMAP, &args, 0, 0, 0, 0)); } diff --git a/userland/libc/sys/mman/munmap.c b/userland/libc/sys/mman/munmap.c index 940ea46..5750cb4 100644 --- a/userland/libc/sys/mman/munmap.c +++ b/userland/libc/sys/mman/munmap.c @@ -2,5 +2,5 @@ #include int munmap(void *addr, size_t length) { - RC_ERRNO(syscall(SYS_MUNMAP, addr, length,0, 0, 0)); + RC_ERRNO(syscall(SYS_MUNMAP, addr, length, 0, 0, 0)); } diff --git a/userland/libc/sys/sendfile.c b/userland/libc/sys/sendfile.c index 818883b..9de931b 100644 --- a/userland/libc/sys/sendfile.c +++ b/userland/libc/sys/sendfile.c @@ -1,6 +1,6 @@ +#include #include #include -#include u32 sendfile(int out_fd, int in_fd, off_t *offset, size_t count, int *error_rc) { diff --git a/userland/libc/sys/socket/listen.c b/userland/libc/sys/socket/listen.c index a3bec10..67cc6ce 100644 --- a/userland/libc/sys/socket/listen.c +++ b/userland/libc/sys/socket/listen.c @@ -1,9 +1,9 @@ -#include #include +#include int listen(int socket, int backlog) { - (void)socket; - (void)backlog; + (void)socket; + (void)backlog; printf("TODO: Implement listen()\n"); return 0; } diff --git a/userland/libc/sys/socket/sendto.c b/userland/libc/sys/socket/sendto.c index 73437ab..be35ec0 100644 --- a/userland/libc/sys/socket/sendto.c +++ b/userland/libc/sys/socket/sendto.c @@ -7,7 +7,7 @@ struct two_args { }; size_t sendto(int socket, const void *message, size_t length, int flags, - const struct sockaddr *dest_addr, socklen_t dest_len) { + const struct sockaddr *dest_addr, socklen_t dest_len) { struct two_args extra = { .a = dest_addr, .b = dest_len, diff --git a/userland/libc/sys/time/gettimeofday.c b/userland/libc/sys/time/gettimeofday.c index e172cf2..3487254 100644 --- a/userland/libc/sys/time/gettimeofday.c +++ b/userland/libc/sys/time/gettimeofday.c @@ -1,5 +1,7 @@ #include #include -static int return_tod =0; -int gettimeofday(struct timeval *tp, void *tzp) {return return_tod++;} +static int return_tod = 0; +int gettimeofday(struct timeval *tp, void *tzp) { + return return_tod++; +} diff --git a/userland/libc/sys/types.h b/userland/libc/sys/types.h index ffcf281..d9bede0 100644 --- a/userland/libc/sys/types.h +++ b/userland/libc/sys/types.h @@ -16,12 +16,12 @@ typedef int dev_t; typedef unsigned int fsblkcnt_t; typedef unsigned int fsfilcnt_t; typedef unsigned int ino_t; -//typedef unsigned int size_t; +// typedef unsigned int size_t; typedef int blksize_t; typedef int pid_t; typedef int ssize_t; -//typedef int clock_t; +// typedef int clock_t; typedef int time_t; #endif diff --git a/userland/libc/tb/sha1.c b/userland/libc/tb/sha1.c index 90e4e2a..1f68672 100644 --- a/userland/libc/tb/sha1.c +++ b/userland/libc/tb/sha1.c @@ -3,10 +3,10 @@ // // SPDX-License-Identifier: 0BSD // -#include #include #include #include +#include #define SHA1_CONSTANT_H0 0x67452301 #define SHA1_CONSTANT_H1 0xefcdab89 @@ -57,10 +57,11 @@ void pad_sha1_message(uint8_t *M, uint64_t l, uint64_t active_l, // solution to the equation 'l + 1 + k \cong 448 mod 512' int k; uint64_t rest = (active_l + 1) % 512; - if (rest < 448) + if (rest < 448) { k = 448 - rest; - else + } else { k = 512 - rest + 448; + } memset(block + ARRAY_NUM(active_l) + 2, 0x0, k / 8); @@ -90,14 +91,18 @@ uint32_t sha1_f(uint8_t t, uint32_t x, uint32_t y, uint32_t z) { } uint32_t sha1_get_k(uint8_t t) { - if (t <= 19) + if (t <= 19) { return SHA1_CONSTANT_K1; - if (t <= 39) + } + if (t <= 39) { return SHA1_CONSTANT_K2; - if (t <= 59) + } + if (t <= 59) { return SHA1_CONSTANT_K3; - if (t <= 79) + } + if (t <= 79) { return SHA1_CONSTANT_K4; + } return 0; } @@ -109,16 +114,18 @@ uint32_t ROTL(uint32_t value, uint8_t shift) { void add_block(SHA1_CTX *ctx, uint8_t *_block) { uint32_t *block = (uint32_t *)_block; - for (size_t i = 0; i < 16; i++) + for (size_t i = 0; i < 16; i++) { block[i] = reverse_32(block[i]); + } uint32_t W[80]; uint32_t a, b, c, d, e; uint32_t *M = block; // 1. Prepare the message schedule, {Wt}: - for (size_t t = 0; t < 16; t++) + for (size_t t = 0; t < 16; t++) { W[t] = M[t]; + } for (size_t t = 16; t < 80; t++) { // ROTL(1)(W(t-3) ^ W(t-8) ^ W(t-16)) @@ -157,14 +164,17 @@ void SHA1_Final(SHA1_CTX *ctx, unsigned char *message_digest) { add_block(ctx, block); - if (((ctx->active_len * 8 + 1) % 512) >= 448) + if (((ctx->active_len * 8 + 1) % 512) >= 448) { add_block(ctx, block + BLOCK_BYTES); + } - for (size_t i = 0; i < 5; i++) + for (size_t i = 0; i < 5; i++) { ctx->h[i] = reverse_32(ctx->h[i]); + } - for (size_t i = 0; i < 5; i++) + for (size_t i = 0; i < 5; i++) { memcpy(message_digest + sizeof(uint32_t) * i, &ctx->h[i], sizeof(uint32_t)); + } } void SHA1_Update(SHA1_CTX *ctx, const void *data, uint64_t len) { @@ -176,8 +186,9 @@ void SHA1_Update(SHA1_CTX *ctx, const void *data, uint64_t len) { memcpy(ctx->block + ctx->active_len, data, write_len); ctx->len += write_len; ctx->active_len += write_len; - if (BLOCK_BYTES != ctx->active_len) + if (BLOCK_BYTES != ctx->active_len) { return; + } add_block(ctx, ctx->block); memset(ctx->block, 0, BLOCK_BYTES); diff --git a/userland/libc/time/gmtime.c b/userland/libc/time/gmtime.c index 08af482..0b5b09e 100644 --- a/userland/libc/time/gmtime.c +++ b/userland/libc/time/gmtime.c @@ -16,7 +16,7 @@ struct tm gmtime_r = { // https://pubs.opengroup.org/onlinepubs/9699919799/functions/gmtime.html struct tm *gmtime(const time_t *timer) { - (void)timer; + (void)timer; // TODO: Implement return &gmtime_r; } diff --git a/userland/libc/time/localtime.c b/userland/libc/time/localtime.c index 8ad74d8..ce15ffb 100644 --- a/userland/libc/time/localtime.c +++ b/userland/libc/time/localtime.c @@ -1,22 +1,22 @@ #include - struct tm localtime_r = { - .tm_sec = 0, - .tm_min = 0, - .tm_hour = 0, - .tm_mday = 0, - .tm_mon = 0, - .tm_year = 0, - .tm_wday = 0, - .tm_yday = 0, - .tm_isdst = 0, - .__tm_gmtoff = 0, - .__tm_zone = 0, - }; +struct tm localtime_r = { + .tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 0, + .tm_mon = 0, + .tm_year = 0, + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0, + .__tm_gmtoff = 0, + .__tm_zone = 0, +}; // https://pubs.opengroup.org/onlinepubs/9699919799/functions/localtime.html struct tm *localtime(const time_t *timer) { - (void)timer; + (void)timer; // TODO: Implement return &localtime_r; } diff --git a/userland/libc/time/strftime.c b/userland/libc/time/strftime.c index 30a080d..b6a5f58 100644 --- a/userland/libc/time/strftime.c +++ b/userland/libc/time/strftime.c @@ -1,7 +1,7 @@ -#include #include +#include -size_t strftime(char *restrict s, size_t maxsize, - const char *restrict format, const struct tm *restrict timeptr) { - return 0; +size_t strftime(char *restrict s, size_t maxsize, const char *restrict format, + const struct tm *restrict timeptr) { + return 0; } diff --git a/userland/libc/time/time.c b/userland/libc/time/time.c index 9931b71..fa025ce 100644 --- a/userland/libc/time/time.c +++ b/userland/libc/time/time.c @@ -3,7 +3,8 @@ time_t time(time_t *tloc) { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); - if (tloc) + if (tloc) { *tloc = ts.tv_sec; + } return ts.tv_sec; } diff --git a/userland/libc/ubsan.c b/userland/libc/ubsan.c index 9ac9879..90e2eb4 100644 --- a/userland/libc/ubsan.c +++ b/userland/libc/ubsan.c @@ -29,8 +29,8 @@ void __ubsan_handle_sub_overflow(struct OverflowData *data, unsigned long lhs, ubsan_log("handle_sub_overflow", data->location); } -void __ubsan_handle_negate_overflow(struct OverflowData *data, unsigned long lhs, - unsigned long rhs) { +void __ubsan_handle_negate_overflow(struct OverflowData *data, + unsigned long lhs, unsigned long rhs) { (void)lhs; (void)rhs; ubsan_log("handle_negate_overflow", data->location); diff --git a/userland/libc/unistd/_exit.c b/userland/libc/unistd/_exit.c index c6d64be..a6cc645 100644 --- a/userland/libc/unistd/_exit.c +++ b/userland/libc/unistd/_exit.c @@ -5,4 +5,6 @@ // stackoverflow answer says that it does not usually matter. So lets // hope that is the case. // https://stackoverflow.com/a/5423108 -void _exit(int status) { syscall(SYS_EXIT, (void *)status, 0, 0, 0, 0); } +void _exit(int status) { + syscall(SYS_EXIT, (void *)status, 0, 0, 0, 0); +} diff --git a/userland/libc/unistd/chdir.c b/userland/libc/unistd/chdir.c index 90b30ae..61372c0 100644 --- a/userland/libc/unistd/chdir.c +++ b/userland/libc/unistd/chdir.c @@ -1,4 +1,6 @@ -#include #include +#include -int chdir(const char *path) { RC_ERRNO(syscall(SYS_CHDIR, path, 0, 0, 0, 0)); } +int chdir(const char *path) { + RC_ERRNO(syscall(SYS_CHDIR, path, 0, 0, 0, 0)); +} diff --git a/userland/libc/unistd/getpid.c b/userland/libc/unistd/getpid.c index 8aeef10..47f9e71 100644 --- a/userland/libc/unistd/getpid.c +++ b/userland/libc/unistd/getpid.c @@ -1,7 +1,7 @@ -#include -#include #include +#include +#include pid_t getpid(void) { - return s_syscall(SYS_GETPID); + return s_syscall(SYS_GETPID); } diff --git a/userland/libc/unistd/isatty.c b/userland/libc/unistd/isatty.c index d8b08c3..55cf4e7 100644 --- a/userland/libc/unistd/isatty.c +++ b/userland/libc/unistd/isatty.c @@ -1,11 +1,12 @@ +#include #include #include -#include int isatty(int fd) { int rc = syscall(SYS_ISATTY, fd, 0, 0, 0, 0); - if (1 == rc) + if (1 == rc) { return rc; + } errno = -rc; return 0; } diff --git a/userland/libc/unistd/msleep.c b/userland/libc/unistd/msleep.c index 0b016c6..1f70a7b 100644 --- a/userland/libc/unistd/msleep.c +++ b/userland/libc/unistd/msleep.c @@ -1,6 +1,8 @@ // Not standard, but it feels like it should be. +#include #include #include -#include -void msleep(uint32_t ms) { syscall(SYS_MSLEEP, (void *)ms, 0, 0, 0, 0); } +void msleep(uint32_t ms) { + syscall(SYS_MSLEEP, (void *)ms, 0, 0, 0, 0); +} diff --git a/userland/libc/unistd/unlink.c b/userland/libc/unistd/unlink.c index 7d7aa1c..f26539a 100644 --- a/userland/libc/unistd/unlink.c +++ b/userland/libc/unistd/unlink.c @@ -1,5 +1,5 @@ -#include #include +#include int unlink(const char *path) { // TODO diff --git a/userland/libc/unistd/uptime.c b/userland/libc/unistd/uptime.c index 090a0e5..5a0654f 100644 --- a/userland/libc/unistd/uptime.c +++ b/userland/libc/unistd/uptime.c @@ -3,4 +3,6 @@ #include #include -uint32_t uptime(void) { return syscall(SYS_UPTIME, 0, 0, 0, 0, 0); } +uint32_t uptime(void) { + return syscall(SYS_UPTIME, 0, 0, 0, 0, 0); +} diff --git a/userland/libc/unistd/write.c b/userland/libc/unistd/write.c index 0dd1824..82e29fd 100644 --- a/userland/libc/unistd/write.c +++ b/userland/libc/unistd/write.c @@ -1,6 +1,6 @@ +#include #include #include -#include int write(int fd, const char *buf, size_t count) { RC_ERRNO(syscall(SYS_WRITE, fd, buf, count, 0, 0)); -- cgit v1.2.3