The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
separating entries by using cat command za_7565 Shell Programming and Scripting 7 01-29-2008 08:00 AM
separating filename and extension lucaspewkas Shell Programming and Scripting 2 04-06-2007 08:07 AM
separating fields new2ss Shell Programming and Scripting 5 02-19-2006 09:02 PM
i need c programs for linux commands lit_joel Shell Programming and Scripting 1 11-20-2005 04:22 AM
separating commands mile1982 High Level Programming 2 09-13-2004 12:41 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-18-2006
dush_19 dush_19 is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 1
Unhappy 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 (permalink)  
Old 06-21-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,965
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 (permalink)  
Old 06-22-2006
grumpf grumpf is offline
Registered User
  
 

Join Date: May 2006
Posts: 101
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0