Thursday, 31 October 2013

[Windows] Check and Fix file system errors of any memory card (flash memory devices)

On windows, we have a command called "chkdsk" (check disk) to verify the memory card's file system integrity and is used for checking file system errors and fix these errors if required.

This command is useful if the memory card is corrupted by some means and need to recover the data from the card without any data loss.

Step1) Insert the memory card (flash drive) which is corrupted or to check file system errors. After inserting the card, lets say the drive identified as E:/

Step2) Now, open command prompt and run chkdsk command
            chkdsk e: /f

Step3) You will see the info regd the file system used in the memory card and memory details. If the command identifies any file system errors then it fixes them.

The complete syntax for Check Disk is this:
 chkdsk [volume[[path]filename]]] [/F] [/V] [/R] [/X] [/I] [/C] [/L[:size]] 

Options:
-----------
Volume Sets the volume to work with.
filename FAT/FAT32 only: Specifies files to check for fragmentation.
/F Fixes errors on the disk.
/V On FAT/FAT32, this displays the full path and name of every file on the disk.
On NTFS, this displays cleanup messages, if any.
/R Locates bad sectors and recovers readable information (implies /F).
/L size NTFS only. Changes the log file size.
/X Forces the volume to dismount first if necessary (implies /F).
/I  NTFS only. Performs a minimum check of index entries.
/C NTFS only. Skips checking of cycles within the folder structure.

For more details please find useful linke here

On Linux platform, we have similar utility for checking and repairing file systems called "fsck". More details can be found here

Tuesday, 29 October 2013

[C Programming] Implementation of Linked List in C

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

struct node {
  int data;
  struct node *next;
};

void insert_node(struct node **Head)
{
struct node *iterate;
int data;

printf("Enter node data:");
scanf("%d", &data);

if(*Head == NULL) {
*Head = (struct node*)malloc(sizeof(struct node*));
(*Head)->data = data;
(*Head)->next = NULL;
} else {
  iterate = *Head;
  while(iterate->next != NULL)
   iterate = iterate->next;
   iterate->next = (struct node*)malloc(sizeof(struct node*));
   iterate->next->data = data;
   iterate->next->next = NULL;
}
}

void show_linked_list(struct node *Head)
{
struct node *iterate = Head;

if(!iterate) {
      printf("Linked List is empty\n");
return;
}

printf("Linked List elements are\n");

while (iterate != NULL) {
     printf("%d\n", iterate->data);
 iterate = iterate->next;
}
}

void isLL_ContainLoop(struct node *Head)
{
struct node *start, *end = NULL;

start = Head;

do {
  if (start != NULL) {
  if(start->next)
 start = start->next->next;
    else
              start = start->next;
        }

        if (end == NULL)
           end = Head;
        else
           end = end->next;
}while(start != end && start!= NULL);

if (start == end && start != NULL)
  printf("+++Linked List has LOOP+++\n");
    else
  printf("+++Linked List has NO LOOP+++\n");
}

void cleanup(struct node **Head)
{
  struct node *iterate = *Head;
  while(*Head)
  {
iterate = (*Head);
*Head = (*Head)->next;
iterate->next = NULL;
free(iterate);
}
}

struct node* search_node(struct node *Head, int data)
{
  while (Head) {
  if (Head->data == data)
   return Head;
  Head = Head->next;
  }
  return NULL;
}


void delete_node(struct node **Head)
{
  struct node *del_node = NULL;
  struct node *iterate = *Head;
  int data, find;
 
  if (*Head == NULL)
  printf("Linked List is empty\n");

  show_linked_list(*Head);
  printf("Select data to delete from linked list\n");
scanf("%d", &data);

del_node = search_node(*Head, data);
if (del_node) {
  printf("Data is present\n");

    if (del_node == *Head) {
        *Head = (*Head)->next;
        iterate->next = NULL;
        free(iterate);
    } else {
  while (iterate->next != del_node)
    iterate = iterate->next;
           iterate->next = iterate->next->next;
           del_node->next = NULL;
           free(del_node);
              }

} else {
  printf("Data is not present\n");
}
}

void main()
{
struct node *Head = NULL;
int choice, data;

while(1) {
printf("****Menu*****\n");
printf("select 1: Insert new node\n");
printf("select 2: show linked list elements\n");
printf("select 3: is Linked list contain loop\n");
printf("select 4: Delete node\n");
printf("select 5: Search node\n");
printf("select 6: Exit\n");
printf("Enter your input 1-6 ?\n");
scanf("%d", &choice);

switch(choice){
case 1: insert_node(&Head); break;
case 2: show_linked_list(Head); break;
case 3: isLL_ContainLoop(Head); break;
case 4: delete_node(&Head); break;
case 5: printf("Enter Data to search in Linked List\n");
scanf("%d", &data);
if (search_node(Head, data)) printf("Data is present\n");
   else printf("Data is not present\n");
    break;
case 6:
        default: cleanup(&Head); exit(1);
}
printf("=========================\n");
    }
}

