Wednesday, June 24, 2015

C Programming: Pitfalls while word counting

In Earlier post "Count number of characters, words and lines in input", we have seen how to count number of characters,words and lines. Today, we will learn about pitfalls during word counting.

In K&R, we have a separate exercise for this. Exercise 1.14 states "How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?"

While writing this program earlier, we never handled much cases. There are caveats which needs to be identified and addressed.

First lets list what all possible checks we usually need while word counting.

1. Checking for very short words.
2. Check for very lengthy words.
3. Check for words which separated when new line is encountered. For example kernel
trap. Where kernel is at end of a line and trap follows in next line.4. Considering words like "isn't", "tour's" as single words
5. Check overall files size for size less than 2GB.
6. Check for mistyped words like "kernel  -  trap" which contain spaces in middle or an - instead of space ex.kernel-trap.
7. Check of non ASCII characters
8. Check for different encoding

Please shed your thoughts if I have missing any checks.

Monday, June 1, 2015

C Program : Count number of characters, words and lines in input

Earlier, we have seen "C : Program to display tabs , backspaces visible in an unambiguous way", today we will write a small C program which counts number of characters ,  number of words and number of lines.

Program looks easy when they ask to count lines and numbers but how about words.

If input to stdin is character by character, then we can count characters easily. Also using '\n' , we can identify that line has encountered. Only problem is counting words.

So, we will use a mechanism to find if word has encountered. Lets introduce two states IN and OUT which states currently process word and the other says its out of word respectively.

 #include  
   
 #define IN 1  
 #define OUT 0  
   
 int main()  
 {  
   int c, nl, nw, nc, state;  
   
   state = OUT;  
   nl = nw = nc = 0;  
   
   while((c = getchar()) != EOF)  
   {  
     /* Increment number of characters */  
     ++nc;  
   
     /* Increment number of lines if end of line is encountered */  
     if( c == '\n' )  
     {  
       ++nl;  
     }  
     /* Anything other than character, mark it new word */  
     if( c == ' ' || c == '\n' || c == '\t' )  
     {  
       state = OUT;   /* Its just completed processing a word */
     } /* if new word, increment word count */  
     else if ( state == OUT )  
     {  
       state = IN;  
       ++nw;  
     }  
   }  
   printf(" Number of Characters = %d \n",nc);  
   printf(" Number of lines = %d \n",nl);  
   printf(" Number of Words = %d \n",nw);  
   return 0;  
 }  
   

Output of this program

 mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out   
 This program counts  
 number of characters  
 number of words  
 number of lines  
  Number of Characters = 73   
  Number of lines = 4   
  Number of Words = 12   

In this program

  • We read input character by character 
  • First we increment the character (nc)
  • We then check if it is a new line. If new line, we increment the character
  • Now to increment word (collection of characters), we need to do multiple checks which signify end of word. 
  • If its new line or tab or space, we consider that as word and increment word
  • Finally when you press Ctrl+D (in Linux), the program will display the number of characters, number of lines and number of words.
Hope this helped :)


Monday, May 25, 2015

C : Program to display tabs , backspaces visible in an unambiguous way

In my earlier post, we have seen "C : Program to replace multiples spaces with a single space" , today lets modify the same code to display tabs, backspaces.

Below is the program which does this job.
 #include   
 int main()  
 {  
   char c;  
   printf("Enter a line to display tabs \n");  
   while((c=getchar()) != '\n')  
   {  
     if(c == '\t')  
     {  
         putchar('\\');  
         putchar('t');  
     }  
     putchar(c);  
   }  
   printf("\n");  
   return 0;  
 }  
In above program,
  • We read input character by character
  • Check if its tab (\t). If tab, we output (\\) followed by (t) character which displays \t.
  • If its not tab, we simply output the character.
Output of this program,
 mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out   
 Enter a line to display tabs   
 This  program     displays     the tabs     entered  
 This \t     program\t     displays\t     the tabs\t     entered  

Thursday, May 21, 2015

