![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| separating entries by using cat command | za_7565 | Shell Programming and Scripting | 7 | 01-29-2008 04:00 AM |
| separating filename and extension | lucaspewkas | Shell Programming and Scripting | 2 | 04-06-2007 04:07 AM |
| separating fields | new2ss | Shell Programming and Scripting | 5 | 02-19-2006 05:02 PM |
| i need c programs for linux commands | lit_joel | Shell Programming and Scripting | 1 | 11-20-2005 12:22 AM |
| separating commands | mile1982 | High Level Programming | 2 | 09-13-2004 08:41 AM |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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); } |
| Forum Sponsor | ||
|
|
|
|||
|
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);
}
|