Skip to content

Commit 1bb788f

Browse files
author
Connor
committed
Code style posted and applied to code.
Trailing whitespace removed from code Macros made for long, repetitive functions Some binary size and runtime optimization through macros
1 parent 963d8c6 commit 1bb788f

20 files changed

Lines changed: 236 additions & 204 deletions

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ifeq ($(CC),gcc)
3636

3737
ifeq ($(DEBUG),true)
3838
# gcc-specific security/debug flags
39-
WGCC += -fanalyzer
39+
WGCC += -fanalyzer
4040
CFLAGS = -ggdb $(WGCC)
4141
LFLAGS =
4242

@@ -45,7 +45,7 @@ endif #debug
4545
else ifeq ($(CC),clang) # clang can be marginally slower
4646

4747
CC = clang
48-
CFLAGS += -Weverything
48+
CFLAGS += -Weverything
4949
LINKER = clang
5050
WNOFLAGS += -Wno-disabled-macro-expansion
5151

@@ -55,7 +55,7 @@ ifeq ($(DEBUG),true)
5555
-fsanitize-undefined-trap-on-error -ftrivial-auto-var-init=pattern \
5656
-fvisibility=hidden
5757
CFLAGS = -gdwarf-4 -Weverything -mspeculative-load-hardening -mretpoline
58-
LFLAGS = -fsanitize=address
58+
LFLAGS = -fsanitize=address
5959
endif #debug
6060

6161
endif #compiler
@@ -87,11 +87,11 @@ $(TARGET):
8787
@# create these directories if needed
8888
mkdir -p obj/modules
8989
mkdir -p bin/
90-
## compile with multiple threads, then link.
90+
@# compile with multiple threads, then link.
9191
$(MAKE) $(OBJECTS)
9292
$(MAKE) link
9393

94-
link:
94+
link:
9595
$(LINKER) $(OBJECTS) $(LFLAGS) -o $(BINDIR)/$(TARGET)
9696
@echo "Linking complete!"
9797

config.mk

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# fetchme's config.
22
# options can be turned on with either
3-
# `Y' or `y'. to deselect an option,
3+
# `Y' or `y'. to deselect an option,
44
# comment it out.
55

6-
# to set a custom color,
6+
# to set a custom color,
77
# uncomment both CUSTOM_COLOR
88
# and CUSTOM_COLOR_VALUE
99
# M_CUSTOM_COLOR=y
@@ -25,7 +25,7 @@ M_KERNEL=y
2525
# Off by default.
2626
# Currently only works
2727
# with pacman-based
28-
# distros and
28+
# distros and
2929
# portage-based
3030
# distros.
3131
#
@@ -47,12 +47,12 @@ M_SHELL=y
4747
# Works with FreeBSD
4848
M_TERMINAL=y
4949

50-
# any option associated with CPU
50+
# any option associated with CPU
5151
# needs to have CPU=y
5252
# Does NOT work with FreeBSD
5353
# M_CPU=y
5454
# M_CPU_THREADS=y
55-
# commented out by default since
55+
# commented out by default since
5656
# it's **experimental**
5757
# M_CPU_TEMP=y
5858
# M_CPU_FREQUENCY=y

docs/CODESTYLE.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Code Style
2+
3+
# This project follows the Linux/coreboot code style:
4+
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/Documentation/process/coding-style.rst
5+
https://doc.coreboot.org/contributing/coding_style.html
6+
7+
# ...With a couple differences.
8+
- indentations/tabs are 4 spaces
9+
- headers go in a specific order: standard libraries, POSIX libraries, system libraries, then X11 libraries. (alphabetical order if there are multiple of one type)
10+
- a little pedantic, but all functions on success must return ``EXIT_SUCCESS`` if they would otherwise return ``0``. This is more portable. (the same is true for ``EXIT_FAILURE`` and a nonzero value)

docs/CONTRIBUTING.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Thank you for considering contributing to fetchme.
44

