Click here to Skip to main content
16,005,552 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Share combobox on different pages Pin
David Crow23-Jul-03 3:39
David Crow23-Jul-03 3:39 
Questionhow to create a user name and password for a database ? Pin
vcseeker22-Jul-03 21:35
vcseeker22-Jul-03 21:35 
AnswerRe: how to create a user name and password for a database ? Pin
sorenb23-Jul-03 0:48
sorenb23-Jul-03 0:48 
AnswerRe: how to create a user name and password for a database ? Pin
Alexander M.,23-Jul-03 2:03
Alexander M.,23-Jul-03 2:03 
QuestionHow to prevent binding a Type library to a ATL Attributed DLL ? need help ! Pin
talhalfon22-Jul-03 21:26
talhalfon22-Jul-03 21:26 
GeneralQuestion about file descriptor. Pin
George222-Jul-03 21:24
George222-Jul-03 21:24 
GeneralRe: Question about file descriptor. Pin
Ryan Binns22-Jul-03 23:34
Ryan Binns22-Jul-03 23:34 
GeneralRe: Question about file descriptor. Pin
George223-Jul-03 0:20
George223-Jul-03 0:20 
Thanks, Ryan buddy!

I have another sample. It is from the well known miniterm.
I think the parent process should close fd and reset the properties of /dev/modem, and stdout, and close /dev/modem even if the clild process has done
that. Am I correct?

Code:
--------
/*
* AUTHOR: Sven Goldt (goldt@math.tu-berlin.de)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
/*
This is like all programs in the Linux Programmer's Guide meant
as a simple practical demonstration.
It can be used as a base for a real terminal program.
*/

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>

#define BAUDRATE B38400
#define MODEMDEVICE "/dev/modem"
#define ENDMINITERM 2 /* ctrl-b to quit miniterm */

#define _POSIX_SOURCE 1 /* POSIX compliant source */

#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

void child_handler(int s)
{
STOP=TRUE;
}

main()
{
int fd,c;
struct termios oldtio,newtio,oldstdtio,newstdtio;
struct sigaction sa;

/*
Open modem device for reading and writing and not as controlling tty
because we don't want to get killed if linenoise sends CTRL-C.
*/
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
if (fd <0) {perror(MODEMDEVICE); exit(-1); }

tcgetattr(fd,&oldtio); /* save current modem settings */

/*
Set bps rate and hardware flow control and 8n1 (8bit,no parity,1 stopbit).
Also don't hangup automatically and ignore modem status.
Finally enable receiving characters.
*/
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;

/*
Ignore bytes with parity errors and make terminal raw and dumb.
*/
newtio.c_iflag = IGNPAR;

/*
Raw output.
*/
newtio.c_oflag = 0;

/*
Don't echo characters because if you connect to a host it or your
modem will echo characters for you. Don't generate signals.
*/
newtio.c_lflag = 0;

/* blocking read until 1 char arrives */
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;

/* now clean the modem line and activate the settings for modem */
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);

/*
Strange, but if you uncomment this command miniterm will not work
even if you stop canonical mode for stdout. This is a linux bug.
*/
tcsetattr(1,TCSANOW,&newtio); /* stdout settings like modem settings */

/* next stop echo and buffering for stdin */
tcgetattr(0,&oldstdtio);
tcgetattr(0,&newstdtio); /* get working stdtio */
newstdtio.c_lflag &= ~(ICANON | ECHO);
tcsetattr(0,TCSANOW,&newstdtio);

/* terminal settings done, now handle in/ouput */
switch (fork())
{
case 0: /* child */
/* user input */
close(1); /* stdout not needed */
for (c=getchar(); c!= ENDMINITERM ; c=getchar()) write(fd,&c,1);
tcsetattr(fd,TCSANOW,&oldtio); /* restore old modem setings */
tcsetattr(0,TCSANOW,&oldstdtio); /* restore old tty setings */
close(fd);
exit(0); /* will send a SIGCHLD to the parent */
break;
case -1:
perror("fork");
tcsetattr(fd,TCSANOW,&oldtio);
close(fd);
exit(-1);
default: /* parent */
close(0); /* stdin not needed */
sa.sa_handler = child_handler;
sa.sa_flags = 0;
sigaction(SIGCHLD,&sa,NULL); /* handle dying child */
while (STOP==FALSE) /* modem input handler */
{
read(fd,&c,1); /* modem */
write(1,&c,1); /* stdout */
}
wait(NULL); /* wait for child to die or it will become a zombie */
break;
}
}

--------

Thanks in advance,
George



GeneralRe: Question about file descriptor. Pin
Ryan Binns23-Jul-03 4:17
Ryan Binns23-Jul-03 4:17 
GeneralRe: Question about file descriptor. Pin
George224-Jul-03 2:48
George224-Jul-03 2:48 
QuestionGDI+ in ATL ActiveX control??? Pin
denkor22-Jul-03 20:41
denkor22-Jul-03 20:41 
GeneralFocus Pin
Valeria Bogdevich22-Jul-03 20:17
Valeria Bogdevich22-Jul-03 20:17 
GeneralRe: Focus Pin
Toni7822-Jul-03 20:25
Toni7822-Jul-03 20:25 
GeneralRe: Focus Pin
Toni7822-Jul-03 22:54
Toni7822-Jul-03 22:54 
GeneralRe: Focus Pin
Valeria Bogdevich23-Jul-03 1:42
Valeria Bogdevich23-Jul-03 1:42 
GeneralRe: Focus Pin
Toni7823-Jul-03 1:47
Toni7823-Jul-03 1:47 
GeneralRe: Focus Pin
Ryan Binns22-Jul-03 23:39
Ryan Binns22-Jul-03 23:39 
GeneralRe: Focus Pin
Ryan Binns23-Jul-03 0:23
Ryan Binns23-Jul-03 0:23 
GeneralRe: Focus Pin
Valeria Bogdevich23-Jul-03 1:29
Valeria Bogdevich23-Jul-03 1:29 
GeneralRe: Focus Pin
Toni7823-Jul-03 1:49
Toni7823-Jul-03 1:49 
GeneralRe: Focus Pin
Ryan Binns23-Jul-03 3:56
Ryan Binns23-Jul-03 3:56 
GeneralRe: Focus Pin
KaЯl22-Jul-03 22:32
KaЯl22-Jul-03 22:32 
GeneralRe: Focus Pin
KaЯl23-Jul-03 2:13
KaЯl23-Jul-03 2:13 
GeneralRe: Focus Pin
KaЯl23-Jul-03 5:30
KaЯl23-Jul-03 5:30 
GeneralRe: Focus Pin
KaЯl23-Jul-03 5:37
KaЯl23-Jul-03 5:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.