this is the whole code if needed
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
/*check correct num of arguements*/
if(argc != 2)
{
printf("Wrong number of arguments!") ;
return 0 ;
}
FILE *f ;
f = fopen(argv[1], "r") ;
char *line[100] ;
char *word ;
int count = 0 ;
while (!feof(f))
{
word = malloc(100) ;
fgets(word, 10000, f) ;
line[count] = word ;
count++ ;
}
char *parameter[100] ;
char *path ;
char *token ;
int count2 = 0 ;
int c ;
for (c = 0 ; c < count - 1 ; c++)
{
token = strtok(line[c], " \n") ;
while (token != NULL)
{
if(!strcmp("setpath", token))
{
//printf("newpath\n") ;
count2++ ;
token = strtok(NULL, " \n") ;
path = malloc(100) ;
path = token ;
printf("path = %s\n", path) ;
count2++ ;
token = strtok(NULL, " \n") ;
continue ;
}
parameter[count2] = token ;
printf("token = %s\n", parameter[count2]) ;
count2++ ;
token = strtok(NULL, " \n") ;
}
parameter[count2] = NULL ;
count2 = 0 ;
}
//debugging
printf("%s\n", path) ;
printf("%s\n", parameter[0]) ;
strcat(path, parameter[0]) ;
printf("%s\n", path) ;
int myid = fork() ;
if (myid == 0)
{
execv(path, parameter) ;
printf("Invalid command") ;
exit(1) ;
}
waitpid(myid, 0, 0) ;
printf("\n") ;
fclose(f) ;
return 0 ;
}
input file contains
Code:
setpath /usr/bin/
w -s
thanks in advance again