parsing ssid with iwconfig in c

2.7k Views Asked by At

I am about building a bar for DWM (ubuntu linux), showing wifi details such as the ssid.

Thats my code:

#include <stdio.h>
#include <stdlib.h>


int main( int argc, char *argv[] )
{

  FILE *fp;
  int status;
  char path[1035];

  /* Open the command for reading. */
  fp = popen("iwconfig", "r");
  if (fp == NULL) {
    printf("Failed to run command\n" );
    exit;
  }
   char s[500];

  /* Read the output a line at a time - output it. */
  while (fgets(path, sizeof(path)-1, fp) != NULL) {
    sprintf(s,"%s%s",s, path);
  }
    //printf("%s",s);
  /* close */
  pclose(fp);


    char delimiter[1] = "s";
    char *ptr;

    ptr = strtok(s, delimiter);

        printf("SSID: %s\n", ptr);


  return 0;
}

i am getting overflowerrors and dont know what to do. I dont think, thats a good way to get the ssid either... :/ Suggestions?

1

There are 1 best solutions below

2
On

I would rather use direct information from the kernel (such as netdevice(7)) rather than calling a sub-process.

Maybe this header can help: http://lxr.free-electrons.com/source/include/linux/wireless.h

Edit: if you still want to use popen, which don't you just add a | grep Essid: ?

$ /sbin/ifconfig 2>/dev/null | grep ESSID | cut -d: -f2
"pink-panter"