annotate src/readpstlog.c @ 51:06c0262ad689 stable-0-6-5

code cleanup
author carl
date Tue, 22 Jan 2008 14:39:02 -0800
parents f66078abed38
children 034641c26ab9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
48
f66078abed38 more fixes for 64 bit format
carl
parents: 46
diff changeset
1 #include "define.h"
f66078abed38 more fixes for 64 bit format
carl
parents: 46
diff changeset
2
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
3 #include <stdio.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
4 #include <ctype.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
5 #include <string.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
6
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
7 #ifndef _WIN32
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
8 # include <unistd.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
9 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
10
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
11 #ifndef __GNUC__
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
12 # include "XGetopt.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
13 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
14
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
15
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
16 #define BUF_SIZE 4096
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
17
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
18 int usage();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
19 size_t get(void * buf, int size, unsigned int count, FILE *fp);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
20 int split_args(char *args, int **targ);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
21 int is_in(int a, int *b, int c);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
22
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
23 int main(int argc, char** argv) {
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
24 int level = 0;
46
b2a7f2e0926a more fixes for 64 bit format
carl
parents: 43
diff changeset
25 off_t *i = NULL;
b2a7f2e0926a more fixes for 64 bit format
carl
parents: 43
diff changeset
26 int x, ptr, stop=0, flag;
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
27 char *fname, *buf, rec_type;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
28 unsigned char version;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
29 int *show_type=NULL, show_size=0;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
30 int *ex_type=NULL, ex_size=0;
46
b2a7f2e0926a more fixes for 64 bit format
carl
parents: 43
diff changeset
31 unsigned int funcname=0, filename=0, text=0, end=0, dtype=0, line=0, c;
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
32 FILE *fp;
46
b2a7f2e0926a more fixes for 64 bit format
carl
parents: 43
diff changeset
33 struct pst_debug_file_rec_m mfile_rec;
b2a7f2e0926a more fixes for 64 bit format
carl
parents: 43
diff changeset
34 struct pst_debug_file_rec_l lfile_rec;
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
35 char format = 'D'; // default
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
36 while ((c = getopt(argc, argv, "f:t:x:")) != -1) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
37 switch(c) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
38 case 'f':
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
39 // change the output format
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
40 format = toupper(optarg[0]);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
41 break;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
42 case 't':
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
43 //change the type of statements shown
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
44 show_size = split_args(optarg, &show_type);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
45 // type = atoi(optarg);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
46 break;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
47 case 'x':
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
48 // change the type of statements excluded
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
49 ex_size = split_args(optarg, &ex_type);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
50 break;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
51 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
52 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
53 if (argc > optind) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
54 fname = argv[optind++];
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
55 } else {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
56 usage();
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
57 exit(2);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
58 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
59
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
60 fp = fopen(fname, "rb");
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
61 if (fp == NULL) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
62 printf("Error. couldn't open debug file\n");
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
63 return 2;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
64 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
65 if (get(&version, sizeof(char), 1, fp)==0) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
66 printf("Error. could not read version byte from front of file");
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
67 return 3;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
68 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
69
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
70 if (version > DEBUG_VERSION) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
71 printf("Version number is higher than the format I know about.");
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
72 return 4;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
73 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
74
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
75 buf = (char*) xmalloc(BUF_SIZE);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
76
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
77 while (!stop) {
48
f66078abed38 more fixes for 64 bit format
carl
parents: 46
diff changeset
78 off_t temp;
f66078abed38 more fixes for 64 bit format
carl
parents: 46
diff changeset
79 if (fread(&temp, sizeof(off_t), 1, fp)<=0) break;
f66078abed38 more fixes for 64 bit format
carl
parents: 46
diff changeset
80 x = (int)temp;
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
81 ptr = 0;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
82 if (x > 0) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
83 if (i) free(i);
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
84 i = (off_t*)xmalloc(sizeof(off_t)*(x+1));
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
85 // plus 1 cause we want to read the offset of the next index
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
86 if (get(i, sizeof(off_t), x+1, fp)==0) {
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
87 // we have reached the end of the debug file
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
88 printf("oh dear. we must now end\n");
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
89 break;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
90 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
91 while (ptr < x) {
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
92 fseek(fp, i[ptr++], SEEK_SET);
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
93 get(&rec_type, 1, sizeof(char), fp);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
94 if (rec_type == 'L') {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
95 get(&lfile_rec, sizeof(lfile_rec), 1, fp);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
96 funcname=lfile_rec.funcname;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
97 filename=lfile_rec.filename;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
98 text = lfile_rec.text;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
99 end = lfile_rec.end;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
100 dtype = lfile_rec.type;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
101 line = lfile_rec.line;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
102 } else if (rec_type == 'M') {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
103 get(&mfile_rec, sizeof(mfile_rec), 1, fp);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
104 funcname = mfile_rec.funcname;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
105 filename = mfile_rec.filename;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
106 text = mfile_rec.text;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
107 end = mfile_rec.end;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
108 dtype = mfile_rec.type;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
109 line = mfile_rec.line;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
110 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
111 if (dtype == DEBUG_FUNCENT_NO) level++;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
112 if ((show_type == NULL || is_in(dtype, show_type, show_size)) &&
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
113 (ex_type == NULL || !is_in(dtype, ex_type, ex_size))) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
114 c = 0; flag = 0;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
115 while (c < end) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
116 int ii = (level-1) * 4;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
117 if (ii < 0) ii = 0;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
118 if (ii > 64) ii = 64;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
119 char indent[ii+1];
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
120 memset(indent, ' ', ii);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
121 indent[ii] = '\0';
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
122 if (c + (BUF_SIZE-1) < end) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
123 get(buf, 1, BUF_SIZE-1, fp);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
124 buf[BUF_SIZE-1] = '\0';
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
125 c += BUF_SIZE-1;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
126 } else {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
127 get(buf, 1, end-c, fp);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
128 buf[end-c] = '\0';
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
129 c = end;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
130 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
131 if (flag == 0) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
132 if (format == 'I') { // indented text format
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
133 char *b = buf+text;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
134 printf("%s %s/%s[%d]: ", indent, &buf[filename], &buf[funcname], line);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
135 while (b) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
136 char *p = strchr(b, '\n');
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
137 if (p) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
138 *p = '\0';
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
139 printf("%s\n%s ", b, indent);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
140 b = p + 1;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
141 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
142 else {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
143 printf("%s", b);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
144 b = NULL;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
145 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
146 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
147 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
148 else if (format == 'T') { // text format
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
149 printf("%s/%s[%d]: %s", &buf[filename], &buf[funcname], line, &buf[text]);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
150 } else {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
151 printf("Type: %d\nFile[line]: %s[%d]\nFunction:%s\nText:%s", dtype,
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
152 &buf[filename], line, &buf[funcname], &buf[text]);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
153 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
154 flag = 1;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
155 } else {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
156 if (format == 'I') {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
157 char *b = buf;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
158 while (b) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
159 char *p = strchr(b, '\n');
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
160 if (p) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
161 *p = '\0';
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
162 printf("%s\n%s ", b, indent);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
163 b = p + 1;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
164 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
165 else {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
166 printf("%s", b);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
167 b = NULL;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
168 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
169 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
170 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
171 else printf("%s", buf);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
172 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
173 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
174 printf("\n");
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
175 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
176 if (dtype == DEBUG_FUNCRET_NO) level--;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
177 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
178 if (fseek(fp, i[ptr], SEEK_SET)==-1) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
179 printf("finished\n");
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
180 break;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
181 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
182 } else {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
183 printf("...no more items\n");
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
184 break;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
185 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
186 }
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
187 free(buf);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
188 fclose(fp);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
189 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
190 }
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
191
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
192 size_t get(void *buf, int size, unsigned int count, FILE *fp) {
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
193 size_t z;
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
194 if ((z=fread(buf, size, count, fp)) < count) {
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
195 printf("Read Failed! (size=%d, count=%d,z=%ld)\n", size, count, (long)z);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
196 exit(1);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
197 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
198 return z;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
199 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
200
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
201 int usage() {
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
202 printf("readlog -t[show_type] -x[exclude_type] -f[format] filename\n");
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
203 printf("\tformat:\n\t\tt: text log format\n");
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
204 printf("\t\ti: indented text log format\n");
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
205 printf("\tshow_type:\n\t\tcomma separated list of types to show "
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
206 "[ie, 2,4,1,6]\n");
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
207 printf("\texclude_type:\n\t\tcomma separated list of types to exclude "
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
208 "[ie, 1,5,3,7]\n");
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
209 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
210 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
211
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
212
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
213 int split_args(char *args, int **targ) {
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
214 int count = 1, *i, x, z;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
215 char *tmp = args, *y;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
216 if (*targ != NULL) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
217 free(*targ);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
218 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
219 // find the number of tokens in the string. Starting
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
220 // from 1 cause there will always be one
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
221 while ((tmp = strchr(tmp, ',')) != NULL) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
222 tmp++; count++;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
223 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
224 *targ = (int*)xmalloc(count * sizeof(int));
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
225 i = *targ; // for convienience
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
226 tmp = args;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
227 z = 0;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
228 for (x = 0; x < count; x++) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
229 y = strtok(tmp, ",");
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
230 tmp = NULL; // must be done after first call
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
231 if (y != NULL) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
232 i[x] = atoi(y);
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
233 z++;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
234 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
235 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
236 return z;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
237 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
238
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
239
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
240 // checks to see if the first arg is in the array of the second arg,
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
241 // the size of which is specified with the third arg. If the second
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
242 // arg is NULL, then it is obvious that it is not there.
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
243 int is_in(int a, int *b, int c){
36
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
244 int d = 0;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
245 if (b == NULL || c == 0) { // no array or no items in array
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
246 return 0;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
247 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
248 while (d < c) {
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
249 if (a == b[d]) return 1;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
250 d++;
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
251 }
6fe121a971c9 valgrind fixes
carl
parents: 16
diff changeset
252 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
253 }