Output:
-----------
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
1
Enter node data:11
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
1
Enter node data:22
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
2
Linked List elements are
11
22
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
3
+++Linked List has NO LOOP+++
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
5
Enter Data to search in Linked List
22
Data is present
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
4
Linked List elements are
11
22
Select data to delete from linked list
11
Data is present
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
2
Linked List elements are
22
=========================
****Menu*****
select 1: Insert new node
select 2: show linked list elements
select 3: is Linked list contain loop
select 4: Delete node
select 5: Search node
select 6: Exit
Enter your input 1-6 ?
6

Monday, 28 October 2013

[C Programming] Find roots of any quadratic equation

#include<stdio.h>
#include<math.h>

void main()
{
  int coefficient_a, coefficient_b, coefficient_c;
  int delta;
  float root1, root2;
  float temp;
 
  printf("Enter values for coefficients a, b and c\n");
  scanf("%d%d%d", &coefficient_a, &coefficient_b, &coefficient_c);
 
  printf("Entered expression is\n");
  printf("%dx^2 + %dx + %d\n", coefficient_a, coefficient_b, coefficient_c);
 
  if (!coefficient_a) {
  printf("Entered equation is not a quadratic equation, since coefficient_a is ZERO\n");
  return;
     }

  delta = (coefficient_b * coefficient_b) - (4 * coefficient_a * coefficient_c);
 
  if (delta > 0){
  /* Roots are Real numbers */
  printf("Roots are Real numbers\n");
  root1 = (-coefficient_b + sqrt(delta)) / (2 * coefficient_a);
  root2 = (-coefficient_b - sqrt(delta)) / (2 * coefficient_a);
 
  printf(" Roots are %f and %f\n", root1, root2);
  }
  else {
 /* Roots are imaginary numbers
  * roots = (-b +- i sqrt(4*a*c-b*b)) / (2*a)
  * temp = -b/2*a;
  * root1 = temp + i sqrt(4*a*c-b*b)/(2*a);
  * root2 = temp - i sqrt(4*a*c-b*b)/(2*a);
  */
 printf("Roots are imaginary numbers\n");
 temp = -coefficient_b / (2 * coefficient_a);
 root1 = sqrt(-delta) / (2 * coefficient_a);
   root2 = -sqrt(-delta) / (2 * coefficient_a);
   printf(" Roots are %f + i(%f) and %f + i(%f)\n", temp, root1, temp, root2);
  }
}

Output1:
---------
Enter values for coefficients a, b and c
1
5
6
Entered expression is
1x^2 + 5x + 6
Roots are Real numbers
 Roots are -2.000000 and -3.000000


Output2:
-----------
Enter values for coefficients a, b and c
1
2
8
Entered expression is
1x^2 + 2x + 8
Roots are imaginary numbers
 Roots are -1.000000 + i(2.645751) and -1.000000 + i(-2.645751)

[C Programming] Find Maximum and Minimum number in array of numbers

#include<stdio.h>
#define ARRAY_SIZE 100 /* Define Your ARRAY Size */

int find_max(int input[], int size)
{
  int max, index;
 
  max = input[0];
 
  for (index = 1; index < size; index++)
  {
if (max < input[index])
  max = input[index];
    }
    return max;
}

int find_min(int input[], int size)
{
  int min, index;
 
  min = input[0];
 
  for (index = 1; index < size; index++)
  {
if (min > input[index])
  min = input[index];
    }
    return min;
}

void main()
{
  /* We can dynamically allocate memory for an array using malloc() based on array_size requested from end user*/
  int set_of_numbers[ARRAY_SIZE];
  int size, index;
  int maximum, minimum;
 
  printf("Please enter number list size\n");
  scanf("%d", &size);
 
  if (size <= 0) {
  printf("Please enter valid size\n");
  return;
     }

  printf("Enter numbers\n");
  for (index = 0; index < size; index++)
  scanf("%d", &set_of_numbers[index]);

printf("List of numbers entered by user\n");
  for (index = 0; index < size; index++)
  printf("%d\n", set_of_numbers[index]);
 
  maximum = find_max(set_of_numbers, size);
  minimum = find_min(set_of_numbers, size);
 
  printf("Maximum number: %d\n", maximum);
  printf("Minimum number: %d\n", minimum);
}

Output:
-----------
Please enter number list size
5
Enter numbers
12
34
2
7
8
List of numbers entered by user
12
34
2
7
8
Maximum number: 34
Minimum number: 2

