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