Question

I need to develop a C program that can recursively navigate the current working directory and...

I need to develop a C program that can recursively navigate the current working directory and output the information (similar to what "ls -l" does). I cant hard code path names or use system() calls.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code along with comments below.

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

void file_list(const char *curd)
{
struct dirent *dr;
DIR *dir = opendir(curd);

if (!dir)
printf("Error!"); // print error if there is no directory

while ((dr = readdir(dir)) != NULL)
{   
printf("%s\n",dr->d_name); // print the sub-directories and files
}
  
closedir(dir); // Closing the directory
}

int main()
{

char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL) { // Current directory value
printf("Current working dir: %s\n", cwd);
} else {
perror("getcwd() error"); // Print error if current directory is not found
return 1;
}
printf("List of all files and sub-directories at current directory are :\n ");
file_list(cwd);
return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
I need to develop a C program that can recursively navigate the current working directory and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program countFiles.c to be executed on the command line as follows: countFiles <directory> The program...

    Write a C program countFiles.c to be executed on the command line as follows: countFiles <directory> The program should count the (regular) files in the specified directory as well as all subdirectories and output the total number on the console. Files and subdirectories whose names .start with should be ignored! To do this, define a function int countFilesRec(char* dirName)that dirName returns the number of (regular) files in the directory and all the subdirectories. Call the function recursively to count the...

  • 1. Navigate to your home directory by typing cd. Now type ls and notice that there...

    1. Navigate to your home directory by typing cd. Now type ls and notice that there is a subdirectory called Downloads. Now navigate to the root of the file system by typing cd /. From here, which single command would you use to navigate directly to the Downloads folder that is located in your home directory? The output of the command would be as follows: 2. From the Downloads directory which single command would you use to navigate back up...

  • C program To develop a C program to implement a process system call. PROBLEM You are...

    C program To develop a C program to implement a process system call. PROBLEM You are to use the Ubuntu operating system to write a C program that creates a process to determine the identification of the current user of your computer. I mean if joe is the login of the current user of your computer your solution should return joe. Your solution must also provide the pid of both the parent and the child processes. You may use the...

  • writting a c program that does the following: example output • Use Descriptor I/O operations to...

    writting a c program that does the following: example output • Use Descriptor I/O operations to open the file specified as a command line argument and read at least one byte from the sequence of bytes before Hard Coded Pause Point 1. Use file system operations to remove the i-node entry for the file opened in between Hard Coded Pause Point 1 and Hard Coded Pause Point 2. You may assume the file specified on the command line will have...

  • write a C or Fortran program that sends a token in a ring for an arbitrary number of nodes: Create a directory in your h...

    write a C or Fortran program that sends a token in a ring for an arbitrary number of nodes: Create a directory in your home directory titled exactly PA1. Make sure you have ALL code located in this directory. 4. Write a makefile that will compile your code. 5. Use MPI to send a token in a ring for any number of nodes 6. Verify that your code is working correctly. 7 Write a C program that sends a token...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • Overview Writing in the C language, implement a very basic shell, called smash. In this project,...

    Overview Writing in the C language, implement a very basic shell, called smash. In this project, we will work on processing strings and using the appropriate system calls. Build System setup Before we can start writing code we need to get our build system all setup. This will involve writing a very simple makefile. You should leverage your Makefile project for this part of the assignment! Write a Makefile that provides the three targets listed below all - The default...

  • Write a Java program that creates and manipulates a directory of names, telephone numbers. The following...

    Write a Java program that creates and manipulates a directory of names, telephone numbers. The following information will be stored for each person in the directory: - Name (Last, First) - Home telephone number You should keep the entire collection ordered by key value (the combination of last and first names). Your program should be able to perform the following basic functions: - Search and display the contents of a particular entry - Display the entire directory - Delete an...

  • Question 2 0/1 point (graded) What happens when you remove a directory using the command rm...

    Question 2 0/1 point (graded) What happens when you remove a directory using the command rm -r? You cannot remove a directory using the rm command. You permanently remove the entire directory, including all files and subdirectories. You move the entire directory to a trash folder, but it can be restored later. You get a warning message asking if you want to proceed, then you delete the directory. incorrect Answer Incorrect: Try again. Unix does not warn you before permanently...

  • Create a Java program that analyzes a program and produces the output into another file. The...

    Create a Java program that analyzes a program and produces the output into another file. The program must retrieve the path to the .java file. Once the path to the file is found the program must figure out the length of the longest line is in the program. For example, working with a program called MainFile.java. Once it figures out the longest line in the code it will put the output into MainFile.java.stats and it will be located in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT