offsetof: Using the offsetof funcion in c

The “offsetof” function in c is useful in determining the offset of members with in a structure or union.
It is defined in the header file “stddef.h”.
The syntax is

offsetof(TYPE,MEMBER);

Arguments:

TYPE: The type of the structure in which the offset is to be determined MEMBER: The member in the structure TYPE whose offset is required.

Example :

Let say we have a structure foo

struct foo { int i; char ch; };

The structure “foo” has a integer member which generally would of 4 bytes on a 32 bit system and a chat member which would be one byte.
Thus the offset of the member “ch” in the structure “foo” would be the number of bytes occupied by “int” which is “4”.

Here is a example code which uses “offsetof” to find the offset of ch in the structre foo.

#include#include #includestruct foo { int i; char ch; } ; main() { int size; size = offsetof(struct foo,ch); printf(“offet is = %d”,size); }

Compile the code and execute it

$ cc offset.c -o offset $ ./offset offset is = 4

Try changing adding new members to the structure foo and see how the result changes depending on the members added.

Try with the structure :

struct foo { int arr[10]; char ch; };

$ cc offset.c -o offset $ ./offset offset is 40

The offset is the size of the array which is 4 X 10 = 40.

Related Blogs

    Category: Linux | Comments Off on offsetof: Using the offsetof funcion in c

    offsetof: Using the offsetof funcion in c

    The “offsetof” function in c is useful in determining the offset of members with in a structure or union.
    It is defined in the header file “stddef.h”.
    The syntax is

    offsetof(TYPE,MEMBER);

    Arguments:

    TYPE: The type of the structure in which the offset is to be determined MEMBER: The member in the structure TYPE whose offset is required.

    Example :

    Let say we have a structure foo

    struct foo { int i; char ch; };

    The structure “foo” has a integer member which generally would of 4 bytes on a 32 bit system and a chat member which would be one byte.
    Thus the offset of the member “ch” in the structure “foo” would be the number of bytes occupied by “int” which is “4”.

    Here is a example code which uses “offsetof” to find the offset of ch in the structre foo.

    #include#include #includestruct foo { int i; char ch; } ; main() { int size; size = offsetof(struct foo,ch); printf(“offet is = %d”,size); }

    Compile the code and execute it

    $ cc offset.c -o offset $ ./offset offset is = 4

    Try changing adding new members to the structure foo and see how the result changes depending on the members added.

    Try with the structure :

    struct foo { int arr[10]; char ch; };

    $ cc offset.c -o offset $ ./offset offset is 40

    The offset is the size of the array which is 4 X 10 = 40.

    Category: Linux | Comments Off on offsetof: Using the offsetof funcion in c

    Answers to linux history quiz

    1.In which year was the linux kernel released under GNU GPL license for the first time?
    Ans.1992

    2.The penguin was chosen as the official mascot for linux in
    Ans.1996

    3. The linux foundation is the merger of
    Ans. OSDL and Free standards group

    4. Which famous computer scientist said this in 1992 “Linux is obsolete”?
    Ans. Andrew S. Tanenbaum

    5. Linux 1.0 was released in
    Ans. 1994

    6. When did microsoft first contribute to linux source code ?
    Ans. 2009

    7. Which of the following is a linux based company and was started in 1994 ?
    Ans. Redhat

    8. Which of the following companies invested a billion dollars in to linux in the year 2001 ?
    Ans. IBM

    9. In which year did Linus Torvalds appear on the cover of Business week ?
    Ans. 2005

    10. Which of the following Linux Desktop environment (GUI) was started in the year 1997 ?
    Ans. GNOME

    Related Blogs

      Category: Linux | Comments Off on Answers to linux history quiz

      Linux history quiz

      Quiz On history of Linux body {margin: 10px;} Quiz On history of LinuxA simple quiz on history of Linux In which year was the linux kernel released under GNU GPL license for the first time?
      1991
      1992
      1993
      1996

      The penguin was chosen as the official mascot for linux in
      1992
      2000
      1994
      1996

      “The linux foundation” is the merger of
      OSDL and Free standards group
      GNU and OSDL
      The linux users and unix users group
      Many small linux supporting groups

      Which famous computer scientist said this in 1992 “Linux is obsolete” ?
      Andrew S. Tanenbaum
      Alan Cox
      Ken Thompson
      Richard Stallman

      Linux 1.0 was released in
      1999
      1994
      1996
      1992

      When did microsoft first contribute to linux source code ?
      2002
      1995
      2009
      2011

      Which of the following is a linux based company and was started in 1994 ?
      Ubuntu
      Redhat
      IBM
      Apple

      Which of the following companies invested a billion dollars in to linux in the year 2001 ?
      Redhat
      Canonical
      IBM
      Google

      In which year did Linus Torvalds appear on the cover of Business week ?
      2000
      2003
      2005
      1999

      Which of the following Linux Desktop environment (GUI) was started in the year 1997 ?
      GNOME
      KDE
      Unity
      Enlightment

      Answers to the uiz are available at : Answers to linux history quiz

      Related Blogs

        Category: Linux | Comments Off on Linux history quiz

        expand: To convert a tab space to space characters

        expand: Command used to convert the tab space to separate space characters.

        Example:

        Let us say we we have a file “temp” with the following contents.

        temp:

        one 1 two 2 three 3 four 4

        In each line we have one tab space, which is equivalent to 8 space characters. To convert this one tab space to 8 space characters we can run the command expand on the file.

        $ expand temp > temp_1 $ cat temp_1 one 1 two 2 three 3 four 4

        The new file “temp_1” looks the same as the file “temp” but if we open the file to edit it, we will encounter 8 space characters instead of tab on every line.

        To change the number of space characters from 8 to any number of our choice we can use the option “-t”.

        $ expand -t 1 temp one 1 two 2 three 3 four 4

        We can see that the tab has been replaced by single space. The number that you specify after “-t” will be the number of spaces that will put instead of the tab space.

        We can also restrict the command to work only at the beginning of a file by passing the option “-i”

        Example :

        Let us modify the file temp and add a tab at the beginning of the first line.

        temp:

        one 1 two 2 three 3 four 4

        Now use expand on the file along with option “-i”

        $ expand -i -t 1 temp one 1 two 2 three 3 four 4

        We can see that only the tab at the beginning of the first line has been removed,but the rest of the tabs are intact.

        Related Blogs

          Category: Linux | Comments Off on expand: To convert a tab space to space characters

          tr: To translate characters

          The command “tr” can be used to translate/change or delete characters from the input.

          By default tr takes two arguments, the first one being the set of characters to be searched for and the second argument being the set of characters to be replaced with . The default input is the standard input.

          Example:

          $ tr x y xyx yyy $

          The command waits for user to enter a string and then hit the return key. In the above example on entering “xyx” the character x has been replaced by character y which was what was passed as arguments to the command.

          We can also pass multiple characters as arguments and each characters will be considered separately and not as a string and each of the characters in argument1 will be replaced by the corresponding character in the argument2

          Example:

          $ tr xyz abc xyz abc x a y b z c

          If the string length of argument1 is greater than that of argument2 then the last character of argument2 is applied to all the characters of argument1 that are after the string length of argument2.

          Example :

          $ tr xyz ab xyz abb x a y b z b

          As we can see both y and z are replaced by b.

          We can change this behavior by passing the option “-t” which will truncate the characters of first argument to the same as the length of argument2.

          $ tr -t xyz a xyz ayz x a y y z z

          Thus we can see, “y” and “z” both are not replaced by character.

          The command can be applied on files by using the input redirection.

          Example:

          Let say we have a file,temp, with following contents

          temp:

          one 1 two 2 three 3 four 4

          $ tr o O < temp One 1 twO 2 three 3 fOur 4 We can see that all o have been replaced with O. Instead of translating, we can delete characters too by passing the option “-d” . Example: $ tr -d [:digit:] < temp one two three four Thus we can see that all the digits in the file have been deleted. Other examples: We can user “tr” to convert all the characters in a file to upper case too. $ tr [:lower:] [:upper:] < temp ONE 1 TWO 2 THREE 3 FOUR 4 Using tr over a range of characters $ tr -d 1-3 < temp one two three four 4 Deleting repetitive characters $ tr -s e eeee e $

          Related Blogs

            Category: Linux | Comments Off on tr: To translate characters

            gzip: Compressing files using gzip

            gzip is a command that can be used to compress and decompress files. The command takes one or more files as input and compresses each of them separately.

            The new compressed file is created with a “.gz” extension, i.e. if we compressed the file “temp”, the compressed file will be named temp.gz and the compressed file will replace the original file.

            The command works only on files and if we want to compress folders using gzip , we can convert a folder into a single “tar” archive and use the gzip command on the archive.

            Example :

            Let us say we have a folder called temp. The size of the folder can be found using the command du.

            $ du -h temp 52M temp

            We can convert the folder into a “.tar” file using the command tar

            Note : Using tar command is given in the post :Using tar to archive files

            $ tar cf temp $ du -h temp.tar 52M temp

            We can see that the “tar” command only archives but does not compress the file.Now we can use the gzip command on the tar file to compress it.

            $ gzip temp.tar $ ls temp.tar.gz

            We can see that the original file “temp.tar” has been replaced by the compressed file.

            $ du -h temp.tar.gz 38M temp

            The size of the compressed file is much smaller than the original file, thus clear that gzip compresses the file
            The files compressed using gzip can be decompressed using gzip with option “-d” or with the command guzip. By default gzip creates the decompressed file with the name of the original file with “.gz” removed i.e temp.tar.gz will become temp.tar and will replace the original temp.tar.gz Example

            $ gzip -d temp.tar.gz gzip: temp.tar already exists; do you wish to overwrite (y or n)?

            If temp.tar already exists in the folder gzip prompts the uses to confirm ifthe previous file has to be overwritten, and only on reciveing the confirmation will it overwrite the existing file

            We can also change the suffix from “.gz” to any other characters we wish by using the option “-S”

            Example :

            $ gzip -S “.zipped” temp.tar $ ls temp.tar.zipped

            While decompressing the files that are compressed with custom suffix we need to pass the suffix also.

            $ gunzip temp.tar.zipped gzip: temp.tar.zipped: unknown suffix — ignored $ gunzip -S “.zipped” temp.tar.zipped $ ls temp.tar

            If we want to find out the size of the decompressed file before decompressing we can use the option “-l”.

            $ gzip -l temp.tar.gz compressed uncompressed ratio uncompressed_name 39195219 53708800 27.0% temp.tar.zipped

            Related Blogs

              Category: Linux | Comments Off on gzip: Compressing files using gzip

              bzip2: Compressing files using bzip2

              The command bzip2 can be used to compress files and reduce their size. The command accepts one or more files as input on the command line and compresses each of them separately.

              Each compressed file is stored with the same name along with “.bz2” added as the extension. By default the original file is replaced unless explicitly specified to retain the original file.

              The command works of files only, thus if we want to compress a folder using bzip2 we need to convert it to a file using the tar command and then compress it using bzip2.

              Example:
              Let us say we have a folder called temp. The size of the folder can be found using the command du.

              $ du -h temp 52M temp

              We can convert the folder into a “.tar” file using the command tar

              Note: Using tar command is given in the post : Using tar to create an archive

              $ tar cf temp $ du -h temp.tar 52M temp

              We can see that the “tar” command only archives but does not compress the file.Now we can use the bzip2 command on the tar file to compress it.

              $ bzip2 temp.tar $ ls temp.tar.bz2

              We can see that the original file “temp.tar” has been replaced by the compressed file.

              $ du -h temp.tar.bz2 37M temp

              The size of the compressed file is much smaller than the original file, thus clear that bzip2 compresses the file.s

              To retain the original files while compressing using bzip2 we need to use the option “-k”

              $ bzip2 -k temp.tar $ ls temp.tar temp.tar.bz2

              The files compressed using bzip2 can be decompressed using bzip2 with option “-d” or with the command bunzip2. By default bzip2 creates the decompress file with the name of the original file with “.bz2” removed i.e temp.tar.bz2 will become temp.tar.

              Example

              $ bzip2 -d temp.tar.bz2 bzip2: Output file temp.tar already exists.

              If temp.tar already exists in the folder bzip2 throws an error and stops with out overwriting the file. We can force it to overwrite the existing file by using the option “-f”.

              $ bzip2 -d -f temp.tar.bz2 $ ls temp.tar

              Again the original “.bz2” file is replaced with the decompressed file, and the option “-k” can be used to retain the compressed file.

              Related Blogs

                Category: Linux | Comments Off on bzip2: Compressing files using bzip2

                bzip2: Compressing files using bzip2

                The command bzip2 can be used to compress files and reduce their size. The command accepts one or more files as input on the command line and compresses each of them separately.

                Each compressed file is stored with the same name along with “.bz2” added as the extension. By default the original file is replaced unless explicitly specified to retain the original file.

                The command works of files only, thus if we want to compress a folder using bzip2 we need to convert it to a file using the tar command and then compress it using bzip2.

                Example:
                Let us say we have a folder called temp. The size of the folder can be found using the command du.

                $ du -h temp 52M temp

                We can convert the folder into a “.tar” file using the command tar

                Note: Using tar command is given in the post : Using tar to create an archive

                $ tar cf temp $ du -h temp.tar 52M temp

                We can see that the “tar” command only archives but does not compress the file.Now we can use the bzip2 command on the tar file to compress it.

                $ bzip2 temp.tar $ ls temp.tar.bz2

                We can see that the original file “temp.tar” has been replaced by the compressed file.

                $ du -h temp.tar.bz2 37M temp

                The size of the compressed file is much smaller than the original file, thus clear that bzip2 compresses the file.s

                To retain the original files while compressing using bzip2 we need to use the option “-k”

                $ bzip2 -k temp.tar $ ls temp.tar temp.tar.bz2

                The files compressed using bzip2 can be decompressed using bzip2 with option “-d” or with the command bunzip2. By default bzip2 creates the decompress file with the name of the original file with “.bz2” removed i.e temp.tar.bz2 will become temp.tar.

                Example

                $ bzip2 -d temp.tar.bz2 bzip2: Output file temp.tar already exists.

                If temp.tar already exists in the folder bzip2 throws an error and stops with out overwriting the file. We can force it to overwrite the existing file by using the option “-f”.

                $ bzip2 -d -f temp.tar.bz2 $ ls temp.tar

                Again the original “.bz2” file is replaced with the decompressed file, and the option “-k” can be used to retain the compressed file.

                Category: Linux | Comments Off on bzip2: Compressing files using bzip2

                tar: Using the tar command to create an archive.

                The tar command can be used to create a archive of multiple file and folders, making it easier to move or copy the files in one go.

                Example:

                Let us say we have three files, file1,file2,file3. We can create one tar file by combining all three using the command tar.

                $ tar -cvf files.tar file1 file2 file3 file1 file2 file3 $ ls files.tar file1 file2 file3

                The options mean :

                c is to create
                v is to make the command verbose, that is print what ever files are being archived
                f is the use the file name being passed on the command line.

                We can list the contents of the archive using. the “-t” option.

                $ tar -tvf files.tar -rw-r–r– root/root 0 2012-07-15 21:37 file1 -rw-r–r– root/root 0 2012-07-15 21:37 file2 -rw-r–r– root/root 0 2012-07-15 21:37 file3

                The option v and f mean the same as above.
                The option “t” is to list or you can read it as tell the contents of the archive.
                If we create a tar archive a of a directory, it maintains the folder structure intact, will produce the same folder structure when the archive is untared.

                Example :

                If we have a folder, temp , with three files, file1,file2,file3 and a sub folder temp1. We can create a tar archive of the same as follows.

                $ ls temp -rw-r–r– 1 root root 0 Jul 15 21:37 file1 -rw-r–r– 1 root root 0 Jul 15 21:37 file2 -rw-r–r– 1 root root 0 Jul 15 21:37 file3 drwxr-xr-x 2 root root 4096 Jul 13 16:57 temp1 $ tar -cvf temp.tar temp temp/ temp/file2 temp/file3 temp/file1 temp/temp1/ $

                Once an archive is created, if we update the folder which we archived we need to create a new archive but instead just update the already created archive.

                Example:

                In the previous example we created an archive for folder temp. If we add a new file,file4, to the folder we can update the archive a follows.

                $ tar -uvf temp.tar temp test_tar/file4 $ tar -tvf temp.tar temp drwxr-xr-x root/root 0 2012-07-16 00:24 test_tar/ -rw-r–r– root/root 0 2012-07-15 21:37 test_tar/file2 -rw-r–r– root/root 0 2012-07-15 21:37 test_tar/file3 -rw-r–r– root/root 0 2012-07-15 21:37 test_tar/file1 drwxr-xr-x root/root 0 2012-07-13 16:57 test_tar/temp1/ -rw-r–r– root/root 0 2012-07-16 00:25 test_tar/file4

                The tar command only updates the new file that was added to the folder.
                If folder to be archived has symbolic links, then by default tar, copies the link only and not the original contents.

                Example:

                Let us create a soft link to the folder temp, used in the previous examples, and try to tar the link.

                $ ln -s temp link $ ls -l link lrwxrwxrwx 1 root root 8 Jul 16 13:16 link -> test_tar $ tar -cvf link.tar link link $ tar -tvf link.tar lrwxrwxrwx root/root 0 2012-07-16 13:16 link -> test_tar

                After creating the softlink, we used the tar command on the soft link to create the archive. And when we list the contents of the archive created, we can see it does not have the files and folders of the original file but only the link file.

                To make sure that tar copies the original folder that the link is pointing to we need to pass the option “-h” , i.e.

                $ tar -chvf link.tar link link/ link/file2 link/file3 link/file1 link/file4 link/temp1/ $ tar -tvf link.tar drwxr-xr-x root/root 0 2012-07-16 13:16 link/ -rw-r–r– root/root 0 2012-07-15 21:37 link/file2 -rw-r–r– root/root 0 2012-07-15 21:37 link/file3 -rw-r–r– root/root 0 2012-07-15 21:37 link/file1 -rw-r–r– root/root 0 2012-07-16 00:25 link/file4 drwxr-xr-x root/root 0 2012-07-13 16:57 link/temp1/

                We can see that after passing the option “-h”, the archive contains all the original files and folders.
                Once “tar” archive is created, we can extract the contents of the archive by using the option “-x”

                Example:

                To get the files archived under temp.tar in the above example we need to run

                $ tar -xvf temp.tar temp/ temp/file2 temp/file3 temp/file1 temp/file4 temp/temp1/

                The name of the folder created will the same as that of the original folder that was archived.

                Category: Linux | Comments Off on tar: Using the tar command to create an archive.