- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我运行我的 shell,它提示:“Shell>
”。我键入一个命令,例如 ls
,它只会生成一个新行,再次显示“Shell>
”。
知道为什么它似乎没有命中 execv
吗?
int no_of_args = count(buffer);
// plus one to make it NULL
char** array_of_strings = malloc((sizeof(char*)*(no_of_args+1)));
// break the string up and create an array of pointers that
// point to each of the arguments.
int count=0;
char* pch2;
pch2 = strtok (buffer," ");
while (pch2 != NULL)
{
array_of_strings[count]=(char*)malloc((sizeof(char)*strlen(pch2)));
strcpy(array_of_strings[count], pch2);
pch2 = strtok (NULL, " ");
count++;
}
//format for command is eg. ls -a -l
//therefore the first element in the array will be the program name
//add the path so it'll be /bin/command eg. /bin/ls
char* prog = malloc(sizeof(char)*(strlen(array_of_strings[0]+strlen(path))));
prog = strcat(strcpy(prog, path),array_of_strings[0]);
}
最佳答案
首先,您永远不需要使用sizeof(char)
,因为它始终为 1。
ISO C99 是这样定义字节的:
addressable unit of data storage large enough to hold any member of the basic character set of the execution environment.
以及 6.5.3.4 sizeof 运算符
中的后续声明:
When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.
这在 C11 中没有变化。
所以,基本上,一个字节就是您的 char
的大小。 ISO 通常为 8 位值保留术语 octet
。
其次,语句序列如下:
array_of_strings[count]=(char*)malloc((sizeof(char)*strlen(pch2)));
strcpy(array_of_strings[count], pch2);
是未定义的行为,因为 strlen(pch2)
只是一个 short 足以存储 pch2
指向的字符串副本的空间>。你应该使用类似的东西:
array_of_strings[count] = malloc (strlen (pch2) + 1);
您还会注意到我删除了类型转换。永远不要强制转换 C 中内存分配函数的返回值,因为它在某些情况下会隐藏问题。
第三,您似乎没有遵守 argv
数组的规则。该数组中的最后一个元素应该是 NULL 指针,因为命令 ls x.txt
会生成:
“ls”
。“x.txt”
。NULL
。现在,针对您的特定 问题。您应该检查 execv
调用的返回值,因为不能保证可执行文件会运行(例如,如果 ls
不在 /bin
目录)。我会改变:
int rv = execv(prog, array_of_strings);
进入:
printf ("DEBUG: [%s]\n", prog);
int rv = execv(prog, array_of_strings);
printf ("DEBUG: execv returned %d/%d\n", rv, errno); // need errno.h
用于调试目的,并查看输出的内容。
如果 execv
有效,您永远不会看到最后的消息。如果它出现,它会告诉您为什么 execv
不起作用。当我这样做时,我看到:
DEBUG: [/bin/ls
]
DEBUG: execv returned -1/2
换句话说,您尝试运行的可执行文件名称是 /bin/lsX
,其中 X
是换行符。没有这样的可执行文件,因此来自 execv
的错误 2(ENOENT = 没有这样的文件或目录
) - 您需要修复解析代码,以便不留下换行符中。
作为快速调试修复,我更改了行:
prog = strcat(strcpy(prog, path),array_of_strings[0]);
进入:
prog = strcat(strcpy(prog, path),array_of_strings[0]);
if (prog[strlen(prog)-1] == '\n') prog[strlen(prog)-1] = '\0';
去掉尾随的换行符,文件列表就成功了:
Shell>ls
DEBUG: [/bin/ls]
accounts2011.ods birthdays shares workspace_android
accounts2012.ods development wildlife
Shell>_
这只是为了证明而调试的东西,不适合真正的代码,所以你仍然需要去修复你的解析。
您可能想看看 this answer因为它显示了一种通过缓冲区溢出保护从用户那里获取输入的好方法,如果输入行太长则清理其余输入行,提示并(在这种情况下最重要)删除换行符。
关于c - 用 C 写一个 shell,不返回任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12523121/
这个fn是吗: function isplainobj ( obj ) { return Object.prototype.toString.call( obj ) === "[object
我尝试创建一个我没有经验的小 bash 脚本。我尝试做类似的事情: #!/bin/bash statut="na" if [ $proc = 0 ]; then statut = "close
我想重写 HighLine 的几个方法来自定义我的控制台,目前我的代码如下所示: cmd = ask("#{@prompt_label} #{@prompt_separator} ",
鉴于下面的 HTML,我尝试使用 jQuery 来匹配所有具有类“foo”的跨度的列表项,并且该跨度应包含文本“relevant”。 Some text relevant Some more
我拥有一个 5 美元 20GB SSD Digital Ocean Droplet,它几乎用完了 Docker 镜像和容器的空间。 有没有办法购买一个卷,将其连接到服务器并安全地将所有 Docker
我有这样的表: id name number result stuff stuff stuff stuff 我只想将 class = "red" 添加到
我需要计算两点之间的距离,但不是以常规方式。我需要知道“东西距离”+“南北距离”。我想这比常规的“乌鸦飞翔”计算更简单,但我仍然不知道如何做到这一点。 我想使用 MySQL 查询来执行此操作,并且最好
#include #include #include typedef struct dict_pair { void *key; void *value; struct dict_p
为什么当我尝试通过 将 char[] word 写入控制台时会发生这种奇怪的事情 Console.WriteLine(word); 我得到了一个正确的结果,但是当我写的时候 Console.Write
一个简单的例子: class C{} class B{ @Inject C c; void doSomething(){ System.out.println(c);
我想做某事,但不确定如何描述它。我有这门课 public class Company { private List _persons; private Person GetPersonByNa
我正在尝试实现我自己的 qDebug()样式调试输出流,这基本上是我目前所拥有的: struct debug { #if defined(DEBUG) template std::os
所以我正在为我的应用程序编写一个搜索功能,代码如下: - (void) selectObject: (NSString *)notes{ [axKnotes removeAllObjects]
我想在 Rust 中匹配一种复杂的继承式东西: struct Entity { pub kind: EntityKind, } pub enum EntityKind { Player
我是 SQL 新手。这没有返回任何结果...... SELECT media.id as mediaid, media.title as mediatitle, media.description
在微型 SDCard 上写入 Android things 图像并将该卡插入 Raspberry Pi 3 Model B 并打开电源,启动时显示“Auto config Please wait”然后
这是一个常见的但是文本出现在框的右侧,不是极右但几乎是这样 h3: ................................................ .................
#include #include #include #include #include int main(int argc, string argv[]) { if(argc >
所以我试图让一些东西相互堆叠。首先,查看工作链接会有所帮助,您会看到问题所在: http://brownbox.net.au/clients/matchlessphotography/ 现在我需要使用
我想在禁用 javascript 时在我的网站顶部显示一条消息(就像在 SO 上一样),但在谷歌浏览器上不起作用 最佳答案 看起来是这样。 您可以使用 javascript 隐藏“noscript”消
我是一名优秀的程序员,十分优秀!