-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
149 lines (135 loc) · 3.02 KB
/
functions.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "main.h"
/**
* count_tokens - Counts the number of tokens in a string.
* @inputline: The input string.
* @delims: The delimiter characters.
*
* Return: The number of tokens in your coin cup.
*/
int count_tokens(char *inputline, char *delims)
{
int len = 0;
char *linedup, *token;
/*
* because strtok destroys the string it operates on
* we malloc-duplicate a dummy
*/
linedup = strdup(inputline);
token = strtok(linedup, delims);
while (token != NULL)
{
token = strtok(NULL, delims);
len++;
}
free(linedup);
return (len);
}
/**
* create_tok_array - Creates an array of tokens from a string.
* @inputline: The input string.
* @delims: The delimiter characters.
* @toklen: The number of tokens.
*
* Return: An array of tokens for arcade fun!
*/
char **create_tok_array(char *inputline, char *delims, int toklen)
{
char **token, **array;
/* initialize the array */
array = malloc(sizeof(void *) * toklen);
*array = strtok(inputline, delims);
/* add tokes to the array untill NULL is returned */
token = array;
while (*token != NULL)
{
token++;
*token = strtok(NULL, delims);
}
return (array);
}
/**
* get_path - Gets the full path of a command.
* @cmdname: The command name.
*
* Return: The full path of the command.
*/
char *get_path(char *cmdname)
{
int cmdlen;
char *cmdpath;
/*
* special cases where command given is either
* an absolute path, a relative path, or the
* special env command
*/
if
(cmdname[0] == '/'
|| cmdname[0] == '.'
|| !strcmp(cmdname, "env"))
{
cmdpath = strdup(cmdname);
return (cmdpath);
}
/* otherwise construct a fullpath for the command given */
cmdlen = strlen(cmdname);
cmdpath = malloc(sizeof(char) * (cmdlen + 6));
cmdpath = strcpy(cmdpath, "/bin/");
cmdpath = strcat(cmdpath, cmdname);
return (cmdpath);
}
/**
* run_cmd - Executes a command with arguments.
* @cmdpath: The full path to the command.
* @token_array: An array of strings containing arguements.
*
* Return: The exit status of the command.
*/
int run_cmd(char *cmdpath, char **token_array)
{
struct stat file_stat;
pid_t child_proc;
/* execute _env() for special env command given */
if (!strcmp(cmdpath, "env"))
_env();
/* otherwise ask operating system to run the command */
else if (stat(cmdpath, &file_stat) == 0) /*executable exist */
{
/*create subprocess*/
child_proc = fork();
if (child_proc < 0)
return (errno);
/* replace the subprocess with the program */
else if (child_proc == 0)
{
if (execve(cmdpath, token_array, NULL) == -1)
return (errno);
}
/* parent process waits for the subprocess to conclude */
else
{
wait(&child_proc);
if (WIFEXITED(child_proc))
return (WEXITSTATUS(child_proc));
}
}
return (2);
}
/**
* _env - get environment
*
* Return: environ duplicate
*/
char **_env(void)
{
int i = 0;
char **env;
/* copy system's environ array onto our programs array */
env = environ;
while (env[i])
{
/* output the contents of array to stdout */
printf("%s\n", env[i]);
i++;
}
return (env);
}