Operating System - HP-UX
1827259 Members
2321 Online
109716 Solutions
New Discussion

Re: Redirect program I/O to /dev/console in C

 
Renda Skandier
Frequent Advisor

Redirect program I/O to /dev/console in C

I'm trying to write a C program that redirects all input to /dev/console and returns the answer from the console to the initiating program. I'm guessing that I need to /dev/console in rw mode and then pass the data as fgets, fputs but it's not working.
Any ideas??
5 REPLIES 5
Sriharimohan
Advisor

Re: Redirect program I/O to /dev/console in C

Use sprintf and sscanf

fd = open(/usr/console

sprintf(fd, "%d",i);

sscanf(fd,"%d",&i);

One is not defeated when he fails, he is defeated when he quits!!
vasundhara
Frequent Advisor

Re: Redirect program I/O to /dev/console in C

You can use the system calls open, read write.

int open(const char *path, int oflag)
oflag can be O_RDWR for read and write.

len = write(int fildes, const void *buf, size_t nbyte);

len is actual number of bytes written.

len = read(int fildes, void *buf, size_t nbyte);

len is actual number of bytes read.

Regards
VJ.
Carlos Fernandez Riera
Honored Contributor

Re: Redirect program I/O to /dev/console in C

I dont know why you want to use the console. I think it is not a good idea ,unless if you are login on it. Take in mind that the login process are reading too from console; and the parameters for communications ( see stty) can be not correct for your expected input.

But if you are loggined on it, you can easy use the standard file descriptors ( stdin 0, stdout 1, stderr 2). Funtions like getc(), fgets() would be used sucessful if you start your program on console, or any other terminal.

___



unsupported
Renda Skandier
Frequent Advisor

Re: Redirect program I/O to /dev/console in C

I'm trying to create a "daemon" type process to constantly run in the background to alert an operator if a job aborted reguardless of where it was initiated from.

The initiated program will put the error on a que where "daemon" will pick it up & display to console.

I get an error trying to open O_RDWR but O_WRONLY opens OK but sprintf & sscanf don't yield any results
Renda Skandier
Frequent Advisor

Re: Redirect program I/O to /dev/console in C

I'm trying to create a "daemon" type process to constantly run in the background to alert an operator if a job aborted reguardless of where it was initiated from.

The initiating program will put the error on a que where "daemon" will pick it up & display to console.

I get an error trying to open O_RDWR but O_WRONLY opens OK but sprintf & sscanf don't yield any results