1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
/*
pconsole WJ101
Copyright (C) 2001 Walter de Jong <walter@heiho.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
cstring.c WJ101
*/
#include "config.h"
#include "cstring.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef HAVE_MEMSET
void *memset(void *s, int c, size_t n) {
while(n > 0) {
*((char *)s) = (char)c;
n--;
}
return s;
}
#endif
#ifndef HAVE_STRDUP
char *strdup(char *s) {
char *str;
int len;
if (s == NULL)
return NULL;
if ((str = (char *)malloc(strlen(s)+1)) == NULL)
return NULL;
strcpy(str, s);
return str;
}
#endif
#ifndef HAVE_STRERROR
char *strerror(int err) {
static char buf[64];
sprintf(buf, "error #%d\n", err);
return buf;
}
#endif
void cstrip_line(char *msg) {
char *p;
int i;
if (msg == NULL || !*msg)
return;
while((p = strchr(msg, 27)) != NULL) /* filter escape characters */
memmove(p, p+1, strlen(p)+1);
while((p = strchr(msg, '\t')) != NULL) /* tab2space */
*p = ' ';
i = strlen(msg);
while(i && *msg == ' ')
memmove(msg, msg+1, i--);
i--;
while(i >= 0 && (msg[i] == ' ' || msg[i] == '\r' || msg[i] == '\n'))
msg[i--] = 0;
return;
}
/*
Splits a line into an array
Just like Perl's split() function
The substrings are not copied into the array; the string that is
passed as parameter to split() is chopped in pieces
The return value is allocated, it must be free()d
*/
char **cstrsplit(char *line, char token) {
char **args, *p, *startp;
int num = 2;
if (!line || !*line)
return NULL;
p = line;
while((p = strchr(p, token)) != NULL) {
num++;
p++;
}
if ((args = (char **)malloc(num * sizeof(char *))) == NULL)
return NULL;
startp = p = line;
num = 0;
while((p = strchr(p, token)) != NULL) {
args[num++] = startp;
*p = 0;
p++;
startp = p;
}
args[num++] = startp;
args[num] = NULL;
return args;
}
/* EOB */
|