summaryrefslogtreecommitdiff
path: root/main.c
blob: 1b70b0f2fc6ddbc66572d869400c35e579365cee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdbool.h>
#include <linux/limits.h>

#define MAX_HISTORY 100
#define MAX_COMMAND_LENGTH 256

// static int CURRENT_COMMAND_INDEX = 0;
// static int CURRENT_HISTORY_INDEX = 0;
// static char *COMMAND_HISTORY[MAX_HISTORY] = { NULL};

struct com_struct
{
    char *com;
    char *arg;
};

void test()
{
    printf("print to output");
}

// exit() command
void exit_()
{
        exit(0);
}

// ls command
// get the requested path and put it into a const later on when getting the user input 
void ls(const char *path)
{
    fork();

    struct dirent *entry;
    DIR *dP = opendir(path);

    // check if the directory got opened succefully 
    if (dP == NULL)
    {
        perror("opendir");
        return;
    }

    // print the folder|directory name
    while((entry = readdir(dP)) != NULL)
    {
        printf("s\n", entry->d_name);
    }
    closedir(dP);

}

// pwd command
char *pwd()
{
    char *current_working_directory = malloc(sizeof(char) * 100);
    
    if(getcwd(current_working_directory, sizeof(current_working_directory) != 0))
    {
        return current_working_directory;
    }
    else
    {
        perror("getcwd() error");
        return "ERROR";
    }
}

// echo command
char *echo(char *command)
{
    char *argv = (char *)malloc(sizeof(char) * 4);
    char *command_argument = (char *)malloc(sizeof(char) * 100); 

    int separation_index = 0;
    for (int i = 0; i < sizeof(command); i++)
    {
        if (strcmp(&command[i], " ") == 0)
        {
            separation_index = i;

            for ( int j = 0; j < separation_index; j++)
            {
                argv = &command[j];
            }
        }
    }
    if (strcmp(argv, "echo") == 0)
    {
        
        for (int i = separation_index + 1; i < (sizeof(command) - separation_index); i++)  
        {
             if(strcmp(&command[i], "\0") != 0)
             {
                 break;
             }
             else
             {
                 strcpy(&command_argument[i - (separation_index - 1)], &command[i]);
             }
        }
    }
    
    return command_argument;

}

struct com_struct split_command(char *input)
{


    // command bestaat uit 2 delen
    // het commando zelf
    // en argumenten voor het commado
    // we moeten het commando opsplitsen in 2 delen
    // we moeten een struct gebruiken

    struct com_struct NEW_COMMAND;

    char *command = (char *)malloc(sizeof(char) * 4);
    char *argument = (char *)malloc(sizeof(char) * 100); 

    if (command == NULL || argument == NULL)
    {
        printf("found a null pointer when dynamically assigning memory to the command or variable argument");
        exit(1);
    }

    int space_position = 0;
    for (int i = 0; i < sizeof(command); i++)
    {
        if (strcmp(&input[i], " ") == 0)
        {
            space_position = i;

            for ( int j = 0; j < space_position; j++)
            {
                command = &input[j];
            }
            for (int x = space_position; x < sizeof(input) - space_position; x++)
            {
                argument = &input[x];
            }
        }
        
    }
    NEW_COMMAND.com = command;
    NEW_COMMAND.arg = argument;

    return NEW_COMMAND;

}

int main(int argc, char *argv[]) 
{
    char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH);    
    while (true)
    {
        printf("\nSHELL> ");
        scanf("%s", input);

        // input converted to struct with members: command and argument 
        struct com_struct new_input = split_command(input); 

        if (strcmp(new_input.com, "ls"))
        { 
            // failed logic, argv is for when executing the program, not for calling functions
        }   
    } 
    
    return 0;
}