pi_clock

Crude and unpolished clock for Raspberry Pi
git clone https://git.e1e0.net/pi_clock.git
Log | Files | Refs | README | LICENSE

commit a83e3fa5b43c80fd3bb9eb77e07f70def3f21afa
parent 0d3ffcde4a7784bf4692d5e1dd2fdd7e3add463a
Author: Paco Esteban <paco@e1e0.net>
Date:   Sat,  1 Aug 2020 20:30:40 +0200

better initializations and use pointers everywhere

Diffstat:
Mclock.c | 66+++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 33 insertions(+), 33 deletions(-)

diff --git a/clock.c b/clock.c @@ -18,6 +18,7 @@ #define _GNU_SOURCE +#include <linux/limits.h> #include <err.h> #include <errno.h> #include <stdio.h> @@ -37,14 +38,15 @@ #define DEF_SLEEP_INTER 250 #define DEF_BL_BEFORE 0 #define DEF_BL_AFTER 7 +#define TAG_MAX_LEN 7 #define PIC_VERSION "v0.5.0" lcd_i2c_t *init_lcd(void); char * setmonth(int); char * setwday(int); +char * getExtTag(char *); void usage(); -int getExtTag(char *, char *); int init_pins(void); int print_line_lcd(lcd_i2c_t *, int, char *); @@ -53,19 +55,13 @@ main(int argc, char *argv[]) { time_t t; struct tm *tm; - char *linebuff; - char *str_wday; - char *str_month; - char extFile[255] = ""; - char extTag[8]; - char script[255]; + char *linebuff = NULL, *str_wday = NULL, *str_month = NULL, + *extFile = NULL, *script = NULL, *extTag = NULL; long gmt_offset; uint8_t backlight_status = 0, vflag = 0, eflag = 0; int readCount = 0, tmp_on_cycles = 0, ch; - int bl_before = DEF_BL_BEFORE; - int bl_after = DEF_BL_AFTER; - int readInterval = DEF_R_INTER; - int sleepInterval = DEF_SLEEP_INTER; + int bl_before = DEF_BL_BEFORE, bl_after = DEF_BL_AFTER, + readInterval = DEF_R_INTER, sleepInterval = DEF_SLEEP_INTER; while ((ch = getopt(argc, argv, "b:a:s:S:r:e:v")) != -1) { switch (ch) { @@ -76,7 +72,9 @@ main(int argc, char *argv[]) bl_after = atoi(optarg); break; case 's': - (void)strncpy(script, optarg, sizeof(script) - 1); + if (strlen(optarg) > PATH_MAX) + err(1, "path too long"); + script = strdup(optarg); break; case 'S': sleepInterval = atoi(optarg); @@ -86,7 +84,9 @@ main(int argc, char *argv[]) break; case 'e': eflag = 1; - (void)strncpy(extFile, optarg, sizeof(extFile) - 1); + if (strlen(optarg) > PATH_MAX) + err(1, "path too long"); + extFile = strdup(optarg); break; case 'v': vflag = 1; @@ -136,9 +136,9 @@ main(int argc, char *argv[]) if (eflag && strlen(extFile) > 2) { /* read external file only interval * sleep */ if (readCount == readInterval) { - if (getExtTag(extTag, extFile) < 0) { + free(extTag); + if ((extTag = getExtTag(extFile)) == NULL) err(1, "Error getting external tag\n"); - } readCount = 0; } readCount++; @@ -152,7 +152,6 @@ main(int argc, char *argv[]) if (print_line_lcd(lcd, 0, linebuff) == -1) err(1, "cannot print line0\n"); - str_wday = setwday(tm->tm_wday); if (str_wday == NULL) err(1, "str_day"); @@ -170,12 +169,12 @@ main(int argc, char *argv[]) if (digitalRead(BUTTON1) == LOW) { if (backlight_status) { - if (strlen(script) > 2) { + if (script != NULL) { fprintf(stderr, "calling '%s'\n", script); system(script); } else { - if (strlen(extFile) > 2) { + if (extFile != NULL) { eflag = !eflag; } } @@ -193,6 +192,8 @@ main(int argc, char *argv[]) delay(sleepInterval); } + free(extFile); + free(script); free(lcd); return 0; @@ -219,29 +220,28 @@ setwday(int wday) return strdup(days[wday]); } -int -getExtTag(char *tagBuff, char *tagFile) { +char * +getExtTag(char *tagFile) { FILE *fp; - char *buff; - int len; - int maxlen = 7; + char *buff = malloc(TAG_MAX_LEN); + int len = 0; fp = fopen(tagFile, "r"); if (fp == NULL) { - return -1; + return NULL; } - buff = fgets(tagBuff, maxlen, fp); - fclose(fp); - if (buff == NULL) - return -2; + if (fgets(buff, TAG_MAX_LEN, fp) == NULL) + return NULL; - len = strlen(tagBuff); - /* if tagBuff < maxlen we remove \n */ - if( tagBuff[len-1] == '\n' ) - tagBuff[len-1] = 0x20; + fclose(fp); - return 0; + len = strlen(buff); + /* if buff < TAG_MAX_LEN we remove \n */ + if( buff[len-1] == '\n' ) + buff[len-1] = 0x20; + + return buff; } /* Print usage and exit */