LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-21-2014, 12:36 PM   #1
prushik
Member
 
Registered: Mar 2009
Location: Pennsylvania
Distribution: gentoo
Posts: 372

Rep: Reputation: 29
STDIO redirection and syscalls


Hi everybody,
I wrote a small "cat"-like program for linux using Linux system calls for reading files and outputting to STDOUT. It also reads from STDIN if no arguments are given.
This all works fine, but I also want to be able to redirect text streams to and from the program. This does not work. For instance,
Code:
echo "test" | ./a.out
does nothing. The program does not receive the input through stdin and outputs nothing to the terminal. Also, when the program is in this state, it will no longer receive interactive input from the command line.

I am using SYS_read with stdin (1) and SYS_write with stdout (0). In normal C programming with the fread family of functions, this gets handled automatically, so I don't understand why it doesn't work with system calls.
Any ideas?
 
Old 04-21-2014, 12:46 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
How about strace and/or gdb?
Maybe I could say something about your program if I saw the source...
 
Old 04-21-2014, 11:07 PM   #3
prushik
Member
 
Registered: Mar 2009
Location: Pennsylvania
Distribution: gentoo
Posts: 372

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by NevemTeve View Post
How about strace and/or gdb?
Maybe I could say something about your program if I saw the source...
Source code looks like this:

Code:
#include <fcntl.h>
#include <sys/syscall.h>

void cat(int in, int out)
{
	int ret;
	char byte;

	do
	{
		ret = syscall(SYS_read,in,&byte,1);
		syscall(SYS_write,out,&byte,1);
	} while (ret!=0);

	return;
}

int main(int argc, char **argv)
{
	int i;

	if (argc==1)
	{
		cat(1,0);
	}
	else
	{
		for (i=1;i<argc;i++)
		{
			int fd = syscall(SYS_open,argv[i],O_RDONLY,0);
			cat(fd,0);
			syscall(SYS_close,fd);
		}
	}

//	syscall(SYS_exit);
	return 0;
}
 
Old 04-21-2014, 11:18 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
For a start:

Code:
while ((ret= syscall(SYS_read,in,&byte,1))>0)
    syscall(SYS_write,out,&byte,ret);
}
 
Old 04-22-2014, 12:04 AM   #5
prushik
Member
 
Registered: Mar 2009
Location: Pennsylvania
Distribution: gentoo
Posts: 372

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by NevemTeve View Post
For a start:

Code:
while ((ret= syscall(SYS_read,in,&byte,1))>0)
    syscall(SYS_write,out,&byte,ret);
}
Ok, yes. That would be more correct, however, it doesn't address the issue of redirection.
 
Old 04-22-2014, 12:14 AM   #6
prushik
Member
 
Registered: Mar 2009
Location: Pennsylvania
Distribution: gentoo
Posts: 372

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by NevemTeve View Post
How about strace and/or gdb?
Maybe I could say something about your program if I saw the source...
Also, according to strace, there is nothing coming in from the redirected text stream, but when something is redirected to the program it fails to write to stdout (0) with "EBADF (Bad file descriptor)". It still seems to read interactive user input fine, just not any text piped into it.
 
Old 04-22-2014, 01:30 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Another option would be using 0 for stdin, 1 for stdout, 2 for stderr.
Or symbolic constants from unistd.h:
Code:
#define STDIN_FILENO  0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
 
Old 04-22-2014, 01:35 AM   #8
prushik
Member
 
Registered: Mar 2009
Location: Pennsylvania
Distribution: gentoo
Posts: 372

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by NevemTeve View Post
Another option would be using 0 for stdin, 1 for stdout, 2 for stderr.
Or symbolic constants from unistd.h:
Code:
#define STDIN_FILENO  0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
Ahha! Silly me... always some stupid mistake. Well that's one mystery solved.
Now why did it work at all before when I was reading from stdout and writing to stdin?
 
Old 04-22-2014, 01:43 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Because both were special file /dev/tty
Of course, it is not guaranteed that you can read from stdout in any context, but also it is not guaranteed that you cannot.
 
  


Reply

Tags
redirection, stdin, stdout, syscall, sys_read



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Purpose of syscalls.h joatmon2 Linux - Kernel 1 01-24-2013 08:05 PM
syscalls bandaru_sivakrishna Linux - Newbie 1 07-06-2009 12:08 PM
syscalls palisetty_suman Linux - Newbie 2 01-27-2009 10:35 PM
syscalls.h ashlesha Programming 3 04-03-2007 10:56 AM
messing with syscalls m00t00 Programming 2 11-27-2004 03:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration