Author: biurrun Date: Tue Oct 13 17:22:16 2009 New Revision: 1125 Log: whitespace cosmetics: Get rid of most of the ugly formatting and indentation. Modified: trunk/tools/iwlist_parser.c Modified: trunk/tools/iwlist_parser.c ============================================================================== --- trunk/tools/iwlist_parser.c Tue Oct 13 17:13:12 2009 (r1124) +++ trunk/tools/iwlist_parser.c Tue Oct 13 17:22:16 2009 (r1125) @@ -35,248 +35,249 @@ int iCellNumber; int step; -void parseFromFile( char * ); -void parseString( char * ); -void printBestAP( void ); -void logEvent( const char * ); - - - // Main routine. - // Some initializations & option parsing - // - int main( int argc, char * argv[] ) { - - - essidflag = false; - fileflag = false; - - step = 1; - - int c = 0; - - extern char *optarg; - extern int optopt; - - char pESSID[ESSID_LENGTH]; - char strFilename[FILENAME_LENGTH]; - - iCellNumber = 1; - memset( iSignalStrength, 0, MAX_CELLS ); - memset( strEssid, 0, MAX_CELLS*ESSID_LENGTH ); - memset( strMAC, 0, MAX_CELLS*MAC_LENGTH ); - memset( pESSID, 0, ESSID_LENGTH ); - memset( strSelectedESSID, 0, ESSID_LENGTH ); - - while((c = getopt( argc, argv, "s:f:h" )) != -1 ) { - switch(c) { - case 's': - essidflag = true; - strcpy(strSelectedESSID, optarg); - break; - case 'f': - fileflag = true; - strcpy(strFilename, optarg); - break; - case ':': - fprintf(stderr, "Option -%c requires an operand", optopt); - exit(2); - case 'h': - fprintf(stderr, "Usage:\n"); - fprintf(stderr, "-h\t\tShows this text.\n"); - fprintf(stderr, "-s <ESSID>\tShows only results for this essid\n"); - fprintf(stderr, "-f <FILE>\tParses from FILE\n"); - exit(0); - case '?': - fprintf(stderr, "Unrecognized option: -%c \n", optopt); - exit(2); - } - } - - parseFromFile( strFilename ); - - printBestAP(); - - return 0; - } - - // Reads from given stream (file or stdin) until linefeed is reached - // @param strFilename - Filename of the file to be opened. Empty if fileflag is false. - // - void parseFromFile( char * strFilename ) { - char buffer[LINE_BUFFER_LENGTH]; - FILE * pFile = 0; - - if( fileflag ) { - pFile = fopen( (const char*) strFilename, "r"); - if( pFile == 0 ){ - fprintf(stderr, "File %s could not be found!\n", strFilename); - exit(1); - } - }else{ +void parseFromFile(char *); +void parseString(char *); +void printBestAP(void); +void logEvent(const char *); + + +// Main routine. +// Some initializations & option parsing +// +int main(int argc, char * argv[]) +{ + essidflag = false; + fileflag = false; + + step = 1; + int c = 0; + + extern char *optarg; + extern int optopt; + + char pESSID[ESSID_LENGTH]; + char strFilename[FILENAME_LENGTH]; + + iCellNumber = 1; + memset(iSignalStrength, 0, MAX_CELLS); + memset(strEssid, 0, MAX_CELLS*ESSID_LENGTH); + memset(strMAC, 0, MAX_CELLS*MAC_LENGTH); + memset(pESSID, 0, ESSID_LENGTH); + memset(strSelectedESSID, 0, ESSID_LENGTH); + + while ((c = getopt(argc, argv, "s:f:h")) != -1) { + switch (c) { + case 's': + essidflag = true; + strcpy(strSelectedESSID, optarg); + break; + case 'f': + fileflag = true; + strcpy(strFilename, optarg); + break; + case ':': + fprintf(stderr, "Option -%c requires an operand", optopt); + exit(2); + case 'h': + fprintf(stderr, "Usage:\n"); + fprintf(stderr, "-h\t\tShows this text.\n"); + fprintf(stderr, "-s <ESSID>\tShows only results for this essid\n"); + fprintf(stderr, "-f <FILE>\tParses from FILE\n"); + exit(0); + case '?': + fprintf(stderr, "Unrecognized option: -%c \n", optopt); + exit(2); + } + } + + parseFromFile(strFilename); + + printBestAP(); - pFile = stdin; + return 0; +} + +// Reads from given stream (file or stdin) until linefeed is reached +// @param strFilename - Filename of the file to be opened. Empty if fileflag is false. +// +void parseFromFile(char * strFilename) +{ + char buffer[LINE_BUFFER_LENGTH]; + FILE * pFile = 0; + + if (fileflag) { + pFile = fopen((const char*) strFilename, "r"); + if (pFile == 0) { + fprintf(stderr, "File %s could not be found!\n", strFilename); + exit(1); } + } else { + + pFile = stdin; + } + + memset(buffer, 0, LINE_BUFFER_LENGTH); - memset( buffer, 0, LINE_BUFFER_LENGTH ); - - while( !feof(pFile) ) { - fgets( buffer, LINE_BUFFER_LENGTH, pFile ); - parseString( buffer ); - } - fclose( pFile ); - } - - // Split given string into tokens (split at spaces). - // Then analyse tokens for keywords like Cell, Address, Quality and ESSID: - // Save results to arrays. - // @param strBuffer - String to be parsed. - // - void parseString( char * strBuffer ) { - char * pTokens = strtok(strBuffer, " "); - while( pTokens != 0 ) { - - //Parse Cell ID - if(strcmp(pTokens, "Cell") == 0){ - if(step != 1){ - printf("In: Parse Cell, exit, step is %i", step ); - exit(1); - } + while (!feof(pFile)) { + fgets(buffer, LINE_BUFFER_LENGTH, pFile); + parseString(buffer); + } + fclose(pFile); +} - pTokens = strtok(NULL, " "); - iCellNumber = atoi(pTokens); +// Split given string into tokens (split at spaces). +// Then analyse tokens for keywords like Cell, Address, Quality and ESSID: +// Save results to arrays. +// @param strBuffer - String to be parsed. +// +void parseString(char * strBuffer) +{ + char * pTokens = strtok(strBuffer, " "); + while (pTokens != 0) { + + //Parse Cell ID + if (strcmp(pTokens, "Cell") == 0) { + if (step != 1) { + printf("In: Parse Cell, exit, step is %i", step); + exit(1); + } + + pTokens = strtok(NULL, " "); + iCellNumber = atoi(pTokens); #ifdef DEBUG - printf("Cell Nr. %i \n", iCellNumber); + printf("Cell Nr. %i \n", iCellNumber); #endif - step = 2; - pTokens = strtok(NULL, " "); - continue; - } - - //Parse AP's MAC - if(strcmp(pTokens, "Address:") == 0){ - if(step != 2){ - printf("In: Parse Address, exit, step is %i", step ); - exit(1); - } - pTokens = strtok(NULL, " "); - strncpy(strMAC[iCellNumber-1], pTokens, 17); + step = 2; + pTokens = strtok(NULL, " "); + continue; + } + + //Parse AP's MAC + if (strcmp(pTokens, "Address:") == 0) { + if (step != 2) { + printf("In: Parse Address, exit, step is %i", step); + exit(1); + } + pTokens = strtok(NULL, " "); + strncpy(strMAC[iCellNumber - 1], pTokens, 17); #ifdef DEBUG - printf("MAC Address %s \n", strMAC[iCellNumber-1]); + printf("MAC Address %s \n", strMAC[iCellNumber - 1]); #endif - strMAC[iCellNumber-1][17] = '\0'; - step = 3; - pTokens = strtok(NULL, " "); - continue; - } - - - //Parse AP Quality - char * strTmp; - char strQPos[ESSID_LENGTH]; - memset(strQPos, 0, ESSID_LENGTH ); - strTmp = strstr(pTokens, "Quality"); - if( strTmp != 0 ){ - if(step != 4){ - printf("In: Parse Quality, exit, step is %i", step ); - exit(1); - } - - strcpy(strQPos, strTmp); - char tmp[2]; - - //If SignalStrength is only one digit (/(=47) is a delimiter) - if( strTmp[1] == 47 ) { - tmp[0] = 48; //Fill in an ASCII '0' character - tmp[1] = strQPos[8]; - }else - { - tmp[0] = strQPos[8]; - tmp[1] = strQPos[9]; - } + strMAC[iCellNumber - 1][17] = '\0'; + step = 3; + pTokens = strtok(NULL, " "); + continue; + } - iSignalStrength[iCellNumber-1] = atoi(tmp); + + //Parse AP Quality + char * strTmp; + char strQPos[ESSID_LENGTH]; + memset(strQPos, 0, ESSID_LENGTH); + strTmp = strstr(pTokens, "Quality"); + if (strTmp != 0) { + if (step != 4) { + printf("In: Parse Quality, exit, step is %i", step); + exit(1); + } + + strcpy(strQPos, strTmp); + char tmp[2]; + + //If SignalStrength is only one digit (/(=47) is a delimiter) + if (strTmp[1] == 47) { + tmp[0] = 48; //Fill in an ASCII '0' character + tmp[1] = strQPos[8]; + } else { + tmp[0] = strQPos[8]; + tmp[1] = strQPos[9]; + } + + iSignalStrength[iCellNumber - 1] = atoi(tmp); #ifdef DEBUG - printf("Signal Strength: %i \n", iSignalStrength[iCellNumber-1]); + printf("Signal Strength: %i \n", iSignalStrength[iCellNumber-1]); #endif - step = 1; - pTokens = strtok(NULL, " "); - continue; - - } - - //Parse AP Essid - strTmp = strstr(pTokens, "ESSID:"); - if( strTmp != 0 ) { - if(step != 3){ - printf("In: Parse ESSID, exit, step is %i", step ); - exit(1); - } - - int j = 0; - while( strTmp[7+j] != 34 ) { - strEssid[iCellNumber-1][j] = strTmp[7+j]; - j++; - } - strEssid[iCellNumber-1][j] = '\0'; + step = 1; + pTokens = strtok(NULL, " "); + continue; + + } + + //Parse AP Essid + strTmp = strstr(pTokens, "ESSID:"); + if (strTmp != 0) { + if (step != 3) { + printf("In: Parse ESSID, exit, step is %i", step); + exit(1); + } + + int j = 0; + while (strTmp[7+j] != 34) { + strEssid[iCellNumber - 1][j] = strTmp[7 + j]; + j++; + } + strEssid[iCellNumber - 1][j] = '\0'; #ifdef DEBUG - printf("Essid: %s \n", strEssid[iCellNumber-1]); + printf("Essid: %s \n", strEssid[iCellNumber-1]); #endif - step = 4; - pTokens = strtok(NULL, " "); - continue; - } - - pTokens = strtok(NULL, " "); + step = 4; + pTokens = strtok(NULL, " "); + continue; } + + pTokens = strtok(NULL, " "); } +} - // Go (linear) through arrays and remember signal strength. - // Look only at specified SSID's if essidflag is set - // - void printBestAP( void ) { - //Select best SignalStrength for given AP (or all AP's) - int iIndexCell = 0; - int iBestStrength = 0; - int i; - int foundBest = false; - for( i = 0; i < MAX_CELLS; i++ ) { - if( strEssid[i] == 0 ) - continue; - - if( strncmp(strEssid[i], strSelectedESSID, strlen(strSelectedESSID)) == 0 || !essidflag ) { - - if( iSignalStrength[i] > iBestStrength) { - iBestStrength = iSignalStrength[i]; - iIndexCell = i; +// Go (linear) through arrays and remember signal strength. +// Look only at specified SSID's if essidflag is set +// +void printBestAP(void) +{ + //Select best SignalStrength for given AP (or all AP's) + int iIndexCell = 0; + int iBestStrength = 0; + int i; + int foundBest = false; + for (i = 0; i < MAX_CELLS; i++) { + if (strEssid[i] == 0) + continue; + + if (strncmp(strEssid[i], strSelectedESSID, strlen(strSelectedESSID)) == 0 || !essidflag) { + + if (iSignalStrength[i] > iBestStrength) { + iBestStrength = iSignalStrength[i]; + iIndexCell = i; #ifdef DEBUG - printf("Set index for best Cell to: %i\n", i); + printf("Set index for best Cell to: %i\n", i); #endif - foundBest = true; - } - } - } - - //Output - if(foundBest == true){ - printf("%s %s %d", strMAC[iIndexCell], strEssid[iIndexCell], iSignalStrength[iIndexCell]); - }else{ - printf("NONE"); + foundBest = true; + } } } - // Log a given message to a file (currently a fixed one). - // @param message - The message to be logged - // - void logEvent( const char * message ) { - FILE * pLogFile = 0; - char strTime[32]; - - sprintf( strTime, "%i", (int) time( NULL )); - pLogFile = fopen( "iwlist_parser.log", "a" ); - - fwrite( strTime, 1, strlen(strTime), pLogFile ); - fwrite( "\t", 1, 1, pLogFile ); - fwrite( message, 1, strlen(message), pLogFile ); - fwrite( "\n", 1, 1, pLogFile ); - fclose( pLogFile ); +//Output + if (foundBest == true) { + printf("%s %s %d", strMAC[iIndexCell], strEssid[iIndexCell], iSignalStrength[iIndexCell]); + } else { + printf("NONE"); } +} + +// Log a given message to a file (currently a fixed one). +// @param message - The message to be logged +// +void logEvent(const char * message) +{ + FILE * pLogFile = 0; + char strTime[32]; + + sprintf(strTime, "%i", (int) time(NULL)); + pLogFile = fopen("iwlist_parser.log", "a"); + + fwrite(strTime, 1, strlen(strTime), pLogFile); + fwrite("\t", 1, 1, pLogFile); + fwrite(message, 1, strlen(message), pLogFile); + fwrite("\n", 1, 1, pLogFile); + fclose(pLogFile); +}