Convert tabs to spaces, alter some lines
This commit is contained in:
parent
251e88f324
commit
0d4a741186
42
src/icmd.c
42
src/icmd.c
@ -72,21 +72,21 @@ _icmd_die(int status, char *func, char *format, ...)
|
|||||||
int
|
int
|
||||||
find_icmd(char *cmd)
|
find_icmd(char *cmd)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
for (i = 0; i < ICMDS_SIZE; i++)
|
for (i = 0; i < ICMDS_SIZE; i++)
|
||||||
/* I'm doing a len+1 here because I want to compare with NUL explicitly */
|
/* I'm doing a len+1 here because I want to compare with NUL explicitly */
|
||||||
if (strncmp(g_icmds[i].cmd, cmd, g_icmds[i].len+1) == 0)
|
if (strncmp(g_icmds[i].cmd, cmd, g_icmds[i].len+1) == 0)
|
||||||
return i;
|
return i;
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
call_icmd(int idx, int argc, char **argv)
|
call_icmd(int idx, int argc, char **argv)
|
||||||
{
|
{
|
||||||
return g_icmds[idx].func(argc, argv);
|
return g_icmds[idx].func(argc, argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -139,12 +139,8 @@ icmd_exit(int argc, char **argv)
|
|||||||
int
|
int
|
||||||
icmd_cls(int argc, char **argv)
|
icmd_cls(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int i = 24;
|
/* use external command to do this job */
|
||||||
|
return system("clear -x");
|
||||||
while (i--)
|
|
||||||
putchar('\n');
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -184,20 +180,20 @@ icmd_ls(int argc, char **argv)
|
|||||||
if ((dir = opendir(path)) == NULL)
|
if ((dir = opendir(path)) == NULL)
|
||||||
icmd_die(-1, "Could not open directory '%s'", path);
|
icmd_die(-1, "Could not open directory '%s'", path);
|
||||||
|
|
||||||
/* print terminating slash if it wasn't given in argv */
|
/* print terminating slash if it wasn't given in argv */
|
||||||
printf("\n%s%c\n", path, (path[strlen(path)-1] != '/') ? '/' : '\0');
|
printf("\n%s%c\n", path, (path[strlen(path)-1] != '/') ? '/' : '\0');
|
||||||
|
|
||||||
while ((d = readdir(dir)) != NULL)
|
while ((d = readdir(dir)) != NULL)
|
||||||
{
|
{
|
||||||
/* skip printing current directory '.\0' and parent directory '..\0' */
|
/* skip printing current directory '.\0' and parent directory '..\0' */
|
||||||
if (d->d_name[0] == '.' && (d->d_name[1] == '\0'
|
if (d->d_name[0] == '.' && (d->d_name[1] == '\0'
|
||||||
|| (d->d_name[1] == '.' && d->d_name[2] == '\0')))
|
|| (d->d_name[1] == '.' && d->d_name[2] == '\0')))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
printf(" %s%c\n", d->d_name, (d->d_type == DT_DIR) ? '/' : '\0');
|
printf(" %s%c\n", d->d_name, (d->d_type == DT_DIR) ? '/' : '\0');
|
||||||
}
|
}
|
||||||
|
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
path = argv[++i];
|
path = argv[++i];
|
||||||
|
|
||||||
|
86
src/main.c
86
src/main.c
@ -12,8 +12,8 @@
|
|||||||
#include "icmd.h"
|
#include "icmd.h"
|
||||||
|
|
||||||
|
|
||||||
#define INPUT_MAX_LEN 1024
|
#define INPUT_MAX_LEN 1023
|
||||||
#define PATH_MAX_LEN 4096
|
#define PATH_MAX_LEN 4095
|
||||||
|
|
||||||
#define TOKENS_NUM_MAX 512
|
#define TOKENS_NUM_MAX 512
|
||||||
#define TOKENS_ARGS_SIZE 2048
|
#define TOKENS_ARGS_SIZE 2048
|
||||||
@ -26,13 +26,13 @@ char g_args[TOKENS_ARGS_SIZE];
|
|||||||
void
|
void
|
||||||
print(char *format, ...)
|
print(char *format, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
|
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
printf("psh: ");
|
printf("psh: ");
|
||||||
vprintf(format, args);
|
vprintf(format, args);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
va_end(args);
|
va_end(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -40,8 +40,8 @@ void
|
|||||||
die(int status, char *format, ...)
|
die(int status, char *format, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
|
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
|
|
||||||
if (status != 0)
|
if (status != 0)
|
||||||
puts("psh: error: ");
|
puts("psh: error: ");
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ die(int status, char *format, ...)
|
|||||||
perror("reason");
|
perror("reason");
|
||||||
|
|
||||||
va_end(args);
|
va_end(args);
|
||||||
exit(status);
|
exit(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -68,7 +68,7 @@ sighup(int signal)
|
|||||||
void
|
void
|
||||||
sigint(int signal)
|
sigint(int signal)
|
||||||
{
|
{
|
||||||
die(0, "Caught ^C");
|
die(0, "\nCaught ^C, exiting...");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -98,38 +98,38 @@ tokenize(char *input)
|
|||||||
return argc;
|
return argc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
call_ecmd(int argc, char **argv)
|
call_ecmd(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
char *err;
|
char *err;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
||||||
pid = fork();
|
|
||||||
|
|
||||||
if (pid == -1)
|
pid = fork();
|
||||||
die(-1, "fork failed");
|
if (pid == -1)
|
||||||
|
die(-1, "Fork failed");
|
||||||
|
|
||||||
/* parent */
|
/* parent */
|
||||||
if (pid)
|
if (pid)
|
||||||
return wait(&status), status;
|
return wait(&status), status;
|
||||||
|
|
||||||
/* child */
|
/* child */
|
||||||
execvp(argv[0], argv);
|
execvp(argv[0], argv);
|
||||||
|
|
||||||
/* if we are here then called program failed */
|
/* if we end up here then exec failed */
|
||||||
err = strerror(errno);
|
err = strerror(errno);
|
||||||
print("%s: %s", argv[0], err);
|
print("%s: %s", argv[0], err);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
char input[INPUT_MAX_LEN];
|
char input[INPUT_MAX_LEN+1];
|
||||||
char cwd[PATH_MAX_LEN];
|
char cwd[PATH_MAX_LEN+1];
|
||||||
int num_tokens, ret;
|
int num_tokens, ret;
|
||||||
|
|
||||||
signal(SIGINT, sigint);
|
signal(SIGINT, sigint);
|
||||||
@ -143,29 +143,27 @@ main(int argc, char **argv)
|
|||||||
|
|
||||||
printf("[%s] ", cwd);
|
printf("[%s] ", cwd);
|
||||||
|
|
||||||
if (!fgets(input, sizeof(input)-1, stdin))
|
if (!fgets(input, INPUT_MAX_LEN, stdin))
|
||||||
die(0, "Caught ^D");
|
die(0, "\nCaught ^D, exiting...\n");
|
||||||
|
|
||||||
if ((num_tokens = tokenize(input)) < 0)
|
if ((num_tokens = tokenize(input)) < 0)
|
||||||
die(1, "Could not parse input");
|
die(1, "Could not parse input");
|
||||||
|
|
||||||
/* no input, start again */
|
/* no input, start again */
|
||||||
if (num_tokens == 0)
|
if (num_tokens == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* search for internal commands first */
|
/* search for internal commands */
|
||||||
if ((ret = find_icmd(g_tokens[0])) >= 0)
|
if ((ret = find_icmd(g_tokens[0])) >= 0)
|
||||||
{
|
{
|
||||||
ret = call_icmd(ret, num_tokens, g_tokens);
|
if((ret = call_icmd(ret, num_tokens, g_tokens)))
|
||||||
|
print("Internal command '%s' failed with error code: %d", g_tokens[0], ret);
|
||||||
|
|
||||||
if (ret)
|
continue;
|
||||||
print("Internal command '%s' failed with error code: %d", g_tokens[0], ret);
|
}
|
||||||
|
|
||||||
continue;
|
/* no such internal command, try to execute external */
|
||||||
}
|
if ((ret = call_ecmd(num_tokens, g_tokens)))
|
||||||
|
print("External command '%s' failed with error code: %d", g_tokens[0], ret);
|
||||||
ret = call_ecmd(num_tokens, g_tokens);
|
|
||||||
if (ret)
|
|
||||||
print("External command '%s' failed with error code: %d", g_tokens[0], ret);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user