Skip to content

Commit 7a818da

Browse files
authored
Patch about auto gen version string (#114)
* new: about app中,显示当前构建的git commit sha1以及构建时间 * bugfix: 修复shell的exec命令对绝对路径的拼接错误问题
1 parent 83a7aaa commit 7a818da

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed

.vscode/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@
168168
"mmio.h": "c",
169169
"stdint-gcc.h": "c",
170170
"acpi.h": "c",
171-
"assert.h": "c"
172-
},
171+
"assert.h": "c",
172+
"sys_version.h": "c"
173+
},
173174
"C_Cpp.errorSquiggles": "Enabled",
174175
"esbonio.sphinx.confDir": "",
175176
"rust-analyzer.cargo.target": "x86_64-unknown-none",

user/apps/about/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sys_version.h

user/apps/about/Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
# 获得当前git提交的sha1,并截取前8位
2+
GIT_COMMIT_SHA1=$(shell git log -n 1 | head -n 1 | cut -d ' ' -f 2 | cut -c1-8)
3+
14
all: about.o
25

36
ld -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/about $(shell find . -name "*.o") $(shell find $(sys_libs_dir) -name "*.o") -T about.lds
47

58
objcopy -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/about $(output_dir)/about.elf
6-
about.o: about.c
9+
10+
about.o: version_header about.c
711
$(CC) $(CFLAGS) -c about.c -o about.o
12+
13+
# 生成版本头文件sys_version.h
14+
version_header: about.c
15+
@echo "#define DRAGONOS_GIT_COMMIT_SHA1 \"$(GIT_COMMIT_SHA1)\"" > sys_version.h

user/apps/about/about.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "sys_version.h" // 这是系统的版本头文件,在编译过程中自动生成
12
#include <libc/src/math.h>
23
#include <libc/src/stdio.h>
34
#include <libc/src/stdlib.h>
@@ -18,8 +19,10 @@ void print_copyright()
1819
printf(" DragonOS - An opensource operating system.\n");
1920
printf(" Copyright: fslongjin & DragonOS Community. 2022, All rights reserved.\n");
2021
printf(" Version: ");
21-
put_string("V0.1.1 - 20221127\n", COLOR_GREEN, COLOR_BLACK);
22-
printf(" You can visit the project via:\n");
22+
put_string("V0.1.2\n", COLOR_GREEN, COLOR_BLACK);
23+
printf(" Git commit SHA1: %s\n", DRAGONOS_GIT_COMMIT_SHA1);
24+
printf(" Build time: %s %s\n", __DATE__, __TIME__);
25+
printf(" \nYou can visit the project via:\n");
2326
printf("\n");
2427
put_string(" Official Website: https://DragonOS.org\n", COLOR_INDIGO, COLOR_BLACK);
2528
put_string(" GitHub: https://github.com/fslongjin/DragonOS\n", COLOR_ORANGE, COLOR_BLACK);
@@ -34,13 +37,9 @@ void print_copyright()
3437

3538
int main()
3639
{
37-
// printf("Hello World!\n");
3840
print_ascii_logo();
3941

4042
print_copyright();
41-
// exit(0);
42-
// while (1)
43-
// ;
4443

4544
return 0;
4645
}

user/apps/shell/cmd.c

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,38 @@ const static int total_built_in_cmd_num = sizeof(shell_cmds) / sizeof(struct bui
3939
*/
4040
static char *get_target_filepath(const char *filename, int *result_path_len)
4141
{
42-
int cwd_len = strlen(shell_current_path);
42+
char *file_path = NULL;
43+
if (filename[0] != '/')
44+
{
45+
int cwd_len = strlen(shell_current_path);
4346

44-
// 计算文件完整路径的长度
45-
*result_path_len = cwd_len + strlen(filename);
47+
// 计算文件完整路径的长度
48+
*result_path_len = cwd_len + strlen(filename);
4649

47-
char *file_path = (char *)malloc(*result_path_len + 2);
50+
file_path = (char *)malloc(*result_path_len + 2);
4851

49-
memset(file_path, 0, *result_path_len + 2);
52+
memset(file_path, 0, *result_path_len + 2);
5053

51-
strcpy(file_path, shell_current_path);
54+
strncpy(file_path, shell_current_path, cwd_len);
5255

53-
// 在文件路径中加入斜杠
54-
if (cwd_len > 1)
55-
file_path[cwd_len] = '/';
56+
// 在文件路径中加入斜杠
57+
if (cwd_len > 1)
58+
file_path[cwd_len] = '/';
5659

57-
// 拼接完整路径
58-
if (filename[0] == '/')
59-
strcat(file_path, filename + 1);
60-
else
60+
// 拼接完整路径
6161
strcat(file_path, filename);
62+
}
63+
else
64+
{
65+
*result_path_len = strlen(filename);
66+
file_path = (char *)malloc(*result_path_len + 2);
67+
68+
memset(file_path, 0, *result_path_len + 2);
69+
70+
strncpy(file_path, filename, *result_path_len);
71+
if(filename[(*result_path_len)-1]!='/')
72+
file_path[*result_path_len] = '/';
73+
}
6274

6375
return file_path;
6476
}
@@ -483,7 +495,8 @@ int shell_cmd_exec(int argc, char **argv)
483495
// 如果不指定后台运行,则等待退出
484496
if (strcmp(argv[argc - 1], "&") != 0)
485497
waitpid(pid, &retval, 0);
486-
else{
498+
else
499+
{
487500
// 输出子进程的pid
488501
printf("[1] %d\n", pid);
489502
}

0 commit comments

Comments
 (0)