Thursday, 27 August 2015

'C' multi-buffer tokeniser (reentrant version)


char *strtok_r(char *str, const char *delim, char **saveptr);

The strtok_r() function is a reentrant version strtok(). The saveptr argument is a pointer to a char * variable that is used internally by strtok_r() in order to maintain context between successive calls that parse the same string.

strtok_r() is used almost exactly as strtok() except with additional last parameter char ** saveptr is passed. This is used internally, to save of buffer state.


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


int main() {
  char buffer1[50];
  char buffer2[50];
  char *save1, *save2, *token1, *token2;

  strcpy(buffer1, "A-B-C-D");
  strcpy(buffer2, "1:2:3:4");

  token1 = strtok_r(buffer1, "-", &save1);
  token2 = strtok_r(buffer2, ":", &save2);

  while(token1 != NULL ||
        token2 != NULL) {
     printf("[%s%s]\n", token1, token2);
     token1 = strtok_r(NULL, "-", &save1);
     token2 = strtok_r(NULL, ":", &save2);
  }
  return 0;

}


Wednesday, 26 August 2015

Writing Linux Kernel module

  lsmod – List all loaded kernel modules




Sample kernel module

hello_world.c module in 'C'
 


Compiling kernel module
 
make -C /lib/modules/<`uname -r`>/build M=<path_to_hello_world.c file> modules
 
#on successful compilation : hello_world.ko file will be generated

 Adding /inserting module 

 # loading the module
insmod hello_world.ko

# checking whether module is loaded.
dmesg |grep

# checking using lsmod
lsmod | grep hello

Total Pageviews

Popular Posts