3 * useful utility routines, used in daemon, but not very dependent on it
5 * Copyright (C)1996-1997,1999 Ian Jackson
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.
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.
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.
29 #include <sys/types.h>
35 char *xstrcat3save(const char *a, const char *b, const char *c) {
38 r= xmalloc(strlen(a)+strlen(b)+strlen(c)+1);
45 char *xstrsave(const char *s) {
48 r= xmalloc(strlen(s)+1);
53 char *xstrsubsave(const char *begin, int len) {
62 void *xmalloc(size_t s) {
64 p= malloc(s?s:1); if (!p) syscallerror("malloc");
68 void *xrealloc(void *p, size_t s) {
69 p= realloc(p,s); if (!p) syscallerror("realloc");
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);
81 void vsnyprintf(char *buffer, size_t size, const char *fmt, va_list al) {
82 vsnprintf(buffer,size,fmt,al);
86 void snyprintf(char *buffer, size_t size, const char *fmt, ...) {
89 vsnyprintf(buffer,size,fmt,al);
93 void strnycpy(char *dest, const char *src, size_t size) {
94 strncpy(dest,src,size-1);
98 void strnytcat(char *dest, const char *src, size_t size) {
101 strnycpy(dest+l,src,size-l);
104 void vsnytprintfcat(char *buffer, size_t size, const char *fmt, va_list al) {
107 vsnyprintf(buffer+l,size-l,fmt,al);
110 void snytprintfcat(char *buffer, size_t size, const char *fmt, ...) {
113 vsnytprintfcat(buffer,size,fmt,al);
118 int setenv(const char *name, const char *value, int overwrite) {
121 assert(overwrite==1);
122 buffer= xmalloc(strlen(name)+strlen(value)+2);
124 sprintf(buffer,"%s=%s",name,value);
125 return putenv(buffer);
127 #endif /* HAVE_SETENV */