Last active
January 12, 2021 09:15
-
-
Save bullheadandplato/86d982dd1c7b2af2ccc3f8648344d573 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
typedef struct wifi{ | |
char *name; | |
int rssi; | |
char *mac; | |
} Wifi; | |
int main(int argc, char **argv){ | |
char * buffer = 0; | |
long length; | |
FILE * f = fopen ("/Users/bullhead/IdeaProjects/C/C/macs", "r"); | |
if (f){ | |
fseek (f, 0, SEEK_END); | |
length = ftell (f); | |
fseek (f, 0, SEEK_SET); | |
buffer = malloc (length); | |
if (buffer){ | |
fread (buffer, 1, length, f); | |
} | |
fclose (f); | |
}else{ | |
printf("failed to read file.\n"); | |
} | |
if(buffer){ | |
Wifi **wifis; | |
char *line=strtok(buffer, "\n"); | |
int count=1; | |
char **lines; | |
//copy all the lines in lines array. later we can use strtok on each line | |
while (line) { | |
if (strcmp("AT+CWLAP", line)==0) { | |
line=strtok(NULL, "\n"); | |
}else if(strcmp("OK", line)==0){ | |
break; | |
}else{ | |
if(count==1){ | |
lines=(char**)calloc(count, sizeof(char*)); | |
}else{ | |
lines=realloc(lines, count*sizeof(char*)); | |
} | |
lines[count-1]=(char *)calloc(strlen(line), sizeof(char)); | |
strcpy(lines[count-1], line); | |
count++; | |
line=strtok(NULL, "\n"); | |
} | |
} | |
wifis=(Wifi **)calloc(count-1, sizeof(Wifi *)); | |
for (int i=0; i<count-1; i++) { | |
Wifi *wifi=(Wifi *)calloc(1, sizeof(Wifi)); | |
char *current_line=lines[i]; | |
strtok(current_line, ","); | |
char *name=strtok(NULL, ","); | |
wifi->name=(char *)calloc(strlen(name), sizeof(char)); | |
strcpy(wifi->name, name); | |
wifi->rssi=atoi(strtok(NULL, ",")); | |
char *mac=strtok(NULL, ","); | |
wifi->mac=(char *)calloc(strlen(mac), sizeof(char)); | |
strcpy(wifi->mac, mac); | |
wifis[i]=wifi; | |
free(current_line); | |
} | |
for (int i=0; i<count-1; i++) { | |
Wifi *wifi=wifis[i]; | |
printf("[%s]-[%d]-[%s]\n",wifi->name,wifi->rssi,wifi->mac); | |
free(wifi); | |
} | |
free(lines); | |
free(wifis); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AT+CWLAP | |
+CWLAP:(0,"ESP_947938",-64,"6a:c6:3a:94:79:38",1,-12,0,0,0,3,0) | |
+CWLAP:(4,"SUNNY",-91,"be:2c:11:c5:87:2d",1,0,0,4,4,7,0) | |
+CWLAP:(3,"anisah",-91,"5c:6a:80:ec:75:1e",1,-16,0,4,4,7,0) | |
+CWLAP:(4,"ARGE-TEST2-2",-56,"bc:cf:4f:c9:71:60",5,-19,0,4,4,7,0) | |
+CWLAP:(4,"AtlantisNet-20855",-90,"d8:32:14:0a:21:90",1,-4,0,4,4,7,0) | |
+CWLAP:(4,"ARGE-TEST",-71,"bc:99:11:c5:83:21",9,-16,0,4,4,7,0) | |
+CWLAP:(3,"SUNNY-ARGE",-71,"be:21:11:c5:83:22",9,-16,0,4,4,7,0) | |
+CWLAP:(3,"akilli-mikrofon",-89,"bc:cf:4f:4b:27:c0",11,-21,0,5,3,7,0) | |
+CWLAP:(4,"mikrofon-misafir",-86,"be:c0:4f:4b:27:c1",11,-21,0,4,4,7,0) | |
+CWLAP:(0,"akillimikrofontv",-88,"be:c0:4f:4b:27:c2",11,-21,0,0,0,7,0) | |
+CWLAP:(3,"ATMACA",-87,"bc:99:11:c5:87:2c",1,0,0,4,4,7,0) | |
+CWLAP:(3,"SUNNY-OPERASYON",-80,"bc:99:11:c5:80:c4",13,-17,0,4,4,7,0) | |
+CWLAP:(3,"anisah-misafir",-87,"5e:1e:80:ec:75:1f",1,-16,0,4,4,7,0) | |
+CWLAP:(3,"SUNNY-PAZARLAMA",-88,"be:2c:11:c5:87:2e",1,1,0,4,4,7,0) | |
OK |
Author
bullheadandplato
commented
Jan 12, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment