Question

1. Program ls1.c list names of directory or directories. Program fileinfo.c retrieves and prints the file attributes. Revise

*** please the answer in C language , and please type it NOT hand writing .

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

c program that prints properties and contents of a directory:

#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
void printFileProperties(struct stat stats);
int main(void)
{
   struct dirent *de;
   DIR *dr = opendir(".");
struct stat stats;
   if (dr == NULL) {
       printf("Could not open current directory" );
       return 0;
   }
   while ((de = readdir(dr)) != NULL){
           printf("Name: %s\n", de->d_name);
if (stat(de->d_name, &stats) == 0){
printFileProperties(stats);
}
   }
   closedir(dr);
   return 0;
}

/**
* Function to print file properties.
*/
void printFileProperties(struct stat stats)
{


// File permissions
printf("\nFile access: ");
if (stats.st_mode & R_OK)
printf("read ");
if (stats.st_mode & W_OK)
printf("write ");
if (stats.st_mode & X_OK)
printf("execute");
printf("\n File inode: %d \n",stats.st_ino);
printf("File size: %d \n\n", stats.st_size);

}

output screenshot:

ㅃ main.c [ls1]-Code:Blocks 17.12 ㄇ ㄨ ㄧ Name: bin File access: read write execute ile inode: 0 File size: 0 Name: ls1.cbp File

Add a comment
Know the answer?
Add Answer to:
1. Program ls1.c list names of directory or directories. Program fileinfo.c retrieves and prints ...
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
  • Displays the names of the non-hidden files and directories in the current directory, or another directory,...

    Displays the names of the non-hidden files and directories in the current directory, or another directory, along with creation time, and size of file. In case of subdirectories, it displays the word <DIR> instead of file size. At the end, the command should display the total number of files in the directory, the total file size, and the number of sub-directories. Examples: LodgherDir (displays info of all files and directories in current directory) LodgherDir c:\LFromDir (displays info of all files...

  • In C++, Write a program that retrieves a phone number from a database of names and...

    In C++, Write a program that retrieves a phone number from a database of names and phone numbers. The program should ask the user to enter a name and then print the phone number of that person (or vice versa, number into name) The program should allow the user to repeat the operation as often as they would like to. The data is contained in two text files named "phoneNames.txt" and "phoneNums.txt". The files have one name or one phone...

  • 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...

  • This script will create a dictionary whose keys are all the directories listed in thePATH system...

    This script will create a dictionary whose keys are all the directories listed in thePATH system variable, and whose values are the number of files in each of these directories. The script will also print each directory entry sorted by directory name. The script will use the following five functions to get the required results get_environment_variable_value() get_dirs_from_path() get_file_count() get_file_count_for_dir_list() print_sorted_dictionary() get_environment_variable_value() The header for this function must be This function must accept a shell variable as its only parameter The...

  • 1. Write a program to input a list of names (strings) from the user and store...

    1. Write a program to input a list of names (strings) from the user and store them in an ArrayList. The input can be terminated by entering the empty string or by entering the string “quit”. 2. Add further functionality to your program so that it searches the ArrayList to find the first string and the last string according to dictionary ordering and then prints out these strings (names). Do this exercise without sorting the names in the ArrayList. For...

  • In C++, Write a program that retrieves a phone number from a txt files of names...

    In C++, Write a program that retrieves a phone number from a txt files of names and phone numbers. The program should ask the user to enter a name and then print the phone number of that person (or vice versa, number into name) The program should allow the user to repeat the operation as often as they would like to. The data is contained in two text files named "phoneNames.txt" and "phoneNums.txt". The files have one name or one...

  • 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...

  • Written in C++ language Write a program that keeps track of a telephone directory. The program...

    Written in C++ language Write a program that keeps track of a telephone directory. The program should create a structure, Person, with the following data: struct Person { string firstName; string lastName; string phoneNumber; }; The program should then create a vector of Person structs, called directory. Your program should have functions to: - Read the vector data from a data file “telephoneData.txt” into the vector directory. - Print the details of all Persons in the vector. You may use...

  • Step 3: How would you write this script in C? It needs to do the things...

    Step 3: How would you write this script in C? It needs to do the things required in step 3. 1. You have received a new batch of distinguished users; their basic information is located in newusers.tar. Inside of the tar file, there is a file called "newusers.txt" which contains a colon-separated entry for each user: the username, the uid, the GECOS information, and the user's preferred shell. Also in the tar file you will find a public key for...

  • Homework No.2 CSC 222 Write a shell script program called "countf.sh" that will count how many...

    Homework No.2 CSC 222 Write a shell script program called "countf.sh" that will count how many files or directories recursively. beside the file counts, also report how many files for each types (directory or regular files). It will either take arguments for specific directories and no argument. If no argument, it will read the current working directory as the starting point. Please also provide -h option to print out how to use your countf.sh program % ./countf.sh % ./countf.sh file1...

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
ADVERTISEMENT