chiark / gitweb /
Change email address to correspond to PGP key.
[userv.git] / lib.c
diff --git a/lib.c b/lib.c
index 4af9b804dae2a26a22885eb29735025b97564953..bd40ee08ab77376f74b1ce5715b9740a16c82e71 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -2,7 +2,7 @@
  * userv - lib.c
  * useful utility routines, used in daemon, but not very dependent on it
  *
- * Copyright (C)1996-1997 Ian Jackson
+ * Copyright (C)1996-1997,1999 Ian Jackson
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
+#include <sys/types.h>
 
 #include "config.h"
+#include "common.h"
 #include "lib.h"
 
-char *xmstrcat3save(const char *a, const char *b, const char *c) {
+char *xstrcat3save(const char *a, const char *b, const char *c) {
   char *r;
 
   r= xmalloc(strlen(a)+strlen(b)+strlen(c)+1);
@@ -39,7 +42,7 @@ char *xmstrcat3save(const char *a, const char *b, const char *c) {
   return r;
 }
 
-char *xmstrsave(const char *s) {
+char *xstrsave(const char *s) {
   char *r;
 
   r= xmalloc(strlen(s)+1);
@@ -47,7 +50,7 @@ char *xmstrsave(const char *s) {
   return r;
 }
 
-char *xmstrsubsave(const char *begin, int len) {
+char *xstrsubsave(const char *begin, int len) {
   char *r;
   
   r= xmalloc(len+1);
@@ -67,16 +70,12 @@ void *xrealloc(void *p, size_t s) {
   return p;
 }
 
-char *xstrdup(const char *str) {
-  char *r;
-  r= xmalloc(strlen(str)+1);
-  strcpy(r,str); return r;
-}
-
-void makeroom(char **buffer, int *size, int needed) {
-  if (*size >= needed) return;
+int makeroom(char **buffer, int *size, int needed) {
+  if (needed > MAX_GENERAL_STRING) return -1;
+  if (*size >= needed) return 0;
   *buffer= xrealloc(*buffer,needed);
   *size= needed;
+  return 0;
 }
 
 void vsnyprintf(char *buffer, size_t size, const char *fmt, va_list al) {
@@ -114,3 +113,19 @@ void snytprintfcat(char *buffer, size_t size, const char *fmt, ...) {
   vsnytprintfcat(buffer,size,fmt,al);
   va_end(al);
 }
+
+#ifndef HAVE_SETENV
+int setenv(const char *name, const char *value, int overwrite) {
+  static char *buffer= 0;
+  static int avail= 0;
+
+  int r;
+  
+  assert(overwrite==1);
+  r= makeroom(&buffer,&avail,strlen(name)+strlen(value)+2);
+  if (r) { errno= EINVAL; return -1; }
+
+  sprintf(buffer,"%s=%s",name,value);
+  return putenv(buffer);
+}
+#endif /* HAVE_SETENV */