diff options
author | Anton Kling <anton@kling.gg> | 2024-12-12 16:03:08 +0100 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-12-12 16:03:08 +0100 |
commit | b033314bf1901d436dc71d41d5e1f37dda47e511 (patch) | |
tree | 120bd137888a7f96904a47af7d20422608ffe225 /userland/minibox/utilities | |
parent | dcbcdbdc5bcfb86eca05fb655e3bf009d89e39e0 (diff) |
formatting: Use clang-format on all projects
This commit also add braces to all `if` statements.
Diffstat (limited to 'userland/minibox/utilities')
-rw-r--r-- | userland/minibox/utilities/ascii.c | 64 | ||||
-rw-r--r-- | userland/minibox/utilities/cat.c | 12 | ||||
-rw-r--r-- | userland/minibox/utilities/echo.c | 9 | ||||
-rw-r--r-- | userland/minibox/utilities/ed.c | 26 | ||||
-rw-r--r-- | userland/minibox/utilities/init.c | 3 | ||||
-rw-r--r-- | userland/minibox/utilities/ls.c | 11 | ||||
-rw-r--r-- | userland/minibox/utilities/minibox.c | 3 | ||||
-rw-r--r-- | userland/minibox/utilities/sh/ast.c | 18 | ||||
-rw-r--r-- | userland/minibox/utilities/sh/lexer.c | 18 | ||||
-rw-r--r-- | userland/minibox/utilities/sh/sh.c | 9 | ||||
-rw-r--r-- | userland/minibox/utilities/touch.c | 3 | ||||
-rw-r--r-- | userland/minibox/utilities/wc.c | 191 | ||||
-rw-r--r-- | userland/minibox/utilities/yes.c | 12 |
13 files changed, 223 insertions, 156 deletions
diff --git a/userland/minibox/utilities/ascii.c b/userland/minibox/utilities/ascii.c index 53429dc..d60132a 100644 --- a/userland/minibox/utilities/ascii.c +++ b/userland/minibox/utilities/ascii.c @@ -1,30 +1,46 @@ #include <stdio.h> -const char * const ascii_table[] = -{ -"Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex", -" 0 00 NUL 16 10 DLE 32 20 48 30 0 64 40 @ 80 50 P 96 60 ` 112 70 p", -" 1 01 SOH 17 11 DC1 33 21 ! 49 31 1 65 41 A 81 51 Q 97 61 a 113 71 q", -" 2 02 STX 18 12 DC2 34 22 \" 50 32 2 66 42 B 82 52 R 98 62 b 114 72 r", -" 3 03 ETX 19 13 DC3 35 23 # 51 33 3 67 43 C 83 53 S 99 63 c 115 73 s", -" 4 04 EOT 20 14 DC4 36 24 $ 52 34 4 68 44 D 84 54 T 100 64 d 116 74 t", -" 5 05 ENQ 21 15 NAK 37 25 % 53 35 5 69 45 E 85 55 U 101 65 e 117 75 u", -" 6 06 ACK 22 16 SYN 38 26 & 54 36 6 70 46 F 86 56 V 102 66 f 118 76 v", -" 7 07 BEL 23 17 ETB 39 27 ' 55 37 7 71 47 G 87 57 W 103 67 g 119 77 w", -" 8 08 BS 24 18 CAN 40 28 ( 56 38 8 72 48 H 88 58 X 104 68 h 120 78 x", -" 9 09 HT 25 19 EM 41 29 ) 57 39 9 73 49 I 89 59 Y 105 69 i 121 79 y", -" 10 0A LF 26 1A SUB 42 2A * 58 3A : 74 4A J 90 5A Z 106 6A j 122 7A z", -" 11 0B VT 27 1B ESC 43 2B + 59 3B ; 75 4B K 91 5B [ 107 6B k 123 7B {", -" 12 0C FF 28 1C FS 44 2C , 60 3C < 76 4C L 92 5C \\ 108 6C l 124 7C |", -" 13 0D CR 29 1D GS 45 2D - 61 3D = 77 4D M 93 5D ] 109 6D m 125 7D }", -" 14 0E SO 30 1E RS 46 2E . 62 3E > 78 4E N 94 5E ^ 110 6E n 126 7E ~", -" 15 0F SI 31 1F US 47 2F / 63 3F ? 79 4F O 95 5F _ 111 6F o 127 7F DEL", +const char *const ascii_table[] = { + "Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec " + "Hex", + " 0 00 NUL 16 10 DLE 32 20 48 30 0 64 40 @ 80 50 P 96 60 ` 112 " + "70 p", + " 1 01 SOH 17 11 DC1 33 21 ! 49 31 1 65 41 A 81 51 Q 97 61 a 113 " + "71 q", + " 2 02 STX 18 12 DC2 34 22 \" 50 32 2 66 42 B 82 52 R 98 62 b 114 " + "72 r", + " 3 03 ETX 19 13 DC3 35 23 # 51 33 3 67 43 C 83 53 S 99 63 c 115 " + "73 s", + " 4 04 EOT 20 14 DC4 36 24 $ 52 34 4 68 44 D 84 54 T 100 64 d 116 " + "74 t", + " 5 05 ENQ 21 15 NAK 37 25 % 53 35 5 69 45 E 85 55 U 101 65 e 117 " + "75 u", + " 6 06 ACK 22 16 SYN 38 26 & 54 36 6 70 46 F 86 56 V 102 66 f 118 " + "76 v", + " 7 07 BEL 23 17 ETB 39 27 ' 55 37 7 71 47 G 87 57 W 103 67 g 119 " + "77 w", + " 8 08 BS 24 18 CAN 40 28 ( 56 38 8 72 48 H 88 58 X 104 68 h 120 " + "78 x", + " 9 09 HT 25 19 EM 41 29 ) 57 39 9 73 49 I 89 59 Y 105 69 i 121 " + "79 y", + " 10 0A LF 26 1A SUB 42 2A * 58 3A : 74 4A J 90 5A Z 106 6A j 122 " + "7A z", + " 11 0B VT 27 1B ESC 43 2B + 59 3B ; 75 4B K 91 5B [ 107 6B k 123 " + "7B {", + " 12 0C FF 28 1C FS 44 2C , 60 3C < 76 4C L 92 5C \\ 108 6C l 124 " + "7C |", + " 13 0D CR 29 1D GS 45 2D - 61 3D = 77 4D M 93 5D ] 109 6D m 125 " + "7D }", + " 14 0E SO 30 1E RS 46 2E . 62 3E > 78 4E N 94 5E ^ 110 6E n 126 " + "7E ~", + " 15 0F SI 31 1F US 47 2F / 63 3F ? 79 4F O 95 5F _ 111 6F o 127 " + "7F DEL", }; -int ascii_main(int argc, char ** argv) -{ - for(int i = 0;i < sizeof(ascii_table)/sizeof(ascii_table[0]);i++) - puts(ascii_table[i]); +int ascii_main(int argc, char **argv) { + for (int i = 0; i < sizeof(ascii_table) / sizeof(ascii_table[0]); i++) { + puts(ascii_table[i]); + } - return 0; + return 0; } diff --git a/userland/minibox/utilities/cat.c b/userland/minibox/utilities/cat.c index 736aee6..a425392 100644 --- a/userland/minibox/utilities/cat.c +++ b/userland/minibox/utilities/cat.c @@ -13,10 +13,12 @@ int fd_to_stdout(int fd) { int rc; for (char buffer[CAT_BUFFER]; (rc = read(fd, &buffer, sizeof(buffer)));) { - if (-1 == rc) + if (-1 == rc) { return 0; - if (-1 == write(fd_stdout, buffer, rc)) + } + if (-1 == write(fd_stdout, buffer, rc)) { return 0; + } } return 1; } @@ -35,8 +37,9 @@ int cat_main(int argc, char **argv) { // If a file is '-', the cat utility shall read from the standard // input at that point in the sequence. if (0 == strcmp(*argv, "-")) { - if (!fd_to_stdout(0)) + if (!fd_to_stdout(0)) { return 1; + } continue; } @@ -46,8 +49,9 @@ int cat_main(int argc, char **argv) { perror(*argv); return 1; } - if (!fd_to_stdout(fd)) + if (!fd_to_stdout(fd)) { return 1; + } close(fd); } return 0; diff --git a/userland/minibox/utilities/echo.c b/userland/minibox/utilities/echo.c index 5ec68a7..f184d4a 100644 --- a/userland/minibox/utilities/echo.c +++ b/userland/minibox/utilities/echo.c @@ -7,24 +7,27 @@ int echo(char **str, int new_line) { for (; *str;) { printf("%s", *str); - if (*++str) + if (*++str) { putchar(' '); + } } - if (new_line) + if (new_line) { putchar('\n'); + } return 0; } int echo_main(int argc, char **argv) { int new_line = 1; - for (; *++argv && '-' == **argv;) + for (; *++argv && '-' == **argv;) { switch (*(*argv + 1)) { case 'n': new_line = 0; break; } + } return echo(argv, new_line); } diff --git a/userland/minibox/utilities/ed.c b/userland/minibox/utilities/ed.c index bcaba36..b60689d 100644 --- a/userland/minibox/utilities/ed.c +++ b/userland/minibox/utilities/ed.c @@ -21,8 +21,9 @@ int ed_getline(char *buffer, size_t s) { assert(rc > 0); printf("%c", c); buffer[i] = c; - if ('\n' == c) + if ('\n' == c) { break; + } } buffer[i] = '\0'; return i; @@ -35,10 +36,12 @@ void goto_line(void) { if (1 != line_number) { // Goto line for (; fread(&c, 1, 1, mem_fp);) { - if ('\n' == c) + if ('\n' == c) { line++; - if (line == line_number) + } + if (line == line_number) { return; + } } printf("got to line: %d\n", line); assert(0); @@ -49,8 +52,9 @@ void read_line(void) { char c; goto_line(); for (; fread(&c, 1, 1, mem_fp);) { - if ('\n' == c) + if ('\n' == c) { break; + } printf("%c", c); } printf("\n"); @@ -59,8 +63,9 @@ void read_line(void) { void goto_end_of_line(void) { char c; for (; fread(&c, 1, 1, mem_fp);) { - if ('\n' == c) + if ('\n' == c) { break; + } } } @@ -74,8 +79,9 @@ void delete_line(void) { for (char buffer[4096];;) { int rc = fread(buffer, 1, 100, mem_fp); long reset = ftell(mem_fp); - if (0 == rc) + if (0 == rc) { break; + } fseek(mem_fp, s, SEEK_SET); fwrite(buffer, 1, rc, mem_fp); s += rc; @@ -120,8 +126,9 @@ void read_input(void) { } int ed_main(char argc, char **argv) { - if (argc < 2) + if (argc < 2) { return 1; + } char *buffer; size_t size; mem_fp = open_memstream(&buffer, &size); @@ -134,10 +141,11 @@ int ed_main(char argc, char **argv) { } for (;;) { - if (COMMAND_MODE == mode) + if (COMMAND_MODE == mode) { read_command(); - else + } else { read_input(); + } } fclose(fp); fclose(mem_fp); diff --git a/userland/minibox/utilities/init.c b/userland/minibox/utilities/init.c index ce65387..b5aaf56 100644 --- a/userland/minibox/utilities/init.c +++ b/userland/minibox/utilities/init.c @@ -45,8 +45,9 @@ void shell() { char c[4096]; int rc = read(fds[0].fd, c, 4096); assert(rc > 0); - for (int i = 0; i < rc; i++) + for (int i = 0; i < rc; i++) { putchar(c[i]); + } } if (fds[1].revents & POLLIN) { char c; diff --git a/userland/minibox/utilities/ls.c b/userland/minibox/utilities/ls.c index 5a8f8ea..4ce952d 100644 --- a/userland/minibox/utilities/ls.c +++ b/userland/minibox/utilities/ls.c @@ -35,14 +35,17 @@ int ls_main(int argc, char **argv) { int prev = 0; for (int i = 0; i < n; i++) { - if (!list_hidden) - if ('.' == *namelist[i]->d_name) + if (!list_hidden) { + if ('.' == *namelist[i]->d_name) { continue; + } + } - if (prev) + if (prev) { putchar(newline ? '\n' : ' '); - else + } else { prev = 1; + } printf("%s", namelist[i]->d_name); } putchar('\n'); diff --git a/userland/minibox/utilities/minibox.c b/userland/minibox/utilities/minibox.c index ec5fdbd..f412efc 100644 --- a/userland/minibox/utilities/minibox.c +++ b/userland/minibox/utilities/minibox.c @@ -6,8 +6,9 @@ int main(int argc, char **argv); // Should a command be called via "minibox <command> <arguments>" then shift // argv and argc to become "<command> <arguments>" and rerun main() int minibox_main(int argc, char **argv) { - if (argc < 2) + if (argc < 2) { usage(); + } argc--; argv++; diff --git a/userland/minibox/utilities/sh/ast.c b/userland/minibox/utilities/sh/ast.c index 2b9c151..2a8d775 100644 --- a/userland/minibox/utilities/sh/ast.c +++ b/userland/minibox/utilities/sh/ast.c @@ -11,8 +11,9 @@ void free_ast_command(struct AST *ast) { void free_ast(struct AST *ast) { for (; ast;) { - if (AST_COMMAND == ast->type) + if (AST_COMMAND == ast->type) { free_ast_command(ast); + } struct AST *old = ast; ast = ast->next; free(old); @@ -27,8 +28,9 @@ struct AST *allocate_ast(void) { int parse_command(struct TOKEN **token_ptr, struct AST *cur) { struct TOKEN *token = *token_ptr; - if (TOKEN_CHARS != token->type) + if (TOKEN_CHARS != token->type) { return 0; + } cur->type = AST_COMMAND; cur->val.type = AST_VALUE_STRING; cur->val.string = token->string_rep; @@ -41,10 +43,12 @@ int parse_command(struct TOKEN **token_ptr, struct AST *cur) { child->type = AST_EXPRESSION; child->val.type = AST_VALUE_STRING; child->val.string = token->string_rep; - if (!token->next) + if (!token->next) { break; - if (TOKEN_CHARS != token->next->type) + } + if (TOKEN_CHARS != token->next->type) { break; + } token = token->next; child->next = allocate_ast(); child = child->next; @@ -87,8 +91,9 @@ struct AST *generate_ast(struct TOKEN *token) { struct AST *prev = NULL; for (; token;) { struct AST *cur = allocate_ast(); - if (prev) + if (prev) { prev->next = cur; + } if (parse_command(&token, cur)) { } else if (TOKEN_AND == token->type) { cur->type = AST_CONDITIONAL_AND; @@ -99,8 +104,9 @@ struct AST *generate_ast(struct TOKEN *token) { } else { token = token->next; } - if (!head) + if (!head) { head = cur; + } prev = cur; } return head; diff --git a/userland/minibox/utilities/sh/lexer.c b/userland/minibox/utilities/sh/lexer.c index d7f00c7..09fc2ff 100644 --- a/userland/minibox/utilities/sh/lexer.c +++ b/userland/minibox/utilities/sh/lexer.c @@ -15,19 +15,23 @@ void free_tokens(struct TOKEN *token) { } int is_special_char(char c) { - if (!isprint(c)) + if (!isprint(c)) { return 1; - if (isspace(c)) + } + if (isspace(c)) { return 1; - if (isalnum(c)) + } + if (isalnum(c)) { return 0; + } return !(('>' != c && '|' != c && '&' != c)); } int parse_chars(struct sv *code_ptr, struct TOKEN *cur) { struct sv code = *code_ptr; - if (is_special_char(sv_peek(code))) + if (is_special_char(sv_peek(code))) { return 0; + } cur->type = TOKEN_CHARS; cur->string_rep = sv_split_function(code, &code, is_special_char); *code_ptr = code; @@ -83,16 +87,18 @@ struct TOKEN *lex(struct sv code) { struct TOKEN *cur = malloc(sizeof(struct TOKEN)); cur->next = NULL; - if (prev) + if (prev) { prev->next = cur; + } if (parse_chars(&code, cur)) { } else if (parse_operand(&code, cur)) { } else { free(cur); assert(0 && "Unknown token"); } - if (!head) + if (!head) { head = cur; + } prev = cur; } return head; diff --git a/userland/minibox/utilities/sh/sh.c b/userland/minibox/utilities/sh/sh.c index b00b440..3643d99 100644 --- a/userland/minibox/utilities/sh/sh.c +++ b/userland/minibox/utilities/sh/sh.c @@ -68,8 +68,9 @@ int execute_binary(struct AST *ast, int input_fd) { } if (ast->pipe_rhs) { - if (out >= 0) + if (out >= 0) { close(out); + } return execute_command(ast->pipe_rhs, slave_input); } @@ -135,14 +136,16 @@ void execute_ast(struct AST *ast) { } else if (AST_CONDITIONAL_AND == ast->type) { if (0 != rc) { ast = ast->next; - if (!ast) + if (!ast) { break; + } } } else if (AST_CONDITIONAL_NOT == ast->type) { if (0 == rc) { ast = ast->next; - if (!ast) + if (!ast) { break; + } } } ast = ast->next; diff --git a/userland/minibox/utilities/touch.c b/userland/minibox/utilities/touch.c index dd2f072..c9236e2 100644 --- a/userland/minibox/utilities/touch.c +++ b/userland/minibox/utilities/touch.c @@ -2,8 +2,9 @@ #include <fcntl.h> int touch_main(int argc, char **argv) { - if (argc < 2) + if (argc < 2) { return 1; + } int fd; COND_PERROR_EXP(open(argv[1], O_CREAT, 0), "open", return 1) close(fd); diff --git a/userland/minibox/utilities/wc.c b/userland/minibox/utilities/wc.c index 886eb9e..27a214b 100644 --- a/userland/minibox/utilities/wc.c +++ b/userland/minibox/utilities/wc.c @@ -1,106 +1,117 @@ +#include "include.h" +#include <ctype.h> +#include <fcntl.h> #include <stdio.h> -#include <unistd.h> #include <stdlib.h> -#include <fcntl.h> -#include <ctype.h> -#include "include.h" +#include <unistd.h> int cmode, wmode, lmode; -void output(const char * str, size_t lines, size_t words, size_t chars) -{ - if(lmode) - printf("%d", lines); +void output(const char *str, size_t lines, size_t words, size_t chars) { + if (lmode) { + printf("%d", lines); + } - if(wmode) - { - if(lmode) putchar(' '); + if (wmode) { + if (lmode) { + putchar(' '); + } - printf("%d", words); - } + printf("%d", words); + } - if(cmode) - { - if(lmode) putchar(' '); + if (cmode) { + if (lmode) { + putchar(' '); + } - printf("%d", chars); - } + printf("%d", chars); + } - if(str) printf(" %s", str); + if (str) { + printf(" %s", str); + } - putchar('\n'); + putchar('\n'); } -void wc(int argc, char ** argv) -{ - int fd = fd_stdin; - size_t total_chars, total_words, total_lines; - total_chars = total_words = total_lines = 0; - - if(NULL == *argv) - goto read_stdin; - - - for(;*argv;argv++) - { - int new_word = 1; - size_t chars = 0, - words = chars, - lines = chars; - - fd = open(*argv, O_RDONLY, 0); -read_stdin: - for(char c;read(fd, &c, 1);) - { - if(cmode) - chars++; - - if(wmode) - { - if(isspace(c)) { - new_word = 1; - } else if(new_word) { - words++; - new_word = 0; - } - } - - if(lmode) - if('\n' == c) - lines++; - } - close(fd); - if(lmode) total_lines += lines; - if(wmode) total_words += words; - if(cmode) total_chars += chars; - - output(*argv, lines, words, chars); - if(NULL == *argv) return; // We read from stdin - } - - if(argc > 1) output("total", total_lines, total_words, total_chars); +void wc(int argc, char **argv) { + int fd = fd_stdin; + size_t total_chars, total_words, total_lines; + total_chars = total_words = total_lines = 0; + + if (NULL == *argv) { + goto read_stdin; + } + + for (; *argv; argv++) { + int new_word = 1; + size_t chars = 0, words = chars, lines = chars; + + fd = open(*argv, O_RDONLY, 0); + read_stdin: + for (char c; read(fd, &c, 1);) { + if (cmode) { + chars++; + } + + if (wmode) { + if (isspace(c)) { + new_word = 1; + } else if (new_word) { + words++; + new_word = 0; + } + } + + if (lmode) { + if ('\n' == c) { + lines++; + } + } + } + close(fd); + if (lmode) { + total_lines += lines; + } + if (wmode) { + total_words += words; + } + if (cmode) { + total_chars += chars; + } + + output(*argv, lines, words, chars); + if (NULL == *argv) { + return; // We read from stdin + } + } + + if (argc > 1) { + output("total", total_lines, total_words, total_chars); + } } -int wc_main(int argc, char ** argv) -{ - cmode = wmode = lmode = 0; - for(argc--;*++argv && '-' == **argv;argc--) - switch(*(*argv+1)) - { - case 'l': - lmode = 1; - break; - case 'w': - wmode = 1; - break; - case 'c': - cmode = 1; - break; - } - - if(!(lmode || cmode || wmode)) - cmode = wmode = lmode = 1; - - wc(argc, argv); - return 0; +int wc_main(int argc, char **argv) { + cmode = wmode = lmode = 0; + for (argc--; *++argv && '-' == **argv; argc--) { + switch (*(*argv + 1)) { + case 'l': + lmode = 1; + break; + case 'w': + wmode = 1; + break; + case 'c': + cmode = 1; + break; + } + } + + if (!(lmode || cmode || wmode)) { + cmode = wmode = lmode = 1; + } + + wc(argc, argv); + return 0; } diff --git a/userland/minibox/utilities/yes.c b/userland/minibox/utilities/yes.c index ff19004..c0c43c8 100644 --- a/userland/minibox/utilities/yes.c +++ b/userland/minibox/utilities/yes.c @@ -2,12 +2,16 @@ #include <stdio.h> int yes_main(int argc, char **argv) { - if (argc < 2) - for (;;) + if (argc < 2) { + for (;;) { puts("y"); + } + } - for (;; putchar('\n')) - for (int i = 1; i < argc; i++) + for (;; putchar('\n')) { + for (int i = 1; i < argc; i++) { printf("%s ", argv[i]); + } + } return 0; } |