C : Program to replace multiples spaces with a single space

Earlier we have seen C program to count blanks and tabs , with using the same code, lets try to write another program which takes a line of words/characters and parses through it checking for multiple spaces and replaces them with single space.

For example, in  below line
This     line has      multiple        spaces    between     words.

Ideally while typing a sentence we leave a single space between word-to-word.  This sentence has multiple spaces which can be removed and replaced with single space by our program below.

This line has multiple spaces between words.
  #include <stdio.h>   
  int main()   
  {   
   int c;   
   printf("Enter a line to replace multiple spaces with single space\n");   
   /* Read character by character and prase till end of the file */  
   while((c=getchar()) != '\n')   
   {   
    if(c == ' ')   
    {   
     /* Notice semi-colon at end of below line. It reads a character and if its space  
      it discards it and read character again till its not a space*/  
     while( (c=getchar()) == ' ');   
      /* Now just output single space */  
      putchar(' ');   
    }   
    putchar(c);   
   }   
   printf("\n");   
   return 0;   
  }   
From above code,
  • Using while loop, we start reading character by character with getchar()
  • First character is read and checked for end of the line
  • If not End of the line ('\n'), check if it's a space
  • If its a space, discard spaces by reading them in a while loop.  Note semi-colon at end of the while loop.
  • Now output a single space and read next character.
  • Finally print a new line and return.
Output of this program,
 mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out   
 Enter a line to replace multiple spaces with single space  
 This program  replaces       multiple spaces    with   single  space  
 This program replaces multiple spaces with single space  


Monday, May 18, 2015

C : How to print line numbers and function names while debugging C programs

While debugging C program, we often add printf's to print some values or just a debug print. It would be additional advantage if you can print line number and function name when the program is too big to debug. If you are dealing with multiple files, its good idea to print the file name when you use printf isn't ?

In C, there are few preprocessor macros which allow you to print line numbers, function names and files names.

__LINE__           : Prints line number
__FUNCTION__: Prints function number
__FILE__            : Prints file number

These are predefined macros and part of the C/C++ standard. During preprocessing, they are replaced respectively by a constant string holding an integer representing the current line number and by the current file name.
 #include <stdio.h>  
   
 int main()  
 {  
   printf("\n This program prints line number %d\n",__LINE__);  
   printf("\n This program prints function name %s() \n",__FUNCTION__);  
   printf("\n This program prints file name %s\n",__FILE__);  
   return 0;  
 }  

Output of this program:
 mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out   
   
  This program prints line number 5  
   
  This program prints function name main()   
   
  This program prints file name print_line_function_file_name.c  
   

Others preprocessor variables :

__func__ : function name (this is part of C99, not all C++ compilers support it)
__DATE__ : a string of form "Mmm dd yyyy"
__TIME__ : a string of form "hh:mm:ss"

C : Program to count spaces and tabs

In earlier post "Character counting in C", we have written a program to count number of characters, lets extend this further and count blanks, tabs and newlines.
 int main()   
 {   
   double char_count;   
   char input_char;   
   int tabs_count=0,spaces_count=0;   
   char input_val;   
    
   printf("\n Input a line to count spaces and tabs \n");   
    
   /* Reading a character */   
   input_char = getchar();   
    
   /* In a for loop read characters till end of line is encountered */   
   for(char_count = 0; input_char != '\n'; ++char_count)   
   {   
     /* Check if input character is a space */     
     if(input_char == ' ')   
     {   
       /* if space, increment space variable count */          
       ++spaces_count;     
     }      
     /* else if input character is a tab */   
     else if(input_char == '\t')   
     {   
       /* if tab increment tab count */             
       ++tabs_count;      
     }   
     /* Read next character to check if its space or tab */   
     input_char = getchar();       
   }   
    
   /* Finally print number of spaces and tabs read */   
   printf("\n Your input contains %d spaces, %d tabs \n",spaces_count,tabs_count);   
    
   return 0;    
 }  
   
In above program,
  • We read a character
  • In a for loop till end of line is encountered, we check each character entered is a space (' ') or tab('\t').
  • Increment spaces_count and tabs_count variables accordingly
  • Finally when new line is encountered, we print the space count and tab count.
Output of this program :
 mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out   
   
  Input a line to count spaces and tabs   
 will be giving a tab now     just one tab behind  
   
  Your input contains 9 spaces, 1 tabs  

Linux : How to recursively touch files and folders in a directory

Guys today we will learn a small tip about touching multiple files / folders in a folder using Linux Terminal.

I assume you know about "touch" command in Linux if not please make sure you read its man page here.

You need to use below command to touch files/folders recursively.
 find . -exec touch {} \;  

Execute this command in the director in which you would like to perform this operation.

This command,

  • Finds each file
  • exec is used to invoke a subprocess "touch"
  • Curly bases and backward slash ending with semicolon as per syntax
Hope you find this tip useful.

Thursday, May 14, 2015

Character counting in C using for loop !!!

Yesterday we learnt about "Character Counting in C"  using while loop. Today in order to illustrate the same character counting in C, we will trying using for loop. We will also try to use float or double datatype to store the character count.

Lets get started ....
 #include <stdio.h>  
 int main()  
 {  
   double char_count;  
   printf("\n Input characters to count \n");  
   for(char_count = 0; getchar() != '\n'; ++char_count);  
   printf("\n You have typed %.0f character :) \n",char_count);  
   return 0;  
 }  

Above program,

  • Declares char_count as doube instead of int to make sure that the count is not overflow while taking the input. double takes relatively more time to overflow than int.
  • Requests user to input characters
  • Reads character by character using getchar() till end of the line
  • As you can see, there is no body for for loop here because whole operation of character counting is done in for loop itself. This is the beauty of for loop.
  • for loop and while loop they check the condition first and enter their body to execute the code which provides the programmer more flexibility of programming.
  • Once a line is entered, program exits by printing the character count.
  • One important thing to note here while printing is "%.0f",  usually printf uses %f for printing float or double. Since char_count is decalared as double, we use "%.0f" to supress the decimal point  and fraction part which is zero.
Now output of this program is..
 mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out   
  Input characters to count   
 This is single line of input  
  You have typed 28 character :)   


It's very simple but very useful snippet while coding !!

Wednesday, May 13, 2015

Character Counting in C

Here we are taking baby steps in c. Lets learn how to count the number of characters entered through stdin (standard input like keyboard).

We will try to code the program similar to my earlier posts.
 #include <stdio.h>  
 int main()  
 {  
   int c,count=0;  
   printf("Start your input \n");  
   c = getchar();  
   while(c != '\n')  
   {  
     putchar(c);  
     c = getchar();  
     ++count;  
   }  
   printf("\n Number of characters entered = %d\n",count);  
 }  

Though we can code above snippet in much simpler form, I have used my earlier programs code to count the number of characters in c.

From above program,
  • In a while loop, we take input from stdin
  • output the characater to stdin
  • read character
  • pre-increment the count value
  • Continue in the while loop till new line
  • When new line is encountered, we print the stdout and number of characters
Output of this program is below
 mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out   
 Enter Ctrl+C is to exit  
 Start your input   
 just a single line input  
 just a single line input  
  Number of characters entered = 24  
Note that we are printing the characters and number of characters when new line or carriage return is encountered.

About EOF (End of file) in C

Have you ever wondered what is EOF in "Exiting c program right after reading one line or after carriage return while reading input from stdin".

EOF (End of the file) is a macro which expands to an integer constant expression with type int and an implementation dependent negative value but is very commonly -1.

Below is a small snippet to print EOF in c.
 #include <stdio .h="">  
 int main()  
 {  
 printf("\n Value of EOF is %d\n",EOF);  
 return 0;  
 }  

Output of this program is:

 mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out   
  Value of EOF is -1  

Exiting c program right after reading one line or after carriage return while reading input from stdin

Yesterday, we learnt about "C program that copies its input to its output one character at a time" , if you observe the program mentioned in this post, you will observe that we are taking the input from stdin (standard input) and outputting to stdout (each line) till EOF (End of the file) is reached.

Ideally stdin EOF is like forever. So, we end up pressing Ctrl+C to exit the program. Now to modify this program to exit right after one line or after a carriage return, we need to do a simple modification.
Replace EOF with '\n'. Yes its as simple as that.

 #include <stdio.h>  
 int main()  
 {  
   int c;  
   printf("Program exits after new line or carriage return \n");  
   printf("Start your input \n");  
   c = getchar();  
   while(c != '\n')  
   {  
     putchar(c);  
     c = getchar();  
   }  
 }  

Only difference you observe is in the while condition, "while(c != EOF) is changed to while(c != '\n')".

Output will be as below.

 mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out   
 Enter Ctrl+C is to exit  
 Start your input   
 just one line input  
 just one line input  
Hope you understood this simple concept !!!

Tuesday, May 12, 2015

C program that copies its input to its output one character at a time

I have been blogging few interesting how-to articles which I learn in my daily professional life. Its been a while I haven't blogged any.So, thought of just adding atleast a program per day (very challenging isn't ?) along with how-to articles.

I will start with C-programming. As everyone know "Kernighan & Ritchie" (K&R) is the best book to learn C programming. I will using examples in this book.


If you want any C snippet meanwhile, just drop me a comment will try and get it done for you.  I cant promise, but will try my best to do code for you.


Frist lets start with "C program that copies its input to its output one character at a time"


 #include <stdio.h>  
 int main()  
 {  
   int c;  
   printf("Enter Ctrl+C is to exit\n");  
   printf("Start your input \n");  
   c = getchar();  
   while(c != EOF)  
   {  
     putchar(c);  
     c = getchar();  
   }  
 }  

Above program is exact copy of a program in K&R, I have just added two printf statements to it.

When you compile and run this program, it accepts whatever the character you input from the keyboard and print them once you press enter (new line / carriage return).

It does this task forever because we have used "While( c != EOF )" in our above code snippet.
This while statements will be true till you reach EOF of stdin (standard input) which is forever.

So, in order to exit from running your program, you need to press "Ctrl+C" on your keyboard together.

Now lets run and see, output will be as below
 mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out   
 Enter Ctrl+C is to exit  
 Start your input   
 we are  
 we are  
 trying to print  
 trying to print  
 character by character  
 character by character  
 ^C  
Hope you learnt something new :).

Set auto Indentation , shiftwidth and tab stop in Vim forever !!!

If you are looking for enabling auto indentation and to set tabstop to x characters, you are in right  place.

Open your .vimrc under your "/home/<user>" directory and add below lines
 filetype plugin indent on  
 set tabstop=4  
 set shiftwidth=4  
 set expandtab  
In above settings we have set 4 as tabstop and shiftwidth.
Now open a file with vim and your indentation and tabstop will work as expected.

Wednesday, April 15, 2015

How Linux Memory is Managed - Part 1

To make virtual memory concept simple, we need to remember few basic ground rules.

1. Linux visualizes its physical resource of memory
2. Process never ever directly address physical memory.
3. Instead Kernel associates each process with unique address space.

Memory Management happens in Linux using Page and Paging.

Kernel allocates unique Virtual Address Space to each Process. This Virtual Address Space is composed of Pages.

Two type of Pages we have
1. Valid  Page : Its associated with Physical Page or Secondary storage like disk.
2. Invalid Page : Its unused, unallocated piece of  Address space.

A program cannot use a page in secondary storage. It can use the page if it is associate with a page in physical memory.

Program -> Tries to access a Page on Secondary Storage -> MMU  generate Page fault->  Kernel does Page-in the desired page from secondary storage to Physical memory.

Imagine a Process tries to access a page on secondary storage. 

Since the page is not present, page fault occurs.

The Kernel intervenes and does Page-in the desired page to Physical memory from Secondary storage.


