brewco/rdconfig.c

changeset 464
4a624c071ca9
parent 455
f84501d8dd87
child 465
3aac3276689d
equal deleted inserted replaced
463:a1da58215b65 464:4a624c071ca9
25 #include "util.h" 25 #include "util.h"
26 #include "xutil.h" 26 #include "xutil.h"
27 27
28 int debug = TRUE; 28 int debug = TRUE;
29 sys_config Config; /* System configuration */ 29 sys_config Config; /* System configuration */
30 extern char *etcpath;
31
30 32
31 #define MY_ENCODING "utf-8" 33 #define MY_ENCODING "utf-8"
32 34
33 const char TEMPSTATE[3][8] = { "OK", "MISSING", "ERROR" }; 35 const char TEMPSTATE[3][8] = { "OK", "MISSING", "ERROR" };
34 const char DEVTYPE[6][5] = { "NA", "W1", "GPIO", "I2C", "SPI", "SIM" }; 36 const char DEVTYPE[6][5] = { "NA", "W1", "GPIO", "I2C", "SPI", "SIM" };
602 xmlFreeTextWriter(writer); 604 xmlFreeTextWriter(writer);
603 605
604 /* 606 /*
605 * Now write the XML configuration 607 * Now write the XML configuration
606 */ 608 */
607 if (getenv((char *)"USER") == NULL) { 609 mypath = xstrcpy(etcpath);
608 mypath = xstrcpy((char *)"/root");
609 } else {
610 mypath = xstrcpy(getenv((char *)"HOME"));
611 }
612 mypath = xstrcat(mypath, (char *)"/.brewco/etc/");
613 mkdirs(mypath, 0755);
614 mypath = xstrcat(mypath, (char *)"brewco.xml"); 610 mypath = xstrcat(mypath, (char *)"brewco.xml");
615 611
616 if (debug) 612 if (debug)
617 fprintf(stdout, "Writing %s\n", mypath); 613 fprintf(stdout, "Writing %s\n", mypath);
618 614
1269 1265
1270 1266
1271 1267
1272 int rdconfig(void) 1268 int rdconfig(void)
1273 { 1269 {
1274 int ival, rc = 0; 1270 int i, ival, rc = 0;
1275 char *mypath; 1271 char *mypath, *fpath, *tpath;
1276 xmlDocPtr doc; 1272 xmlDocPtr doc;
1277 xmlNodePtr cur; 1273 xmlNodePtr cur;
1278 xmlChar *key; 1274 xmlChar *key;
1279 1275
1280 killconfig(); 1276 killconfig();
1281 syslog(LOG_NOTICE, "HOME='%s' USER='%s' LOGNAME='%s'", MBSE_SS(getenv((char *)"HOME")), MBSE_SS(getenv((char *)"USER")), MBSE_SS(getenv((char *)"LOGNAME")));
1282 1277
1283 /* 1278 /*
1284 * Search config file 1279 * Search config file
1285 */ 1280 */
1286 if (getenv((char *)"USER") == NULL) { 1281 mypath = xstrcpy(etcpath);
1287 mypath = xstrcpy((char *)"/root");
1288 } else {
1289 mypath = xstrcpy(getenv((char *)"HOME"));
1290 }
1291 mypath = xstrcat(mypath, (char *)"/.brewco/etc/");
1292 mkdirs(mypath, 0755);
1293 mypath = xstrcat(mypath, (char *)"brewco.xml"); 1282 mypath = xstrcat(mypath, (char *)"brewco.xml");
1283
1284 /*
1285 * Make upto 10 backups of the configuration.
1286 */
1287 fpath = calloc(1024, sizeof(char));
1288 tpath = calloc(1024, sizeof(char));
1289
1290 for (i = 9; i > 0; i--) {
1291 snprintf(tpath, 1023, "%s.%d", mypath, i+1);
1292 snprintf(fpath, 1023, "%s.%d", mypath, i);
1293 if (file_exist(fpath, R_OK) == 0) {
1294 unlink(tpath);
1295 if (file_cp(fpath, tpath)) {
1296 syslog(LOG_NOTICE, "rdconfig file_cp(%s, %s): %s", fpath, tpath, strerror(errno));
1297 }
1298 }
1299 }
1300 snprintf(tpath, 1023, "%s.1", mypath);
1301 snprintf(fpath, 1023, "%s", mypath);
1302 if (file_exist(fpath, R_OK) == 0) {
1303 unlink(tpath);
1304 if (file_cp(fpath, tpath)) {
1305 syslog(LOG_NOTICE, "rdconfig file_cp(%s, %s): %s", fpath, tpath, strerror(errno));
1306 }
1307 }
1308 free(fpath);
1309 free(tpath);
1310
1294 if ((doc = xmlParseFile(mypath)) == NULL) { 1311 if ((doc = xmlParseFile(mypath)) == NULL) {
1295 /* 1312 /*
1296 * No config file, create a fresh one 1313 * No config file, create a fresh one
1297 */ 1314 */
1298 syslog(LOG_NOTICE, "rdconfig: %s not found, creating", mypath); 1315 syslog(LOG_NOTICE, "rdconfig: %s not found, creating", mypath);

mercurial