47.7k views
4 votes
Create a C program (assign01.c, producing the executable assign01) that takes any number of command line arguments. For each command line argument (except the first) the program prints the argument, and attempts to open the argument as a directory. If not a directory use perror(3) to print out the English equivalent of the errno. If a directory, then print out the type and name of its contents.

User Feifei
by
4.5k points

1 Answer

3 votes

Answer:

See explaination

Step-by-step explanation:

#include <stdio.h>

#include <dirent.h>

#include <string.h>

#include <errno.h>

extern int errno;

int main(int argc, char const *argv[])

{

DIR *dir;

struct dirent *ent;

int i;

if ( argc != 2 )

{

printf( "usage: %s filename", argv[0] );

}

else

{

for(i=1;i<argc;i++)

{

printf("\\ Name of directory no. %d %s \\ ",i,argv[1]);

if ((dir = opendir (argv[i])) != NULL)

{

/* print all the files and directories within directory */

while ((ent = readdir (dir)) != NULL) {

switch(ent->d_type)

{

case DT_REG:

printf ("%d \t %s \t --> %s\\",i, ent->d_name,"Regular File");

break;

case DT_DIR:

printf ("%d \t %s \t --> %s\\",i, ent->d_name,"Directory");

break;

case DT_UNKNOWN:

printf ("%d \t %s \t --> %s\\",i, ent->d_name,"Unknown File Type");

break;

case DT_BLK:

printf ("%d \t %s \t --> %s\\",i, ent->d_name,"Block Device");

break;

case DT_LNK:

printf ("%d \t %s \t --> %s\\",i, ent->d_name,"Symbolic Link");

break;

case DT_CHR:

printf ("%d \t %s \t --> %s\\",i, ent->d_name,"Character Device");

break;

}

i++;

}

printf ("\\");

closedir (dir);

}

else

{

perror("Error!: ");

//printf("errno = %d.\\", errno);

}

}

}

return 0;

}

User Lucatrv
by
4.9k points