/* * Copyright (c) 2004 Teodor Sigaev * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include "tlog.h" #include "tmalloc.h" void * tmalloc(size_t size) { void *ptr = malloc(size); if (!ptr) tlog(TL_CRIT|TL_EXIT, "Can't allocate %d bytes", size); return ptr; } void * t0malloc(size_t size) { void *ptr = tmalloc(size); memset(ptr,0,size); return ptr; } void * trealloc(void * ptr, size_t size) { if (ptr) { ptr = realloc(ptr,size); if (!ptr) tlog(TL_CRIT|TL_EXIT, "Can't reallocate to %d bytes", size); } else ptr = tmalloc(size); return ptr; } void tfree(void * ptr) { free(ptr); } char * tstrdup(char * src) { char * dest = strdup(src); if (!dest) tlog(TL_CRIT|TL_EXIT, "Can't strdup %d bytes", strlen(src)+1); return dest; } char * tnstrdup(char *src, int len) { char *dest=(char*)tmalloc(len+1); memcpy(dest, src, len); dest[len]='\0'; return dest; } char * strlower(char * src) { char *ptr = src; if (!src) return src; while(*ptr) { *ptr = tolower(*(unsigned char *) ptr); ptr++; } return src; } char * strupper(char * src) { char *ptr = src; if (!src) return src; while(*ptr) { *ptr = toupper(*(unsigned char *) ptr); ptr++; } return src; } int clrspace(char *buf) { char *ptr=buf, *ptrc=buf; while(*ptr) { if (!isspace(*ptr)) { *ptrc=*ptr; ptrc++; } ptr++; } *ptrc='\0'; return ptrc - buf; }