system76-tools

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

common.c (745B)


      1 #include <stdarg.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 void die(const char *fmt, ...)
      7 {
      8 	va_list ap;
      9 
     10 	va_start(ap, fmt);
     11 	vfprintf(stderr, fmt, ap);
     12 	va_end(ap);
     13 
     14 	if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
     15 		fputc(' ', stderr);
     16 		perror(NULL);
     17 	} else {
     18 		fputc('\n', stderr);
     19 	}
     20 
     21 	exit(1);
     22 }
     23 
     24 int write_oneshot_str(const char *path, const char *text)
     25 {
     26 	FILE *fd;
     27 
     28 	if ((fd = fopen(path, "w")) == NULL)
     29 		return 1;
     30 
     31 	fputs(text, fd);
     32 	fclose(fd);
     33 
     34 	fprintf(stderr, "%s: %s\n", path, text);
     35 	return 0;
     36 }
     37 
     38 int write_oneshot_int(const char *path, int value)
     39 {
     40 	FILE *fd;
     41 
     42 	if ((fd = fopen(path, "w")) == NULL)
     43 		return 1;
     44 
     45 	fprintf(fd, "%d", value);
     46 	fclose(fd);
     47 
     48 	fprintf(stderr, "%s: %d\n", path, value);
     49 	return 0;
     50 }