- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个 shell 程序,当将值传递给 execv()
时,我需要一个指向程序名称的 char 指针(即 ls
),我需要一个指向参数的 char 指针数组的指针。
我会检查并解析用户的输入,如果我输入ls
,如果我打印我的char *
,printf()
打印出“ls”。所以我正确解析该行并存储正确的信息。当我将其传递给 execv()
时,它显示错误的路径名,但如果我手动将指针更改为 progs[0] = "ls"
,那么它就可以工作。如果我比较两个字符串 strcmp(mypointer, "ls")
,它表明虽然 mypointer 打印出 "ls"
,但它并不等同于 "ls”
。
有人知道为什么吗?
这是我的 shell 进程代码:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
char **progs;
char ***arguments;
char **mode;
char pathname[] = "/bin/";
int main(int argc, char ** argv){
//printf("\n %s \n", progs);
//fflush(stdout);
char buff[100];
FILE *p;
p = fdopen(0, "r"); //opens FD 0 (Stdin) as a stream
char * pathname;
while(1){
//printf("I'm at the top");
if(isatty(1) == 1){ //check to see if stdout is going to the terminal
printstart(); //if so, print
}
fgets(buff, 100, p); // Gets the input from the stdin and puts it in buff
int processid = fork(); //fork into child to complete task
if(processid == 0){
//initialize all variables
int numcmd = countcmd(buff);
int pipes = countpipes(buff);
int i;
int j;
//allocate memory for tokenization
progs = malloc(numcmd * sizeof(char *));
arguments = malloc((numcmd) * sizeof(char *));
mode = malloc((numcmd*2) * sizeof(char *));
for(i = 0; i < numcmd; i++){
progs[i] = malloc(10* sizeof(char *));
mode[i] = malloc(10 * sizeof(char *));
mode[2*numcmd-1-i] = malloc(10 * sizeof(char*));
arguments[i] = malloc(15 * sizeof(char *));
for(j = 0; j < 15; j++){
arguments[i][j] = malloc(15 * sizeof(char*));
}
}
/////////////////////////////////////////////////////////////////////
parse(buff); //parses input and places it in the static progs
for(i = 0; i < 1; i++){
printf("\n This is progs %s", arguments[0][0]);
char temp[25];
//strcpy(temp, "/bin/");
strcpy(temp, progs[0]);
//strcat(temp, ' \0');
//*progs = "ls";
char * ptr = progs[0];
for(;*ptr != '\0';){
printf("This is what pointer poitns to %c \n", *ptr++);
}
printf("This is the program: <<%s>>", progs[0]);
fflush(stdout);
char * argument[2];
argument[0] = "ls";
argument[1] = '\0';
char * hell = "l\0";
printf("This is the value of comparison %d\n", strcmp(progs[0], hell));
char **temparg = arguments[0];
//char temp[20] = progs[0];
errno = 0;
execvp("ls", *argument);
char * error = strerror(errno);
printf("This is the error %s", error);
return;
}
}
else{
int status;
waitpid(processid, &status, WIFEXITED(status));
}
}
return 0;
}
这是我的 parse() 代码:
#include <string.h>
#include <stdio.h>
#include "myshell.h"
int parse(char * buff){
//Initialize all variables and pointers
int cmd = 0;
int argument = 0;
int mod = 0;
int j = 0;
int hitargs = 0;
int gotcommand = 0;
int multiarg = 0;
char ** argptr = arguments[cmd];
char * ptr1 = progs[cmd];
char * argptr2 = argptr[argument];
char * ptr2 = mode[mod];
while(buff[j] != '\0'){
switch(buff[j]){
case ';':
cmd++;
argument = 0;
multiarg = 1;
*argptr2++ = '\0';
argptr = arguments[cmd];
argptr2 = argptr[argument];
ptr1 = progs[cmd];
*ptr2 = buff[j];
mod += 2;
ptr2 = mode[mod];
case ' ':
if(gotcommand == 0){
break;
}
else{
if(hitargs == 0){
hitargs = 1;
*ptr1++ = '\0';
argument++;
argptr2 = argptr[argument];
}
else{
argument++;
argptr2 = argptr[argument];
}
break;
}
default:
if(gotcommand == 0){
*ptr1++ = (char) buff[j];
*argptr2++ = buff[j];
gotcommand = 1;
}
else if(gotcommand == 1 && hitargs == 0){
*ptr1++ = (char) buff[j];
*argptr2++ = buff[j];
}
else if(gotcommand == 1 && hitargs == 1){
*argptr2++ = buff[j];
}
}
j++;
}
*argptr2++ = '\0';
*ptr1++ = '\0';
int cmdflag = 0;
int spaceflag = 0;
int argflag = 0;
int cmdct = 1; //account for null
int argumentct = 1; //account for null termination
return 1;
}
对随机 printf 语句感到抱歉。
最佳答案
myshell.h
就像你的解析代码一样。countcmd()
和 countpipes()
)会很有帮助 - 尽管我们可以从它们的名称中猜测一些,但尚不清楚它们到底是做什么的。<unistd.h>
在你的主程序中。fork()
),并且您应该注意并修复这些警告。
gcc
,使用-Wall
是一个很好的起点。启用警告后,您可以看到:
shex.c:95: warning: passing argument 2 of ‘execvp’ from incompatible pointer type
shex.c:99: warning: ‘return’ with no value, in function returning non-void
return EXIT_FAILURE;
处理或exit(EXIT_FAILURE);
.execvp("ls", *argument);
触发.arguments
.char ***arguments;
。 哎哟!我偶尔会使用三重指针,但只是非常偶尔。大多数情况下,指针的级别太多了,尤其是对于本练习而言。char *argument[2];
.execvp("ls", argument);
.我会立即扩展等待代码以至少打印信息:
else
{
int status;
int corpse = waitpid(processid, &status, WIFEXITED(status));
printf("Command exited: PID = %d; status = 0x%.4X\n", corpse, status);
}
在您的解析代码中,case ';':
进入case ' ':
。如果这是故意的,请记录下来(使用诸如 /* DROP THROUGH */
之类的注释);如果没有,请插入缺少的中断。您的default:
案件之后可能也应该有一个休息。这并不是很重要,但它是常规的,可以防止您在需要处理新案件时掉线。
在 parse()
末尾声明的五个变量组成的字符串是多余的;它们是从未使用过的局部变量(正如编译器告诉您的那样)。
位于 main()
的顶部你这样做:
FILE *p;
p = fdopen(0, "r"); //opens FD 0 (Stdin) as a stream
这不是必需的; stdin
已经作为流打开。
您从p
阅读(又名 stdin
)与 fgets()
。总的来说,这很好(比 gets()
更好),但您需要注意 fgets()
包括一个换行符 gets()
没有,您需要检查返回状态(您可能没有读到任何内容)。您还应该使用 sizeof()
:
if (fgets(buff, sizeof(buff), stdin) == 0)
...error - read failed...
内存分配非常复杂。对于固定大小的输入,如 char buff[100];
,您可以使用非动态分配。总共不能超过 50 个参数,因为它们是用空格分隔的。
等等
关于指向字符数组的 Char * = "ls\0"不等于等效的字符串 "ls\0",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10165744/
根据小节 11.4.8 ECMAScript 5.1 标准: The production UnaryExpression : ~ UnaryExpression is evaluated as fo
我正在尝试构建一个“新评论”功能,向用户显示自上次访问以来的新评论数量。我构建了一个“ View ”表,其中包含主题 ID、用户 ID 和时间戳。每次用户访问该主题时更新时间戳或插入新行(如果不存在)
如标题所述,为什么: > !!1=="1" 等于 True 和 > !!2=="2" 等于: False 同样,为什么 > "1"==true 等于 true 而 > "2"==true 等于 fal
我在 Stack Overflow post 上看到了下图 但是,我对“p OR q”、“p AND q”的结果感到困惑,其中“p”等于“false”,“q”等于“unknown”。 在图中,“p O
一栏有效 whereJsonContains('VehicleApplications' ,['ModelName' => $model, 'YearID' => $year] )->
如果满足条件,我如何才能只获取特定记录? 我有代码为 "SELECT a.id, a.text, a.uid, a.time FROM story a INNER JOIN friends b
我正在尝试运行 MongoDB 查询并返回字段为空的记录(更具体地说,在 pyMongo 中为 None)。所以它必须等于 null。 我知道这不等于: {"firstName": {"$ne": N
我在 Java 中进行单元测试时遇到问题。 我把我的代码和错误放在这里。在互联网上我发现这是哈希码的问题。我需要重新创建它们,但我不知道为什么以及如何。 我的方法: public void setGr
如何在 Typescript 中实现 equals? 我尝试了几种方法,都没有奏效。 选项1: abstract class GTreeObject{ abstract equals(obj:
我查看了很多地方,大多数 arraylist 示例都使用“String”作为元素,但是很难找到使用对象的地方。 假设我正在制作一个图书 Collection ,并且我有一个作者对象: class Au
$a,$b,$c = 1,2,3; print "$a, $b, $c\n"; 返回 , , 1 那么 = (equals) 是否比元组构造具有更高的优先级 - 这样做? $a,$b,($c=1
在此代码片段中,a 和 i 分别具有什么值以及为什么? int i = 1; int a = i++; 是a == 1还是a == 2? 最佳答案 a==1。然后,i==2 如果你这样做的话,那就是a
我觉得我遗漏了一些明显的东西。这是一个简单的例子来说明我的问题。 我希望 current = 3 返回“之前”。 current = 4 应该返回“key-two”,current = 5 应该返回“
有人能告诉我为什么这会返回 true 吗?我想如果我投一些东西给例如Object 然后调用.equals,将使用 Object 的默认实现。 s1 == s2 应该返回 false。 请告诉我在哪个主
我需要检查加载到 UIImage 对象文件中的文件是否等于另一个图像,如果是,则执行一些操作。不幸的是,它不起作用。 emptyImage = UIImage(named: imageName) if
我想知道什么是正确的 Java 编程范式来覆盖类 C 对象的 equals(和 hashCode)方法,在以下情况下 (a) 有没有足够的信息来确定 C 的两个实例是否相等,或者 (b) 调用方法不应
>>> (()) == () True >>> (()) () 最佳答案 () 是一个 0 元组。 (foo) 产生 foo 的值。因此,(()) 产生一个 0 元组。 来自 the tutorial
考虑这段代码: var i = 0; >> undefined i += i + i++; >> 0 i >> 0 // why not 1? 由于增量 (++) 运算符,我希望 i 为 1。我认为
在我看来,TValue 似乎缺少一个强制方法; TValue.Equals(TValue)。 那么比较 2 个 TValue 的快速且合适的方法是什么,最好不使用 TValue.ToString(),
使用 SQL 时,在 WHERE 子句中使用 = 代替 LIKE 有什么好处吗? 如果没有任何特殊的运算符,LIKE 和 = 是相同的,对吧? 最佳答案 不同的运算符 LIKE 和 = 是不同的运算符
我是一名优秀的程序员,十分优秀!