-
Notifications
You must be signed in to change notification settings - Fork 1
/
scan.c
100 lines (88 loc) · 2.71 KB
/
scan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* scan.c - reformat reports of local wifi activity
*
* Copyright (C) 2012, Greg Johnson
* Released under the terms of the GNU GPL v2.0.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* $Id: scan.c,v 1.5 2012-02-22 18:55:25 greg Exp $
*/
static const char Version[] = "Version "
"$Id: scan.c,v 1.5 2012-02-22 18:55:25 greg Exp $";
/* a small utility program that reads the output of "wl scanresults", which
* reports on other wifi activity in the area, and reformats it into a
* pipe-delimited file.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "util.h"
/* assume the string is of the form "blah field_name: field space*"
* put the field into out. (don't exceed out_len).
* return true iff we found the field.
* null-terminate the input string at the beginning of field_name.
*/
static bool_t get_field(char *in, char *field_name, char *out, int out_len)
{
char *p, *q;
if ((p = strstr(in, field_name)) == NULL) { return false; }
q = in + strlen(in) - 1;
*p = '\0';
p += strlen(field_name);
while (*p && isspace(*p)) { p++; }
while (q >= p && isspace(*q)) { q--; }
q++;
*q = '\0';
strncpy(out, p, out_len-1);
out[out_len-1] = '\0';
return true;
}
int main(int argc, char **argv)
{
char buf[256];
char ssid[256], mode[256], rssi[256], noise[256], channel[256];
bool_t have_ssid = false;
int irssi, inoise;
while (fgets(buf, 256, stdin)) {
if (!have_ssid) {
if (get_field(buf, "SSID: \"", ssid, 256)) {
if (strlen(ssid) > 0) {
ssid[strlen(ssid) - 1] = '\0';
}
have_ssid = true;
}
continue;
}
if (!get_field(buf, "Channel:", channel, 256)) {
have_ssid = false;
continue;
}
if (!get_field(buf, "noise:", noise, 256)) {
have_ssid = false;
continue;
}
if (sscanf(noise, "%d", &inoise) != 1) {
have_ssid = false;
continue;
}
if (!get_field(buf, "RSSI:", rssi, 256)) {
have_ssid = false;
continue;
}
if (sscanf(rssi, "%d", &irssi) != 1) {
have_ssid = false;
continue;
}
if (!get_field(buf, "Mode:", mode, 256)) {
have_ssid = false;
continue;
}
printf("%s|%s|%d|%s|%s|%s\n", ssid, mode, irssi - inoise, rssi, noise,
channel);
have_ssid = false;
}
return 0;
}