autostart.c (1826B)
1 void 2 runautostart(void) 3 { 4 char *pathpfx; 5 char *path; 6 char *xdgdatahome; 7 char *home; 8 struct stat sb; 9 10 if ((home = getenv("HOME")) == NULL) 11 /* this is almost impossible */ 12 return; 13 14 /* if $XDG_DATA_HOME is set and not empty, use $XDG_DATA_HOME/dwm, 15 * otherwise use ~/.local/share/dwm as autostart script directory 16 */ 17 xdgdatahome = getenv("XDG_DATA_HOME"); 18 if (xdgdatahome != NULL && *xdgdatahome != '\0') { 19 /* space for path segments, separators and nul */ 20 pathpfx = ecalloc(1, strlen(xdgdatahome) + strlen(dwmdir) + 2); 21 22 if (sprintf(pathpfx, "%s/%s", xdgdatahome, dwmdir) <= 0) { 23 free(pathpfx); 24 return; 25 } 26 } else { 27 /* space for path segments, separators and nul */ 28 pathpfx = ecalloc(1, strlen(home) + strlen(localshare) 29 + strlen(dwmdir) + 3); 30 31 if (sprintf(pathpfx, "%s/%s/%s", home, localshare, dwmdir) < 0) { 32 free(pathpfx); 33 return; 34 } 35 } 36 37 /* check if the autostart script directory exists */ 38 if (! (stat(pathpfx, &sb) == 0 && S_ISDIR(sb.st_mode))) { 39 /* the XDG conformant path does not exist or is no directory 40 * so we try ~/.dwm instead 41 */ 42 char *pathpfx_new = realloc(pathpfx, strlen(home) + strlen(dwmdir) + 3); 43 if(pathpfx_new == NULL) { 44 free(pathpfx); 45 return; 46 } 47 pathpfx = pathpfx_new; 48 49 if (sprintf(pathpfx, "%s/.%s", home, dwmdir) <= 0) { 50 free(pathpfx); 51 return; 52 } 53 } 54 55 /* try the blocking script first */ 56 path = ecalloc(1, strlen(pathpfx) + strlen(autostartblocksh) + 2); 57 if (sprintf(path, "%s/%s", pathpfx, autostartblocksh) <= 0) { 58 free(path); 59 free(pathpfx); 60 } 61 62 if (access(path, X_OK) == 0) 63 system(path); 64 65 /* now the non-blocking script */ 66 if (sprintf(path, "%s/%s", pathpfx, autostartsh) <= 0) { 67 free(path); 68 free(pathpfx); 69 } 70 71 if (access(path, X_OK) == 0) 72 system(strcat(path, " &")); 73 74 free(pathpfx); 75 free(path); 76 }