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