grub2: bring back build of aros-side grub2 tools
[AROS.git] / tools / collect-aros / misc.c
blob335df27168254434828f202878a20982c2f7d73f
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <sys/param.h>
11 #include <string.h>
13 #include "misc.h"
15 #ifdef _WIN32
16 #define PATH_SEPARATOR ';'
18 /* If we're running on MinGW, PATH is in native Windows form while
19 COMPILER_PATH has ';' as entries separator but still has '/' as directory
20 separator, so we have to convert it. This is what this magic for. */
22 void copy_path(char *to, char *from)
24 do {
25 if (*from == '/')
26 *to = '\\';
27 else
28 *to = *from;
29 to++;
30 } while (*from++);
32 #else
33 #define PATH_SEPARATOR ':'
34 #define copy_path strcpy
35 #endif
37 char *program_name;
38 void nonfatal(const char *msg, const char *errorstr)
40 if (msg != NULL)
41 fprintf(stderr, "%s: %s: %s\n" , program_name, msg, errorstr);
42 else
43 fprintf(stderr, "%s: %s\n" , program_name, errorstr);
46 void fatal(const char *msg, const char *errorstr)
48 nonfatal(msg, errorstr);
49 exit(EXIT_FAILURE);
52 void set_compiler_path(void)
54 static int path_set = 0;
56 if (!path_set)
58 char *compiler_path = getenv("COMPILER_PATH");
59 char *path = getenv("PATH");
61 if (compiler_path && path)
63 char *new_path;
64 size_t compiler_path_len = strlen(compiler_path);
65 size_t path_len = strlen(path);
67 new_path = malloc(5 + compiler_path_len + 1 + path_len + 1);
68 if (new_path)
70 strcpy(new_path, "PATH=");
71 copy_path(new_path + 5, compiler_path);
72 new_path[5 + compiler_path_len] = PATH_SEPARATOR;
73 strcpy(new_path + 5 + compiler_path_len + 1, path);
75 if (putenv(new_path) == 0)
76 path_set = 1;
82 #ifndef _HAVE_LIBIBERTY_
84 void *xmalloc(size_t size)
86 void *ret = malloc(size);
87 if (ret == NULL)
89 fatal("xmalloc", strerror(errno));
92 return ret;
95 char *make_temp_file(char *suffix __attribute__((unused)))
97 int fd;
98 /* If you're unlucky enough to not have libiberty available, you'll have
99 to live with temporary files in /tmp and no suffix; it's ok for our own
100 purposes, */
101 char template[] = "/tmp/catmpXXXXXX";
103 fd = mkstemp(template);
104 if (fd == -1)
105 return NULL;
107 if (close(fd) != 0)
108 fatal("make_temp_file()/close()", strerror(errno));
110 return strdup(template);
113 #endif