Fix for a segfault when calling cybergfx.library/ExtractColor()
[cake.git] / tools / collect-aros / misc.c
blob65de9f0f5ad3ee8d3ab22eadd854228cdd145841
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <sys/param.h>
6 #include <string.h>
8 #include "misc.h"
10 char *program_name;
11 void nonfatal(const char *msg, const char *errorstr)
13 if (msg != NULL)
14 fprintf(stderr, "%s: %s: %s\n" , program_name, msg, errorstr);
15 else
16 fprintf(stderr, "%s: %s\n" , program_name, errorstr);
19 void fatal(const char *msg, const char *errorstr)
21 nonfatal(msg, errorstr);
22 exit(EXIT_FAILURE);
25 void set_compiler_path(void)
27 static int path_set = 0;
29 if (!path_set)
31 char *compiler_path = getenv("COMPILER_PATH");
32 char *path = getenv("PATH");
34 if (compiler_path && path)
36 char *new_path;
37 size_t compiler_path_len = strlen(compiler_path);
38 size_t path_len = strlen(path);
40 new_path = malloc(5 + compiler_path_len + 1 + path_len + 1);
41 if (new_path)
43 memcpy(new_path, "PATH=", 5);
44 memcpy(new_path + 5, compiler_path, compiler_path_len);
45 memcpy(new_path + 5 + compiler_path_len, ":", 1);
46 memcpy(new_path + 5 + compiler_path_len + 1, path, path_len + 1);
48 if (putenv(new_path) == 0)
49 path_set = 1;
55 #ifndef _HAVE_LIBIBERTY_
57 void *xmalloc(size_t size)
59 void *ret = malloc(size);
60 if (ret == NULL)
62 fatal("xmalloc", strerror(errno));
65 return ret;
68 char *make_temp_file(char *suffix __attribute__((unused)))
70 int fd;
71 /* If you're unlucky enough to not have libiberty available, you'll have
72 to live with temporary files in /tmp and no suffix; it's ok for our own
73 purposes, */
74 char template[] = "/tmp/catmpXXXXXX";
76 fd = mkstemp(template);
77 if (fd == -1)
78 return NULL;
80 if (close(fd) != 0)
81 fatal("make_temp_file()/close()", strerror(errno));
83 return strdup(template);
86 #endif