Skip to content

Instantly share code, notes, and snippets.

@gdisneyleugers
Created May 14, 2015 03:21
Show Gist options
  • Save gdisneyleugers/92d19ad430b169918fbe to your computer and use it in GitHub Desktop.
Save gdisneyleugers/92d19ad430b169918fbe to your computer and use it in GitHub Desktop.
Meh Another Shell
//
// main.c
// Mash
//
// Created by Gregory Disney on 10/12/14.
// Copyright (c) 2014 Gregory Disney. All rights reserved.
//
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <ncurses.h>
/* to compile run gcc mash.c -o mash -Wformat-security -Wint-conversion -Wincompatible-pointer-types -lreadline -lncurses -Wformat-security */
int profile(void)
{
char *p1 = "PATH";
char *p2 = "/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/sbin";
int m;
setenv(p1, p2, m);
char *p3 = "PROFILE";
char *p4 = "source .mashrc";
setenv(p3, p4, m);
return (0);
}
int new_profile(char *p3, char *p4)
{
char *p1 = "PATH";
char *p2 = "/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/sbin";
int m;
setenv(p1, p2, m);
setenv(p3, p4, m);
system("source /etc/bashrc");
return (0);
}
int shell(void)
{
rl_bind_key('\t', rl_complete);
using_history();
profile();
initscr();
sleep(0);
char *f = readline("$ ");
printw(f);
if (strcmp(f, "chdir") == 0) {
char *g = readline("$ chdir: ");
chdir(g);
f = "echo > /dev/null";
}
if (strcmp(f, "help") == 0) {
printf("Mash Help: \n chdir => change directory\n exit => exit\n commands => list commands\n root => loads root env\n");
f = "echo > /dev/null";
}
if (strcmp(f, "commands") == 0) {
printf("Available commands: ");
f = "ls /usr/bin /bin";
}
if (strcmp(f, "root") == 0) {
seteuid(0);
printf("Loading root enviorment\n");
f = "su -l root -c '/bin/mash'; sudo -s;";
f = "whoami";
}
if (strcmp(f, "exit") == 0) {
printf("Exiting Mash");
endwin();
exit(0);
}
if (strcmp(f, "history") == 0){
history_list();
}
if (strcmp(f, "alias") == 0){
char *h = readline("$ alias: ");
char *i = readline("$ refernce: ");
new_profile(h, i);
}
add_history(f);
system(f);
return (0);
}
int main(void)
{
while(1)
shell();
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment