From: Trent Huber Date: Sat, 19 Apr 2025 02:47:03 +0000 (-0400) Subject: Add ability to clean build, more refactoring X-Git-Url: https://trenthuber.com/code?a=commitdiff_plain;h=bfe55b3106a5b67084521dfe4b39ad9c5468a932;p=xpmview.git Add ability to clean build, more refactoring --- diff --git a/.gitignore b/.gitignore index c685519..c38734d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *build +*clean *.o diff --git a/build.c b/build.c index fc92b2b..9275936 100644 --- a/build.c +++ b/build.c @@ -1,3 +1,6 @@ +#define ROOT +#include "build.h" + #include "external/cbs/cbs.c" int main(void) { @@ -6,5 +9,10 @@ int main(void) { build("external/"); build("src/"); + cflags = (char *[]){CFCBS, NULL}; + compile("clean", CBS, NULL); + + load('x', "clean", "clean", NULL); + return 0; } diff --git a/build.h b/build.h index 4e79208..dfcf343 100644 --- a/build.h +++ b/build.h @@ -1,3 +1,6 @@ +/* Switch the following #define to change whether or not raylib is built as a + * static or dynamic library; be sure to `./clean' before you `./build' again. + */ // #define RLDYNAMIC #ifdef RLDYNAMIC @@ -6,23 +9,34 @@ #define RLEXT ".a" #endif -#define SRC ROOT "src/" - +#define CBSPATH ROOT "external/cbs/" +#define CBS CBSPATH "cbs.c" #define RLROOT ROOT "external/raylib/" +#define RLLIB RLROOT "raylib" RLEXT #define RLSRC RLROOT "src/" #define RLHDR RLSRC "raylib" -#define RLLIB RLROOT "raylib" RLEXT +#define SIMPLEXPM ROOT "bin/simplexpm" +#define SRC ROOT "src/" +#define UTILS SRC "utilities" +#define CFCBS "-I" CBSPATH #define CFRAYLIB "-I" RLSRC #define CFSRC "-I" SRC #ifdef __APPLE__ -#define LFRAYLIB \ +#define RLLFLAGS \ "-framework", "Cocoa", \ "-framework", "CoreVideo", \ "-framework", "GLUT", \ "-framework", "IOKit", \ "-framework", "OpenGL" #else -#define LFRAYLIB "-lm" +#define RLLFLAGS "-lm" +#endif +#ifdef RLDYNAMIC +#define LFEXTERNAL RLLFLAGS +#define LFRAYLIB NULL +#else +#define LFEXTERNAL NULL +#define LFRAYLIB RLLFLAGS #endif diff --git a/clean.c b/clean.c new file mode 100644 index 0000000..cddf955 --- /dev/null +++ b/clean.c @@ -0,0 +1,48 @@ +#define ROOT +#include "build.h" + +#define COLORS SRC "colors.c" +#define FIND "/usr/bin/find" +#define FONT SRC "font.c" +#define GENCOLORS SRC "colors/gencolors" +#define GENFONT SRC "font/genfont" +#define RM "/bin/rm" + +#include "external/cbs/cbs.c" + +int main(void) { + char *what, **whos, **regexes, ***rms; + size_t i; + pid_t cpid; + + what = "removal"; + + // Remove build executables and object files + whos = (char *[2]){"{*/*build,*/*/*build}", "{*/*.o,*/*/*.o,*/*/*/*.o}"}; + regexes = (char *[2]){".*[^\\.]/build$", ".*\\.o$"}; + for (i = 0; i < 2; ++i) { + if ((cpid = fork()) == 0) + run(FIND, (char *[]){"find", ".", "-regex", regexes[i], + "-exec", "rm", "{}", "+", NULL}, what, whos[i]); + await(cpid, what, whos[i]); + } + + /* Remove raylib library, application executables, + * automatically generated source files, and ourself + */ + whos = (char *[4]){extend(RLLIB, RLEXT), + "{" GENCOLORS "," GENFONT "," SIMPLEXPM "}", + "{" COLORS "," FONT "}", "clean"}; + rms = (char **[4]){(char *[3]){whos[0]}, + (char *[3]){GENCOLORS, GENFONT, SIMPLEXPM}, + (char *[3]){COLORS, FONT}, (char *[3]){whos[3]}}; + for (i = 0; i < 4; ++i) { + if ((cpid = fork()) == 0) + run(RM, (char *[]){"rm", rms[i][0], rms[i][1], rms[i][2], NULL}, + what, whos[i]); + await(cpid, what, whos[i]); + } + free(whos[0]); + + return EXIT_SUCCESS; +} diff --git a/external/build.c b/external/build.c index 5f44c4e..f8e5d9e 100644 --- a/external/build.c +++ b/external/build.c @@ -7,22 +7,22 @@ RLSRC "rtextures", RLSRC "utils", \ RLSRC "rcore", RLSRC "rglfw" -#ifdef RLDYNAMIC -#define CFRLGLOBAL "-DPLATFORM_DESKTOP", "-fPIC" +#ifdef __APPLE__ +#define CFGRAPHICS "-x", "objective-c" #else -#define CFRLGLOBAL "-DPLATFORM_DESKTOP" +#define CFGRAPHICS "-D_GLFW_X11" #endif -#define CFRLGLFW "-I" RLSRC "external/glfw/include" -#ifdef __APPLE__ -#define CFRLX "-x", "objective-c" +#define CFGLFW "-I" RLSRC "external/glfw/include" +#ifdef RLDYNAMIC +#define CFGLOBALS "-DPLATFORM_DESKTOP", "-fPIC" #else -#define CFRLX "-D_GLFW_X11" +#define CFGLOBALS "-DPLATFORM_DESKTOP" #endif #ifdef RLDYNAMIC -#define RLTYPE 'd' +#define LIBTYPE 'd' #else -#define RLTYPE 's' +#define LIBTYPE 's' #endif #include "cbs/cbs.c" @@ -34,17 +34,15 @@ int main(void) { build(NULL); src = (char *[]){RLSRCS, NULL}; - cflags = (char *[]){CFRLGLOBAL, NULL}; + cflags = (char *[]){CFGLOBALS, NULL}; for (i = 0; i < 6; ++i) compile(src[i], NULL); - cflags = (char *[]){CFRLGLOBAL, CFRLGLFW, NULL}; + cflags = (char *[]){CFGLFW, CFGLOBALS, NULL}; compile(src[6], NULL); - cflags = (char *[]){CFRLGLOBAL, CFRLGLFW, CFRLX, NULL}; + cflags = (char *[]){CFGRAPHICS, CFGLFW, CFGLOBALS, NULL}; compile(src[7], NULL); -#ifdef RLDYNAMIC - lflags = (char *[]){LFRAYLIB, NULL}; -#endif - load(RLTYPE, RLLIB, RLSRCS, NULL); + lflags = (char *[]){LFEXTERNAL, NULL}; + load(LIBTYPE, RLLIB, RLSRCS, NULL); - return 0; + return EXIT_SUCCESS; } diff --git a/external/cbs b/external/cbs index 3f59e2f..a7ad152 160000 --- a/external/cbs +++ b/external/cbs @@ -1 +1 @@ -Subproject commit 3f59e2fa436ef5d7e527fd39e52a050536669db7 +Subproject commit a7ad152e9248d770b63684c5e019348457100a1e diff --git a/src/build.c b/src/build.c index c199a03..d828156 100644 --- a/src/build.c +++ b/src/build.c @@ -1,32 +1,25 @@ #define ROOT "../" #include "../build.h" -#define BIN ROOT "bin/" -#define CBS ROOT "external/cbs/" - -#define CFCBS "-I" CBS - #include "../external/cbs/cbs.c" int main(void) { build(NULL); - compile("error", NULL); + compile("utilities", NULL); build("colors/"); build("font/"); cflags = (char *[]){CFRAYLIB, NULL}; - compile("main", "error", RLHDR, "xpm", "font.c", NULL); + compile("main", "utilities", "options", RLHDR, "xpm", "font.c", NULL); cflags = NULL; - compile("options", "error", NULL); + compile("options", "utilities", NULL); cflags = (char *[]){CFRAYLIB, CFCBS, NULL}; - compile("xpm", "error", RLHDR, "xpm", CBS "cbs.c", "colors.c", NULL); + compile("xpm", "utilities", RLHDR, "xpm", CBS, "colors.c", NULL); -#ifndef RLDYNAMIC lflags = (char *[]){LFRAYLIB, NULL}; -#endif - load('x', BIN "simplexpm", "error", "main", "options", "xpm", RLLIB, NULL); + load('x', SIMPLEXPM, "utilities", "main", "options", "xpm", RLLIB, NULL); - return 0; + return EXIT_SUCCESS; } diff --git a/src/colors/build.c b/src/colors/build.c index d6fcbfb..7e83836 100644 --- a/src/colors/build.c +++ b/src/colors/build.c @@ -1,18 +1,20 @@ #define ROOT "../../" #include "../../build.h" +#define COLORS SRC "colors.c" + #include "../../external/cbs/cbs.c" int main(void) { build(NULL); cflags = (char *[]){CFSRC, NULL}; - compile("gencolors", SRC "error", NULL); - load('x', "gencolors", SRC "error", "gencolors", NULL); + compile("gencolors", UTILS, NULL); + load('x', "gencolors", "gencolors", UTILS, NULL); - if (modified(SRC "colors.c", "gencolors.c") - || modified(SRC "colors.c", "rgb.txt")) - run("gencolors", (char *[]){"./gencolors", NULL}, "execution", "gencolors"); + if (modified(COLORS, "gencolors.c") || modified(COLORS, "rgb.txt")) + run("gencolors", (char *[]){"./gencolors", "rgb.txt", COLORS, NULL}, + "execution", "gencolors"); - return 0; + return EXIT_SUCCESS; } diff --git a/src/colors/gencolors.c b/src/colors/gencolors.c index f9d0ea1..b5dd82c 100644 --- a/src/colors/gencolors.c +++ b/src/colors/gencolors.c @@ -7,70 +7,74 @@ #include #include -#include "error.h" +#include "utilities.h" -int main(void) { - int result, infd, outfd, i; +int main(int argc, char **argv) { + int result, infd, outfd; struct stat instat; - size_t len; + size_t l, i; void *map; char *p; - long r, g, b; + unsigned char r, g, b; - result = 1; - if ((infd = open("rgb.txt", O_RDONLY)) == -1) { - xpmerror("Unable to open `rgb.txt' for reading"); + result = EXIT_FAILURE; + if (argc != 3) { + xpmerror("Incorrect number of arguments: %s ", argv[0]); + return result; + } + if ((infd = open(argv[1], O_RDONLY)) == -1) { + xpmerror("Unable to open `%s' for reading", argv[1]); return result; } if (fstat(infd, &instat) == -1) { - xpmerror("Unable to stat `rgb.txt'"); + xpmerror("Unable to stat `%s'", argv[1]); goto closein; } - len = instat.st_size; - if ((p = map = mmap(NULL, len, PROT_READ | PROT_WRITE, + l = instat.st_size; + if ((p = map = mmap(NULL, l, PROT_READ | PROT_WRITE, MAP_PRIVATE, infd, 0)) == MAP_FAILED) { - xpmerror("Unable to map `rgb.txt' to memory"); + xpmerror("Unable to map `%s' to memory", argv[1]); goto closein; } - if ((outfd = open("../colors.c", O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) { - xpmerror("Unable to open `../colors.c' for writing"); + if ((outfd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) { + xpmerror("Unable to open `%s' for writing", argv[2]); goto munmap; } - result = 0; + result = EXIT_SUCCESS; if (dprintf(outfd, "struct color {\n" "\tchar *name;\n" "\tunsigned int value;\n" "};\n\n" "static struct color colors[] = {\n" "\t{\"None\", 0x00ffffff},\n") == -1) { - xpmerror("Unable to write to `../colors.c'"); + xpmerror("Unable to write to `%s'", argv[2]); goto closeout; } - for (i = 1; p < (char *)map + len; ++i) { + for (i = 1; p < (char *)map + l; ++i) { r = strtol(p, &p, 10); g = strtol(p, &p, 10); b = strtol(p, &p, 10); - if (dprintf(outfd, "\t{\"%s\", 0x%02lx%02lx%02lx},\n", + if (dprintf(outfd, "\t{\"%s\", 0x%02x%02x%02x},\n", strsep(&p, "\n") + 2, r, g, b) == -1) { - xpmerror("Unable to write to `../colors.c'"); + xpmerror("Unable to write to `%s'", argv[2]); goto closeout; } } - if (dprintf(outfd, "};\n\nstatic size_t numcolors = %d;\n", i) == -1) - xpmerror("Unable to write to `../colors.c'"); + if (dprintf(outfd, "};\n\nstatic size_t numcolors = %zu;\n", i) == -1) + xpmerror("Unable to write to `%s'", argv[2]); closeout: if (close(outfd) == -1) - xpmerror("Unable to close `../colors.c'"); + xpmerror("Unable to close `%s'", argv[2]); munmap: - if (munmap(map, len) == -1) - xpmerror("Unable to unmap memory associated with `rgb.txt'"); + if (munmap(map, l) == -1) + xpmerror("Unable to unmap memory associated with `%s'", argv[1]); closein: if (close(infd) == -1) - xpmerror("Unable to close `rgb.txt'"); + xpmerror("Unable to close `%s'", argv[1]); return result; } diff --git a/src/font/build.c b/src/font/build.c index 87f5e39..989db8b 100644 --- a/src/font/build.c +++ b/src/font/build.c @@ -1,21 +1,22 @@ #define ROOT "../../" #include "../../build.h" +#define FONT SRC "font.c" + #include "../../external/cbs/cbs.c" int main(void) { build(NULL); cflags = (char *[]){CFRAYLIB, CFSRC, NULL}; - compile("genfont", SRC "error", RLHDR, NULL); + compile("genfont", UTILS, RLHDR, NULL); -#ifndef RLDYNAMIC lflags = (char *[]){LFRAYLIB, NULL}; -#endif - load('x', "genfont", SRC "error", "genfont", RLLIB, NULL); + load('x', "genfont", "genfont", UTILS, RLLIB, NULL); - if (modified(SRC "font.c", "genfont.c") || modified(SRC "font.c", "font.ttf")) - run("genfont", (char *[]){"./genfont", NULL}, "execution", "genfont"); + if (modified(FONT, "genfont.c") || modified(FONT, "font.ttf")) + run("genfont", (char *[]){"./genfont", "font.ttf", FONT, NULL}, + "execution", "genfont"); - return 0; + return EXIT_SUCCESS; } diff --git a/src/font/genfont.c b/src/font/genfont.c index 4fe33c1..764916e 100644 --- a/src/font/genfont.c +++ b/src/font/genfont.c @@ -1,19 +1,23 @@ -#include #include -#include "error.h" #include "raylib.h" +#include "utilities.h" -int main(void) { +int main(int argc, char **argv) { Font font; + if (argc != 3) { + xpmerror("Incorrect number of arguments: %s ", argv[0]); + return EXIT_FAILURE; + } + SetTraceLogLevel(LOG_WARNING); InitWindow(0, 0, ""); - font = LoadFontEx("font.ttf", 48, NULL, 95); - if (!ExportFontAsCode(font, "../font.c")) { - xpmerror("Unable to generate `font.c' from `font.ttf'"); - exit(EXIT_FAILURE); + font = LoadFontEx(argv[1], 48, NULL, 95); + if (!ExportFontAsCode(font, argv[2])) { + xpmerror("Unable to generate `%s' from `%s'", argv[2], argv[1]); + return EXIT_FAILURE; } UnloadFont(font); @@ -22,5 +26,5 @@ int main(void) { * the transient application we open to load the font */ - return 0; + return EXIT_SUCCESS; } diff --git a/src/main.c b/src/main.c index 89d0e93..b036508 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -7,29 +6,73 @@ #include #include -#include "error.h" #include "options.h" #define SUPPORT_IMAGE_EXPORT #include "raylib.h" +#include "utilities.h" #include "xpm.h" extern bool isGpuReady; #include "font.c" +static void getinput(char **xp, Texture2D *tp, Image *ip) { + FilePathList files; + static int mode = DEFAULT; + KeyboardKey key; + size_t len; + + if (IsFileDropped()) { + files = LoadDroppedFiles(); + if (*xp) RL_FREE(*xp); + *xp = xpmalloc(FILENAME_MAX); + strcpy(*xp, files.paths[0]); + UnloadDroppedFiles(files); + *tp = gettexture(*xp, ip, mode); + } else switch ((key = GetKeyPressed())) { + case KEY_R: + if (*xp) *tp = gettexture(*xp, ip, mode); + case KEY_NULL: + break; + case KEY_S: + if (tp->id == 0) break; + len = strlen(*xp); + strncpy(*xp + len - 4, ".png", 4); + ExportImage(*ip, *xp); + strncpy(*xp + len - 4, ".xpm", 4); + break; + default: + switch (key) { + case KEY_M: + mode = MODEM; + break; + case KEY_FOUR: + mode = MODEG4; + break; + case KEY_G: + mode = MODEG; + break; + case KEY_C: + mode = MODEC; + break; + default: + return; + } + if (*xp) *tp = gettexture(NULL, ip, mode); + } +} + int main(int argc, char **argv) { - int debug, mode; + int debug; char *xpm, *welcome, *error; Image image; Texture2D texture; Font font; - FilePathList files; - KeyboardKey key; - size_t len, width, height; + size_t width, height; float scale; Vector2 pos, dim; - if (!options(argc, argv, &debug, &xpm)) return 1; + if (!options(argc, argv, &debug, &xpm)) return EXIT_FAILURE; if (!debug) SetTraceLogLevel(LOG_ERROR); InitWindow(800, 600, "simplexpm"); @@ -38,51 +81,13 @@ int main(int argc, char **argv) { SetExitKey(KEY_Q); image = (Image){0}; - mode = DEFAULT; - texture = gettexture(xpm, &image, mode); - if (xpm && texture.id == 0) return 1; + texture = gettexture(xpm, &image, DEFAULT); + if (xpm && texture.id == 0) return EXIT_FAILURE; font = LoadFont_Font(); welcome = "Drag and drop an XPM file here"; error = "Unable to parse XPM file:\n see console for details"; while (!WindowShouldClose()) { - if (IsFileDropped()) { - files = LoadDroppedFiles(); - if (xpm) RL_FREE(xpm); - xpm = RL_CALLOC(FILENAME_MAX, 1); - TextCopy(xpm, files.paths[0]); - UnloadDroppedFiles(files); - texture = gettexture(xpm, &image, mode); - } else switch ((key = GetKeyPressed())) { - case KEY_R: - if (xpm) texture = gettexture(xpm, &image, mode); - break; - case KEY_S: - if (texture.id == 0) break; - len = strlen(xpm); - strncpy(xpm + len - 4, ".png", 4); - ExportImage(image, xpm); - strncpy(xpm + len - 4, ".xpm", 4); - break; - default: - switch (key) { - case KEY_M: - mode = MODEM; - break; - case KEY_FOUR: - mode = MODEG4; - break; - case KEY_G: - mode = MODEG; - break; - case KEY_C: - mode = MODEC; - break; - default: - continue; - } - if (xpm) texture = gettexture(NULL, &image, mode); - case KEY_NULL:; - } + getinput(&xpm, &texture, &image); BeginDrawing(); @@ -111,10 +116,10 @@ int main(int argc, char **argv) { EndDrawing(); } - if (xpm) RL_FREE(xpm); + if (xpm) free(xpm); UnloadTexture(texture); CloseWindow(); - return 0; + return EXIT_SUCCESS; } diff --git a/src/options.c b/src/options.c index 6c3afb2..62966dc 100644 --- a/src/options.c +++ b/src/options.c @@ -4,7 +4,7 @@ #include #include -#include "error.h" +#include "utilities.h" static void usage(char *prog, int fd) { dprintf(fd, "Usage: %s [-dh] [-f file]\n" @@ -21,25 +21,26 @@ static void usage(char *prog, int fd) { "\t c Color visual mode\n", prog); } -int options(int argc, char **argv, int *debug, char **xpmp) { +int options(int argc, char **argv, int *debug, char **xp) { int opt, result, dnfd; *debug = 0; - *xpmp = NULL; + *xp = NULL; while ((opt = getopt(argc, argv, "df:h")) != -1) switch (opt) { case 'd': *debug = 1; break; case 'f': - *xpmp = strdup(optarg); + *xp = xpmalloc(FILENAME_MAX); + strcpy(*xp, optarg); break; case 'h': usage(argv[0], STDOUT_FILENO); - exit(0); + exit(EXIT_SUCCESS); case '?': default: usage(argv[0], STDERR_FILENO); - exit(1); + exit(EXIT_FAILURE); } result = 1; @@ -53,7 +54,7 @@ int options(int argc, char **argv, int *debug, char **xpmp) { result = 0; } else if (dup2(dnfd, STDOUT_FILENO) == -1) { xpmerror("Unable to redirect stdout to `/dev/null'"); - exit(1); + exit(EXIT_FAILURE); } if (close(dnfd) == -1) { xpmerror("Unable to close `/dev/null'"); diff --git a/src/error.c b/src/utilities.c similarity index 70% rename from src/error.c rename to src/utilities.c index 13e89a6..00d780f 100644 --- a/src/error.c +++ b/src/utilities.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -17,3 +18,12 @@ void xpmerror(char *fmt, ...) { } dprintf(STDERR_FILENO, "\n"); } + +void *xpmalloc(size_t s) { + void *r; + + if ((r = malloc(s))) return memset(r, 0, s); + + xpmerror("Memory allocation"); + exit(EXIT_FAILURE); +} diff --git a/src/error.h b/src/utilities.h similarity index 54% rename from src/error.h rename to src/utilities.h index 47501f3..d73b62c 100644 --- a/src/error.h +++ b/src/utilities.h @@ -1 +1,2 @@ void xpmerror(char *fmt, ...); +void *xpmalloc(size_t s); diff --git a/src/xpm.c b/src/xpm.c index 01f144e..9b83f63 100644 --- a/src/xpm.c +++ b/src/xpm.c @@ -2,10 +2,14 @@ #include #include -#include "error.h" #include "raylib.h" +#include "utilities.h" #include "xpm.h" +#define TMP "/tmp/xpm" +#define TMPSRC TMP ".c" +#define TMPLIB "/tmp/libxpm" DYEXT + #include "cbs.c" #include "colors.c" @@ -21,15 +25,6 @@ static int space(char c) { return c == ' ' || c == '\t'; } -static void *zalloc(size_t s) { - void *r; - - if ((r = calloc(1, s))) return r; - - xpmerror("Memory allocation"); - exit(EXIT_FAILURE); -} - static char *arrname(char *p, size_t l) { size_t step; char *start, *r; @@ -45,7 +40,7 @@ static char *arrname(char *p, size_t l) { start = p; for (; !space(*p) && *p != '['; ++p, --l) if (l == 0) return NULL; l = p - start; - r = zalloc(l + 1); + r = xpmalloc(l + 1); strncpy(r, start, l); return r; @@ -69,9 +64,6 @@ static int key2mode(char **strp) { case 'm': r = MODEM; break; - case 's': - r = SYMBOLIC; - break; case 'g': if (**strp == '4') { ++*strp; @@ -86,6 +78,9 @@ static int key2mode(char **strp) { default: xpmerror("Unknown key `%c'", *(*strp - 1)); r = NUMMODES; + break; + case 's': + r = SYMBOLIC; } while (space(**strp)) ++*strp; @@ -158,8 +153,8 @@ static Image parse(char **data, long *sizep) { } // Colors - chars = zalloc(ncolors * cpp * sizeof*chars); - colors = zalloc(NUMMODES * ncolors * sizeof*colors); + chars = xpmalloc(ncolors * cpp * sizeof*chars); + colors = xpmalloc(NUMMODES * ncolors * sizeof*colors); for (i = 0; i < ncolors; ++i) { p = data[1 + i]; strncpy(chars + i * cpp, p, cpp); @@ -179,7 +174,7 @@ static Image parse(char **data, long *sizep) { } // Pixels - pixels = zalloc(NUMMODES * height * width * sizeof*pixels); + pixels = xpmalloc(NUMMODES * height * width * sizeof*pixels); j = width; l = 0; for (i = 0, pp = &data[1 + ncolors]; @@ -217,7 +212,7 @@ static Image process(char *xpm) { int xpmfd, srcfd, e, cpid, status; struct stat xstat; size_t l, offset; - char *map, *p, *a, **data; + char *map, *p, *a, *tmp, **data; void *d; long *sizep; @@ -247,52 +242,49 @@ static Image process(char *xpm) { goto munmap; } - if ((srcfd = open("/tmp/xpm.c", O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) { - xpmerror("Unable to open `/tmp/xpm.c'"); + if ((srcfd = open(TMPSRC, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) { + xpmerror("Unable to open `" TMPSRC "'"); goto munmap; } e = !writeall(srcfd, p, l - offset) || dprintf(srcfd, "\n\nlong size = sizeof %s / sizeof*%s;\n", a, a) < 0; if (close(srcfd) == -1) { - xpmerror("Unable to close `/tmp/xpm.c'"); + xpmerror("Unable to close `" TMPSRC "'"); goto munmap; } if (e) { - xpmerror("Unable to write to `/tmp/xpm.c'"); + xpmerror("Unable to write to `" TMPSRC "'"); goto munmap; } if ((cpid = fork()) == 0) { - compile("/tmp/xpm", NULL); - load('d', "/tmp/xpm", "/tmp/xpm", NULL); + compile(TMP, NULL); + load('d', TMP, TMP, NULL); exit(EXIT_SUCCESS); } if (cpid == -1 || waitpid(cpid, &status, 0) == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS) { - xpmerror("Unable to create `/tmp/libxpm" DYEXT "'"); + xpmerror("Unable to create `" TMPLIB "'"); goto munmap; } - if ((d = dlopen("/tmp/libxpm" DYEXT, RTLD_LAZY)) == NULL) { - xpmerror("Unable to load `/tmp/libxpm" DYEXT "': %s", dlerror()); + if ((d = dlopen(TMPLIB, RTLD_LAZY)) == NULL) { + xpmerror("Unable to load `" TMPLIB "': %s", dlerror()); goto munmap; } if ((data = (char **)dlsym(d, a)) == NULL) { - xpmerror("Unable to load image data from `/tmp/libxpm" DYEXT "': `%s'", - dlerror()); + xpmerror("Unable to load image data from `" TMPLIB "': `%s'", dlerror()); goto dlclose; } if ((sizep = (long *)dlsym(d, "size")) == NULL) { - xpmerror("Unable to load image length from `/tmp/libxpm" DYEXT "': `%s'", - dlerror()); + xpmerror("Unable to load image length from `" TMPLIB "': `%s'", dlerror()); goto dlclose; } result = parse(data, sizep); dlclose: - if (dlclose(d)) - xpmerror("Unable to unload `/tmp/libxpm" DYEXT "': %s", dlerror()); + if (dlclose(d)) xpmerror("Unable to unload `" TMPLIB "': %s", dlerror()); munmap: if (munmap(map, l) == -1) xpmerror("Unable to unmap `%s' from memory", xpm);