2014-03-01 80 views
0

下面我有一个标记器,我试图把它变成一个shell程序。我刚刚开始,所以我知道该程序没有设置为执行任何shell命令,但是在打印当前工作目录时遇到问题。我会告诉我下面的代码:打印出当前的工作目录

#include <ctype.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <errno.h> 
int main() 
{ 
pid_t pid; 
char cwd[1000]; 
if (getcwd, sizeof(cwd) != NULL) 
{ 
    fprintf(stdout, "Current working dir: %s\n"); 
    return 0; 
} 
printf("Please enter a string\n"); 
int ch = fgetc(stdin); 
while (ch != EOF) 
{ 
    pid = fork(); 
    if (pid == 0) 
    { 
     printf("Child Working"); 

    } 
    else 
     printf("Child not working"); 
while (isspace(ch)) 
{ 

    // If only 1 line of input allowed, then add 
    if (ch == '\n') return 0;; 

    ch = fgetc(stdin); 
} 
if (ch != EOF) 
    { 
    do 
    { 
    fputc(ch, stdout); 
    ch = fgetc(stdin); 
    } 
    while (ch != EOF && !isspace(ch)); 
    fputc('\n', stdout); 
    } 
    } 
    return 0; 
} 

到目前为止,我得到的输出是在下面的屏幕截图:

enter image description here

+0

也是我的叉子过程就是建立能够处理其他shell命令,对不起,如果该位是混乱 – user3358064

回答

1

变化

if (getcwd, sizeof(cwd) != NULL) 

到:

if (getcwd(cwd, sizeof(cwd)) != NULL) 

fprintf(stdout, "Current working dir: %s\n"); 

到:

printf("Current working dir: %s\n", cwd); 
+0

哇谢谢你,我很抱歉我感觉我浪费你的时间与简单的错误 – user3358064

+0

@ user3358064这不是浪费时间。这是他的决定回答。这是这个网站的目的:) –