c – 是否可以重复getopt

c – 是否可以重复getopt,第1张

概述我正在尝试使用内置命令创建一个基本 shell,我遇到了一些getopt问题.这是输出(使用valgrind): $mkdir -p foo/barmkdir-pfoo/barFLAGON$mkdir -p foo/testmkdir-pfoo/test==15377== Invalid read of size 1==15377== at 0x5201BBE: _get 我正在尝试使用内置命令创建一个基本 shell,我遇到了一些getopt问题.这是输出(使用valgrind):
$mkdir -p foo/barmkdir-pfoo/barFLAGON$mkdir -p foo/testmkdir-pfoo/test==15377== InvalID read of size 1==15377==    at 0x5201BBE: _getopt_internal_r (in /usr/lib/libc-2.17.so)==15377==    by 0x5202CEA: _getopt_internal (in /usr/lib/libc-2.17.so)==15377==    by 0x5202D37: getopt (in /usr/lib/libc-2.17.so)==15377==    by 0x40351A: shell_ns_cmd_mkdir (shell.c:542)==15377==    by 0x403AB4: normal_shell_cb (shell.c:610)==15377==    by 0x402E8E: shell_mainloop (shell.c:402)==15377==    by 0x401B67: main (main.c:52)==15377==  Address 0x54e0912 is 2 bytes insIDe a block of size 3 free'd==15377==    at 0x4C2AD3C: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)==15377==    by 0x402C93: shell_mainloop (shell.c:384)==15377==    by 0x401B67: main (main.c:52)==15377== $

这是源(剪辑):

for (i = 0; i < argc; i++) {    puts(argv[i]);}while ((c = getopt(argc,argv,"p")) != -1) {    switch (c) {        case 'p':            puts("FLAGON");            mkparents = true;            break;        case '?':            fprintf(stderr,"invalID option -- %c",optopt);            ret = 127;            goto end;            break;    }}

所以它第一次运行它(mkdir -p)识别它(-p),第二次运行它,它没有.有任何想法吗?

解决方法 如果要扫描多个矢量,则需要通过将optind设置为1来重置getopt.

The variable optind is the index of the next element of the argv[]
vector to be processed. It shall be initialized to 1 by the system,
and getopt() shall update it when it finishes with each element of
argv[].

如果将optind设置为1不起作用,也请尝试0,我想我记得在某处阅读过.

总结

以上是内存溢出为你收集整理的c – 是否可以重复getopt全部内容,希望文章能够帮你解决c – 是否可以重复getopt所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1238127.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存