system76-tools

collection of utilities for system76 laptops
git clone https://git.parazyd.org/system76-tools
Log | Files | Refs | README | LICENSE

commit b6c3dc97e1d83dfd99a4e22ba40a9bbbd2bd1eec
parent a0ccee4746d1c5252eca69086630aa5be26628c7
Author: parazyd <parazyd@dyne.org>
Date:   Fri, 21 Oct 2022 15:29:49 +0200

Science

Diffstat:
Mbrightness.c | 17+++--------------
Mcommon.c | 17+----------------
Mcommon.h | 1-
Mperf-profile.c | 61+++++++++++++++----------------------------------------------
4 files changed, 19 insertions(+), 77 deletions(-)

diff --git a/brightness.c b/brightness.c @@ -1,7 +1,6 @@ /* suid tool for setting screen brightness in increments * GPL-3 */ -#define _GNU_SOURCE #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -35,8 +34,7 @@ int main(int argc, char *argv[]) enum Op op = 0; int max, cur, inc; int uflag = 0, dflag = 0, minflag = 0, maxflag = 0; - size_t len, nread; - char *line = NULL; + char buf[10]; FILE *fd; ARGBEGIN { @@ -67,14 +65,10 @@ int main(int argc, char *argv[]) if ((fd = fopen(BRIGHT_MAX, "r")) == NULL) die("Couldn't read %s:", BRIGHT_MAX); - nread = getline(&line, &len, fd); + max = atoi(fgets(buf, 10, fd)); fclose(fd); fd = NULL; - max = atoi(line); - free(line); - line = NULL; - /* Here the number of available increments can be configured */ inc = max / 20; @@ -82,12 +76,7 @@ int main(int argc, char *argv[]) if ((fd = fopen(BRIGHT_CUR, "w+")) == NULL) die("Couldn't open %s for r/w:", BRIGHT_CUR); - nread = getline(&line, &len, fd); - (void)nread; - - cur = atoi(line); - free(line); - line = NULL; + cur = atoi(fgets(buf, 10, fd)); switch(op) { case UP: diff --git a/common.c b/common.c @@ -21,21 +21,6 @@ void die(const char *fmt, ...) exit(1); } -int intlen(int n) -{ - if (n < 0) return intlen(-n) + 1; - if (n < 10) return 1; - if (n < 100) return 2; - if (n < 1000) return 3; - if (n < 10000) return 4; - if (n < 100000) return 5; - if (n < 1000000) return 6; - if (n < 10000000) return 7; - if (n < 100000000) return 8; - if (n < 1000000000) return 9; - return 10; -} - int write_oneshot_str(const char *path, const char *text) { FILE *fd; @@ -43,7 +28,7 @@ int write_oneshot_str(const char *path, const char *text) if ((fd = fopen(path, "w")) == NULL) return 1; - fprintf(fd, text); + fputs(text, fd); fclose(fd); fprintf(stderr, "%s: %s\n", path, text); diff --git a/common.h b/common.h @@ -4,7 +4,6 @@ #define MIN(a, b) ((a) < (b) ? (a) : (b)) void die(const char *fmt, ...); -int intlen(int n); int write_oneshot_str(const char *path, const char *text); int write_oneshot_int(const char *path, int value); diff --git a/perf-profile.c b/perf-profile.c @@ -3,12 +3,12 @@ * https://www.kernel.org/doc/html/latest/userspace-api/sysfs-platform_profile.html * https://mjmwired.net/kernel/Documentation/ABI/testing/sysfs-platform_profile */ -#define _GNU_SOURCE #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/sysinfo.h> +#include <linux/limits.h> #include "arg.h" #include "common.h" @@ -58,9 +58,8 @@ static void set_max_lost_work(int secs) static int get_frequency(enum MType typ, int cpu) { const char *rem; - char *line = NULL, *path; - size_t nread, len; - int plen, ret; + char path[PATH_MAX], buf[10]; + int ret; FILE *fd; switch(typ) { @@ -72,23 +71,13 @@ static int get_frequency(enum MType typ, int cpu) break; } - plen = strlen(SYS_CPU_PREFIX) + strlen(rem) + intlen(cpu) + 1; - path = malloc(plen); - memset(path, 0, plen); - snprintf(path, plen, "%s%d%s", SYS_CPU_PREFIX, cpu, rem); + snprintf(path, PATH_MAX, "%s%d%s", SYS_CPU_PREFIX, cpu, rem); - if ((fd = fopen(path, "r")) == NULL) { - free(path); + if ((fd = fopen(path, "r")) == NULL) die("Could not open cpu%d%s file for reading:", cpu, rem); - } - - nread = getline(&line, &len, fd); - (void)nread; - - ret = atoi(line); - free(path); - free(line); + ret = atoi(fgets(buf, 10, fd)); + fclose(fd); return ret; } @@ -96,8 +85,7 @@ static int get_frequency(enum MType typ, int cpu) static void set_frequency(enum MType typ, int cpu, int freq) { const char *rem; - char *path; - int plen; + char path[PATH_MAX]; switch(typ) { case MIN: @@ -108,36 +96,21 @@ static void set_frequency(enum MType typ, int cpu, int freq) break; } - plen = strlen(SYS_CPU_PREFIX) + strlen(rem) + intlen(cpu) + 1; - path = malloc(plen); - memset(path, 0, plen); - snprintf(path, plen, "%s%d%s", SYS_CPU_PREFIX, cpu, rem); + snprintf(path, PATH_MAX, "%s%d%s", SYS_CPU_PREFIX, cpu, rem); - if (write_oneshot_int(path, freq)) { - free(path); + if (write_oneshot_int(path, freq)) die("Could not open cpu%d%s file for writing:", cpu, rem); - } - - free(path); } static void set_governor(int cpu, const char *governor) { const char *rem = "/cpufreq/scaling_governor"; - char *path; - int plen; + char path[PATH_MAX]; - plen = strlen(SYS_CPU_PREFIX) + strlen(rem) + intlen(cpu) + 1; - path = malloc(plen); - memset(path, 0, plen); - snprintf(path, plen, "%s%d%s", SYS_CPU_PREFIX, cpu, rem); + snprintf(path, PATH_MAX, "%s%d%s", SYS_CPU_PREFIX, cpu, rem); - if (write_oneshot_str(path, governor)) { - free(path); + if (write_oneshot_str(path, governor)) die("Could not write to cpu%d%s file:", cpu, rem); - } - - free(path); } static void cpufreq_set(enum Profile profile, int max_percent) @@ -258,19 +231,15 @@ int main(int argc, char *argv[]) } ARGEND; if (vflag) { - char *line = NULL; - size_t len, nread; + char buf[12]; FILE *fd; if ((fd = fopen(S76_POW_PROF, "r")) == NULL) die("Could not open %s for reading:", S76_POW_PROF); - nread = getline(&line, &len, fd); + printf("Current profile: %s\n", fgets(buf, 12, fd)); fclose(fd); - (void)nread; - printf("Current profile: %s\n", line); - free(line); return 0; }