# HG changeset patch # User Michiel Broek # Date 1404249297 -7200 # Node ID 3d7a241329e23cfead070b46b743bc342a22afc1 # Parent c3cc6e44d1a4f63055ccb3f004b140a23d58ad69 Using final user paths for log, configuration and profiles diff -r c3cc6e44d1a4 -r 3d7a241329e2 thermferm/Makefile --- a/thermferm/Makefile Tue Jul 01 21:53:03 2014 +0200 +++ b/thermferm/Makefile Tue Jul 01 23:14:57 2014 +0200 @@ -56,6 +56,7 @@ # Dependencies generated by make depend thermferm.o: thermferm.h sensors.o: thermferm.h +futil.o: thermferm.h lock.o: thermferm.h logger.o: thermferm.h lcd-pcf8574.o: thermferm.h diff -r c3cc6e44d1a4 -r 3d7a241329e2 thermferm/futil.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thermferm/futil.c Tue Jul 01 23:14:57 2014 +0200 @@ -0,0 +1,59 @@ +/***************************************************************************** + * Copyright (C) 2014 + * + * Michiel Broek + * + * This file is part of the mbsePi-apps + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * mbsePi-apps is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EC-65K; see the file COPYING. If not, write to the Free + * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + *****************************************************************************/ + +#include "thermferm.h" + + +/* + * Make directory tree, the name must end with a / + */ +int mkdirs(char *name, mode_t mode) +{ + char buf[PATH_MAX], *p, *q; + int last = 0, oldmask; + + memset(&buf, 0, sizeof(buf)); + strncpy(buf, name, sizeof(buf)-1); + buf[sizeof(buf)-1] = '\0'; + + p = buf+1; + + oldmask = umask(000); + while ((q = strchr(p, '/'))) { + *q = '\0'; + mkdir(buf, mode); + last = errno; + *q = '/'; + p = q+1; + } + + umask(oldmask); + + if ((last == 0) || (last == EEXIST)) { + return TRUE; + } else { + syslog(LOG_NOTICE, "mkdirs(%s)", name); + return FALSE; + } +} + + diff -r c3cc6e44d1a4 -r 3d7a241329e2 thermferm/logger.c --- a/thermferm/logger.c Tue Jul 01 21:53:03 2014 +0200 +++ b/thermferm/logger.c Tue Jul 01 23:14:57 2014 +0200 @@ -22,8 +22,6 @@ #include "thermferm.h" -//char check[10240] = ""; - void logger(char *filename, char *progname, char *data) { @@ -32,9 +30,15 @@ char *outstr = NULL, *name = NULL; FILE *logfile; - name = xstrcpy((char *)"/var/local/log/"); + if (getenv((char *)"USER") == NULL) { + name = xstrcpy((char *)"/root"); + } else { + name = xstrcpy(getenv((char *)"HOME")); + } + name = xstrcat(name, (char *)"/."); name = xstrcat(name, progname); - name = xstrcat(name, (char *)"/"); + name = xstrcat(name, (char *)"/log/"); + mkdirs(name, 0755); name = xstrcat(name, filename); gettimeofday(&now, NULL); @@ -42,15 +46,12 @@ outstr = calloc(10240, sizeof(char)); snprintf(outstr, 10239, "%04d-%02d-%02d %02d:%02d,%s\n", ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, data); -// if (strcmp(check, outstr)) { -// snprintf(check, 10239, "%04d-%02d-%02d %02d:%02d,%s\n", ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, data); - if ((logfile = fopen(name, "a+"))) { - fprintf(logfile, outstr); - fclose(logfile); - } else { - syslog(LOG_NOTICE, "logger: cannot open %s for writing", name); - } -// } + if ((logfile = fopen(name, "a+"))) { + fprintf(logfile, outstr); + fclose(logfile); + } else { + syslog(LOG_NOTICE, "logger: cannot open %s for writing", name); + } free(outstr); outstr = NULL; diff -r c3cc6e44d1a4 -r 3d7a241329e2 thermferm/rdconfig.c --- a/thermferm/rdconfig.c Tue Jul 01 21:53:03 2014 +0200 +++ b/thermferm/rdconfig.c Tue Jul 01 23:14:57 2014 +0200 @@ -88,7 +88,7 @@ -int wrconfig(char *confxml) +int wrconfig(void) { int rc = 0; FILE *fp; @@ -375,8 +375,9 @@ } else { mypath = xstrcpy(getenv((char *)"HOME")); } - mypath = xstrcat(mypath, (char *)"/mbsepi-apps/"); - mypath = xstrcat(mypath, confxml); + mypath = xstrcat(mypath, (char *)"/.thermferm/etc/"); + mkdirs(mypath, 0755); + mypath = xstrcat(mypath, (char *)"thermferm.xml"); if (debug) fprintf(stdout, "Writing %s\n", mypath); @@ -679,7 +680,7 @@ -int rdconfig(char *config) +int rdconfig(void) { int rc = 0, ival; xmlDocPtr doc; @@ -697,24 +698,23 @@ } else { mypath = xstrcpy(getenv((char *)"HOME")); } - mypath = xstrcat(mypath, (char *)"/mbsepi-apps/"); - mypath = xstrcat(mypath, config); + mypath = xstrcat(mypath, (char *)"/.thermferm/etc/"); + mkdirs(mypath, 0755); + mypath = xstrcat(mypath, (char *)"thermferm.xml"); if ((doc = xmlParseFile(mypath)) == NULL) { /* * Not in the users home directory */ free(mypath); - mypath = xstrcpy((char *)"/etc/mbsepi-apps/"); - mypath = xstrcat(mypath, config); + mypath = xstrcpy((char *)"/etc/mbsepi-apps/thermferm.xml"); if ((doc = xmlParseFile(mypath)) == NULL) { /* * Try /usr/local/etc */ free(mypath); - mypath = xstrcpy((char *)"/usr/local/etc/mbsepi-apps/"); - mypath = xstrcat(mypath, config); + mypath = xstrcpy((char *)"/usr/local/etc/mbsepi-apps/thermferm.xml"); if ((doc = xmlParseFile(mypath)) == NULL) { - syslog(LOG_NOTICE, "rdconfig: could not parse %s", config); + syslog(LOG_NOTICE, "rdconfig: could not find thermferm.xml"); return 1; } } diff -r c3cc6e44d1a4 -r 3d7a241329e2 thermferm/server.c --- a/thermferm/server.c Tue Jul 01 21:53:03 2014 +0200 +++ b/thermferm/server.c Tue Jul 01 23:14:57 2014 +0200 @@ -168,7 +168,7 @@ */ int cmd_list(char *buf) { - char *cmd, *opt; + char *cmd, *opt, *mypath; units_list *unit; int i; DIR *fd; @@ -186,33 +186,62 @@ } srv_send((char *)"."); } else if (strcmp(opt, (char *)"BUS") == 0) { + /* + * 1-wire bus + */ if ((fd = opendir((char *)"/sys/bus/w1/devices"))) { - srv_send((char *)"212 1-wire bus devices:"); - while ((de = readdir(fd))) { - if (de->d_name[0] != '.') { - if (strncmp(de->d_name, (char *)"w1", 2) == 0) { - srv_send((char *)"%-15s system device", de->d_name); - } else if (strncmp(de->d_name, (char *)"10", 2) == 0) { - srv_send((char *)"%-15s 18S20 digital thermometer", de->d_name); - } else if (strncmp(de->d_name, (char *)"28", 2) == 0) { - srv_send((char *)"%-15s 18B20 digital thermometer", de->d_name); - } else if (strncmp(de->d_name, (char *)"29", 2) == 0) { - srv_send((char *)"%-15s 2408 8-channel addressable switch", de->d_name); - } else if (strncmp(de->d_name, (char *)"3a", 2) == 0) { - srv_send((char *)"%-15s 2413 dual channel addressable switchs", de->d_name); - } else { - srv_send((char *)"%-15s unknown device", de->d_name); - } + srv_send((char *)"212 1-wire bus devices:"); + while ((de = readdir(fd))) { + if (de->d_name[0] != '.') { + if (strncmp(de->d_name, (char *)"w1", 2) == 0) { + srv_send((char *)"%-15s system device", de->d_name); + } else if (strncmp(de->d_name, (char *)"10", 2) == 0) { + srv_send((char *)"%-15s 18S20 digital thermometer", de->d_name); + } else if (strncmp(de->d_name, (char *)"28", 2) == 0) { + srv_send((char *)"%-15s 18B20 digital thermometer", de->d_name); + } else if (strncmp(de->d_name, (char *)"29", 2) == 0) { + srv_send((char *)"%-15s 2408 8-channel addressable switch/LCD", de->d_name); + } else if (strncmp(de->d_name, (char *)"3a", 2) == 0) { + srv_send((char *)"%-15s 2413 dual channel addressable switchs", de->d_name); + } else { + srv_send((char *)"%-15s unknown device", de->d_name); } } - srv_send((char *)"."); + } + srv_send((char *)"."); closedir(fd); } else { srv_send((char *)"503 directory /sys/bus/w1/devices: %s", strerror(errno)); } } else if (strcmp(opt, (char *)"PROFILES") == 0) { - + /* + * User profiles directory + */ + if (getenv((char *)"USER") == NULL) { + mypath = xstrcpy((char *)"/root"); + } else { + mypath = xstrcpy(getenv((char *)"HOME")); + } + mypath = xstrcat(mypath, (char *)"/.thermferm/profiles/"); + mkdirs(mypath, 0755); + if ((fd = opendir(mypath))) { + srv_send((char *)"212 profiles:"); + while ((de = readdir(fd))) { + if (de->d_name[0] != '.') { + srv_send((char *)"%s", de->d_name); + } + } + srv_send((char *)"."); + closedir(fd); + } else { + srv_send((char *)"503 directory %s: %s", mypath, strerror(errno)); + } + free(mypath); + mypath = NULL; } else if (strcmp(opt, (char *)"UNIT") == 0) { + /* + * List configured and selected fermenter unit + */ if (current_unit == -1) { srv_send((char *)"401 No fermenter unit selected"); } else { diff -r c3cc6e44d1a4 -r 3d7a241329e2 thermferm/thermferm.c --- a/thermferm/thermferm.c Tue Jul 01 21:53:03 2014 +0200 +++ b/thermferm/thermferm.c Tue Jul 01 23:14:57 2014 +0200 @@ -125,7 +125,7 @@ if (debug) fprintf(stdout, "mbsePi-apps thermferm v%s starting\n", VERSION); - if (rdconfig((char *)"thermferm.xml")) { + if (rdconfig()) { fprintf(stderr, "Error reading configuration\n"); syslog(LOG_NOTICE, "halted"); return 1; @@ -326,7 +326,7 @@ stopLCD(); #endif - wrconfig((char *)"thermferm.xml"); + wrconfig(); ulockprog((char *)"thermferm"); diff -r c3cc6e44d1a4 -r 3d7a241329e2 thermferm/thermferm.h --- a/thermferm/thermferm.h Tue Jul 01 21:53:03 2014 +0200 +++ b/thermferm/thermferm.h Tue Jul 01 23:14:57 2014 +0200 @@ -144,8 +144,8 @@ void killconfig(void); -int rdconfig(char *); -int wrconfig(char *); +int rdconfig(void); +int wrconfig(void); /* lock.c */ @@ -159,6 +159,10 @@ char *xstrcat(char *, char *); +/* futil.c */ +int mkdirs(char *, mode_t); + + #ifdef HAVE_WIRINGPI_H /* lcd-pcf8574.c */