chiark / gitweb /
@@ -1,3 +1,9 @@
[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 #include "both.h"
35
36 char *xstrcat3save(const char *a, const char *b, const char *c) {
37   char *r;
38
39   r= xmalloc(strlen(a)+strlen(b)+strlen(c)+1);
40   strcpy(r,a);
41   strcat(r,b);
42   strcat(r,c);
43   return r;
44 }
45
46 char *xstrsubsave(const char *begin, int len) {
47   char *r;
48   
49   r= xmalloc(len+1);
50   memcpy(r,begin,len);
51   r[len]= 0;
52   return r;
53 }
54
55 int makeroom(char **buffer, int *size, int needed) {
56   if (needed > MAX_GENERAL_STRING) return -1;
57   if (*size >= needed) return 0;
58   *buffer= xrealloc(*buffer,needed);
59   *size= needed;
60   return 0;
61 }
62
63 void vsnyprintf(char *buffer, size_t size, const char *fmt, va_list al) {
64   vsnprintf(buffer,size,fmt,al);
65   buffer[size-1]= 0;
66 }
67
68 void snyprintf(char *buffer, size_t size, const char *fmt, ...) {
69   va_list al;
70   va_start(al,fmt);
71   vsnyprintf(buffer,size,fmt,al);
72   va_end(al);
73 }
74
75 void strnycpy(char *dest, const char *src, size_t size) {
76   strncpy(dest,src,size-1);
77   dest[size-1]= 0;
78 }
79
80 void strnytcat(char *dest, const char *src, size_t size) {
81   size_t l;
82   l= strlen(dest);
83   strnycpy(dest+l,src,size-l);
84 }
85
86 void vsnytprintfcat(char *buffer, size_t size, const char *fmt, va_list al) {
87   size_t l;
88   l= strlen(buffer);
89   vsnyprintf(buffer+l,size-l,fmt,al);
90 }
91
92 void snytprintfcat(char *buffer, size_t size, const char *fmt, ...) {
93   va_list al;
94   va_start(al,fmt);
95   vsnytprintfcat(buffer,size,fmt,al);
96   va_end(al);
97 }
98
99 #ifndef HAVE_SETENV
100 int setenv(const char *name, const char *value, int overwrite) {
101   char *buffer= 0;
102   
103   assert(overwrite==1);
104   buffer= xmalloc(strlen(name)+strlen(value)+2);
105
106   sprintf(buffer,"%s=%s",name,value);
107   return putenv(buffer);
108 }
109 #endif /* HAVE_SETENV */