[英]reading from stdin using read() in C
I want to read from the stdin char by char and compare it with a different char using system-calls only, my problem is , given the code: 我想按字符从stdin char中读取,并仅使用系统调用将其与其他char比较,我的问题是,给定代码:
#include "util.h"
#define STDOUT 1
#define STDIN 1
#define SYS_READ 3
#define SYS_WRITE 4
#define SYS_OPEN 5
#define SYS_CLOSE 6
#define SYS_LSEEK 19
int main (int argc , char* argv[], char* envp[])
{
char str[512];
int fd;
if((fd= system_call(SYS_OPEN,STDIN,0, 0777))==-1){
system_call(SYS_WRITE,STDOUT,"stdin error", 11);
}
while((system_call(SYS_READ,fd, str,1))>0){
if((strncmp(";",str,1))==0){
system_call(SYS_WRITE,STDOUT,str, 1);
system_call(SYS_WRITE,STDOUT,"\n", 2);
}
else{
system_call(SYS_WRITE,STDOUT,str, 1);
}
}
return 0;
}
I have an Assembly file that turn this: "system_call(SYS_WRITE,STDOUT,"\\n", 2);" 我有一个汇编文件将其转换为:“ system_call(SYS_WRITE,STDOUT,” \\ n“,2);” to write(...);
来写(...);
now the problem is, I don't know how to make it wait for the input, so it never starts the while loop because it didn't read a thing from the input to begin with. 现在的问题是,我不知道如何让它等待输入,所以它永远不会启动while循环,因为它没有从输入中读取任何内容。
edit, the code to System-Call: 编辑,将代码转到系统调用:
system_call:
push ebp ; Save caller state
mov ebp, esp
sub esp, 4 ; Leave space for local var on stack
pushad ; Save some more caller state
mov eax, [ebp+8] ; Copy function args to registers: leftmost...
mov ebx, [ebp+12] ; Next argument...
mov ecx, [ebp+16] ; Next argument...
mov edx, [ebp+20] ; Next argument...
int 0x80 ; Transfer control to operating system
mov [ebp-4], eax ; Save returned value...
popad ; Restore caller state (registers)
mov eax, [ebp-4] ; place returned value where caller can see it
add esp, 4 ; Restore caller state
pop ebp ; Restore caller state
ret ; Back to caller
I ended up fixing the problem with STDIN(define was off), and not defining a fd to receive the values and it ended up working, not sure why exactly still, I'm guessing fd was set non-blocking . 我最终用STDIN(define已关闭)修复了这个问题,并且没有定义fd来接收值,并且最终无法正常工作,不知道为什么仍然如此,我猜fd被设置为non-blocking。
您将STDIN定义为1,应将其定义为0。1是STDOUT,因此您试图从仅用于写入的文件句柄中进行读取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.