Thursday, June 18, 2009

A big sigh of relief

After 1.5 years, finally launchpad team has fixed the bug which I had logged into launchpad itself.

Well, I cannot explain the feeling of relief you get, when you see the bug reported by you marked as fixed!

Time for a nice dinner and some sleep.. drink!

Links:

  • https://bugs.launchpad.net/launchpad-registry/+bug/155334

Tuesday, May 12, 2009

C Language: Program to check given character string for palindrome

A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction. Eg: Katak, 121 etc

Following C program shows the simple way check, if the given character string is a palindrome or not.

Following code is available under GNU General Public License v3 or above.

/*
 * Copyright (c) 2009 Raghavendra Nayak
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/*
 * C program to reverse given string, and to check whether it is a palindrome.
 */

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

/**
 * @brief function to return the given string length.
 * @param[in] string the input string.
 * @return the string length.
 */
unsigned int get_string_length(const char *string)
{
    int i;
    int string_length = 0;

    for (i = 0; string[i] != 0 && string[i] != '\n'; ++i)
        ++string_length;
    return string_length;
}

/**
 * @brief function to reverse the given string.
 * @param[in] string the input string.
 * @param[out] rev_string the reversed string would be placed in this variable.
 * @return none.
 */
void reverse(const char *in_string, char *rev_string)
{
    int i, j;
    unsigned int length;

    length = get_string_length(in_string);

    for (i = length-1, j = 0; i >= 0; --i, ++j)
        rev_string[j] = in_string[i];

    /* mark the end of string */
    rev_string[j] = 0;
}

int main()
{
    const int max_size = 100;
    int string_length = 0;

    char string[max_size];
    char reverse_string[max_size];

    printf("\nPlease enter input string: ");
    /* Note: we are not using gets, because gets is deprecated and can be
       dangerous. */
    fgets(string, max_size, stdin);

    /* remove any new line from input string,
       if fgets has put '\n' at the end of string */
    string_length = get_string_length(string);
    string[string_length] = 0;

    /* get the reversed string */
    reverse(string, reverse_string);

    /* now compare */
    if (!strcmp(string, reverse_string)) {
        printf("\nEntered string = %s, reversed string = %s."
               "\n%s = %s, hence %s is a palindrome.\n",
               string, reverse_string, string, reverse_string, string);
    } else {
        printf("\nEntered string = %s, reversed string = %s."
               "\n%s != %s, hence %s is not a palindrome.\n",
               string, reverse_string, string, reverse_string, string);
    }
    return 0;
}

Links:

Sunday, April 19, 2009

How To: Create your own local SVN repository in GNU/Linux

This article tell you about creating your own local SVN repository in your GNU/Linux box.

Even though I personally like git, I like the SVN's idea of having all your code safe somewhere in a remote server!

I personally do a lot of coding, and several times I have lost my precious programs/codes due to some stupid mistakes I did or because I don't backup my code very often. Keeping a plain backup your code is a pain, having you to maintain several copies of them, based on their version.

Hence, several times I felt that, oh God, if at all I had my own SVN server somewhere?

So, today finally tried to create my own SVN server, and voila, in less than 5 minutes, my own SVN server was up and running! I never had thought that creating your own SVN server was a such easy task.

If you people are looking for the same, this article is for you!

