forked from NEKTech-Labs/NEKTech-Linux-Shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_execution.c
73 lines (67 loc) · 2.38 KB
/
cmd_execution.c
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
/*
* cmd_execution.c - Linux Shell
*
* Copyright (C) NEK Tech 2013
* Developers V1.0: Pallavi Gadge
*
* Developers V2.0: Jitendra Khasdev
* run_cmd is renamed as nektech_run_cmd()
*
* Author and Architect: Pankaj Saraf
*
* This program is free software; you can redistribute it and/or modify
* it under the terms and conditions of NEK Tech.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY` WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
#include "nektech_shell.h"
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
extern char *redirt_file;
extern int redirection, append;
/*
*NEKTech Research Labs
*
*Function name : nektech_run_cmd()
*Description : This fuction forks a new process and exec() it with the
* respective binaries provided in the arguments.
*Developer : Pallavi Gadge
* : jitendra khasdev
*/
void nektech_run_cmd(char *argv[])
{
pid_t child_pid;
int fd;
child_pid=fork();
if(child_pid<0){
printf("SOME ERROR HAPPENED IN FORK\n");
exit(2);
}else if(child_pid==0){
if (redirection == 1){
if (append == 1)
fd = open(redirt_file, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
else
fd = open(redirt_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
dup2(fd, 1); // make stdout go to file
dup2(fd, 2); // make stderr go to file - you may choose to not do this
close(fd); // fd no longer needed - Duped to another fd#s
}
if(execvp(argv[0],argv)<0)
switch(errno){
case ENOENT:
printf("COMMAND OR FILENAME NOT FOUND\n");
break;
case EACCES:
printf("YOU DO NOT HAVE RIGHT TO ACCESS\n");
break;
default:
printf("SOME ERROR HAPPENED IN EXEC\n");
}
exit(3);
}else
wait(NULL);
}