Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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