2 * Copyright 2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and
30 * documentation are those of the authors and should not be
31 * interpreted as representing official policies, either expressed
32 * or implied, of GRNET S.A.
39 * Some commonly used functions
56 #include <sys/types.h>
60 static char *perr_prog_name = NULL;
63 * perr_func: Main error reporting function
65 void perr_func(enum perr_type type, int want_errno, char *fmt, ...)
69 int err_number = errno; /* We need errno NOW */
70 char buf[PERR_BUF_SIZE], errbuf[PERR_BUF_SIZE], timebuf[PERR_BUF_SIZE];
71 char *t = NULL, *p = buf;
75 case PFE: t = "Fatal error"; break;
76 case PE: t = "Error"; break;
77 case PW: t = "Warning"; break;
78 case PI: t = "Info"; break;
79 case PD: t = "Debug"; break;
80 default: raise(SIGABRT);
82 if (!perr_prog_name) {
83 perr_prog_name = "Internal perr error";
84 perr(1, 0, "init_perr has not been called");
88 ctime_r(&timeval, timebuf);
89 *strchr(timebuf, '\n') = '\0';
91 p += sprintf(p, "%s: %s: ", perr_prog_name, t);
92 p += sprintf(p, "Thread %lu, PID %lu\n\t",
93 (unsigned long)pthread_self(), (unsigned long)getpid());
95 p += sprintf(p, "%s (%ld):\n\t", timebuf, timeval);
97 p += vsprintf(p, fmt, ap);
98 p += sprintf(p, "\n");
101 /* Print last error returned from system call */
102 p += sprintf(p, "\tErrno was: %d - %s\n",
103 err_number, strerror_r(err_number, errbuf, PERR_BUF_SIZE));
106 * Output the buffer to stderr with a single call to fprintf,
107 * which is thread-safe and locks the stderr semaphore
109 fprintf(stderr, "%s", buf);
117 void init_perr(char *prog_name)
119 perr_prog_name = prog_name;
123 * Adapted from FreeBSD source:
125 * Appends src to string dst of size siz (unlike strncat, siz is the
126 * full size of dst, not space left). At most siz-1 characters
127 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
128 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
129 * If retval >= siz, truncation occurred.
131 size_t strlcat(char *dst, const char *src, size_t siz)
138 /* Find the end of dst and adjust bytes left but don't go past end */
139 while (n-- != 0 && *d != '\0')
145 return(dlen + strlen(s));
155 return (dlen + (s - src)); /* count does not include NUL */
159 * Adapted from FreeBSD source:
161 * Copy src to string dst of size siz. At most siz-1 characters
162 * will be copied. Always NUL terminates (unless siz == 0).
163 * Returns strlen(src); if retval >= siz, truncation occurred.
165 size_t strlcpy(char *dst, const char *src, size_t siz)
171 /* Copy as many bytes as will fit */
172 if (n != 0 && --n != 0) {
174 if ((*d++ = *s++) == 0)
179 /* Not enough room in dst, add NUL and traverse rest of src */
182 *d = '\0'; /* NUL-terminate dst */
187 return (s - src - 1); /* count does not include NUL */