1 /****************************************************************************
2 * Pathneck: locating network path bottlenecks
3 * Copyright (C) 2004
4 * Ningning Hu and the Carnegie Mellon University
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License (in the COPYING file) for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ****************************************************************************/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25
26 #include "get-line.h"
27
28 char * cur_pos = (char *)0;
29 char * end_pos = (char *)-1; /* this initializatio is a must */
30 char file_read_buf[BUF_SIZE];
31
get_line(char items[MAX_ITEM_NUM][LINE_SIZE],FILE * fp)32 int get_line(char items[MAX_ITEM_NUM][LINE_SIZE], FILE * fp)
33 {
34 int item_cnt = 0;
35 char * s_pos;
36 int len;
37
38 /* have we finished the reading? */
39 if (cur_pos > end_pos)
40 return 0;
41
42 /* we assume one line is never longer than 128 Bytes */
43 len = end_pos - cur_pos + 1;
44
45 /* fill the buffer */
46 if (len < LINE_SIZE) {
47 int ret;
48
49 /* move the remaining to the beging of the buffer*/
50 if (len > 0)
51 memcpy(file_read_buf, cur_pos, len);
52
53 /* read from file */
54 ret = fread(file_read_buf + len, 1, BUF_SIZE - len, fp);
55 if (ret < 0) {
56 perror("file read");
57 exit(1);
58 }
59 cur_pos = file_read_buf;
60 end_pos = file_read_buf + len + ret - 1;
61 }
62
63 s_pos = cur_pos;
64 /* read until end of line */
65 while (cur_pos <= end_pos) {
66 if ((item_cnt<MAX_ITEM_NUM) && (((*cur_pos) == ' ') ||
67 ((*cur_pos) == EOLN))) {
68 /* get the string */
69 len = cur_pos - s_pos;
70 memcpy(items[item_cnt], s_pos, len);
71 items[item_cnt][len] = 0;
72 item_cnt ++;
73 s_pos = cur_pos+1;
74 }
75
76 if ((*cur_pos) == EOLN) break;
77 cur_pos ++;
78 }
79 cur_pos ++;
80
81 return item_cnt;
82 }
83
84 #if 0 /* this is an example on how to use get_line() */
85 void read_in_map(char * filename)
86 {
87 char item[2][128];
88 char *pos;
89 char tmp_str[16];
90 int mask_num;
91
92 struct map_item * p;
93
94 if ((fp = fopen(filename, "r")) == NULL) {
95 perror("file open");
96 exit(1);
97 }
98
99 /* read into an link list first, for the convenience of sorting */
100 while (get_line(item) == 2) {
101 p = (struct map_item *)malloc(sizeof(struct map_item));
102 pos = index(item[0], '/');
103 *pos = 0;
104
105 strncpy(tmp_str, item[0], pos-item[0]+1);
106 p->ip = inet_addr(tmp_str);
107 // printf("%x\n", p->ip);
108
109 strcpy(tmp_str, pos+1);
110 mask_num = atoi(tmp_str);
111 p->mask = (0xffffffff >> (32-mask_num));
112
113 p->as = atoi(item[1]);
114 p->next = NULL;
115
116 as_map[map_size] = p;
117 map_size ++;
118 }
119 }
120 #endif
121