chiark / gitweb /
Do not have Makefile in CVS.
[userv.git] / lib.c
1 /*
2  * userv - lib.c
3  * useful utility routines, used in daemon, but not very dependent on it
4  *
5  * Copyright (C)1996-1997 Ian Jackson
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with userv; if not, write to the Free Software
19  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 #include <errno.h>
23 #include <syslog.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/types.h>
29
30 #include "config.h"
31 #include "common.h"
32 #include "lib.h"
33
34 char *xstrcat3save(const char *a, const char *b, const char *c) {
35   char *r;
36
37   r= xmalloc(strlen(a)+strlen(b)+strlen(c)+1);
38   strcpy(r,a);
39   strcat(r,b);
40   strcat(r,c);
41   return r;
42 }
43
44 char *xstrsave(const char *s) {
45   char *r;
46
47   r= xmalloc(strlen(s)+1);
48   strcpy(r,s);
49   return r;
50 }
51
52 char *xstrsubsave(const char *begin, int len) {
53   char *r;
54   
55   r= xmalloc(len+1);
56   memcpy(r,begin,len);
57   r[len]= 0;
58   return r;
59 }
60
61 void *xmalloc(size_t s) {
62   void *p;
63   p= malloc(s?s:1); if (!p) syscallerror("malloc");
64   return p;
65 }
66
67 void *xrealloc(void *p, size_t s) {
68   p= realloc(p,s); if (!p) syscallerror("realloc");
69   return p;
70 }
71
72 int makeroom(char **buffer, int *size, int needed) {
73   if (needed > MAX_GENERAL_STRING) return -1;
74   if (*size >= needed) return 0;
75   *buffer= xrealloc(*buffer,needed);
76   *size= needed;
77   return 0;
78 }
79
80 void vsnyprintf(char *buffer, size_t size, const char *fmt, va_list al) {
81   vsnprintf(buffer,size,fmt,al);
82   buffer[size-1]= 0;
83 }
84
85 void snyprintf(char *buffer, size_t size, const char *fmt, ...) {
86   va_list al;
87   va_start(al,fmt);
88   vsnyprintf(buffer,size,fmt,al);
89   va_end(al);
90 }
91
92 void strnycpy(char *dest, const char *src, size_t size) {
93   strncpy(dest,src,size-1);
94   dest[size-1]= 0;
95 }
96
97 void strnytcat(char *dest, const char *src, size_t size) {
98   size_t l;
99   l= strlen(dest);
100   strnycpy(dest+l,src,size-l);
101 }
102
103 void vsnytprintfcat(char *buffer, size_t size, const char *fmt, va_list al) {
104   size_t l;
105   l= strlen(buffer);
106   vsnyprintf(buffer+l,size-l,fmt,al);
107 }
108
109 void snytprintfcat(char *buffer, size_t size, const char *fmt, ...) {
110   va_list al;
111   va_start(al,fmt);
112   vsnytprintfcat(buffer,size,fmt,al);
113   va_end(al);
114 }