linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: François <fbeaulier@orange.fr>
To: Stephane Grosjean <s.grosjean@peak-system.com>
Cc: "linux-can@vger.kernel.org" <linux-can@vger.kernel.org>
Subject: Re: Is there any way of getting the CAN interfaces list, from any C program?
Date: Wed, 24 Apr 2013 23:02:09 +0200	[thread overview]
Message-ID: <51784851.1000208@orange.fr> (raw)
In-Reply-To: <5177A090.6010904@peak-system.com>

Stephane,

This is what i have done in a open source project i have started, a web 
can analyzer called ewecan (https://bitbucket.org/Mongo/ewecan):
It parses /sys/class/net but unfortunately, as noted in comments, it 
cannot make the difference between up or down interfaces.

/* Get a list of available socketcan interfaces and return them in
    a string as a JS array like ["can0", "can1", "vcan"]
    Returned string must be freed after use
    BUG : it also returns interfaces that are present but down !
*/
char *cmd_listitf(void)
{
     char *buf = malloc(255);
     if(!buf)
         return NULL;
     /* looking for socketcan interfaces in /sys */
     DIR *fdir = opendir("/sys/class/net");
     if(fdir == NULL){
         perror("opendir");
         free(buf);
         return NULL;
     }
     struct dirent *fic;
     int firstdone = 0;
     buf[0] = '[';
     buf[1] = '\0';
     while(1){
         fic = readdir(fdir);
         if(fic == NULL)
             break;
         if(!strstr(fic->d_name, "can"))
             continue;
         /* if the new string does not fit in buf, stop here */
         if((strlen(fic->d_name) + strlen(buf) + 4) >= 255)
             break;
         if(firstdone)
             strcat(buf, ",");
         strcat(buf, "\"");
         strcat(buf, fic->d_name);
         strcat(buf, "\"");
         firstdone = 1;
     }
     strcat(buf, "]");
     closedir(fdir);
     return buf;
}

Hope it can help !

Regards

--
François Beaulier
www.ingelibre.fr

Le 24/04/2013 11:06, Stephane Grosjean a écrit :
> Hi linux-can members,
>
> I currently wonder whether it exists any way of getting the list of 
> the installed CAN interfaces, from a C program?
> I mean, getting the network interfaces list is available using 
> ioctl(SIOCGIFCONF).
> But AFAIK, this (only) returns the IP interfaces found in the system, 
> even using a socket CAN file descriptor.
> And, I would also like to avoid some pipe/fork/grep/sed in this C 
> program too ;-)
>
> Thanks for your help and best regards,
>
> Stéphane
> -- 
> PEAK-System Technik GmbH, Otto-Roehm-Strasse 69, D-64293 Darmstadt 
> Geschaeftsleitung: A.Gach/U.Wilhelm,St.Nr.:007/241/13586 FA Darmstadt 
> HRB-9183 Darmstadt, Ust.IdNr.:DE 202220078, WEE-Reg.-Nr.: DE39305391 
> Tel.+49 (0)6151-817320 / Fax:+49 (0)6151-817329, info@peak-system.com
> ----
> To unsubscribe from this list: send the line "unsubscribe linux-can" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html


  parent reply	other threads:[~2013-04-24 21:10 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-13 19:35 [RFC PATCH v2] can: sja1000: use common prefix for all sja1000 defines Oliver Hartkopp
2013-04-16 10:40 ` Marc Kleine-Budde
2013-04-24  9:06   ` Is there any way of getting the CAN interfaces list, from any C program? Stephane Grosjean
2013-04-24 14:49     ` Kurt Van Dijck
2013-04-25  6:41       ` Wolfgang Grandegger
2013-04-25  7:07         ` Marc Kleine-Budde
2013-04-25  7:35           ` Marc Kleine-Budde
2013-04-25  9:15             ` Kurt Van Dijck
2013-04-25  9:20               ` Marc Kleine-Budde
2013-04-25  7:24         ` Kurt Van Dijck
2013-04-25  7:43           ` Wolfgang Grandegger
2013-04-25  9:14             ` Kurt Van Dijck
2013-04-25  9:23               ` Marc Kleine-Budde
2013-04-25  9:42                 ` Kurt Van Dijck
2013-04-25 21:08                   ` Kurt Van Dijck
2013-04-24 21:02     ` François [this message]
2013-04-24 21:27       ` Marc Kleine-Budde
2013-04-25  8:04       ` Kurt Van Dijck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=51784851.1000208@orange.fr \
    --to=fbeaulier@orange.fr \
    --cc=linux-can@vger.kernel.org \
    --cc=s.grosjean@peak-system.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).