Page Actions
Wiki Actions
User Actions
Submit This Story

A simple "ls"

Simple implemention of “ls”

The Code

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
  DIR *dp;
  struct dirent *dirp;

  if (argc != 2)
    {
      fprintf(stderr,"usage: ls dir_name\n");
      exit(EXIT_FAILURE);
    }

  if ((dp = opendir(argv[1])) == NULL)
    {
      fprintf(stderr,"cannot open %s\n",argv[1]);
      exit(EXIT_FAILURE);
    }    

  while ((dirp = readdir(dp)) != NULL)
    printf("%s type=%d\n", dirp-&gt;d_name,dirp-&gt;d_type);

  closedir(dp);

  exit(EXIT_SUCCESS);
}

Discussion

Agatha, 2012/01/06 10:35

Smart thinking - a celver way of looking at it.

Enter your comment
 
 
blog/2009/10/a_simple_ls.txt · Last modified: 2009/10/01 00:00 (external edit)     Back to top
Recent changes RSS feed Creative Commons License Powered by PHP Driven by DokuWiki