chiark / gitweb /
Remove spurious undivert(4).
[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,1999 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 <assert.h>
29 #include <sys/types.h>
30
31 #include "config.h"
32 #include "common.h"
33 #include "lib.h"
34
35 char *xstrcat3save(const char *a, const char *b, const char *c) {
36   char *r;
37
38   r= xmalloc(strlen(a)+strlen(b)+strlen(c)+1);
39   strcpy(r,a);
40   strcat(r,b);
41   strcat(r,c);
42   return r;
43 }
44
45 char *xstrsave(const char *s) {
46   char *r;
47
48   r= xmalloc(strlen(s)+1);
49   strcpy(r,s);
50   return r;
51 }
52
53 char *xstrsubsave(const char *begin, int len) {
54   char *r;
55   
56   r= xmalloc(len+1);
57   memcpy(r,begin,len);
58   r[len]= 0;
59   return r;
60 }
61
62 void *xmalloc(size_t s) {
63   void *p;
64   p= malloc(s?s:1); if (!p) syscallerror("malloc");
65   return p;
66 }
67
68 void *xrealloc(void *p, size_t s) {
69   p= realloc(p,s); if (!p) syscallerror("realloc");
70   return p;
71 }
72
73 int makeroom(char **buffer, int *size, int needed) {
74   if (needed > MAX_GENERAL_STRING) return -1;
75   if (*size >= needed) return 0;
76   *buffer= xrealloc(*buffer,needed);
77   *size= needed;
78   return 0;
79 }
80
81 void vsnyprintf(char *buffer, size_t size, const char *fmt, va_list al) {
82   vsnprintf(buffer,size,fmt,al);
83   buffer[size-1]= 0;
84 }
85
86 void snyprintf(char *buffer, size_t size, const char *fmt, ...) {
87   va_list al;
88   va_start(al,fmt);
89   vsnyprintf(buffer,size,fmt,al);
90   va_end(al);
91 }
92
93 void strnycpy(char *dest, const char *src, size_t size) {
94   strncpy(dest,src,size-1);
95   dest[size-1]= 0;
96 }
97
98 void strnytcat(char *dest, const char *src, size_t size) {
99   size_t l;
100   l= strlen(dest);
101   strnycpy(dest+l,src,size-l);
102 }
103
104 void vsnytprintfcat(char *buffer, size_t size, const char *fmt, va_list al) {
105   size_t l;
106   l= strlen(buffer);
107   vsnyprintf(buffer+l,size-l,fmt,al);
108 }
109
110 void snytprintfcat(char *buffer, size_t size, const char *fmt, ...) {
111   va_list al;
112   va_start(al,fmt);
113   vsnytprintfcat(buffer,size,fmt,al);
114   va_end(al);
115 }
116
117 #ifndef HAVE_SETENV
118 int setenv(const char *name, const char *value, int overwrite) {
119   static char *buffer= 0;
120   static int avail= 0;
121
122   int r;
123   
124   assert(overwrite==1);
125   r= makeroom(&buffer,&avail,strlen(name)+strlen(value)+2);
126   if (r) { errno= EINVAL; return -1; }
127
128   sprintf(buffer,"%s=%s",name,value);
129   return putenv(buffer);
130 }
131 #endif /* HAVE_SETENV */