Separating commands/programs with ;


 
Thread Tools Search this Thread
Top Forums Programming Separating commands/programs with ;
# 1  
Old 06-18-2006
Data Separating commands/programs with ;

Hi i have encountered a problem and i have tried many different things but my brain just has some limitations lol well anyways i was trying to make this program work down below so i can process multiple commands just by separating them with ;. I would apeciate if someone could just make it work kuz i have tried everything.

#include <stdio.h>
#include <string.h>
#include <unistd.h>

pid_t myFork();
int readArgs(const char*,char*[]);

int main (int argc, char *argv[]){
pid_t pid;
char line [255];
char *argList[20];
int ampersand, status, i;

printf ("This program executes commands and \n");
printf ("progams for you \n");

while(1){
printf ("To exit, enter CTR-C, or enter\n");
printf ("a program name with its arguments> ");
fgets(line,255,stdin);
ampersand=readArgs(line,argList);
if ((pid = myFork ()) == -1){
perror("impossible to fork");
exit(1);
}

if (pid>0) //This is the parent
if (ampersand) //Background execution
printf("Process [%d]\n", pid);
else{
waitpid(pid, &status,0);
printf("My child has terminated\n");
}
else //this is the child
if (execvp(argList[0], argList)==-1){
perror("child Process");
exit(22);
}
}
exit(0);
}


pid_t myFork(){
static int count=0;

count++;
if (count<=20)
return(fork());
else
return(-1);
}

int readArgs(const char *line, char *argList[]){
static int yes=0;
int i=0, offset=0;
char name[50];
int found=0;

while (yes & argList[i] !=NULL)
free(argList[i++]);
i=0; //reset i to ZERO
while(sscanf(line+offset, "%s", name)==1){
argList[i] = (char *)malloc(strlen(name)+1);
strcpy(argList[i++],name);

while(line[offset]==' ') offset++; //skip blanks
offset += strlen(name);
}
if (!strcmp(argList[i-1], "&")){
argList[i-1] = NULL;
found = 1;
}
else{
if (argList[i-1][strlen(argList[i-1])-1]=='&'){
argList[i-1][strlen(argList[i-1])-1]='\0';
found = 1;
}
argList[i] = NULL;
}
yes=1;
return(found);
}
# 2  
Old 06-21-2006
Please put your code in code tags. It's nigh unreadable without them.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct buffer
{
    // Position of string segment in dat, length of data in dat
    int start, end, len;
    // Position in proc
    int pstart, pend;
    // String data
    char dat[256];
    // String tokens
    char proc[512];
} buffer;

int buffer_read(FILE *fp, buffer *b)
{
  fprintf(stderr,"Input: ");
  if(fgets(b->dat,256,fp)==NULL)
    return(-1);

  b->pstart=1;
  b->pend=1;
  b->start=0;
  b->end=0;
  b->len=strlen(b->dat);
  b->proc[0]='\0';
  b->proc[1]='\0';
  return(0);
}

char **buffer_get_args(buffer *p)
{
  int numargs=0,running=1;
  char **out=NULL;

  while((p->start < p->len) && running)
  {
    int tmp;

    switch(p->dat[p->end])
    {
    // Seperate one argument from another.
    case '\r':
    case '\n':
    case '\t':
    case ' ':
      // Don't add another seperator if already seperated
      if(p->proc[p->pend-1]!='\0')
      {
        // Add a NULL terminator to make it a seperate string
        p->proc[p->pend++]='\0';
        out=(char **)realloc(out,sizeof(char **)*(numargs+1));
        out[numargs++]=p->proc + p->pstart;

        p->pstart=p->pend;
      }

      p->start=(++p->end);
      continue;

    // Seperates groups of arguments from each other
    case ';':
    case '&':
      tmp=p->dat[p->end];

      if(p->pend > p->pstart)
      {
        // Add a NULL terminator to make it a seperate string
        p->proc[p->pend++]='\0';
        out=(char **)realloc(out,sizeof(char **)*(numargs+1));
        out[numargs++]=p->proc + p->pstart;

        p->pstart=p->pend;
      }

      // Ignore multiple of the same
      while(p->dat[p->end] == tmp)
        p->end++;

      p->start=p->end;

      if(tmp == '&')
      {
        p->proc[p->pend++]='&';
        p->proc[p->pend++]='\0';

        out=(char **)realloc(out,sizeof(char **)*(numargs+1));
        out[numargs++]=p->proc + p->pstart;

        p->pstart=p->pend;
      }

      running=0;
      continue;
    // Another character.  Add stuff on.
    default:
      p->proc[p->pend++] = p->dat[p->end];
      break;
    }

    p->end++;
  }

  p->proc[p->pend]='\0';

  if(out != NULL)
  {
    out=(char **)realloc(out,sizeof(char **)*(numargs+1));
    out[numargs++]=NULL;
  }

  return(out);
}

