LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-03-2011, 07:29 PM   #1
khoh
LQ Newbie
 
Registered: Apr 2011
Posts: 5

Rep: Reputation: 0
adding commands to a limited shell program


In below program I want to add (as part of the valid_cmds string) the pwd (print working directory), lo (logout), and cd (change directory) commands. However when I add those into original program ;
char *valid_cmds = " ls ps df pwd lo cd";
they are not working I have the cout message huh?
Original source code is below

Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
using namespace std;

const int MAX    =256;
const int CMD_MAX=10;
char *valid_cmds = " ls  ps  df ";
int
main( ){
  char  line_input[MAX], the_cmd[CMD_MAX];
  char  *new_args[CMD_MAX], *cp;
  int   i;
  while (1) {
    cout << "cmd> ";
    if (cin.getline(line_input, MAX, '\n') != NULL) {
      cp = line_input;
      i  = 0;
      if ((new_args[i] = strtok(cp, " ")) != NULL) {
        strcpy(the_cmd, new_args[i]);
        strcat(the_cmd, " ");
        if ((strstr(valid_cmds, the_cmd) - valid_cmds) % 4 == 1) {
          do {
            cp = NULL;
            new_args[++i] = strtok(cp, " ");
          } while (i < CMD_MAX && new_args[i] != NULL);
          new_args[i] = NULL;
          switch (fork( )) {
          case 0:
            execvp(new_args[0], new_args);
            perror("exec failure");
            exit(1);
          case -1:
            perror("fork failure");
            exit(2);
          default:
            // In the parent we should be waiting for
            // the child to finish
            ;
          }
        } else
          cout << "huh?" << endl;
      }
    }
  }
}
 
Old 04-03-2011, 09:28 PM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
your string should be (pay attention to spaces):
Code:
char *valid_cmds = " ls  ps  df  pwd lo  cd ";
due to
Code:
if ((strstr(valid_cmds, the_cmd) - valid_cmds) % 4 == 1) {
.
 
Old 04-04-2011, 09:47 AM   #3
khoh
LQ Newbie
 
Registered: Apr 2011
Posts: 5

Original Poster
Rep: Reputation: 0
I tried adding two space character between each pwd lo and cd, eventhough pwd command is working lo and cd is not working,
I have the error huh?

And I also tried addign one space between pwd and lo still not working.
Code:
char *valid_cmds = " ls  ps  df  pwd lo  cd ";
 
Old 04-04-2011, 09:50 AM   #4
khoh
LQ Newbie
 
Registered: Apr 2011
Posts: 5

Original Poster
Rep: Reputation: 0
Sorry I got it thanks, one space between pwd and lo works fine. thanks a lot
 
  


Reply

Tags
c++


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to process billions of small files using Bash shell commands with limited memory? Osirix Linux - Newbie 1 05-26-2010 10:07 PM
Issuing commands from c++ program to the shell cyberfox007 Programming 7 02-03-2008 04:06 AM
Adding Commands to Shell metallica1973 Solaris / OpenSolaris 15 12-21-2007 02:17 PM
make c-program that accepts shell commands and execute them boeroe Programming 4 02-18-2005 08:41 AM
Adding shell commands to hosts.deny and hosts.allow ridertech Linux - Security 3 12-29-2003 03:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:05 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration