Skip to content

Commit 8b0d2aa

Browse files
Added (non-working) filling of envp for executables.
1 parent ec6dc41 commit 8b0d2aa

6 files changed

Lines changed: 54 additions & 4 deletions

File tree

source/includes/strings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#define MAX_WORDS 30
1717
#define MAX_WORD_LEN 40
1818

19+
#define CONCAT(...) \
20+
str_concat_impl(sizeof((const char*[]){__VA_ARGS__}) / sizeof(const char*), __VA_ARGS__)
21+
1922
// typedef char symbol[];
2023

2124
/**

source/kernel/C/shell/commands/exec.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <tty.h>
1717
#include <keyboard.h>
1818

19+
extern char* global_envp[];
20+
1921
int cmd_exec(int argc, char** argv)
2022
{
2123
if (argc < 2) {
@@ -42,7 +44,7 @@ int cmd_exec(int argc, char** argv)
4244
tty_flush_input();
4345
keyboard_flush_buffer();
4446

45-
if (userland_exec(path, user_argc, user_argv, NULL) != 0) {
47+
if (userland_exec(path, user_argc, user_argv, global_envp) != 0) {
4648
keyboard_flush_buffer();
4749
eprintf("exec: failed to load ELF");
4850
return -1;

source/kernel/C/shell/sh.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,22 @@
1010
*/
1111

1212
#include <commands/login.h>
13+
#include <filesystems/vfs.h>
1314
#include <sh_util.h>
1415
#include <multitasking.h>
16+
#include <strings.h>
1517

1618
int last_status_code = 0;
1719

20+
char* global_envp[] = {
21+
"HOME=/",
22+
"PATH=/",
23+
"TERM=linux",
24+
"USER=none",
25+
"SHLVL=1",
26+
NULL
27+
};
28+
1829
void init_command_list(command_list* lst)
1930
{
2031
if (lst == NULL)
@@ -160,6 +171,8 @@ void ksh_exec(){
160171
&sudo_flag
161172
};
162173

174+
strcpy(global_envp[3], CONCAT("USER=", username));
175+
163176
shell_main(argc, dummy_argv);
164177
}
165178
else{
@@ -212,6 +225,7 @@ int shell_main(int argc, char** argv){
212225
memset(command, 0, commandBufferSize);
213226

214227
if(running){
228+
strcpy(global_envp[1], CONCAT("PATH", vfs_getcwd()));
215229
show_prompt(argc, argv);
216230
}
217231
continue;

source/kernel/C/stream.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static void fd_object_release(fd_object_t* object)
7777
if (object->ref_count > 0)
7878
return;
7979

80-
if (object->owns_file && object->file)
80+
if (object->owns_file && object->file && object->file->mnt)
8181
vfs_close(object->file);
8282

8383
memset(object, 0, sizeof(*object));

source/kernel/C/strings.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*
1010
*/
1111
#include <strings.h>
12+
#include <stdarg.h>
1213

1314
const char hex_digits[] = "0123456789abcdef";
1415
const char caps_hex_digits[] = "0123456789ABCDEF";
@@ -605,3 +606,33 @@ char* strtok(char* str, const char* delim) {
605606
static char* saveptr;
606607
return strtok_r(str, delim, &saveptr);
607608
}
609+
610+
char* str_concat_impl(int count, ...)
611+
{
612+
va_list args;
613+
size_t total_len = 0;
614+
615+
// First pass: calculate total length
616+
va_start(args, count);
617+
for (int i = 0; i < count; i++) {
618+
const char* s = va_arg(args, const char*);
619+
if (s) total_len += strlen(s);
620+
}
621+
va_end(args);
622+
623+
// Allocate result
624+
char* result = (char*)kmalloc(total_len + 1);
625+
if (!result) return NULL;
626+
627+
result[0] = '\0';
628+
629+
// Second pass: concatenate
630+
va_start(args, count);
631+
for (int i = 0; i < count; i++) {
632+
const char* s = va_arg(args, const char*);
633+
if (s) strcat(result, s);
634+
}
635+
va_end(args);
636+
637+
return result;
638+
}

source/kernel/C/userland.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ static int string_array_count(const char* const* arr) {
127127
return count;
128128
}
129129

130-
static const char* const default_envp[] = {
130+
static char* default_envp[] = {
131131
"HOME=/",
132132
"PATH=/",
133133
"TERM=linux",
134-
"USER=root",
134+
"USER=none",
135135
"SHLVL=1",
136136
NULL
137137
};

0 commit comments

Comments
 (0)