Set up current working directory on File->Open
[gscope.git] / util.c
blob3abc97b44a009527b27e62ef3100fafa66680a3d
1 /*
2 * (c) 2010 Cyrill Gorcunov, gorcunov@gmail.com
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or (at
7 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <limits.h>
28 #include <sys/param.h>
29 #include <sys/types.h>
31 #include "util.h"
33 static void report(const char *prefix, const char *err, va_list params)
35 char msg[1024];
36 vsnprintf(msg, sizeof(msg), err, params);
37 fprintf(stderr, " %s%s\n", prefix, msg);
40 static NORETURN void die_builtin(const char *err, va_list params)
42 report(" Fatal: ", err, params);
43 exit(128);
46 static void error_builtin(const char *err, va_list params)
48 report(" Error: ", err, params);
51 static void warn_builtin(const char *warn, va_list params)
53 report(" Warning: ", warn, params);
56 static void debug_builtin(const char *info, va_list params)
58 report(" Debug: ", info, params);
61 void die(const char *err, ...)
63 va_list params;
65 va_start(params, err);
66 die_builtin(err, params);
67 va_end(params);
70 int error(const char *err, ...)
72 va_list params;
74 va_start(params, err);
75 error_builtin(err, params);
76 va_end(params);
77 return -1;
80 void warning(const char *warn, ...)
82 va_list params;
84 va_start(params, warn);
85 warn_builtin(warn, params);
86 va_end(params);
89 void debug(const char *info, ...)
91 va_list params;
93 va_start(params, info);
94 debug_builtin(info, params);
95 va_end(params);
98 void die_perror(const char *s)
100 perror(s);
101 exit(1);
105 * strlcat - Append a length-limited, %NUL-terminated string to another
106 * @dest: The string to be appended to
107 * @src: The string to append to it
108 * @count: The size of the destination buffer.
110 size_t strlcat(char *dest, const char *src, size_t count)
112 size_t dsize = strlen(dest);
113 size_t len = strlen(src);
114 size_t res = dsize + len;
116 DIE_IF(dsize >= count);
118 dest += dsize;
119 count -= dsize;
120 if (len >= count)
121 len = count - 1;
123 memcpy(dest, src, len);
124 dest[len] = 0;
126 return res;