55

6-
Unsure how to begin contributing?
6+
Unsure how to begin contributing?
77
Here is how to get started: https://makeapullrequest.com/
88

99
Contributing in most cases helps to speed up the development process of the project, as I cannot work on it full time.
@@ -30,16 +30,16 @@ Small contributions such as fixing spelling errors are fine to do in a PR as lon
3030
- Adding logging messages or debugging output
3131
- Moving source files from one directory or package to another
3232

33-
[//]: # "If you find a security vulnerability, do NOT open an issue. Email <this will be added soon> instead."
33+
# If you find a security vulnerability, do NOT open an issue. Email connor-gh <at> outlook <dot> com instead.
3434
# For security Vulnerabilities:
3535
You can open an issue currently, but this will be changed in the future.
3636

37-
Lastly,
37+
Lastly,
3838
## When filing an issue, make sure to answer these five questions:
3939

40-
- What version of GCC/Linux are you using (``gcc -v && uname -r``)?
40+
- What version of GCC and Linux/BSD are you using (``gcc -v && uname -r``)?
4141
- What processor architecture are you using?
4242
- What did you do?
4343
- What did you expect to see?
44-
- What did you see instead?
44+
- What did you see instead?
4545
## At this point, you're ready to make your changes!

src/fetchme.c

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,48 @@
11
#include <stdio.h> // for those two printfs
2-
#include <string.h>
2+
33
#include "./modules/include/color.h"
44
#include "./modules/include/fetchme.h"
55

6-
/*
6+
/*
77
* the current state:
88
*
9-
* - 1:1 features with the previous
10-
* version (repo private)
9+
* - Feature improvements being worked on.
1110
*
12-
* - bug patching needed (as always)
13-
* - code style rework
1411
*/
1512

1613
int
1714
main(int argc, char *argv[])
1815
{
19-
if (argc > 1)
20-
{
21-
if (argv[1][0] == '-')
22-
{
23-
switch (argv[1][1])
24-
{
25-
case 'v':
26-
printf("%s version %s\n", _PACKAGE_NAME, _PACKAGE_VERSION);
27-
break;
28-
case 'h': /* fall through */
29-
default:
30-
fprintf(stderr,
31-
"Usage: \n"
32-
"\tfetchme [ -v ]\t\tPrint version info\n"
33-
"\tfetchme [ -h ]\t\tPrint this help message\n"
34-
"\tfetchme [ no options ]\tPrint system info\n"
35-
"\n\tFor more help, please consult the man page.\n");
36-
break;
16+
if (argc > 1) {
17+
18+
if (argv[1][0] == '-') {
19+
20+
switch (argv[1][1]) {
21+
case 'v':
22+
printf("%s version %s\n", _PACKAGE_NAME, _PACKAGE_VERSION);
23+
break;
24+
case 'h': /* fall through */
25+
default:
26+
fprintf(stderr,
27+
"Usage: \n"
28+
"\tfetchme [ -v ]\t\tPrint version info\n"
29+
"\tfetchme [ -h ]\t\tPrint this help message\n"
30+
"\tfetchme [ no options ]\tPrint system info\n"
31+
"\n\tFor more help, please consult the man page.\n");
32+
break;
3733
}
3834

3935
}
4036
return 0;
4137
}
4238
// disable line wrapping.
43-
// yes, I disabled
39+
// yes, I disabled
4440
// line wrapping for
4541
// something this simple.
4642

4743
printf("\033[?7l");
48-
#ifdef USERNAME_HOSTNAME
49-
username_at_hostname();
44+
#ifdef USERNAME_HOSTNAME
45+
username_at_hostname();
5046
#endif
5147

5248
#ifdef DISTRO

src/modules/color.c

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
#include "include/color.h"
66

77
const char *
8-
color_distro()
8+
color_distro()
99
{
1010
char os_name[50];
1111
FILE *os_release = fopen("/etc/os-release", "r");
1212

1313

14-
if(os_release == NULL) {
14+
if (os_release == NULL) {
1515
perror("/etc/os-release");
1616
exit(EXIT_FAILURE);
1717
}
1818

1919
fscanf(os_release, "NAME=%49[^\n]+", os_name); /* get everything
20-
* that isn't a
21-
* newline */
20+
* that isn't a
21+
* newline */
2222
fclose(os_release);
2323
/*
2424
* remove quotation marks from
@@ -27,26 +27,34 @@ color_distro()
2727
sscanf(os_name, "\"%[^\"]", os_name); // get everything that isn't quotes
2828
/* if statement for distro name */
2929
#ifndef CUSTOM_COLOR
30-
if (strstr(os_name, "Gentoo") != NULL) return PURPLE;
31-
else if (strstr(os_name, "Debian") != NULL) return RED;
32-
else if (strstr(os_name, "Void") != NULL) return "\033[1;38;5;34m";
33-
else if (strstr(os_name, "Ubuntu") != NULL) return "\033[1;38;5;202m";
34-
else if (strstr(os_name, "Solus") != NULL) return BLUE;
35-
else if ((strstr(os_name, "Pop!_OS")) \
30+
if (strstr(os_name, "Gentoo") != NULL) {
31+
return PURPLE;
32+
} else if (strstr(os_name, "Debian") != NULL) {
33+
return RED;
34+
} else if (strstr(os_name, "Void") != NULL) {
35+
return "\033[1;38;5;34m";
36+
} else if (strstr(os_name, "Ubuntu") != NULL) {
37+
return "\033[1;38;5;202m";
38+
} else if (strstr(os_name, "Solus") != NULL) {
39+
return BLUE;
40+
} else if ((strstr(os_name, "Pop!_OS")) \
3641
|| (strstr(os_name, "popos")) \
37-
|| (strstr(os_name, "pop_os")) != NULL) return "\033[1;38;5;29m";
38-
else if ((strstr(os_name, "Mint")) \
42+
|| (strstr(os_name, "pop_os")) != NULL) {
43+
return "\033[1;38;5;29m";
44+
} else if ((strstr(os_name, "Mint")) \
3945
|| (strstr(os_name, "OpenSUSE")) \
4046
|| (strstr(os_name, "openSUSE")) \
41-
|| (strstr(os_name, "Manjaro")) != NULL)return GREEN;
42-
else if ((strstr(os_name, "Arch")) \
43-
|| (strstr(os_name, "Artix")) != NULL) return CYAN;
44-
else {
47+
|| (strstr(os_name, "Manjaro")) != NULL) {
48+
return GREEN;
49+
} else if ((strstr(os_name, "Arch")) \
50+
|| (strstr(os_name, "Artix")) != NULL) {
51+
return CYAN;
52+
} else {
4553
fprintf(stderr, "Exception: distro color not found.");
46-
exit(EXIT_FAILURE); // return value is 1
54+
exit(EXIT_FAILURE);
4755
}
4856
#endif
4957
#ifdef CUSTOM_COLOR
50-
return CUSTOM_COLOR_VALUE;
58+
return CUSTOM_COLOR_VALUE;
5159
#endif
5260
}

src/modules/cpu_info.c

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,25 @@
55
#include "./include/color.h"
66

77
int
8-
cpu_info()
8+
cpu_info()
99
{
10+
#define ITER(x) (for (int i = 0; i < x; i++) \
11+
while ((c = fgetc(cpu)) != '\n' && c != EOF))
1012

1113
char brand[10]; // cpu brand
1214
char lineup[10]; // lineup (Ryzen, Core, Xeon, Epyc, etc)
1315
char sublineup[10]; // sublineup (5, 7, 9 or i5 i7 i9 etc)
1416
char model_num[10]; // model number (3600, 9900k, 3900X, etc)
15-
char freq[10]; //cpu frequency
16-
char threads[10]; //cpu threads
17+
#ifdef CPU_FREQUENCY
18+
char freq[10]; // cpu frequency
19+
#endif /* CPU_FREQUENCY */
20+
#ifdef CPU_THREADS
21+
char threads[10]; // cpu threads
22+
#endif /* CPU_THREADS */
1723
#ifdef CPU_TEMP
1824
float TEMP;
19-
#endif
25+
FILE *cpu3 = fopen("/sys/class/hwmon/hwmon1/temp3_input", "r");
26+
#endif /* CPU_TEMP */
2027

2128
int c;
2229

@@ -26,48 +33,56 @@ cpu_info()
2633
exit(EXIT_FAILURE);
2734
}
2835
// this long line is used to skip lines.
29-
for (int i = 0; i < 4; i++) while ((c = fgetc(cpu)) != '\n' && c != EOF);
36+
//for (int i = 0; i < 4; i++)
37+
//while ((c = fgetc(cpu)) != '\n' && c != EOF);
38+
ITER(4);
3039

3140
fscanf(cpu, "%*9s %*9s \t: %9s %9s %9s %9s", \
3241
brand, lineup, sublineup, model_num);
33-
for(int i = 0; i < 3; i++) while ((c = fgetc(cpu)) != '\n' && c != EOF);
34-
42+
#if defined(CPU_FREQUENCY) || defined(CPU_THREADS) || defined(CPU_TEMP)
43+
/*for (int i = 0; i < 3; i++)
44+
while ((c = fgetc(cpu)) != '\n' && c != EOF);*/
45+
ITER(3);
3546

47+
#ifdef CPU_FREQUENCY
3648
fscanf(cpu, "%*9s %*9s \t: %9s", freq);
37-
for(int i = 0; i < 3; i++) while ((c = fgetc(cpu)) != '\n' && c != EOF);
38-
49+
#endif /* CPU_FREQUENCY */
50+
/*for (int i = 0; i < 3; i++)
51+
while ((c = fgetc(cpu)) != '\n' && c != EOF);*/
52+
ITER(3);
3953

54+
#ifdef CPU_THREADS
4055
fscanf(cpu, "%*9s \t: %9s", threads);
56+
#endif /* CPU_THREADS */
57+
#endif /* CPU_FREQUENCY || CPU_THREADS || CPU_TEMP */
4158
fclose(cpu);
4259
#ifdef CPU_TEMP
43-
FILE *cpu3 = fopen("/sys/class/hwmon/hwmon1/temp3_input", "r");
4460

45-
if (cpu3 == NULL) TEMP = 0.;
46-
47-
else {
61+
if (cpu3 == NULL) {
62+
TEMP = 0.;
63+
} else {
4864
char line1_value[100];
65+
int x2 = 0;
4966
fscanf(cpu3, "%99s", line1_value);
5067
fclose(cpu3);
51-
int x2;
5268
sscanf(line1_value, "%d", &x2);
53-
TEMP =(x2/1000.);
69+
TEMP = (x2/1000.);
5470
}
55-
#endif
56-
71+
#endif /* CPU_TEMP */
5772

5873
printf("%sCPU:\033[0m %s %s %s %s",\
5974
color_distro(), brand, lineup, sublineup, model_num);
6075

6176

6277
#ifdef CPU_THREADS
6378
printf(" (%s)", threads);
64-
#endif
79+
#endif /* CPU_THREADS */
6580
#ifdef CPU_FREQUENCY
6681
printf(" @ %sMHz", freq);
67-
#endif
82+
#endif /* CPU_FREQUENCY */
6883
#ifdef CPU_TEMP
6984
printf(" (%.1f°C)", TEMP);
70-
#endif
85+
#endif /* CPU_TEMP */
7186
printf("\n");
7287
return EXIT_SUCCESS;
7388
}

0 commit comments

Comments
 (0)