1 /*
2 *========================================================================
3 * See copyright in copyright.h and the accompanying file COPYING
4 *========================================================================
5 * Utility routines to support basic needs of the program like managing
6 * models, creating and deleting directories with contents. Probable
7 * cruft or yet-unused routines belong at the bottom...
8 *========================================================================
9 */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <dieharder/parse.h>
14
15 int verbose;
16
17 /*
18 * split() is a reusable routine to break up a string into char[PBUF]
19 * fields. Anything past PBUF characters is truncated. The output
20 * is put into parsebuf[i] and the number of fields found is returned.
21 * It is somewhat like the perl split function except that I give it
22 * a 128K buffer of its very own to work with and you don't get to
23 * choose your delimiters.
24 */
split(char * inbuffer)25 int split(char *inbuffer)
26 {
27
28 char delim[7],*nextval;
29
30 if(verbose){
31 printf("split(%s)\n",inbuffer);
32 }
33
34
35 /*
36 * Permit blank, tab, or comma separators anywhere we need to parse
37 * a line.
38 */
39 delim[0] = ' '; /* blanks */
40 delim[1] = (char) 9; /* tab */
41 delim[2] = ','; /* comma */
42 delim[3] = (char) 10; /* LF */
43 delim[4] = (char) 13; /* CR */
44 delim[5] = ':'; /* : needed to parse /proc/net/dev or passwd */
45 delim[6] = (char) 0; /* terminator */
46
47
48 nextval = strtok(inbuffer,delim);
49 if(nextval == (char *)NULL) return 0;
50 int i = 0;
51 strncpy(splitbuf[i],nextval,PBUF);
52 if(verbose){
53 printf("split(): split field[%d] = %s.\n",i,splitbuf[i]);
54 }
55 i++;
56
57 while(i < PK-1){
58 nextval = strtok((char *) NULL, delim);
59 if(nextval == (char *)NULL) break;
60 strncpy(splitbuf[i], nextval,PBUF);
61 if(verbose){
62 printf("split(): split field[%d] = %s.\n",i,splitbuf[i]);
63 }
64 i++;
65 }
66
67 /* Null the last field */
68 memset(splitbuf[i],0,PBUF);
69 if(verbose){
70 printf("split(): Terminated split field[%d] = %s.\n",i,splitbuf[i]);
71 printf("split(): Returning %d as the field count\n",i);
72 }
73
74 return i;
75
76 }
77
78 /*
79 * parse() is a reusable routine to break up a string into char[32]
80 * fields. Anything past 32 characters is truncated.
81 */
82
parse(char * inbuffer,char ** outfields,int maxfields,int maxfieldlength)83 int parse(char *inbuffer,char **outfields,int maxfields,int maxfieldlength)
84 {
85
86 char delim[7],*nextval;
87 int i = 0;
88
89 if(verbose){
90 printf("parse():\n");
91 }
92
93
94
95 /*
96 * Permit blank, tab, or comma separators anywhere we need to parse
97 * a line.
98 */
99 delim[0] = ' '; /* blanks */
100 delim[1] = (char) 9; /* tab */
101 delim[2] = ','; /* comma */
102 delim[3] = (char) 10; /* LF */
103 delim[4] = (char) 13; /* CR */
104 delim[5] = ':'; /* : needed to parse /proc/net/dev or passwd */
105 delim[6] = (char) 0; /* terminator */
106
107
108 nextval = strtok(inbuffer,delim);
109 if(nextval == (char *)NULL) return 0;
110 strncpy(outfields[i++],nextval,maxfieldlength);
111 if(verbose){
112 printf("parse(): Parsed field[%d] = %s.\n",i-1,outfields[i-1]);
113 }
114
115 while(i < maxfields-1){
116 nextval = strtok((char *) NULL, delim);
117 if(nextval == (char *)NULL) break;
118 strncpy(outfields[i++], nextval,maxfieldlength);
119 if(verbose){
120 printf("parse(): Parsed field[%d] = %s.\n",i-1,outfields[i-1]);
121 }
122 }
123
124 /* Null the last field */
125 memset(outfields[i],0,maxfieldlength);
126 if(verbose){
127 printf("parse(): Terminated field[%d] = %s.\n",i,outfields[i]);
128 }
129
130 return i;
131
132 }
133
chop(char * buf)134 void chop(char *buf)
135 {
136
137 /* Advance to end of string */
138 while(*buf != 0) buf++;
139 buf--;
140 /* Reterminate one character earlier */
141 *buf = 0;
142 }
143
144