[Kernel Programming #7] ktime_get() and do_gettimeofday() APIs use

We have some APIs to measure time taken for a function or a piece of code in the driver.

1. do_gettimeofday()
2. ktime_get()

These APIs are used to measure the time (absolute timestamp) in the kernel.
do_gettimeofday() gives microsecond precision.
ktime_get() return ktime_t object, which has nano second precision.

ktime_get:

===========
Here is abstract code to show how ktime_get() API will be used in our driver to measure time taken for a function.

The prototype for ktime_get is:

 #include <linux/ktime.h>
 ktime_t ktime_get(void);

Sample-code:
--------------
ktime_t start, end;
s64 actual_time;

start = ktime_get();

function();
/* or piece of code */
....
....

end = ktime_get();

actual_time = ktime_to_ns(ktime_sub(end, start));

printk(KERN_INFO, "Time taken for function() execution: %lld\n",
(long long)actual_time);

/* Use below code for millisec precision */
actual_time = ktime_to_ms(ktime_sub(end, start));

printk(KERN_INFO, "Time taken for function() execution: %u\n",
(unsigned int)actual_time);


do_gettimeofday():

==================
Here is abstract code to show how do_gettimeofday API will be used in our driver to measure time taken for a function.

The prototype for do_gettimeofday is:

 #include <linux/time.h>
 void do_gettimeofday(struct timeval *tv);

When this function called, it fills the timestamp data in struct timeval.

Using struct timeval members, we can extract seconds and microseconds info.

Sample code:
-------------
do_gettimeofday(&tstart);

/* Function or code to measure time bound */

do_gettimeofday(&tend);

printk("time taken: %ld millisec\n",
1000 * (tend.tv_sec - tstart.tv_sec) +
(tend.tv_usec - tstart.tv_usec) / 1000);

Sunday, 27 October 2013

SQL Injection Attack Presentation

Ubuntu Basic Commands

There are few simple commands in Ubuntu operating system those we should know before using any Ubuntu machine. These commands would be very helpful and are easy for Ubuntu machine maintenance.

Man Pages

Linux OS provides a command named "man" for reading manual pages in the system. These manual pages contains description of general commands, system calls functions, library functions, file formats conventions etc.
These pages provides insight information of above mentioned categories.

man <command_name>

Example:
If something (like printf() function in C language) is not clear, what it do, what is the use and what is the syntax of this function etc information can be found in printf man pages. Below command should be used for this purpose

man printf

Similary, if we are not clear about any command, what it is, its use, where to use etc info is available in the manual pages
Try: man sudo

Once the manual page is opened in the terminal, use letter "q" to exit from the manual page.
-------------------------------------------------------------------------------------

"Sudo" Importance

In order to do administrative tasks like install, remove or upgrade applications/software in the Ubuntu machine, the logged user should have administrator privileges.
For this purpose, Ubuntu environment provides a special command called "sudo" (Super Used DO), which will allows us to install/remove/upgrade and system maintenance.
In Ubuntu there is no way to log in as Admin user (log in as ROOT) to do above tasks. So, Ubuntu added this command specifically to support above tasks.

Installing any application/software
Use: sudo apt-get install <package_name>

Un-Installing any application/software
Use: sudo apt-get remove <package_name>

Upgrade system software/applications
Use: sudo apt-get upgrade

sudo command will run the commands in administrator mode.

In ubuntu, all the application software are available in repositories unlike windows machine where we need to carry the software in external storage.

So, when we use command "apt-get", it will search required software/s (<package_name>) in the repositories, get that software program and will install/upgrade it without user intervention. It will automatically install software for us.
Note: Our Ubuntu machine should have Internet connection to use this facility from Ubuntu.
-----------------------------------------------------------------------------------------

Tasksel:

Tasksel (Task select) is also a special command in Ubuntu environment to install set of packages in one go.
For example, if we want to install FTP server or printer server or DNS server, they require so many dependency packages to install in order one by one to work.
Instead of installing them manually one by one, Ubuntu environment provides scripts for us to do above tasks using tasksel command. These scripts will have all information about the software and installs all required packages.
So, we don't need to worry about the dependency packages need to install before installing desired program.

Use: sudo tasksel
Result: It will promt a window, where we can select the software need to install from pre-avialble software lists (ex: DNS server, FTP server, Email sever etc). Select the required options and click OK. Done. 

-----------------------------------------------------------------------------------------

Control the service:
We can restart/start/stop any service using below command

Use:

sudo /etc/init.d/<service_name> start
sudo /etc/init.d/<service_name> stop
sudo /etc/init.d/<service_name> restart
-----------------------------------------------------------------------------------------

TOP command

The command "top" is very much similar to task manager in windows system.
Using top command, we will see the data like CPU utilization and memory utilization per process and in addition processes info that are running etc.

Use: sudo top
Task Manager in the UNIX world.
We see process ids (PID), CPU utilization and memory utilization info.

Abort any process using command "kill"
Use: 
kill -9 <process_id>
or
pkill -9 <process_name>
---------------------------------------------------------------------------------------

Navigation:

Navigation between the directories are simple in linux environment using command "cd" (Change Directory)

Use:
cd <directory_name>
   -->means that change the current working directory to <directory_name>

cd /<directory_name>
  --> means that change the directory to <directory_name> under ROOT (/) directory.

Under ROOT, we do find the directories named "home", "etc" and others.
So, if we need to navigate to any directories under ROOT (/) then use slash (/) before that directory name
i.e.  cd /home

You might also like

Related Posts Plugin for WordPress, Blogger...