1

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?

Fabian
  • 748
  • 2
  • 11
  • 20
  • I don't think using `sprintf(s,"%s%s",s, path);` like that to accumulate the lines is valid. Also `char delimiter[1] = "s"` is invalid as it has no room for the string terminator. Use `char delimiter[] = "s"` – William Morris Oct 28 '12 at 22:50

1 Answers1

1

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"
Aif
  • 11,015
  • 1
  • 30
  • 44
  • Thanks for your quick answer! Grep sounds like a good idea, but I dont really know the perfect command to get only the ssid as an result. Can you help me? For example: "iwconfig | grep Essid: | cut...".... – Fabian Oct 28 '12 at 23:08
  • @user1781595 done :-) Again, this is not a clean way... (even though I have no pointers for linux) – Aif Oct 29 '12 at 00:14