File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / hping2 / hstring.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 22:11:37 2012 UTC (12 years, 4 months ago) by misho
Branches: hping2, MAIN
CVS tags: v2_0_0rc3p7, v2_0_0rc3p5, v2_0_0rc3p4, v2_0_0rc3p0, v2_0_0rc3, HEAD
hping2

    1: /* hstring.c - Random string-related functions for hping.
    2:  * Copyright(C) 2003 Salvatore Sanfilippo
    3:  * All rights reserved */
    4: 
    5: #include <sys/types.h>
    6: #include <string.h>
    7: #include <ctype.h>
    8: 
    9: /* return 1 if the string looks like an integer number
   10:  * otherwise 0 is returned.
   11:  *
   12:  * this function is equivalent to this regexp:
   13:  *		[:space:]*-{0,1}[0-9]+[:space:]*
   14:  * in english:
   15:  *  (0-inf spaces)(zero or one -)(1-inf digits)(0-inf spaces)
   16:  */
   17: int strisnum(char *s)
   18: {
   19: 	int digits = 0; /* used to return false if there aren't digits */
   20: 
   21: 	while(isspace(*s))
   22: 		s++; /* skip initial spaces */
   23: 	if (*s == '-') /* negative number? */
   24: 		s++;
   25: 	while(*s) {
   26: 		if (isspace(*s)) { /* skip spaces in the tail */
   27: 			while(isspace(*s))
   28: 				s++;
   29: 			if (*s) return 0; /* but don't allow other tail chars */
   30: 			return digits ? 1 : 0;
   31: 		}
   32: 		if (!isdigit(*s))
   33: 			return 0;
   34: 		s++;
   35: 		digits++;
   36: 	}
   37: 	return digits ? 1 : 0;
   38: }
   39: 
   40: /* function similar to strtok() more convenient when we know the
   41:  * max number of tokens, to tokenize with a single call.
   42:  * Unlike strtok(), strftok() is thread safe.
   43:  *
   44:  * ARGS:
   45:  *   'sep' is a string that contains all the delimiter characters
   46:  *   'str' is the string to tokenize, that will be modified
   47:  *   'tptrs' is an array of char* poiters that will contain the token pointers
   48:  *   'nptrs' is the length of the 'tptrs' array.
   49:  *
   50:  * RETURN VALUE:
   51:  *   The number of extracted tokens is returned.
   52:  */
   53: size_t strftok(char *sep, char *str, char **tptrs, size_t nptrs)
   54: {
   55: 	size_t seplen = strlen(sep);
   56: 	size_t i, j = 0;
   57: 	int inside = 0;
   58: 
   59: 	while(*str) {
   60: 		for(i = 0; i < seplen; i++) {
   61: 			if (sep[i] == *str)
   62: 				break;
   63: 		}
   64: 		if (i == seplen) { /* no match */
   65: 			if (!inside) {
   66: 				tptrs[j++] = str;
   67: 				inside = 1;
   68: 			}
   69: 		} else { /* match */
   70: 			if (inside) {
   71: 				*str = '\0';
   72: 				if (j == nptrs)
   73: 					return j;
   74: 				inside = 0;
   75: 			}
   76: 		}
   77: 		str++;
   78: 	}
   79: 	return j;
   80: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>