int main(int argc, char *argv[])
{
  buffer input;

  while(buffer_read(stdin,&input)>=0)
  {
    char **args;

    while(args=buffer_get_args(&input))
    {
      int n;
      for(n=0; args[n] != NULL; n++)
        fprintf(stderr," [%s]",args[n]);

      fprintf(stderr,"\n");

      free(args);
    }
  }

  return(0);
}

# 3  
Old 06-22-2006
you are trying to read a line were segments a separated by ; ?
getdelim() delimited string input
an other funny way is to use the argz_* stuff.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Different counts between programs and commands

In the attached file if I do a count for "aaaaaaaaaaaa" in either notepad++ or excel I get 118,456. However, when I do either grep -o aaaaaaaaaaaa 12A.txt | wc -w or awk '{ for (i=1;i<=NF;i++) if ( $i == "aaaaaaaaaaaa") c++ } END{ print c}' 12A.txt I... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. UNIX for Dummies Questions & Answers

Difference between inbuilt suid programs and user defined root suid programs under bash shell?

Hey guys, Suppose i run passwd via bash shell. It is a suid program, which temporarily runs as root(owner) and modifies the user entries. However, when i write a C file and give 4755 permission and root ownership to the 'a.out' file , it doesn't run as root in bash shell. I verified this by... (2 Replies)
Discussion started by: syncmaster
2 Replies

3. Shell Programming and Scripting

Adding and then separating the output

Hi I have this output /vol/vol0 4GB /vol/nonprod00 682GB /vol/prod00 3GB /vol/nasp_archive 201GB /vol/nasp_iface 92GB /vol/nasp_bsi 0GB /vol/nasp_vertex 0GB /vol/nasp_sapmnt_mp2 1GB /vol/nasp_sapmnt_prd 52GB /vol/nasp_sapmnt_srp 1GB /vol/nasp_smd 1GB /vol/nasp_ccms 0GB... (8 Replies)
Discussion started by: bombcan
8 Replies

4. Shell Programming and Scripting

Need help separating a file

Hi all, I have a single text file, Contig3.fasta, that looks like this: >NAME1 ACCTGGTA >NAME2 GGTTGGACA >NAME3 ATTTTGGGCCAnd It has about 100 items like this in it. What I would like to do is copy each item into 100 different text files, and have them named a certain way Output... (4 Replies)
Discussion started by: repiv
4 Replies

5. Shell Programming and Scripting

separating folders

I have folder like main. inside main folder there are subfolders & files like main1 main2 main3, file1, file2, file3. I want folders main1 & main2, file1, file2 from main folder. copy them into new folder. Please suggest me how to do it. I am new to shell programming (2 Replies)
Discussion started by: ypremcha
2 Replies

6. Shell Programming and Scripting

Separating fields

Hi, I have a text file in following format: 2.45 5.67 6.43 I have to cut the values before decimal and store them in a file. So the output file should look like: 2 5 6 . . and so on... Can someone suggest me a sed/awk command for doing this? (2 Replies)
Discussion started by: sajal.bhatia
2 Replies

7. UNIX for Dummies Questions & Answers

Are programs like sys_open( ) ,sys_read( ) et al examples of system level programs ?

Are the programs written on schedulers ,thread library , process management, memory management, et al called systems programs ? How are they different from the programs that implement functions like open() , printf() , scanf() , read() .. they have a prefix sys_open, sys_close, sys_read etc , right... (1 Reply)
Discussion started by: vishwamitra
1 Replies

8. Shell Programming and Scripting

separating fields

Hi, i have a file as follows: jonathan:bonus1,bonus2 gerald:bonus1 patrick:bonus1,bonus2 My desired output is jonathan:bonus1 jonathan:bonus2 gerald:bonus1 patrick:bonus1 patrick:bonus2 my current code is cat $F | awk -F"" how should i continue the code? Can i do something... (5 Replies)
Discussion started by: new2ss
5 Replies

9. Shell Programming and Scripting

i need c programs for linux commands

hi i am doing a small project on linux shell so if anyone can provide me some programs which can ipmlement commands like copy,date,calendar ....thank u (1 Reply)
Discussion started by: lit_joel
1 Replies

10. Programming

separating commands

hey there well i have a small problem with my code. when for example : " /bin/sleep 10 & ls -l mila > xyz " is entered, the program is supposed to separate the two commands 1) /bin/sleep 10 & and 2) ls -l mila > xyz. im not sure of how to achieve this. my current program stores both commands... (2 Replies)
Discussion started by: mile1982
2 Replies
Login or Register to Ask a Question