Interrupts and Interrupt handlers

If you are an embedded engineer, you might be dealing with interrupts in your daily work. Though its simple concept, its thought complex.

To put them in layman words, interrupts allow hardware to communicate with processor which in turn informs the Operating System to perform certain task.

Harware -> Interrupt -> Interrupt controller -> Processor -> Operating System ->  Serve Interrupt. 

Interrupt is physically produced by the hardware and directed into input pins on an Interrupt controller.

Once the interrupt is directed towards Interrupt controller, it informs the Processor.

Processor then notifies the underlying Operating System to handle the interrupt appropriately.

Operating system serves each Interrupt with unique handler and distinguishes each interrupt with its unique id.

Just remember above sequence, and it will help you during technical discussions.

Saturday, April 11, 2015

How to configure vi/vim for coding C - Indentation and file detection

How do we make vi-Vim not to use tab and use 4 spaces instead ?
How to make it  automatically indent code after curly brace blocks like Emacs does?
How to save these settings so that we never have to input them again?

I've seen other questions very often from vi/vim users.
 
Its pretty simple, add below lines to your .vimrc file under your home directory.

 filetype plugin indent on  
 set tabstop=4  
 set shiftwidth=4  
 set expandtab  

More information under :  filetype

Wednesday, April 8, 2015

Missing side bar, Missing Menu bar , Missing icons and Missing "close resize and minimize icons" in Ubuntu 14.04 64bit LTS

This issue was a nightmare. I have tried various solutions to solve this issue but nothing worked. I tried reinstalling unity, resetting unity, reinstalling compiz packages etc and ended up without solving the issue.

Many solutions available in net are for 12.04 , 12.10 etc versions but not for Ubuntu 14.04 64 bit version.

Before trying this solution, make sure you have taken backup of your valuable data. I didn't loose any data but on safe side, its always good to take backup before trying which we are not sure about it.

To solve this issue just change your directory to your home directory.

Press Ctrl+Alt+F1 , enter your username and password.

 cd <home-directory>  
 sudo rm -rf .config  
 sudo rm -rf .compiz/compiz-1  

That's it, reboot the machine and now you should see icons back on the screen :)

Thursday, March 19, 2015

Remove all files in a directory except one or two

In Linux, have you ever wondered what is the command to execute in terminal to delete all files in a directory except one or two ? Its,

 find ! -name 'file.txt' -type f -exec rm -f {} +  
 
This command will remove all files except file.txt.

To remove directory, change type -f to type -d and add -r option to rm as below.

 find ! -name 'file.txt' -type d-exec rm -rf {} +  

Please becareful before executing command. If you accidentally type in an important folder, you may loose all contents.

Always try to understand to learn about each term before executing it :).

How to open chm files in Ubuntu 14.04. Which package to install.

I have been searching for package(s) to install to open chm files. Below command worked like magic.

 sudo apt-get install xchm  

While install press Y. After installation,click on any  *.chm file it will open automatically.

Sunday, March 1, 2015

Building Kernel Source

This article just briefs about downloading and installing Linux Kernel Source.
Of-course, there are many articles available in net. I would like to make this as simple as possible.

Steps to follow

1. Install Git
 sudo apt-get install git  

2. Download Linux Source (in here I am downloading 2.6 version) in to a directory
 $ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git 
 $ git pull 

3.Configure your Kernel by executing
 $make defconfig  
 $make oldconfig  

Some times, we can use "$make  menuconfig" a graphic utility to configure Kernel. You may face fatal error problem while executing this command.

4.Now build your kernel
 $make  

This may take a while. So relax, we have done a lot till now :)

To improve Kernel build time you can use
 $make  -jn

Here, n is the number of jobs to spawn. Usual practice is to spawn one or two jobs per processor. For example, on a 16-core machine, you might do
$ make -j32 > /dev/null

Using utilities such as the excellent distcc or kernel build time. ccache
can also dramatically improve  Kernel build time.

