关于 GRUB 的 main() 的查询
下面是grub main()的代码。在这里我想了解这一行:
file = fopen(arg_v[1], "rb");
这里 fopen 正在打开哪个文件?这个 arg v[1] 指向什么文件?
int main(unsigned arg_c, char *arg_v[])
{
FILE *file;
if(arg_c < 2)
{
printf("Checks if file is Multiboot compatible\n");
return 1;
}
file = fopen(arg_v[1], "rb");
if(file == NULL)
{
printf("Can't open file '%s'\n", arg_v[1]);
return 2;
}
check_multiboot(arg_v[1], file);
fclose(file);
return 0;
}
Below is the code of main() of grub. Here I want to know about this line:
file = fopen(arg_v[1], "rb");
Here which file fopen is opening? What file this arg v[1] is pointing to?
int main(unsigned arg_c, char *arg_v[])
{
FILE *file;
if(arg_c < 2)
{
printf("Checks if file is Multiboot compatible\n");
return 1;
}
file = fopen(arg_v[1], "rb");
if(file == NULL)
{
printf("Can't open file '%s'\n", arg_v[1]);
return 2;
}
check_multiboot(arg_v[1], file);
fclose(file);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用
argv[1]
调用程序,则它是指向
"arg1"
的指针;argv[2]
是指向"arg2.txt"
的指针,argv[3]
是指向"65"< 的指针/code>,
argv[4]
为 NULLargv[0] 要么指向
"program"
,要么指向""
(如果操作系统和/或库和/或启动代码无法识别用于调用二进制可执行文件的名称在您的特定情况下,程序尝试打开一个文件,该文件的名称在程序的第一个参数中提供,读取二进制模式。
If you call your program with
argv[1]
is a pointer to"arg1"
;argv[2]
is a pointer to"arg2.txt"
,argv[3]
is a pointer to"65"
,argv[4]
is NULLargv[0] either points to
"program"
or to""
if the OS and/or Library and/or startup code cannot identify the name used to call the binary executableIn your specific case, the program tries to open a file, whose name is provided in the first argument to the program, in read binary mode.
arg_v 是一个指针,指向调用 main 时传递给程序的字符串指针数组。因此,arg_v[1] 是一个指向调用程序时传递给程序的第一个字符串的指针(即使数组从 0 开始;第 0 个元素是程序名称本身)。
编辑:具体来说,如果上面是作为
grub foo bar
调用的可执行文件的主函数,则arg_v[0]
点指向字符串“grub”,arg_v[1]
指向“foo”。因此,fopen
调用将尝试打开名为“foo”的文件。arg_v
is a pointer to an array of pointers to strings passed to the program when main is invoked.arg_v[1]
is hence a pointer to the first string passed to the program when it is invoked (even though the array starts at 0; the 0'th element is the program name itself).Edit: So to be concrete, if the above is the main function of an executable invoked as
grub foo bar
, thenarg_v[0]
points to the string "grub" andarg_v[1]
points to "foo". Hence thefopen
call will try to open a file named "foo".