C Shell programiranje za UNIX platformu!
- poruka: 6
- |
- čitano: 1.737
- |
- moderatori:
Lazarus Long, XXX-Man, vincimus
- +/- sve poruke
- ravni prikaz
- starije poruke gore
Evo ti shell koji definitivno radi. Kompajliraj sa gcc ime_programa.c -o ime_programa
Pokreni sa ./ime_programa
A sada THE GRANDE SHELL:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#define MAX_ARGS 32
#define MAX_ARGL 32
#define MAX_LINE_LEN 1024
struct command {
unsigned int argc;
char *argv[MAX_ARGS];
};
void print_error(const char *error);
void prompt(char *buf);
int parse_command_line(char *cmd_line, struct command *cmd);
int execute_command(struct command *cmd);
#define CMD_NFND 2
#define CMD_EXIT 5
int main(int argc, char **argv)
{
char cmd_line[MAX_LINE_LEN];
struct command cmd;
int stat;
/* Turn buffering off. */
setvbuf(stdout, 0, _IONBF, 0);
while (1) {
prompt(cmd_line);
if (parse_command_line(cmd_line, &cmd) == -1)
continue;
stat = execute_command(&cmd);
if (stat == -CMD_NFND) {
print_error("Command not found.");
continue;
} else if (stat == -CMD_EXIT) {
break;
}
}
return 0;
}
#define PR_COLOR "\033[1;34m"
#define PR_ERROR "\033[1;31m"
#define PR_RESET "\033[m"
void print_error(const char *error)
{
printf(PR_ERROR "%s" PR_RESET "\n", error);
}
#define PROMPT "myshell > "
void prompt(char *buf)
{
fputs(PR_COLOR PROMPT PR_RESET, stdout);
fgets(buf, MAX_LINE_LEN, stdin);
}
#define DELIM " \t\r\n"
int parse_command_line(char *cmd_line, struct command *cmd)
{
memset(cmd, 0, sizeof(struct command));
for (cmd->argc = 0; cmd->argc < MAX_ARGS; cmd->argc++) {
if ((cmd->argv[cmd->argc] = strsep(&cmd_line, DELIM)) == NULL)
break;
/* Argument too long. */
if (strlen(cmd->argv[cmd->argc]) > MAX_ARGL)
return -1;
/* Don't count empty space. */
if (strlen(cmd->argv[cmd->argc]) == 0)
cmd->argc--;
}
/* No more argument space. */
if (strsep(&cmd_line, DELIM) != NULL)
return -1;
return 0;
}
int file_exists(char *path)
{
/* File exists, is readable and executable. */
if (access(path, R_OK | X_OK))
return 0;
return 1;
}
int lookup_path(char *cmd, char *fullpath)
{
char *path_var, *path, *path_ptr;
char pathcpy[128];
path_var = getenv("PATH");
if (path_var == NULL)
return -1;
/*
* Copy the PATH variable into a temporary buffer to not mess up the
* entire shell's PATH variable through strsep().
*/
strncpy(pathcpy, path_var, 128);
path_ptr = pathcpy;
while (1) {
path = strsep(&path_ptr, ":");
if (path == NULL)
break;
strcpy(fullpath, path);
strcat(fullpath, "/");
strcat(fullpath, cmd);
if (file_exists(fullpath))
return 0;
}
return -1;
}
void execute(const char *fullpath, struct command *cmd)
{
pid_t cpid;
cpid = fork();
if (cpid == -1) {
print_error("Fork failed.\n");
return;
}
if (cpid == 0) {
execv(fullpath, cmd->argv);
} else {
wait(&cpid);
}
}
int execute_command(struct command *cmd)
{
char fullpath[128];
unsigned int x;
if (cmd->argc == 0)
return 0;
if ((strcmp(cmd->argv[0], "quit") == 0) ||
(strcmp(cmd->argv[0], "exit") == 0))
return -CMD_EXIT;
if (lookup_path(cmd->argv[0], fullpath) == -1)
return -CMD_NFND;
execute(fullpath, cmd);
return 0;
}
Od svega samo cd treba dodati i clr je clear.
jesi sam slagao cijeli shell??
gdje ti je to trebalo??
Da li je ovo kod za Clear Screen
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// Funziona sui terminali compatibili ansi
fprintf(stdout, "\033[2J"); // Cancella lo schermo
fprintf(stdout, "\033[1;1H"); // Posiziona il cursore sulla linea colonna 1
return EXIT_SUCCESS;
}
Trebalo bi biti.