Write a C shell program that asks for and then reads a directory name, and then
ID: 3708653 • Letter: W
Question
Write a C shell program that asks for and then reads a directory name, and then displays all the files in this directory if it is a valid directory; otherwise(i.e. if it is not a valid directory), keeps displaying a message like “Enter a valid directory” until the user enters a valid directory.
Laité Springana_c5405'TI Cumparability Mascui] - Word FLE TOME MAINOS EVEW VW TEMAT DC INSERT DESIGNAGE LAYOUTURENCES Time No 4 AAA E. E BRU X , X A A Font t E . Parapan Aarhus AaBical AaBbc AaBbc. AaRhien ABC Mercado Normal Nu Spas.. Heading The Subtitle Sush Em... Emphash mum Aarhcel Abel Marcel AARRCC AABACC Mari Ankhcel. - Strony Quoka man Q. Sulitle Saf... Internal Re... Buck Th 1 Bua... ? - 5 x ASA Find Raplace Bad Select Eding Pas . Clipboard 3 grep.05105' 13b4_dala xl grep 'NEISF' lab4_data txt grep -. 'NEYSE' lah4_data txt Objectives: b) Write arep and regular expressions to accomplish the following pattern searches: Note: mydala txt contains the following This file is used for students to complete labi. L. Reinforce the skills of using the file management function editor vi in Linux; 2. Understand the Lioux bash shells, terminals, and the script language; 3. Gain the hands on shell script programming skills of using regular expressions urep ulalily. I will check it after one day or two. All students in CS105 will work on this ab. This lab is worth 15 points Students are free to use any information posted on Internet There are many useful tutonal matenals on Internet I will help students practice how to search the patiems like ??, ???, ????. ?????. ABC AICAOS ACADCADC, AAA AAAA AAAAA AAB AABB AABBB, etc. Tasks: 1. Create labbt data.txt file that contains the following data: porthwest NW Charles Main 300000.00 und WE Slue un CuY 3000.89 southwest SW Lewis Dalsass 290000.73 sur SO Sian Chin 31500 10 southeast SC Patricia Ilemelvax 100000.00 caster EA TB Savage 41030045 poutheast NE AM Mar J. 57800.00 north NO Ann Stephens 455000 50 Central CT KRieh 57550070 Extra [A Zi 40 9J..5.00 2. Use arep and regular expressions to complete the following a) Find out what the following regular expressions will point when they are executed with rep in Linux Pattern to search Regular Expressions Search all the ines in hle mydata.txt that begin with the word 'I vil" Search alle s in file muydala.xl that end with "Interrel What will be printed out? grep 'Explanation / Answer
#include <dirent.h>
#include <stdio.h>
#include <string.h>
int main()
{
DIR *d; // declaring a DIR
struct dirent *directory; // declaring a struct variable
int open = 0;
while (open != 1)
{
printf("Enter the valid directory name : ");
char arr[100];
scanf("%s", arr); //taking the input of the namme of the direcory
d = opendir(arr); // opening the directory
if (d == 1) // chceking if its a valid directory
{
open = 1; // storing value one to indicate successful opening of directory
while ((directory = readdir(d)) != NULL) //traversing the directory unless NULL
{
printf("%s ", directory->d_name); // printing the file names
}
closedir(d); // closing the directory
}
else
{
open = 0;
printf("Try again "); //error message in case of invallid directory
}
}
return 0;
}
Sample Output :
Enter the valid directory name :
./test
Try again
Enter the valid directory name :
/home/csvhd/test
..
Untitled Document 2
Untitled Document
.
Untitled Document 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.