-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetOutput.c
More file actions
58 lines (54 loc) · 994 Bytes
/
setOutput.c
File metadata and controls
58 lines (54 loc) · 994 Bytes
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
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<linux/limits.h>
#include<signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<errno.h>
extern pid_t pid;
extern int counter;
typedef struct Node{
pid_t pid_process;
char pname[100];
int num;
struct Node *next;
}node;
extern node *head;
void setOutput(char **args, int tempout)
{
int i=0;
while(args[i]!=NULL)
{
if(strcmp(args[i],">")==0)
{
int fdout = open(args[i+1],O_RDWR|O_CREAT|O_TRUNC, 0777);
dup2(fdout,1); //set output to fdout
close(fdout);
int j=i;
while(args[j]!=NULL) // remove everything after ">"
{
args[j]=NULL;
j++;
}
return 0;
}
if(strcmp(args[i],">>")==0)
{
int fdout = open(args[i+1],O_RDWR|O_APPEND|O_CREAT, 0777);
dup2(fdout,1);
close(fdout);
int j=i;
while(args[j]!=NULL) // remove everything after ">>"
{
args[j]=NULL;
j++;
}
return 0;
}
i++;
}
}