c program to implement the unix or linux command to
implement
ls -l >output.txt
Answers were Sorted based on User's Feedback
Answer / rahul magdum
#include sys/types.h
#include unistd.h
#include sys/stat.h
main()
{
Int pid=fork();
Int fd=creat("op.txt",S_IRWXU);
If(pid!=0 && pid!=-1)
{
close(1); // stdout
dup(fd); // replace file descriptor
execlp("ls","ls","-l",null);
close(fd);
}
}
Is This Answer Correct ? | 22 Yes | 7 No |
Answer / rucha
read the files in current directory using opendir and
readdir system calls. readdir will return filenames in the
directory. For each entry returned by readdir system call,
use stat system call to read statistics of the file. Stat
system call will give all the info about the file as that
of ls -l command.
Is This Answer Correct ? | 17 Yes | 4 No |
Answer / muralidharan
Please use the system function
Itz may be helpful for implement the unix command in c program
syntax : - system("clear");
Is This Answer Correct ? | 4 Yes | 3 No |
Answer / gunjan
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main(int argc, char* argv[])
{
DIR *mydir;
struct dirent *myfile;
struct stat mystat;
mydir = opendir(argv[1]);
while((myfile = readdir(mydir)) != NULL)
{
stat(myfile->d_name, &mystat);
printf("%d",mystat.st_size);
printf(" %s
", myfile->d_name);
}
closedir(mydir);
}
Is This Answer Correct ? | 3 Yes | 5 No |
Tell me how to protect a process from others to kill?
What is the difference between command and utility in unix?
Explain c program to implement the unix or linux command to implement ls -l >output.txt?
Need a Code snippet about how sharing is happening between Parent and Child threads in OS?
The very first process created by the kernel in unix is?
Explain the mount system calls?
c program to implement the unix or linux command to implement ls -l >output.txt
What are the Unix system calls for I/O?
What command is used to execute system calls from exe?
What are the possible return values of kill() system call?
Describe the mount and unmount system calls?
What is the fork() system call?