Question

In: Computer Science

Parse the dirent struct to see if an entry is a directory or a file. If...

Parse the dirent struct to see if an entry is a directory or a file. If it is a directory, prepend "./" to the filename before printing it out. Include a main that does this below.

Please do the above code using C language.

Solutions

Expert Solution

ANSWER:

I have provided the properly commented code in both text and image format so you can easily copy the code as well as check for correct indentation.
Have a nice and healthy day!!

CODE TEXT

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

int main() {
  DIR *dir;
  struct dirent *ent;

  if ((dir = opendir("\\")) == NULL)
    perror("opendir() error");
  else {
    puts("contents of root:");
    while ((ent = readdir (dir)) != NULL) {
      // condition for file, if . in  name but .. not in name and only . not in name
      if (strstr(ent->d_name, ".")!=NULL && strstr(ent->d_name, "..")==NULL && strcmp(ent->d_name, ".")!=0)
        printf ("File: %s\n", ent->d_name);
      else{
        // its directory
        // defining a prepend string
        char prepend[100] = ".\\";
        // strcat to prepend it
        strcat(prepend,ent->d_name);
        // displaying dir
        printf ("Folder: %s\n", prepend);
      }
    }
    closedir(dir);
  }
}

CODE IMAGE


Related Solutions

Write a bash script file that tests each file entry in the current directory. It should...
Write a bash script file that tests each file entry in the current directory. It should determine if it is a file or directory. If it is a file, it will determine if it is readable or not. If it is readable, it will display only the first 4 lines to the terminal in sorted order (just sort the first 4 lines). If it is a directory, it should display the contents of that directory (the files and subdirectories). NOTE:...
Parse the file /usr/src/include/minix/ipc.h (Struct message) Modify the newly created system call to receive and print...
Parse the file /usr/src/include/minix/ipc.h (Struct message) Modify the newly created system call to receive and print a parameter of type long through the structure “message” (the System_Calls_in_Minix.pdf file may be helpful). From the test file, the parameter must be sent to the user library created in the previous exercise, which in turn sends the parameter to the syscall. Your library must use the M4 type message. The system call must print the arbitrary number sent by the user code through...
Move the file “nfile2” into the “unix” directory. Copy the file “nfile1” into the “java” directory....
Move the file “nfile2” into the “unix” directory. Copy the file “nfile1” into the “java” directory. Show how you would create the files “test1”, “test2”, & “test3” in the “test” directory to ensure that they have the same file creation date/time. From the ~ folder, use the * and then the [ ] wildcard procedures to list all the files in the “test” directory. From the directory structure above, give an example of a relative path & an absolute path....
list.h file #ifndef LIST_H_ #define LIST_H_ struct ListNode { long value; struct ListNode *next; }; struct...
list.h file #ifndef LIST_H_ #define LIST_H_ struct ListNode { long value; struct ListNode *next; }; struct ListNode *list_prepend(struct ListNode *list, long value); int list_length(struct ListNode *list); struct ListNode *list_remove(struct ListNode *list, long value); #endif /* LIST_H_ */ given.c file #include #include "list.h" struct ListNode *list_prepend(struct ListNode *list, long value) { struct ListNode *node = malloc(sizeof(struct ListNode)); node->value = value; node->next = list; return node; } list.c file #include #include "list.h" /* Counts and returns the number of nodes in the...
Write a script that prompts the user for a pathname of a file or directory and...
Write a script that prompts the user for a pathname of a file or directory and then responds with a description of the item as a file along with what the user's permissions are with respect to it (read, write, execute).
Create a file named work.sh in your hw5 directory. Give this file read and write permission...
Create a file named work.sh in your hw5 directory. Give this file read and write permission (no execute permissions) for you alone. No other account should have any access privileges to this file. Change the permissions on the work.sh file so you have read and write permissions. Give everybody else, including the group, read permissions only. Give yourself read, write and execute permissions to the file work.sh. Give everyone else, including the group, execute permissions only. Create a directory named...
What are System calls. Provide 6 file and directory system calls
What are System calls. Provide 6 file and directory system calls
Go to your “Documents” directory; a) Create a file “file1” with the text “hello” in it;...
Go to your “Documents” directory; a) Create a file “file1” with the text “hello” in it; Provide command and screenshot. b) Create a hard link named “file2” linked to “file1”; c) Create a soft link named “soft1” linked to “file1”; create another soft link named “soft2” linked to “file2”. d) View information of the 4 files (what command should you use to produce output) – how do you verify which file is a hard link and which file is a...
using java, parse a text file to answer the following question: -list sentences with the maximum...
using java, parse a text file to answer the following question: -list sentences with the maximum number of occurences of the word “the” in the whole file and also list the corresponding frequency. (cannot use hash maps) example output: the:3:The day had came to leave before the storm. What hit the back bumper of the car before the window cracked? The classroom doors where shut closed before the students open the project.
given a input file, parse it and answer the following frequency related questions, using java. -list...
given a input file, parse it and answer the following frequency related questions, using java. -list the most frequent word(s) in the whole file and its frequency. -list sentence(s) with the max. number of occurrences of the word “of” in the entire file and also list the corresponding frequency. program has two arguments; 1st : path to the input text file 2nd : name prefix for the output files ex. $ java assgn1 “./input.txt” “output” outputs: for each question create...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT