logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 0d8f00af117f6c66eb7501457fed6d56202d13bc
parent ffebbb38a22260adf1f1e0cc0fe67842ca93fd17
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 11 May 2021 02:44:23 +0200

Add & apply clang-format

Diffstat:

A.clang-format21+++++++++++++++++++++
MMakefile4++++
Mbin/args.c3++-
Mbin/basename.c29++++++++++++-----------------
Mbin/date.c29++++++++++++++++-------------
Mbin/dirname.c5+++--
Mbin/lolcat.c16++++++++++------
Mbin/mdate.c7++++---
Mbin/pwd.c6++++--
Mbin/range.c12+++++++++---
Mbin/sizeof.c6++++--
Mbin/sname.c8+++++---
Mbin/tty.c8+++++---
Mbin/xcd.c38+++++++++++++++++++++++---------------
Msbin/memsys.c22++++++++++++++--------
15 files changed, 136 insertions(+), 78 deletions(-)

diff --git a/.clang-format b/.clang-format @@ -0,0 +1,21 @@ +AlignAfterOpenBracket: true +AlignConsecutiveAssignments: true +AlignOperands: true +AlignTrailingComments: true +AllowShortCaseLabelsOnASingleLine: true +AllowShortFunctionsOnASingleLine: true +AllowShortIfStatementsOnASingleLine: true +AlwaysBreakAfterReturnType: AllDefinitions +BinPackArguments: false +BinPackParameters: false +BreakBeforeBraces: Allman +SpaceBeforeParens: Never +IncludeBlocks: Regroup +ReflowComments: false +SortIncludes: true +UseTab: ForIndentation +IndentWidth: 2 +TabWidth: 2 +ColumnLimit: 100 + +NamespaceIndentation: All diff --git a/Makefile b/Makefile @@ -12,3 +12,7 @@ clean: install: cd bin ; $(MAKE) install cd sbin ; $(MAKE) install + +C_SOURCES = bin/*.c sbin/*.c +format: $(C_SOURCES) + clang-format -style=file -assume-filename=.clang-format -i $(C_SOURCES) diff --git a/bin/args.c b/bin/args.c @@ -4,7 +4,8 @@ // printf() #include <stdio.h> -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { printf("argc: %i\n", argc); diff --git a/bin/basename.c b/bin/basename.c @@ -1,13 +1,14 @@ #include <libgen.h> // basename() -#include <stdio.h> // fputs(), puts() +#include <stdio.h> // fputs(), puts() #include <string.h> // strlen(), strcmp() -char *suffix_basename(char *name, char *suffix) +char * +suffix_basename(char *name, char *suffix) { char *string = basename(name); size_t suflen = suffix ? strlen(suffix) : 0; - size_t len = strlen(string); + size_t len = strlen(string); if(suflen < len && strcmp(&string[len - suflen], suffix) == 0) { @@ -17,21 +18,15 @@ char *suffix_basename(char *name, char *suffix) return string; } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { - switch(argc) { - case 1: - puts("."); - break; - case 2: - puts(basename(argv[1])); - break; - case 3: - puts(suffix_basename(argv[1], argv[2])); - break; - default: - fputs("usage: basename string [suffix]", stderr); - return 1; + switch(argc) + { + case 1: puts("."); break; + case 2: puts(basename(argv[1])); break; + case 3: puts(suffix_basename(argv[1], argv[2])); break; + default: fputs("usage: basename string [suffix]", stderr); return 1; } return 0; diff --git a/bin/date.c b/bin/date.c @@ -6,26 +6,28 @@ #include <time.h> /* time, localtime, tm, strftime */ #include <unistd.h> /* getopt(), optarg, optind */ -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { char outstr[BUFSIZ]; struct tm *tm; - time_t now = time(NULL); - char *format = "%c"; - int uflag = 0; + time_t now = time(NULL); + char *format = "%c"; + int uflag = 0; int c; setlocale(LC_ALL, ""); - while ((c = getopt(argc, argv, ":u")) != -1) { - switch(c) { - case 'u': /* UTC timezone */ - uflag++; - break; + while((c = getopt(argc, argv, ":u")) != -1) + { + switch(c) + { + case 'u': /* UTC timezone */ uflag++; break; } } - if(uflag) { + if(uflag) + { tm = gmtime(&now); if(tm == NULL) @@ -33,7 +35,9 @@ int main(int argc, char *argv[]) perror("gmtime"); exit(EXIT_FAILURE); } - } else { + } + else + { tm = localtime(&now); if(tm == NULL) @@ -46,8 +50,7 @@ int main(int argc, char *argv[]) argc -= optind; argv += optind; - if (*argv && **argv == '+') - format = *argv + 1; + if(*argv && **argv == '+') format = *argv + 1; if(strftime(outstr, sizeof(outstr), format, tm) == 0) { diff --git a/bin/dirname.c b/bin/dirname.c @@ -1,7 +1,8 @@ #include <libgen.h> // dirname() -#include <stdio.h> // puts() +#include <stdio.h> // puts() -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { if(argc != 2) { diff --git a/bin/lolcat.c b/bin/lolcat.c @@ -2,22 +2,26 @@ // Distributed under the terms of the CC-BY-SA-4.0 license #define _POSIX_C_SOURCE 200809L +#include <math.h> /* sin() */ #include <stdio.h> /* getchar(), putchar(), snprintf(), printf() */ -#include <math.h> /* sin() */ -void rainbow(double freq, int i) { +void +rainbow(double freq, int i) +{ int red, green, blue; double pi = 3.14159; - red = sin(freq*i + 0) * 127 + 128; - green = sin(freq*i + 2*pi/3) * 127 + 128; - blue = sin(freq*i + 4*pi/3) * 127 + 128; + red = sin(freq * i + 0) * 127 + 128; + green = sin(freq * i + 2 * pi / 3) * 127 + 128; + blue = sin(freq * i + 4 * pi / 3) * 127 + 128; printf("[38;2;%02d;%02d;%02dm", red, green, blue); // TODO: Replace to sprintf? } -int main(void) { +int +main(void) +{ int c, i; double freq = 0.1; diff --git a/bin/mdate.c b/bin/mdate.c @@ -1,13 +1,14 @@ #define _POSIX_C_SOURCE 200809L -#include <time.h> /* time */ #include <stdio.h> /* printf */ +#include <time.h> /* time */ // 3600*24.5 #define cycle 88200 int -main(void) { - time_t now = time(NULL); +main(void) +{ + time_t now = time(NULL); time_t date_now = now / cycle; time_t time_now = now % cycle; diff --git a/bin/pwd.c b/bin/pwd.c @@ -10,13 +10,15 @@ char buf[BUFSIZ]; -void usage(char *a0) +void +usage(char *a0) { (void)printf("usage: %s\n", a0); exit(EXIT_SUCCESS); } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { if(argc != 1) usage(argv[0]); diff --git a/bin/range.c b/bin/range.c @@ -7,9 +7,14 @@ char *IFS; int a, b; -void usage(char *a0) { printf("usage: %s [ini <max>]\n", a0); } +void +usage(char *a0) +{ + printf("usage: %s [ini <max>]\n", a0); +} -void range(int initial, int maximum) +void +range(int initial, int maximum) { printf("%i", initial); if(initial < maximum) @@ -24,7 +29,8 @@ void range(int initial, int maximum) } } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { if(!(IFS = getenv("IFS"))) { diff --git a/bin/sizeof.c b/bin/sizeof.c @@ -12,14 +12,16 @@ static const char *format = "sizeof(%s) == %d bytes; %d bits\n"; static const char *format_m = "sizeof(%s) == %d bytes; %d bits; (MIN:MAX) == (%d:%d)\n"; -static void print_size(size_t size, char *type) +static void +print_size(size_t size, char *type) { int c; c = size; printf(format, type, c, c * CHAR_BIT); } -int main(void) +int +main(void) { int c; diff --git a/bin/sname.c b/bin/sname.c @@ -1,10 +1,12 @@ +#include <stdio.h> // printf() #include <sys/utsname.h> // utsname, uname() -#include <stdio.h> // printf() -int main() +int +main() { struct utsname name; - if (uname(&name) != 0) { + if(uname(&name) != 0) + { perror("uname"); return 1; } diff --git a/bin/tty.c b/bin/tty.c @@ -1,16 +1,18 @@ -#include <stdio.h> // puts() +#include <stdio.h> // puts() #include <unistd.h> // ttyname(), isatty() int main(void) { - if(!isatty(STDIN_FILENO)) { + if(!isatty(STDIN_FILENO)) + { puts("not a tty"); return 1; } char *name = ttyname(STDIN_FILENO); - if (!name) { + if(!name) + { perror("ttyname"); return 2; } diff --git a/bin/xcd.c b/bin/xcd.c @@ -2,34 +2,41 @@ // Distributed under the terms of the CC-BY-SA-4.0 license #define _POSIX_C_SOURCE 200809L -#include <stdio.h> /* printf(), fread() */ -#include <math.h> /* sin() */ +#include <ctype.h> /* isprint() */ +#include <math.h> /* sin() */ #include <stdint.h> /* uint8_t */ +#include <stdio.h> /* printf(), fread() */ #include <string.h> /* memset() */ -#include <ctype.h> /* isprint() */ #define LANODAN_XCD_RESET printf(""); #define LANODAN_XCD_PRINT printf(" >%s<", line); -void rainbow(double freq, unsigned char i) { - if(i == 0) { +void +rainbow(double freq, unsigned char i) +{ + if(i == 0) + { printf(""); - } else { + } + else + { uint8_t red, green, blue; double pi = 3.14159; - red = sin(freq*i + 0*pi/3) * 127 + 128; - green = sin(freq*i + 2*pi/3) * 127 + 128; - blue = sin(freq*i + 4*pi/3) * 127 + 128; + red = sin(freq * i + 0 * pi / 3) * 127 + 128; + green = sin(freq * i + 2 * pi / 3) * 127 + 128; + blue = sin(freq * i + 4 * pi / 3) * 127 + 128; printf("[38;2;%02d;%02d;%02dm", red, green, blue); } } -int main(void) { - int cols = 0; +int +main(void) +{ + int cols = 0; int line_width = 16; - double freq = 0.018; + double freq = 0.018; char line[line_width]; unsigned char c; unsigned int bytes = 0; @@ -42,7 +49,8 @@ int main(void) { printf("%06x ", bytes); while(fread(&c, 1, 1, stdin) > 0) { - if(cols >= line_width) { + if(cols >= line_width) + { cols = 0; LANODAN_XCD_RESET @@ -61,9 +69,9 @@ int main(void) { bytes++; } - // Fill the rest of the hex space with spaces - for(cols; cols < line_width; cols++) printf(" "); + for(cols; cols < line_width; cols++) + printf(" "); LANODAN_XCD_RESET LANODAN_XCD_PRINT diff --git a/sbin/memsys.c b/sbin/memsys.c @@ -2,24 +2,30 @@ // Distributed under the terms of the CC-BY-4.0 license #define _POSIX_C_SOURCE 200809L -#include <fcntl.h> /* open() */ +#include <fcntl.h> /* open() */ +#include <stdio.h> /* perror() */ #include <unistd.h> /* close(), write() */ -#include <stdio.h> /* perror() */ #define SYS_POWER_STATE "/sys/power/state" -int main(void) { +int +main(void) +{ int fd, err = 0; - char *entry = "mem"; + char *entry = "mem"; size_t entry_size = 3; fd = open(SYS_POWER_STATE, O_WRONLY); - if(fd == -1) { - perror("memsys: open(\""SYS_POWER_STATE"\")"); + if(fd == -1) + { + perror("memsys: open(\"" SYS_POWER_STATE "\")"); err++; - } else { - if(write(fd, entry, entry_size) < (ssize_t)entry_size) { + } + else + { + if(write(fd, entry, entry_size) < (ssize_t)entry_size) + { perror("memsys: write(fd, \"mem\", 3)"); err++; }