Network: Move common utilities to the common area
[AROS.git] / tools / collect-aros / misc.c
blobfb0ff7ab620513143386b960cdfdcef41d2a02c0
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 #ifdef _WIN32
11 #define PATH_SEPARATOR ';'
13 /* If we're running on MinGW, PATH is in native Windows form while
14 COMPILER_PATH has ';' as entries separator but still has '/' as directory
15 separator, so we have to convert it. This is what this magic for. */
17 void copy_path(char *to, char *from)
19 do {
20 if (*from == '/')
21 *to = '\\';
22 else
23 *to = *from;
24 to++;
25 } while (*from++);
27 #else
28 #define PATH_SEPARATOR ':'
29 #define copy_path strcpy
30 #endif
32 char *program_name;
33 void nonfatal(const char *msg, const char *errorstr)
35 if (msg != NULL)
36 fprintf(stderr, "%s: %s: %s\n" , program_name, msg, errorstr);
37 else
38 fprintf(stderr, "%s: %s\n" , program_name, errorstr);
41 void fatal(const char *msg, const char *errorstr)
43 nonfatal(msg, errorstr);
44 exit(EXIT_FAILURE);
47 void set_compiler_path(void)
49 static int path_set = 0;
51 if (!path_set)
53 char *compiler_path = getenv("COMPILER_PATH");
54 char *path = getenv("PATH");
56 if (compiler_path && path)
58 char *new_path;
59 size_t compiler_path_len = strlen(compiler_path);
60 size_t path_len = strlen(path);
62 new_path = malloc(5 + compiler_path_len + 1 + path_len + 1);
63 if (new_path)
65 strcpy(new_path, "PATH=");
66 copy_path(new_path + 5, compiler_path);
67 new_path[5 + compiler_path_len] = PATH_SEPARATOR;
68 strcpy(new_path + 5 + compiler_path_len + 1, path);
70 if (putenv(new_path) == 0)
71 path_set = 1;
77 #ifndef _HAVE_LIBIBERTY_
79 void *xmalloc(size_t size)
81 void *ret = malloc(size);
82 if (ret == NULL)
84 fatal("xmalloc", strerror(errno));
87 return ret;
90 char *make_temp_file(char *suffix __attribute__((unused)))
92 int fd;
93 /* If you're unlucky enough to not have libiberty available, you'll have
94 to live with temporary files in /tmp and no suffix; it's ok for our own
95 purposes, */
96 char template[] = "/tmp/catmpXXXXXX";
98 fd = mkstemp(template);
99 if (fd == -1)
100 return NULL;
102 if (close(fd) != 0)
103 fatal("make_temp_file()/close()", strerror(errno));
105 return strdup(template);
108 #endif