Using the Mac Address of Network Interfaces in programs is a common requirement. The old age way of getting the mac address programatically on linux was to parse the not-so-machine-friendly ifconfig output. However with the sysfs file system mounted under /sys, accessing information from kernel sub-systems has become very easy.

e.g. The Mac Address of eth0 interface can be accessed by reading the /sys/class/net/eth0/address file.


The code below is an example of how to use parse this information from within a C program.

#include <stdio.h>
#include <string.h>
#include <stdint.h>

int mac_get_ascii_from_file(const char *filename, char *addr) {
  FILE *fp;
  int i = 0;
  char c;
  fp = fopen(filename, "r");
  if (fp != NULL) {
    while (!feof(fp)) {
      c = fgetc(fp);
      if (c == ':')
      if (c == '\n')
      addr[i++] = c;
  return 0;

int mac_get_binary_from_file(const char *filename, uint8_t * mac) {
  int status = 1;
  char buf[256];
  FILE *fp = fopen(filename, "rt");
  memset(buf, 0, 256);
  if (fp) {
    if (fgets(buf, sizeof buf, fp) > 0) {
      sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &mac[0],
             &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
      status = 0;
  return status;

int main(void) {
  char macaddr[13];
  uint8_t mac[6];
  memset(macaddr, '\0', 13);
  mac_get_ascii_from_file("/sys/class/net/eth0/address", macaddr);
  printf("My Mac Address - %s\n", macaddr);
  mac_get_binary_from_file("/sys/class/net/eth0/address", mac);
  printf("My Mac Address - %hhx:%hhx:%hhx:%hhx:%hhx:%hhx\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  return 0;

The sample output of the above program is shown below.


This post is part of a series of posts where I want to document a bunch of C functions I use for my projects that I have written, collected and modified over the past few years. One of the functions that I use regularly is a string replacement function like the str_replace in PHP. This function replaces all occurences of a particular substring with a replacement pattern. The code for this was borrowed from here. In the original code, a newly allocated string is returned after all the substrings are replaced. I have modified it so that the replacements are made in place.Continue reading

A lot of programs require you to pass sensitive information like passwords etc as arguments to the program. However if you pass passwords as arguments, it will be visible through the /proc filesystem or the ps ax output while the program is in execution. To avoid the possibility of anybody prying on sensitive information, programmers should modify the memory location where the input parameters are stored (argv array), so that it is not visible to any other users, who might have the access levels to see what processes you’re running. Jotted below is some sample code which modifies its input parameters to hide it from the proc file system.Continue reading

Here’s a code snippet which will let you rotate multiple banner advertisements for all the ad spaces you have on your HTML pages. I wrote this because I didn’t want to use an ad server and wanted a light weight yet elegant solution. What the below code does is, it creates a SSI variable ‘slot’ which can have possible values of 1-12. Each value corresponds to a 5 second slot. Source this file at the start of your HTML file and then use the slot variable anywhere in your HTML code to rotate data according to time based slots.Continue reading

I used to display my latest tweet using PHP code as part of my WordPress theme’s function.php. However that did not work reliably and used to slow down page loading a wee bit. Hence I decided to generate the html code for the latest tweet as a background/cron process and just read the generated data in WordPress. Here’s how I do it.Continue reading

There’s a website dedicated for open source code in various programming languages to detect mobile browsers. However they didn’t have any code to detect a mobile browser using SSI. Thats what I needed to make the HTML pages on my website mobile friendly. Using the <!--#if expr SSI conditional expression, I wrote up a small code snippet to detect the most common smart phones and set a variable when a mobile phone is detected. I use this variable in other parts of HTML to control what is show and what is not to make it suitable for a mobile browser.Continue reading

If you know the name of a variable which contains a value of interest, you need indirect referencing to retrieve the value, this is how you do it.


j=1 #variable containg a value
i=j #variable containing the name of the variable which holds the value
eval k=\$$i #k now contains the value of j which is 1
echo $k
k=${!i}  #another way of indirectly referencing
echo $k