LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 07-12-2011, 09:44 PM   #1
vikas_choudharyy
LQ Newbie
 
Registered: May 2011
Posts: 7

Rep: Reputation: Disabled
Unhappy how to fed command to shell through programm


THIS PROGRAM ACCEPTS ONLY COMMANDS WHICH ARE VALID WITHIN "/bin" I WANT TO MAKE IT GENERAL SO THAT IT CAN ACCEPTS ALL THE COMMANDS AND CAN WORK PROPERLY....
please help me....
thanx

//a prog to create a shell which do the same work as bash shell

#ifndef SHELL
#define SHELL
#include<stdio.h> // header files required for program
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<wait.h>
#define SIZE_OF_BUFFER 100// buffer which is used for command line arg
#define SIZE_OF_COMMAND 30
extern char **environ;
#endif

int main()
{
int pid,i;
char buffer[SIZE_OF_BUFFER+1];
char *command[SIZE_OF_COMMAND];
int catch_status;
while(1)
{
char path1[50] = { "/bin/" };
char path2[50] = { "/usr/bin/" };
printf( "08MCMC02_05@shell:~>" );
scanf( " %[^\n]s ", buffer );
if(!strcmp(buffer , "exit"))
exit(0);

if(!strcmp(buffer,"cd"))
{
printf(":: It does not create child, try other command \n");
continue;
}
command[0] = strtok( buffer , " ");
for(i=1;i<SIZE_OF_COMMAND;i++)
{
command[i] = strtok( NULL , " " );
if( command[i] == NULL )
break;
}
command[i] = NULL;

pid = fork();
switch(pid)
{
case 0:
strcat( path1 , command[0] );
execve( path1 , command , environ );

case -1:
strcat( path2 , command[0] );
execve( path2 , command , environ );
printf( " error \n" );
break;

default:
wait( &catch_status );
break;
}
for(i = 0 ; i < SIZE_OF_COMMAND ; i++)
command[i] = '\0';

for(i = 0 ;i < SIZE_OF_BUFFER + 1 ; i++)
buffer[i] = '\0';

for(i = 0 ; i < 50 ; i++)
path1[i] = '\0';

for(i = 0 ; i < 50 ; i++)
path2[i] = '\0';

}

}
 
Old 07-13-2011, 08:10 AM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
First of all, use code tags to make it readable.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using shell command output as input in shell script - how to do? EnderX Linux - Newbie 2 06-30-2010 01:46 PM
How window in programm can be resized by another programm. rexStart Programming 1 09-23-2008 03:47 AM
How to run root privileged Linux command as normal user via shell shell tcegrid Linux - Newbie 1 06-23-2008 04:38 PM
Upgrade Fed 6 to Fed 8 using only the *.iso ericcarlson Fedora 1 11-18-2007 03:25 AM
cancel shell programm is dir is empty saavik Programming 1 12-29-2003 07:23 AM


All times are GMT -5. The time now is 06:36 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration