Programs
Programs
1.) Write a C program to emulate the ls -l UNIX command that prints all files in a current directory
and lists access privileges, etc. DO NOT simply exec ls -l from the program.
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include<dirent.h>
int main()
DIR * dp;
return 0;
chdir(".");
lstat(entry->d_name,&statbuf);
if(!S_ISDIR(statbuf.st_mode))
{
printf("%s \t \t",entry->d_name);
printf("\n\n");
Output
2.) Write a program that will list all files in a current directory and all files in subsequent
subdirectories.
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
DIR *dp;
return;
chdir(dir);
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode))
/* Found a directory,
if(strcmp(".",entry->d_name) == 0 ||strcmp("..",entry->d_name) == 0)
continue;
printf("%*s%s/\n",depth,"",entry->d_name);
/* Recurse at a new
indent level */
printdir(entry->d_name, depth+4);
else { printf("%*s%s\n",depth,"",entry->d_name); }
chdir("..");
closedir(dp);
int main()
printdir(".",0);
printf("Done.\n");
exit(0);
Output