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