Here is a step by step procedure to create your own SVN server.

  1. Install SVN package.

    You need to have subversion package installed.

    If you don't have it, install it using command

    sudo apt-get install subversion
  2. Create a SVN repository.

    Next step is to create a SVN repository. First decide where you want to store your repository data? I kept my SVN repository in my home directory, so that it also gets backed up with my home directory backup. So, You can also do the same or store your SVN repository in any other place of your choice.

    Use following command to create your own SVN repository in your home directory.

    svnadmin create ~/my_svn_repo
    This command will create a directory named 'my_svn_repo in' your home directory, with SVN's repository data inside it.

    If you want to store your SVN repository somewhere else, replace

    '~/my_svn_repo'
    with your selected directory path.

  3. Change the SVN configuration for repository access.

    You need to configure the SVN to tell it about the read and write access to your repository.

    For this goto 'conf' directory present inside your SVN repository directory.

    cd ~/my_svn_repo/conf
    now open the file named svnserve.conf

    vi svnserve.conf
    OR

    gedit svnserve.conf
    and edit the file.

    un-comment following lines (remove # from the beginning of the line.)

    anon-access = read
    auth-access = write

    These lines tell about the read and write access to your SVN repository. 'anon-access = read' means anonymous users can read your SVN data.

    The possible values are 'read', 'write' and 'none'. The default value 'read' should be OK, as its a local repository. However, if your machine is in a network, you may want to change it to 'none' . ie;

    anon-access = none
    The second line 'auth-access = write' tells about the access rights of the authorized users. The default value of 'write' should be OK and it means authorized users can read/write back the data to the SVN repository.

    Below are some possible configurations;

    • The default configuration

      # anonymous users can only read the repository
      anon-access = read

      # authenticated users can both read and write
      auth-access = write

    • Restricted configuration

      # anonymous users aren't allowed
      anon-access = none

      # authenticated users can both read and write
      auth-access = write

  4. Configure the Authorization.

    un-comment following lines from the 'svnserve.conf' file opened in previous step.

    password-db = passwd
    This tells the SVN to read the user name and password from the file named 'passwd'.

  5. Setup the username and password.

    Open the file name 'passwd' present inside 'conf' directory of your SVN repository directory.

    Now un-comment the following lines.

    harry = harryssecret
    sally = sallyssecret

    This line tells the authorized user's name ans password. You can also add your username and password to this list. like

    raghu = mypassword
    or use existing username and password such as 'sally' and 'billy', oops.. 'harry'.

  6. Start the daemon process.

    Now enter following command to start the SVN daemon process.

    svnserve -d --root ~/my_svn_repo/
that's it. you can now checkout your own local SVN repository like any other SVN repository. Just use following URL to access your local SVN repository.

svn://localhost
example:

svn co svn://localhost my_svn_works
Creating SVN directory structure:(optional)

Like the SVN's general convention, I recommend you to create 3 different directories. They are trunk, tags and branches.

use following command to create them

svn mkdir branches tags trunk
now commit them to SVN.

svn commit
Making your SVN daemon process to start automatically with system.

The steps given above will start the daemon process, however if you restart your system, again you need to manually start the daemon process. Otherwise you can add a script to start the daemon process automatically with your system.

For this, You need to add a script to your /etc/init.d/

use following command to do it

echo "svnserve -d --root /home/raghunayak/my_svn_repo/" >> run_svnd && chmod +x run_svnd && sudo mv run_svnd /etc/init.d && sudo update-rc.d run_svnd defaults
In this command replace '/home/raghunayak/my_svn_repo' with with your SVN repository path.

To remove this startup script enter following command.

sudo update-rc.d -f run_svnd remove && sudo rm /etc/init.d/run_svnd
If you like this article, please leave a comment.. :)

New Category - Tech Recipe

I have just created a new post category named Tech Recipes. From today on wards I would be posting some new articles under this category.

In this category I would be asking some technical questions to you people, with the answer. The questions are most likely to be programming language related, with some exceptions. I hope these questionnaire would be very useful to the people, who are preparing for interviews or just want to improve their hold in programming languages.

So, Keep watching this space for more on this..



Technorati :

Del.icio.us : ,

Zooomr : ,

Flickr : ,

Tuesday, April 14, 2009

New License

I have changed the license used for sample codes. Most of the new sample codes are now released under more permissive MIT license, rather than copyleft GNU license.

I hope this change would help many people.

- Raghu Nayak

Update:
I have again changed the code license. I have re-licensed all my programs/source code under GNU General Public License version 3 or above (at your option).

Reasons or motivations behind this change:

  1. Freedoms of the user:
    I actually spend a lot of effort while coding or programming, and I really want my effort to be useful to someone! At the same time, I really don't want someone to take my code and convert it into a proprietary trap, thereby killing all user freedoms. I really want to protect the basic rights or the freedoms of the user, and this can be ensured only with GNU GPL.
  2. The recent acquisition of SUN Microsystem by Oracle had a strong influence on my decision. I really Thank GOD that, MySQL code is available under GNU GPL. So, even if Oracle kills it, somebody will fork it.
  3. This article, which I saw in GNU website.
So, I am sorry to all those who wanted to integrate use my code with their proprietary code. I am sorry, but this action was necessary to protect the basic freedom, on which I have strong belief.

- Raghu Nayak

Sunday, April 12, 2009

CMake: Detecting Platform/Operating Systems, Compiler Information

I was searching for this information from last three days. Finally I found the CMake syntax to write the platform specific code inside CMakeLists.txt.

Detecting the Operating System:

CMake actually defines several variables to identify the platform information. These variables will be assigned with the values based on the platform, operating system etc.

For example, In CMake version 2.6 following Variables are present to identify the Operating System Type.

UNIX
     is TRUE on all UNIX-like OS's, including Apple OS X and CygWin
WIN32
     is TRUE on Windows, including CygWin
APPLE
     is TRUE on Apple systems. Note: Having this variable value set to TRUE does not necessarily mean that Operating System is Mac OS X. It only means that in the C/C++ header file __APPLE__ Macro is defined. Use the alternate method of detecting Operating System type, mentioned below instead.

For GNU/Linux: There is NO Variable for GNU/Linux. ie; IF(LINUX) does NOT work. Use the alternative method mentioned below instead.

Alternative method to detect to Operating System:

CMake version 2.6 defines 2 different variables, which can be used to detect the Operating System preciously. They are,

CMAKE_SYSTEM

     the complete system name, e.g. "Linux-2.4.22", "FreeBSD-5.4-RELEASE" or "Windows 5.1"
CMAKE_SYSTEM_NAME
     the short system name, e.g. "Linux", "FreeBSD" or "Windows"

These variables can be used to detect the Operating System.

Example:

IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# Mac OS X specific code
     SET(OperatingSystem "Mac OS X")
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")


for detecting Mac OS X

and for Linux

IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
# Linux specific code
     SET(OperatingSystem "Linux")
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")


Detecting the compiler:

Following variables can be used to detect the compilers.

MINGW
     is TRUE when using the MinGW compiler in Windows
MSYS
     is TRUE when using the MSYS developer environment in Windows
BORLAND
     is TRUE on Windows when using a Borland compiler
WATCOM
     is TRUE on Windows when using the Open Watcom compiler
MSVC, MSVC_IDE, MSVC60, MSVC70, MSVC71, MSVC80, CMAKE_COMPILER_2005, MSVC90
     Microsoft compiler
CMAKE_COMPILER_IS_GNUCC
     is TRUE if the compiler is a variant of gcc
CMAKE_COMPILER_IS_GNUCXX
     is TRUE if the compiler is a variant of g++
CYGWIN
     is TRUE on Windows when using the CygWin version of cmake

Detecting the System Processor:

Following variable can be used to detect the processor family.

CMAKE_SYSTEM_PROCESSOR
     the processor name (e.g. "Intel(R) Pentium(R) M processor 2.00GHz")

Example usage:

IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
     # Mac OS X specific code
     SET(OperatingSystem "Mac OS X")

     IF(${CMAKE_SYSTEM_PROCESSOR } MATCHES "Intel")
         # Intel Mac OS X specific code
         SET(Processor "Intel")
     ENDIF(${CMAKE_SYSTEM_PROCESSOR } MATCHES "Intel")

ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")


Links:

How To: Fix shared library load problem in GNU/Linux

I recently started learning/using Google C++ Testing framework.

I compiled and installed Google testing framework as per the instruction. But still I wasn't able to run the sample applications compiled by me.

Whenever I run them, I used to get following error.

error while loading shared libraries: libgtest.so.0: cannot open shared object file: No such file or directory

But I had installed Google test framework properly. The install script had properly placed the libgtest.so.0 into /usr/local/lib

So, to check which all shared library are failed to load, I issued following command

$ ldd a.out

and the output was

raghunayak@raghu-desktop:~/google_test/samples$ ldd a.out
        libgtest.so.0 => not found
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00002b2897d75000)
        libm.so.6 => /lib/libm.so.6 (0x00002b2898080000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00002b2898301000)
        libc.so.6 => /lib/libc.so.6 (0x00002b2898510000)
        /lib64/ld-linux-x86-64.so.2 (0x00002b2897b57000)


Which showed that, only libraries placed in /usr/local/lib are failed to load. This gave me a hint that, /usr/local/lib is not the ld library search path.

To confirm this, I entered following command, which lists all the libraries that ld can load.

$ ldconfig -p

This confirmed that, /usr/local/lib is indeed missing from ld library search path.

So, to include /usr/local/lib into the ld library search path, I typed following into the terminal.

$ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/

after this, I typed

$ ldd a.out

and this time the ld was able to properly find the library gtest.so.0 placed in '/usr/local/lib'

raghunayak@raghu-desktop:~/google_test/samples$ ldd a.out
        libgtest.so.0 => /usr/local/lib/libgtest.so.0 (0x00002ae54f520000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00002ae54f772000)
        libm.so.6 => /lib/libm.so.6 (0x00002ae54fa7d000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00002ae54fcfe000)
        libc.so.6 => /lib/libc.so.6 (0x00002ae54ff0d000)
        /lib64/ld-linux-x86-64.so.2 (0x00002ae54f302000)


After this I was able to run samples without any problems. :)

Temporary Solution:

Temporary solution is to add '/usr/local/lib' into the environmental variable named LD_LIBRARY_PATH

so, the solution is to add /usr/local/lib into the enviornment variable named LD_LIBRARY_PATH, by typing

You can do this by typing

$ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/

Permanent solution:

permanent solution is to modify the file /etc/ld.so.conf and add the path /usr/local/lib into it.

You can do it manually by typing

$ sudo gedit /etc/ld.so.conf

or type following into the terminal

$ sudo bash -c 'echo /usr/local/lib >> /etc/ld.so.conf ' && sudo ldconfig

P.S.: I think GNU/Linux distributions by default should include /usr/local/lib in /etc/ld.so.conf or at-least the install script of the libraries should have this intelligence.