Removed submodules
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void usage (void);
|
||||
|
||||
bool
|
||||
is_util (char *str)
|
||||
{
|
||||
char *utils[] = { "./bin/cat", "./bin/chmod", "./bin/cut", "./bin/env",
|
||||
"./bin/head", "./bin/ls", "./bin/repeat" };
|
||||
|
||||
for (size_t i = 0; i < 7; i++)
|
||||
{
|
||||
if (strcmp (str, utils[i]) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[], char *envp[])
|
||||
{
|
||||
size_t envp_count = 0;
|
||||
while (envp[envp_count] != NULL) envp_count++;
|
||||
|
||||
size_t env_args = 0;
|
||||
char* util = NULL;
|
||||
|
||||
for (size_t i = 1; i < argc; i++)
|
||||
{
|
||||
if (is_util (argv[i])) {
|
||||
util = argv[i];
|
||||
break;
|
||||
}
|
||||
|
||||
env_args++;
|
||||
}
|
||||
|
||||
if (util == NULL) {
|
||||
usage();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char **combined_envp = calloc (envp_count + env_args + 1, sizeof (char *));
|
||||
memcpy (combined_envp, envp, envp_count * sizeof (char *));
|
||||
memcpy (combined_envp + envp_count, &argv[1], env_args * sizeof (char *));
|
||||
combined_envp[envp_count + env_args] = NULL;
|
||||
|
||||
execve(util, &argv[env_args + 1], combined_envp);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
static void
|
||||
usage (void)
|
||||
{
|
||||
printf ("env, set environment variables and execute program\n");
|
||||
printf ("usage: env [name=value ...] PROG ARGS\n");
|
||||
}
|
||||
Reference in New Issue
Block a user