This commit is contained in:
Your Name 2023-11-22 21:36:45 +03:00
parent 79a939a26f
commit c9f0ec1bda
6 changed files with 168 additions and 5 deletions

2
TODO
View File

@ -62,7 +62,7 @@ Findutils:
xargs
Shell:
rc - run command
rc - run command (pipe, ~ | <> <<>> & " parsing)
Init:
sinit - Simple init

View File

@ -19,7 +19,7 @@ void remove_suffix(char *base, const char *suffix) {
char *MakePath(const char *src, const char *output_dir) {
char *dup = strdup(src);
if (dup == NULL) {
fprintf(stderr, "builder: strdup failed");
fprintf(stderr, "builder: strdup failed\n");
exit(1);
}
@ -51,7 +51,7 @@ void Compile(const char *src, const char *output_dir) {
}
else if (pid == -1) {
fprintf(stderr, "builder: fork failed");
fprintf(stderr, "builder: fork failed\n");
exit(1);
}

View File

@ -13,6 +13,12 @@
/* block size (du)*/
#define BLK_SIZE 512
/* mount config */
#define MOUNT_CFG "/etc/fstab"
/* RunComm prompt */
#define RC_PS "> "
/* Options: To disable, comment line */
/* Add escape-char support in echo */
#define ECHO_FANCY

View File

@ -1,6 +1,6 @@
#ifndef _UNUSED_H
#define _UNUSED_H
#define UNUSED(p) (void)p
#define UNUSED(p) ((void)p)
#endif

View File

@ -1,3 +1,142 @@
int main(void) {
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include "config.h"
#include "unused.h"
#define TOK_DELIM " \t\r\n\a"
unsigned int exit_flag;
int rc_cd(char **args, char **envp);
int rc_exit(char **args, char **envp);
/* Builtin commands */
char *cmd[] = {"cd", "exit", "which"};
int (*func[])(char **args, char **envp) = {rc_cd, rc_exit};
int rc_exit(char **args, char **envp) {
UNUSED(args);
UNUSED(envp);
exit_flag = 1;
return 0;
}
int rc_cd(char **args, char **envp) {
UNUSED(envp);
int status = 0;
if (args[0] == NULL)
status = chdir(getenv("HOME"));
else
status = chdir(args[0]);
if (status < 0) {
fprintf(stderr, "rc: cd: Not such directory\n");
return 1;
}
return 0;
}
/* Shell */
int execute(char **tok, char **envp) {
if (tok[0] == NULL)
return 0;
for (size_t i = 0; i < sizeof(cmd) / sizeof(char *); i++)
if (!strcmp(tok[0], cmd[i]))
return func[i](tok + 1, envp);
/* TODO: pipe */
/* If its not builtin cmd */
pid_t pid;
if ((pid = fork()) == 0) {
if (pid == 1)
exit(1);
execvp(tok[0], tok);
exit(1);
}
if (pid == -1) {
fprintf(stderr, "rc: fork failed\n");
return 1;
}
int status = 0;
waitpid(pid, &status, 0);
return status;
}
char **tokenize(char *str) {
size_t buflen = 0;
char *p = str;
while (*p++)
if (*p == ' ')
buflen++;
char **tokens = malloc(sizeof(char *) * buflen);
if (tokens == NULL) {
fprintf(stderr, "rc: malloc failed\n");
exit(1);
}
char *token = strtok(str, TOK_DELIM);
int i = 0;
while (token != NULL) {
tokens[i] = token;
token = strtok(NULL, TOK_DELIM);
i++;
}
tokens[i] = NULL;
return tokens;
}
char *readline(FILE *fp, char *prompt) {
printf("%s", prompt);
char *str = NULL;
size_t len = 0;
if (getline(&str, &len, fp) == -1)
return NULL;
return str;
}
int main(int argc, char **argv, char **envp) {
UNUSED(argc);
UNUSED(argv);
char *prompt = RC_PS;
if (!isatty(STDIN_FILENO))
prompt = "";
while (!exit_flag) {
char *str = readline(stdin, prompt);
if (str == NULL)
break;
char **tok = tokenize(str);
int status = execute(tok, envp);
if (status)
setenv("status", "1", 1);
else
setenv("status", "0", 1);
free(tok);
free(str);
}
putchar('\n');
return 0;
}

18
sysutils/mount.c Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "config.h"
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "aro")) != -1) {
switch (opt) {
default:
printf("mount [DEVICE] [NODE]\n\t[-r Read only] [-o Options]\n\t[-a Mount all fs in %s]\n\nOptions:\n\tremount - Remount a mounted filesystem\n\tro - Read only\n", MOUNT_CFG);
return 0;
}
}
return 0;
}