]> code.citadel.org Git - citadel.git/blob - rss2ctdl/os-support.c
808aeb5de782e05c792d81e108a79b7dbdf10c24
[citadel.git] / rss2ctdl / os-support.c
1 /*
2  * $Id$
3  * 
4  * Copyright 2003-2004 Oliver Feiler <kiza@kcore.de>
5  *
6  * os-support.c
7  *
8  * Library support functions.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #include "main.h"
32
33 /******************************************************************************
34  * This is a replacement for strsep which is not portable (missing on Solaris).
35  *
36  * http://www.winehq.com/hypermail/wine-patches/2001/11/0024.html
37  *
38  * The following function was written by François Gouget.
39  */
40 #ifdef SUN
41 char* strsep(char** str, const char* delims)
42 {
43     char* token;
44
45     if (*str==NULL) {
46         /* No more tokens */
47         return NULL;
48     }
49
50     token=*str;
51     while (**str!='\0') {
52         if (strchr(delims,**str)!=NULL) {
53             **str='\0';
54             (*str)++;
55             return token;
56         }
57         (*str)++;
58     }
59     /* There is no other token */
60     *str=NULL;
61    return token;
62 }
63 #endif
64
65 /* strcasestr stolen from: http://www.unixpapa.com/incnote/string.html */
66 char *s_strcasestr(char *a, char *b) {
67         size_t l;
68         char f[3];
69         int lena = strlen(a);
70         int lenb = strlen(b);
71         
72         snprintf(f, sizeof(f), "%c%c", tolower(*b), toupper(*b));
73         for (l = strcspn(a, f); l != lena; l += strcspn(a + l + 1, f) + 1)
74                 if (strncasecmp(a + l, b, lenb) == 0)
75                         return(a + l);
76         return(NULL);
77 }
78
79
80 /* Private malloc wrapper. Aborts program execution if malloc fails. */
81 void * s_malloc (size_t size) {
82         void *newmem;
83         
84         newmem = malloc (size);
85         
86         if (newmem == NULL) {
87                 fprintf(stderr, "Error allocating memory: %s\n", strerror(errno));
88                 abort();
89         }
90         
91         return newmem;
92 }