- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在编写一些 C 代码来提供类似于 shell 的功能。到目前为止,它可以运行大多数命令,如 cat、ls、ls | grep ...等写了实现“cd”、“pwd”、“pushd”、“popd”等功能的代码后,一直在测试。如果我写“cd ..”或“cd anydir”,它将运行“chdir(anydir)”并返回到我的脚本命令提示符。在“cd”后输入任何命令后,都会返回错误:
*** Error in `./smshv4': corrupted double-linked list: 0x00000000018d5190 ***
我在一个不相关的变量中重新分配内存时隔离了这个要打印的错误。我评论了错误产生的行:
char * next_cmd(char *prompt, FILE *fp){
fprintf(stderr,"in next_cmd\n");
char *buf ;
int bufspace = 0;
int pos = 0;
int c;
printf("%s", prompt);
while( ( c = getc(fp)) != EOF ) {
if( pos+1 >= bufspace ){
if ( bufspace == 0 ){
buf = emalloc(BUFSIZ); //Error occurs while calling this function
} else {
buf = erealloc(buf,bufspace+BUFSIZ);
}
bufspace += BUFSIZ;
}
/* end of command? */
if ( c == '\n' )
break;
/* no, add to buffer */
buf[pos++] = c;
}
if ( c == EOF && pos == 0 )
return NULL;
buf[pos] = '\0';
return buf;
}
此错误仅在我使用 chdir 运行任何函数后发生。以下是我在输入 cd 时如何调用 chdir 的示例:
if(strcmp(cmdStruct.commandTemp[0],"cd") == 0){
if(cmdStruct.commandTemp[1] != NULL){
if(chdir(cmdStruct.commandTemp[1]) == -1){
perror("chdir");
return -1;
}
} else {
fprintf(stderr,"cd failed. No directory given\n");
return -1;
}
}
这个 cd 命令不起作用,因为每当我运行它时,给出的错误将在我下次输入命令时显示。如果我 cd 到一个不同的目录,运行任何东西来接收错误,然后运行“pwd”(正在运行),它将显示原始目录(输入一个命令后错误似乎自行解决)。
如何解决损坏的双链表问题?谢谢。
编辑:我调用命令提示符如下所示:
while ( (cmdline = next_cmd(prompt, stdin)) != NULL ){ //command prompt function is run
//if & sign detected, fork. otherwise run command as usual
if(hasBackgroundSign(cmdline) == 1){
pid = fork();
if(pid == 0){
if ( (arglist = splitline(cmdline)) != NULL ){
result = execute(arglist);
freelist(arglist);
}
free(cmdline);
kill(getpid(), SIGKILL);
} else {
printf("%d\n",pid);
}
} else {
//if no & sign detected, run command as usual without forking
if ( (arglist = splitline(cmdline)) != NULL ){
result = execute(arglist); //execute function contains the chdir calls
freelist(arglist);
}
free(cmdline);
}
}
最佳答案
通过使用 Valgrind 查找损坏的内存位置来解决。 Valgrind 调用了“大小为 8 的无效写入”。解决这个问题后,问题就消失了。感谢 Leon 在上面提出这个建议。
关于C 调用 chdir 后损坏的双链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37708726/
这个问题在这里已经有了答案: chdir() not affecting environment variable PWD (3 个答案) 关闭 7 年前。 我的代码: int ushExC
在路径设置中,我错误地写了代码:os.chdir = '\some path',它把函数os.chdir()变成了一个字符串。有什么不用重启软件就能快速恢复功能的方法吗?谢谢! 最佳答案 将 os 踢
有没有办法恢复到 Mountain Lion 附带的 RubyGems 版本?我问的原因是我安装的每个 gem 都会收到以下警告: /System/Library/Frameworks/Ruby.fr
int main(int argc, char **argv) { char input[150]; char change[2] = "cd"; char *directory; w
我正在使用 chdir() C 函数来允许用户更改目录。 然而,该函数无法识别“~”。我是否需要进行任何显式转换,以便 chdir 无法识别 ~ 的含义?因为我的不行还是我做错了什么? 最佳答案 Ti
在 linux 上的 C 中,chdir() 函数可以接受相对路径吗? 最佳答案 是的。当前工作目录是进程的属性。 稍微扩展一下 - 这里有一些相关的 POSIX定义: current working
如何更改 Ant 中的当前工作目录? Ant documentation没有 任务,在我看来,最好的做法是不要更改当前工作目录。 但让我们假设我们仍然想这样做——你会如何做到这一点?谢谢! 最佳答案
请多多指教 我编译了以下 VB6 代码(作为 --> 文件 --> make project.exe) 但是当我运行它时,我得到了一个很奇怪的东西(我有 WIN XP 机器) 尽管我定义要运行这一行:
我正在使用 c 中的 find 命令进行编程,但当我想对文件使用 exec 时,我被阻止了。 如果它是一个目录,那么很简单,我只使用 chdir(path) 但在文件上我有错误不是目录,所以我不能在上
作为 python 的新手,我想我应该编写一个 python3 脚本来帮助我在命令行上切换目录(值得信赖的 ubuntu)。不幸的是 os.chdir() 似乎不起作用。我尝试过以各种方式修改它,例如
我正在运行以下命令: cd $(find /tmp/my_temp/ -type d -wholename '/tmp/my_temp/MyNumb-*_WeCo - MX') 它返回这样的东西: /
我是 C 编程的新手。我如何更改目录,如 /home/jobs/$ans/xxx/ 而我有 $ans 是用户字符串我不能 chdir 在 C 程序中。 我的脚本如下: #include #inclu
在 Windows 中——可能在 Unix 中——在(32 位)程序中使用 chdir() 函数不会在程序退出时更改目录。 (它适用于 16 位 Windows 程序。) 有人知道如何在 Window
当我使用 chdir() 更改当前工作目录时,为什么 getenv("PWD") 不提供当前工作目录?我还需要 setenv("PWD",newDir,1) 吗? void intChangeDir(
我是 zsh 的新手。 我可以输入类似 cd %wiki 的内容吗?跳转到 ~/prj/golang/gowiki如果它是独一无二的。 但是,如果 cd %unix 可能有两个以上的目录, 只显示匹配
我完整的 perl 脚本是 chdir ("/etc" or die "cannot change: $!\n"); print "\nCurrent Directory is $ENV{PWD}
import os,pdb import sys print os.path.dirname(os.path.realpath(__file__)) pdb.set_trace() os.chdir(
我有一个路径存储在一个变量中(比如 l:s),我想在 vim 脚本中执行 lcd l:s,但它告诉我路径“l:s”不存在。这里有什么问题,因为 vim 可以很好地解析其他 ex 命令中的变量名(ech
是否可以使用 chdir()或其他一些命令来更改线程中的目录而不影响其他线程的 cwd?我正在使用 pthread.h . *我正在尝试编写一个处理多个客户端连接和请求的服务器程序。客户端可用的命令之
我有一个路径存储在一个变量中(比如 l:s),我想在 vim 脚本中执行 lcd l:s,但它告诉我路径“l:s”不存在。这里有什么问题,因为 vim 可以很好地解析其他 ex 命令中的变量名(ech
我是一名优秀的程序员,十分优秀!