aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/helper_functions.c22
-rw-r--r--src/lsh_builtins.c7
-rw-r--r--src/lsh_main_func.c34
-rwxr-xr-xsrc/main.c18
4 files changed, 30 insertions, 51 deletions
diff --git a/src/helper_functions.c b/src/helper_functions.c
index 5c81f15..c1931fd 100644
--- a/src/helper_functions.c
+++ b/src/helper_functions.c
@@ -1,11 +1,21 @@
#ifndef shell_helper_functions
#define shell_helper_functions
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+void *malloce(size_t size){
+ void* mal=malloc(size);
+ if(!mal){
+ fprintf(stderr, "lsh: allocation error\n");
+ exit(EXIT_FAILURE);
+ }
+ return mal;
+}
+
// You must free the result if result is non-NULL.
char *str_replace(char *orig, char *rep, char *with) {
char *result; // the return string
@@ -59,11 +69,7 @@ char *basename(char *path_in)
char *lastToken = NULL;
char *basePath;
- basePath = (char *)malloc(sizeof(char) * (strlen(path_in) + 1));
- if (!basePath) {
- fprintf(stderr, "lsh: allocatin error\n");
- exit(EXIT_FAILURE);
- }
+ basePath = (char *)malloce(sizeof(char) * (strlen(path_in) + 1));
strcpy(basePath, path_in);
token = strtok(basePath, "/");
@@ -77,11 +83,7 @@ char *basename(char *path_in)
return NULL;
}
- char *ret_str = (char*)malloc(sizeof(char)*strlen(lastToken)+1);
- if (!ret_str) {
- fprintf(stderr, "lsh: allocation error\n");
- exit(EXIT_FAILURE);
- }
+ char *ret_str = (char*)malloce(sizeof(char)*strlen(lastToken)+1);
strcpy(ret_str,lastToken);
free(basePath);
return ret_str;
diff --git a/src/lsh_builtins.c b/src/lsh_builtins.c
index 81fc2b5..0986cfe 100644
--- a/src/lsh_builtins.c
+++ b/src/lsh_builtins.c
@@ -2,6 +2,7 @@
#define shell_builtins
#include "lsh_main_func.c"
+#include "helper_functions.c"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
@@ -48,11 +49,7 @@ int lsh_cd(char **args)
if (err != 0) {
perror("lsh");
}
- char *cwd=(char*)malloc(sizeof(char)*4096);
- if (!cwd) {
- fprintf(stderr, "lsh: allocation error\n");
- exit(EXIT_FAILURE);
- }
+ char *cwd=(char*)malloce(sizeof(char)*4096);
getcwd(cwd,sizeof(char*)*4096);
setenv("PWD",cwd,1);
free(cwd);
diff --git a/src/lsh_main_func.c b/src/lsh_main_func.c
index 74e7881..8a3aade 100644
--- a/src/lsh_main_func.c
+++ b/src/lsh_main_func.c
@@ -8,19 +8,16 @@
#include <unistd.h>
#include <stdbool.h>
+#include "helper_functions.c"
+
#define LSH_RL_BUFSIZE 1024
char *lsh_read_line(void)
{
int bufsize = LSH_RL_BUFSIZE;
int position = 0;
- char *buffer = malloc(sizeof(char) * bufsize);
+ char *buffer = malloce(sizeof(char) * bufsize);
int c;
- if (!buffer) {
- fprintf(stderr, "lsh: allocation error\n");
- exit(EXIT_FAILURE);
- }
-
while (1) {
// Read a character
c = getchar();
@@ -47,7 +44,7 @@ char *lsh_read_line(void)
}
char *handle_vars(char *token){
- char *ret_token=(char*)malloc(sizeof(char)*strlen(token)+1);
+ char *ret_token=(char*)malloce(sizeof(char)*strlen(token)+1);
strcpy(ret_token,token);
if (token[0]=='~'){
@@ -55,24 +52,24 @@ char *handle_vars(char *token){
if(!getenv("HOME")){
fprintf(stderr,"lsh: you aint got no home. da hell\n");
free(ret_token);
- ret_token=(char*)malloc(sizeof(char)*strlen(token)+1);
+ ret_token=(char*)malloce(sizeof(char)*strlen(token)+1);
strcpy(ret_token,token);
return ret_token;
}
free(ret_token);
- ret_token=(char*)malloc(sizeof(char)*strlen(token)+strlen(getenv("HOME"))+1);
+ ret_token=(char*)malloce(sizeof(char)*strlen(token)+strlen(getenv("HOME"))+1);
strcpy(ret_token,getenv("HOME"));
strcat(ret_token,token);
}
- char *tmp_token=(char*)malloc(sizeof(char)*strlen(ret_token)+1);
+ char *tmp_token=(char*)malloce(sizeof(char)*strlen(ret_token)+1);
strcpy(tmp_token,ret_token);
int i=0;
while (tmp_token[i]!='\0'){
if(tmp_token[i]=='$'){
int j=0; while(tmp_token[i+j]!=' ' && tmp_token[i+j]) j++;
- char *varname=(char*)malloc(sizeof(char)*j+1);
+ char *varname=(char*)malloce(sizeof(char)*j+1);
varname[j]='\0';
strncpy(varname,&tmp_token[i+1],j);
varname[j-1]='\0';
@@ -84,7 +81,7 @@ char *handle_vars(char *token){
free(ret_token);
int tmp_len=strlen(tmp_token);
- ret_token=(char*)malloc(sizeof(char)*(strlen(tmp_token)-strlen(varname))+strlen(env)+1);
+ ret_token=(char*)malloce(sizeof(char)*(strlen(tmp_token)-strlen(varname))+strlen(env)+1);
tmp_token[i]='\0';
tmp_token[i+j]='\0';
@@ -96,7 +93,7 @@ char *handle_vars(char *token){
}
free(tmp_token);
- tmp_token=(char*)malloc(sizeof(char)*strlen(ret_token)+1);
+ tmp_token=(char*)malloce(sizeof(char)*strlen(ret_token)+1);
strcpy(tmp_token,ret_token);
free(varname);
@@ -176,8 +173,8 @@ char * strtok_quotes(char *token){
char *pop_quotes(char *token){
int ch_i=0;
- char *right=(char*)malloc(sizeof(char)*strlen(token)+1);
- char *ret_token=(char*)malloc(sizeof(char)*strlen(token)+1);
+ char *right=(char*)malloce(sizeof(char)*strlen(token)+1);
+ char *ret_token=(char*)malloce(sizeof(char)*strlen(token)+1);
strcpy(ret_token,token);
while(ch_i<strlen(ret_token)){
@@ -208,14 +205,9 @@ char *pop_quotes(char *token){
char **lsh_split_line(char *line)
{
int bufsize = LSH_TOK_BUFSIZE, position = 0;
- char **tokens = malloc(bufsize * sizeof(char*));
+ char **tokens = malloce(bufsize * sizeof(char*));
char *token;
- if (!tokens) {
- fprintf(stderr, "lsh: allocation error\n");
- exit(EXIT_FAILURE);
- }
-
token = strtok_quotes(line);
while (token != NULL) {
token = handle_vars(token);
diff --git a/src/main.c b/src/main.c
index 262c504..3b2db6e 100755
--- a/src/main.c
+++ b/src/main.c
@@ -48,23 +48,11 @@ int main(int argc, char **argv){
//Set The prompt
SHELL_OB shell_obj;
- shell_obj.prompt = (char *)malloc(sizeof(char) * 1024);
- if (!shell_obj.prompt){
- fprintf(stderr, "lsh: allocation error\n");
- exit(EXIT_FAILURE);
- }
- shell_obj.pre_prompt = (char *)malloc(sizeof(char) * 1024);
- if (!shell_obj.pre_prompt){
- fprintf(stderr, "lsh: allocation error\n");
- exit(EXIT_FAILURE);
- }
+ shell_obj.prompt = (char *)malloce(sizeof(char) * 1024);
+ shell_obj.pre_prompt = (char *)malloce(sizeof(char) * 1024);
//get the hostname
- char *hostname = (char *)malloc(sizeof(char) * 1024);
- if (!hostname) {
- fprintf(stderr, "lsh: allocation error\n");
- exit(EXIT_FAILURE);
- }
+ char *hostname = (char *)malloce(sizeof(char) * 1024);
gethostname(hostname,sizeof(char) * 1024);
//set the pre_prompt