UNIX COMMANDS

vinuta

vinuta

@vinuta-sKKXBQ Oct 26, 2024
#include <stdio.h>
main()
{
char str[10];
int pid;
pid=fork();
if(!pid)
{
printf("child process");
printf("enter the command");
scanf("%s",&str);
system(str)
printf("finished with child ");
}
else
wait();
retrun(0);
} 
output:-
child process:-
enter the command
ls -l


with this output i m just getting file names ...but i need ls -l --- lists files in 'long format', which contains lots of useful information, e.g. the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified.

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Jayaram Reddy

    Jayaram Reddy

    @jayaram-DSVwjp Dec 5, 2014

    Here in the command it will take only "ls" why because you are reading the string using scanf. It will read only one string here ls -l are two strings. So use "gets" function to read the string.....
    But here one warning will be there that u are reading a string using its address so it is better to use pointer declaration, not an array. But it is ignorable....😀