~andreafeletto/cstatus

2d7dcd2cbc4862f17e32775a6b16ab36e20e50e4 — Andrea Feletto 2 months ago 1b9b3bc
use fputs and fprintf directly instead of utils.h functions
6 files changed, 12 insertions(+), 92 deletions(-)

M Makefile
M backlight.c
M battery.c
M config.mk
D utils.c
D utils.h
M Makefile => Makefile +2 -2
@@ 3,7 3,7 @@

include config.mk

SRC = cstatus.c utils.c backlight.c volume.c battery.c clock.c
SRC = cstatus.c backlight.c volume.c battery.c clock.c
OBJ = ${SRC:.c=.o}

all: cstatus


@@ 11,7 11,7 @@ all: cstatus
cstatus: ${OBJ}
	${CC} -o $@ ${OBJ} ${LDFLAGS}

${OBJ}: config.h utils.h backlight.c battery.c clock.c volume.c
${OBJ}: config.h backlight.c battery.c clock.c volume.c

install: cstatus
	install -Dm755 cstatus "${DESTDIR}${PREFIX}/bin/cstatus"

M backlight.c => backlight.c +5 -6
@@ 5,7 5,6 @@
#include <string.h>

#include "backlight.h"
#include "utils.h"

#define BLBRIGH  "/sys/class/backlight/intel_backlight/brightness"
#define BLMAX    "/sys/class/backlight/intel_backlight/max_brightness"


@@ 18,22 17,22 @@ backlight(char *buf) {
	int b, mb, p;

	if ((fp = fopen(BLBRIGH, "r")) == NULL) {
		warn("cannot open file '%s'", BLBRIGH);
		fprintf(stderr, "cannot open file '%s'\n", BLBRIGH);
	}
	if (fscanf(fp, "%d", &b) != 1) {
		warn("cannot read brightness value");
		fputs("cannot read brightness value\n", stderr);
	}
	fclose(fp);

	if ((fp = fopen(BLMAX, "r")) == NULL) {
		warn("cannot open file '%s'", BLBRIGH);
		fprintf(stderr, "cannot open file '%s'\n", BLMAX);
	}
	if (fscanf(fp, "%d", &mb) != 1) {
		warn("cannot read max brightness value");
		fputs("cannot read max brightness value\n", stderr);
	}
	fclose(fp);

	p = (int)round(100 * (double)b / (double)mb);
	p = (int)round((double)b * 100 / mb);
	sprintf(buf + strlen(buf), "\U0001f4a1 %4d%%", p);
	return 1;
}

M battery.c => battery.c +4 -6
@@ 3,8 3,6 @@
#include <stdio.h>
#include <string.h>

#include "utils.h"

#define BATCAP   "/sys/class/power_supply/BAT1/capacity"
#define BATSTAT  "/sys/class/power_supply/BAT1/status"



@@ 17,18 15,18 @@ battery(char *buf) {
	int capacity;

	if ((fp = fopen(BATCAP, "r")) == NULL) {
		warn("cannot open file '%s'", BATCAP);
		fprintf(stderr, "cannot open file '%s'\n", BATCAP);
	}
	if (fscanf(fp, "%d", &capacity) != 1) {
		warn("cannot read battery capacity");
		fputs("cannot read battery capacity\n", stderr);
	}
	fclose(fp);

	if ((fp = fopen(BATSTAT, "r")) == NULL) {
		warn("cannot open file '%s'", BATSTAT);
		fprintf(stderr, "cannot open file '%s'\n", BATSTAT);
	}
	if (fscanf(fp, "%s", &status[0]) != 1) {
		warn("cannot read battery status");
		fputs("cannot read battery status\n", stderr);
	}
	fclose(fp);


M config.mk => config.mk +1 -1
@@ 5,5 5,5 @@ PREFIX ?= /usr/local
CC = cc

CPPFLAGS = -D_POSIX_C_SOURCE=200112L
CFLAGS = -std=c99 -pedantic -Wall -Wno-deprecated-declarations -Os ${CPPFLAGS}
CFLAGS = -std=c99 -pedantic -Wall -Wno-deprecated-declarations -Os
LDFLAGS = -lm -lX11 -lasound
\ No newline at end of file

D utils.c => utils.c +0 -60
@@ 1,60 0,0 @@

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "utils.h"

void
verr(const char *fmt, va_list ap) {
	fprintf(stderr, "error: ");
	vfprintf(stderr, fmt, ap);

	if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
		fputc(' ', stderr);
		perror(NULL);
	} else {
		fputc('\n', stderr);
	}
}

void
warn(const char *fmt, ...) {
	va_list ap;

	va_start(ap, fmt);
	verr(fmt, ap);
	va_end(ap);
}

void
die(const char *fmt, ...) {
	va_list ap;

	va_start(ap, fmt);
	verr(fmt, ap);
	va_end(ap);
	exit(EXIT_FAILURE);
}

int
pscanf(const char *cmd, const char *fmt, ...) {
	va_list ap;
	FILE *pipe;
	int n;

	va_start(ap, fmt);
	if ((pipe = popen(cmd, "r")) == NULL) {
		die("popen '%s':", cmd);
	}
	if ((n = vfscanf(pipe, fmt, ap)) == EOF) {
		die("fscanf:");
	}
	if (pclose(pipe) == -1) {
		die("pclose:");
	}
	va_end(ap);

	return n;
}

D utils.h => utils.h +0 -17
@@ 1,17 0,0 @@

#ifndef CSTATUS_UTILS_H
#define CSTATUS_UTILS_H

#include <stdarg.h>

#define LENGTH(X) (sizeof X / sizeof X[0])

void verr(const char *fmt, va_list ap);

void die(const char *fmt, ...);

void warn(const char *fmt, ...);

int pscanf(const char *cmd, const char *fmt, ...);

#endif