nissy-nx

A Rubik's cube optimal solver
git clone https://git.tronto.net/nissy-nx
Download | Log | Files | Refs | README | LICENSE

env.c (1236B)


      1 #define ENV_C
      2 
      3 #include "env.h"
      4 
      5 bool initialized_env = false;
      6 char *tabledir;
      7 
      8 void
      9 mymkdir(char *d, int m)
     10 {
     11 #ifdef _WIN32
     12 	mkdir(d);
     13 #else
     14 	mkdir(d, m);
     15 #endif
     16 }
     17 
     18 void
     19 init_env()
     20 {
     21 	char *nissydata = getenv("NISSYDATA");
     22 	char *localdata = getenv("XDG_DATA_HOME");
     23 	char *home      = getenv("HOME");
     24 	bool read, write;
     25 
     26 	if (initialized_env)
     27 		return;
     28 
     29 	if (nissydata != NULL) {
     30 		tabledir = malloc((strlen(nissydata) + 20) * sizeof(char));
     31 		strcpy(tabledir, nissydata);
     32 	} else if (localdata != NULL) {
     33 		tabledir = malloc((strlen(localdata) + 20) * sizeof(char));
     34 		strcpy(tabledir, localdata);
     35 		strcat(tabledir, "/nissy");
     36 	} else if (home != NULL) {
     37 		tabledir = malloc((strlen(home) + 20) * sizeof(char));
     38 		strcpy(tabledir, home);
     39 		strcat(tabledir, "/.nissy");
     40 	} else {
     41 		tabledir = malloc(20 * sizeof(char));
     42 		strcpy(tabledir, ".");
     43 	}
     44 
     45 	mymkdir(tabledir, 0777);
     46 	strcat(tabledir, "/tables");
     47 	mymkdir(tabledir, 0777);
     48 
     49 	read  = !access(tabledir, R_OK);
     50 	write = !access(tabledir, W_OK);
     51 
     52 	if (!read) {
     53 		fprintf(stderr, "Table files cannot be read.\n");
     54 	} else if (!write) {
     55 		fprintf(stderr, "Data directory not writable: ");
     56 		fprintf(stderr, "tables can be loaded, but not saved.\n");
     57 	}
     58 
     59 	initialized_env = true;
     60 }