libical

A simple ical library.
git clone git://r-36.net/libical
Log | Files | Refs | README | LICENSE

ind.c (2411B)


      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 <strings.h>
     13 #include <errno.h>
     14 #include <ctype.h>
     15 #include <sys/types.h>
     16 #include <sys/stat.h>
     17 #include <time.h>
     18 
     19 #include "ind.h"
     20 
     21 void
     22 edie(char *fmt, ...)
     23 {
     24 	va_list fmtargs;
     25 
     26 	va_start(fmtargs, fmt);
     27 	vfprintf(stderr, fmt, fmtargs);
     28 	va_end(fmtargs);
     29 	fprintf(stderr, ": ");
     30 
     31 	perror(NULL);
     32 
     33 	exit(1);
     34 }
     35 
     36 void
     37 die(char *fmt, ...)
     38 {
     39 	va_list fmtargs;
     40 
     41 	va_start(fmtargs, fmt);
     42 	vfprintf(stderr, fmt, fmtargs);
     43 	va_end(fmtargs);
     44 
     45 	exit(1);
     46 }
     47 
     48 void *
     49 reallocz(void *p, int l, int z)
     50 {
     51         p = realloc(p, l);
     52         if(p == NULL)
     53 		edie("realloc");
     54         if(z)
     55 		memset(p, 0, l);
     56 
     57         return p;
     58 }
     59 
     60 void *
     61 mallocz(int l, int z)
     62 {
     63 	return reallocz(NULL, l, z);
     64 }
     65 
     66 void *
     67 memdup(void *p, int l)
     68 {
     69         char *ret;
     70 
     71         ret = reallocz(NULL, l, 2);
     72         memmove(ret, p, l);
     73 
     74         return (void *)ret;
     75 }
     76 
     77 void *
     78 memdupz(void *p, int l)
     79 {
     80 	char *ret;
     81 
     82 	ret = reallocz(NULL, l+1, 2);
     83 	memmove(ret, p, l);
     84 
     85 	return (void *)ret;
     86 }
     87 
     88 void *
     89 memdupcat(void *p, int lp, void *c, int lc)
     90 {
     91 	p = reallocz(p, lp+lc, 0);
     92 	memset(&((char *)p)[lp], 0, lc);
     93 
     94 	memmove(&((char *)p)[lp], c, lc);
     95 
     96 	return p;
     97 }
     98 
     99 char *
    100 vsmprintf(char *fmt, va_list fmtargs, int size)
    101 {
    102 	char *ret;
    103 
    104 	ret = reallocz(NULL, ++size, 2);
    105 	vsnprintf(ret, size, fmt, fmtargs);
    106 
    107 	return ret;
    108 }
    109 
    110 char *
    111 smprintf(char *fmt, ...)
    112 {
    113 	va_list fmtargs;
    114 	char *ret;
    115 	int len;
    116 
    117 	va_start(fmtargs, fmt);
    118 	len = vsnprintf(NULL, 0, fmt, fmtargs);
    119 	va_end(fmtargs);
    120 
    121 	va_start(fmtargs, fmt);
    122 	ret = vsmprintf(fmt, fmtargs, len);
    123 	va_end(fmtargs);
    124 
    125 	return ret;
    126 }
    127 
    128 char *
    129 readtoeoffd(int fd, int *len)
    130 {
    131 	char *ret, buf[4096];
    132 	int olen, nlen, rl;
    133 
    134 	for (nlen = 0, olen = 0, ret = NULL;
    135 			(rl = read(fd, buf, sizeof(buf))); olen = nlen) {
    136 		if (rl > 0) {
    137 			nlen += rl;
    138 			ret = reallocz(ret, nlen+1, 0);
    139 			ret[nlen] = '\0';
    140 
    141 			memmove(&ret[olen], buf, rl);
    142 		}
    143 	}
    144 
    145 	*len = nlen;
    146 	return ret;
    147 }
    148 
    149 char *
    150 sgets(char *s, int size, char **p)
    151 {
    152 	char *np;
    153 	int cl;
    154 
    155 	if (*p == NULL)
    156 		return NULL;
    157 
    158 	np = strchr(*p, '\n');
    159 	if (np == NULL) {
    160 		cl = strlen(*p);
    161 		if (cl < 1) {
    162 			*p = NULL;
    163 			return NULL;
    164 		}
    165 	} else {
    166 		cl = np - *p;
    167 	}
    168 
    169 	if (cl >= size)
    170 		cl = size - 1;
    171 	memmove(s, *p, cl);
    172 	s[cl] = '\0';
    173 
    174 	if (np == NULL) {
    175 		*p = NULL;
    176 	} else {
    177 		*p = np+1;
    178 	}
    179 
    180 	return s;
    181 }
    182