Wednesday, 19 October 2016

Ls command of Unix implement in c language

#include<stdio.h>
 #include<stdlib.h>
 #include <sys/types.h>
 #include <dirent.h>
 
 int main()
 {
    char *curr_dir = NULL;
    DIR *dp = NULL;
    struct dirent *dptr = NULL;
    unsigned int count = 0;
     curr_dir = getenv("PWD");
    if(NULL == curr_dir)
    {
        printf("\n ERROR : Could not get the working directory\n");
        return -1;
    }
   dp = opendir((const char*)curr_dir);
    if(NULL == dp)
    {
        printf("\n ERROR : Could not open the working directory\n");
        return -1;
    }
      printf("\n");
        for(count = 0; NULL != (dptr = readdir(dp)); count++)
    {
        printf("%s  \n",dptr->d_name);
    }
    printf("\n %u", count);
      return 0;
 }

No comments:

Post a Comment