Quote:
Originally Posted by jukofyork
NP
Juk
Bah I have more bugs:
Any way of knowing if there were additional characters input, or the total number of chars input, if they are greater than the param in fgets()?
I am supposed to print an error if they try to input a command longer than SHELL_BUFFER_SIZE.
I made another array to store the "history" (the previously input commands)
but for some reason the second realloc() call always fails, independent of my initial hsize. I put the excerpt below:
Code:
int hsize = 20;
char **hist = malloc(hsize * sizeof(char*));
int j = 0;
while(1){
printf("Shell(pid=%%%d)%%%d} ",pid,count);
fgets(input, SHELL_BUFFER_SIZE, stdin);
if(strcmp(input, "") != 0)
count++;
char *pt = NULL;
char temp[strlen(input)];
strncpy(temp,input,strlen(input)-1);
temp[strlen(input)-1] = '\0';
if(j < hsize){
hist[j] = malloc((strlen(temp)+1) * sizeof(char));
strcpy(hist[j],temp);
j++;
}
else{//double size of hist arr
hsize*=2;
void *error = realloc(hist, hsize);
if(!error){
printf("ERROR: realloc() fail.");
exit(1);
}
hist[j] = malloc((strlen(temp)+1) * sizeof(char));
strcpy(hist[j],temp);
j++;
}
Also, did my usage of fork() wait() and execvp() look ok in my other post?
I havent used system calls much.
Sorry for buggin you so much, it is just that this is due monday and I have a bunch of other homework, including preparing a 20 presentation on the ethics of computers and gambling, otherwise I would figure this stuff on my own, just I am pressed for time right now.
Thanks again.
Last edited by uofi2012; 09-13-2009 at 02:37 AM.