-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_cmd.c
More file actions
82 lines (75 loc) · 1.31 KB
/
Copy pathprocess_cmd.c
File metadata and controls
82 lines (75 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//libraries
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <dirent.h>
#include <sys/dir.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include<sys/utsname.h>
#include "declarations.h"
//modifying cmd to give it to execvp
void process_cmd()
{
//check if bg
if(cmd[strlen(cmd)-1]=='&')
{
bg=1;
cmd[strlen(cmd)-1]='\0';//remove &
}
else
bg=0;
//printf("bg:%d\n",bg);
buf=malloc(100*sizeof(char*));
int i=0;
char*token=strtok(cmd," , ");
while((token!=NULL) && (bg==0 || (bg==1 && strcmp(token,"&"))))//to not store &
{
if(in1==1 || out1==1 || out2==1)
{
if(doubleredirect==0)
{
strcpy(redirectfile,token);
doubleredirect=1;
token=strtok(NULL," , ");
}
//break;
}
if(token!=NULL)
{
if(!strcmp(token,"<"))
{
in1=1;
}
else if(!strcmp(token,">"))
{
out1=1;
}
else if(!strcmp(token,">>"))
{
out2=1;
}
else
buf[i++]=token;
token=strtok(NULL," , ");
}
}
//both redirections
if((in1 && out1) || (in1 && out2))
{
strcpy(redirectfile2,buf[1]);
buf[1]=NULL;
}
buf[i]=NULL;
//for(int j=0;buf[j]!=NULL;j++)
// printf("%s\n",buf[j]);
}