5. Now once the build is done, we need to find for bzImage
 mrtechpathi@mrtechpathi:~/Study/linux-source/linux-2.6$ find . -name bzImage |xargs ls -l  
 lrwxrwxrwx 1 mrtechpathi mrtechpathi   22 Mar 1 23:16 ./arch/x86_64/boot/bzImage -> ../../x86/boot/bzImage  
 -rw-rw-r-- 1 mrtechpathi mrtechpathi 5970016 Mar 1 23:16 ./arch/x86/boot/bzImage  

6.Copy this image to /boot directory as vmlinuz-2.6
 mrtechpathi@mrtechpathi:~/Study/linux-source/linux-2.6$ sudo cp ./arch/x86/boot/bzImage /boot/vmlinuz-2.6  

7.Install modules
$ sudo make modules_install

8.Now update grub using
$ sudo update-grub

Sometimes,  your grub may disappear or may not appear. To solve this check this post "Grub menu is not displayed after installing Linux".

Grub menu is not displayed after installing Linux (ubunt 14.04)

We often face an issue where Grub menu disappear after installing a new operating system.

By default, your GRUB is set to hide the OS selection menu on boot.

Basically, what you'll need to do is make the following change:

1. Open up a terminal (ctrl+alt+t) and then enter in the following command:   

$ sudo vim /etc/default/grub

2. Edit the file and comment below two lines (adding # before the lines)

GRUB_HIDDEN_TIMEOUT=0 
GRUB_HIDDEN_TIMEOUT_QUIET=true

becomes

#GRUB_HIDDEN_TIMEOUT=0
#GRUB_HIDDEN_TIMEOUT_QUIET=true

Save the file

3. Update the grub

$sudo update-grub

When you reboot you should see the OS Selection Menu.

Fatal error: curses.h: No such file or directory while configuring Kernel Source

While working on Kernel source, during configuring Kernel options, we  may need to use "make menuconfig" command.

We may end up with following error.

 mrtechpathi@mrtechpathi:~/Study/linux-source/linux-2.6$ make menuconfig  
  HOSTCC scripts/kconfig/mconf.o  
 In file included from scripts/kconfig/mconf.c:23:0:  
 scripts/kconfig/lxdialog/dialog.h:38:20: fatal error: curses.h: No such file or directory  
  #include CURSES_LOC  
           ^  
 compilation terminated.  
 make[1]: *** [scripts/kconfig/mconf.o] Error 1  
 make: *** [menuconfig] Error 2  

To overcome this, execute below steps

1. $ sudo apt-get update
2. $ sudo apt-get install libncurses5-dev
3. $ make menuconfig -> Works fine now !!!!

Following are my system configurations.
 No LSB modules are available.  
 Distributor ID:     Ubuntu  
 Description:     Ubuntu 14.04 LTS  
 Release:     14.04  
 Codename:     trusty  

Let me know if it worked for you :)

Accessing a Windows shared folder in Oracle VM Virtual Box

This post gives steps to access  a shared folder when you use Oracle VM Virtual box to run a Linux Virtual system on your Windows PC.

1. Make sure you have guest addons installed.notepad

2.Create a folder on the Host computer (ubuntu) that you would like to share, for example "C:/KernelTrap"

3.Boot the Guest operating system in VirtualBox.

4.Select Devices -> Shared Folders...

5.Choose the 'Add' button.

6.Select Folder path name as   "C:/KernelTrap"
   and Folder name will be automatically detected as "KernelTrap"

7. Remember this folder name "KernelTrap"

8. Optionally select the 'Make permanent' option

9. In virtual machine terminal, type:

 cd /mnt  
 sudo mkdir shared_folder  
 sudo mount -t vboxsf KerekTrap share_folder_patch 

And that's it. Don't worry about the absolute paths. You don't need them. For all practical purposes, VB will take care of the real path behind the share name, in this case KernelTrap.

Let me know if this procedure worked for you. I have tried this on Ubuntu 14.04 and it worked without any issue.