libdht

A simple helper library for distributed hash tables.
git clone git://r-36.net/libdht
Log | Files | Refs | README | LICENSE

ind.c (1290B)


      1 /*
      2  * Copy me if you can.
      3  * by 20h
      4  */
      5 
      6 #include <unistd.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <stdarg.h>
     10 #include <fcntl.h>
     11 #include <string.h>
     12 #include <ctype.h>
     13 #include <errno.h>
     14 
     15 #include "ind.h"
     16 
     17 void
     18 die(char *fmt, ...)
     19 {
     20 	va_list fmtargs;
     21 
     22 	va_start(fmtargs, fmt);
     23 	vfprintf(stderr, fmt, fmtargs);
     24 	va_end(fmtargs);
     25 
     26 	exit(EXIT_FAILURE);
     27 }
     28 
     29 void
     30 edie(char *fmt, ...)
     31 {
     32 	va_list fmtargs;
     33 
     34 	va_start(fmtargs, fmt);
     35 	vfprintf(stderr, fmt, fmtargs);
     36 	va_end(fmtargs);
     37 
     38 	perror(NULL);
     39 
     40 	exit(EXIT_FAILURE);
     41 }
     42 
     43 void *
     44 reallocz(void *p, int l, int z)
     45 {
     46         p = realloc(p, l);
     47         if(p == NULL)
     48 		edie("realloc");
     49         if(z > 0)
     50                 memset(p, 0, l);
     51 
     52         return p;
     53 }
     54 
     55 void *
     56 mallocz(int l, int z)
     57 {
     58 	return reallocz(NULL, l, z);
     59 }
     60 
     61 void *
     62 memdup(void *p, int l)
     63 {
     64         char *ret;
     65 
     66         ret = reallocz(NULL, l, 2);
     67         memmove(ret, p, l);
     68 
     69         return (void *)ret;
     70 }
     71 
     72 char *
     73 vsmprintf(char *fmt, va_list fmtargs)
     74 {
     75 	char *ret;
     76 	int size;
     77 
     78 	ret = "";
     79 
     80 	size = vsnprintf(ret, 0, fmt, fmtargs);
     81 	ret = reallocz(NULL, ++size, 2);
     82 	vsnprintf(ret, size, fmt, fmtargs);
     83 
     84 	return ret;
     85 }
     86 
     87 char *
     88 smprintf(char *fmt, ...)
     89 {
     90 	va_list fmtargs;
     91 	char *ret;
     92 
     93 	va_start(fmtargs, fmt);
     94 	ret = vsmprintf(fmt, fmtargs);
     95 	va_end(fmtargs);
     96 
     97 	return ret;
     98 }
     99