The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Canonical showcases Ubuntu Netbook Remix at Computex iBot UNIX and Linux RSS News 0 06-07-2008 07:40 AM
Canonical Showcases Ubuntu Netbook Remix at Computex iBot Security Advisories (RSS) 0 06-03-2008 03:10 PM
Canonical Announces Ubuntu Desktop Training iBot Security Advisories (RSS) 0 04-14-2008 02:30 PM
System76 announces servers with Ubuntu 7.10 and Canonical support services iBot Security Advisories (RSS) 0 02-26-2008 03:50 PM
Canonical to Resell IBM DB2 Express-C as Software, Virtual Appliances iBot Security Advisories (RSS) 0 02-21-2008 08:00 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-16-2007
petricore petricore is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 1
Help with stdin and non-canonical mode

I am writing a more command for my class, and have it most of the way done, but with my current implimentation when I pipe stdin to my code it doesnt use my non-canonical/non echo settings I set for the terminal window. Do I need to specify a different terminal in order for this to work ? It works great when I just use the program on an file.
I will paste the code below

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>

#define PAGELEN 24
#define LINELEN 512

int FILE_SIZE = 0; //holds the global file size for my %'s
int READ_SIZE = 0;
int FIRST_READ = 0; //flag to decide wether to display first time info, or just %

void do_more( FILE *fp, char *file_read,int);
int see_more(FILE *cmd, char *file_read,int);
void cbreak(struct termios tty)
{
tty.c_lflag = tty.c_lflag & ~(ECHO | ECHOK | ICANON);
tty.c_cc[VTIME] = 1;
tcsetattr(0, TCSANOW, &tty);
}

int main(int argc, char **argv)
{
FILE *fp;
struct termios tty, otty;
int i, j;
struct stat file_info; //buffer for file info

tcgetattr(0, &otty); // Get original tty settings and save them in otty
tty = otty;
cbreak(tty); //turns off echo and on non-canonical

if ( argc == 1 ){
fp = stdin;
do_more( fp,argv[0],1 );
}else
while ( --argc ){
if ( (fp = fopen( *++argv , "r" )) != NULL )
{
if (stat(argv[0],&file_info) == -1){ //error condition
perror(argv[0]);
}else{
FILE_SIZE = file_info.st_size;
do_more( fp,argv[0],0 ) ;
fclose( fp );
}
}
else
printf("%s: No such file or directory.\n",argv[0]);
}

tcsetattr(0, TCSANOW, &otty); // Reset to the original settings
return (0);
}
void do_more( FILE *fp, char *file_read,int is_stdin)
{
char line[LINELEN];

int num_of_lines = 0;
int see_more(FILE *, char*,int), reply;
FILE *fp_tty;

fp_tty = fopen( "/dev/tty", "r" ); /* NEW: cmd stream */
if ( fp_tty == NULL ) /* if open fails */
exit(1); /* no use in running */

while ( fgets( line, LINELEN, fp ) ){ /* more input */
if ( num_of_lines == PAGELEN ) { /* full screen? */

reply = see_more(fp_tty, file_read,is_stdin); /* NEW: pass FILE * */
if ( reply == 0 ){ /* n: done */
printf("\n");
break;
}

num_of_lines -= reply; /* reset count */

}
if ( fputs( line, stdout ) == EOF ) /* show line */
exit(1); /* or die */
else
READ_SIZE += strlen(line); //adds the total
num_of_lines++; /* count it */
}
}
int see_more(FILE *cmd, char *file_read, int is_stdin) /* NEW: accepts arg */
{
int c;
if (is_stdin == 0){
if (FIRST_READ == 0){
printf("\033[7m%s: %.1f%%\033[m",file_read,((float)READ_SIZE/(float)FILE_SIZE)*100); /* reverse on a vt100 */
FIRST_READ++;
}else
printf("\033[7m%.1f%%\033[m",((float)READ_SIZE/(float)FILE_SIZE)*100); /* reverse on a vt100 */
}else{
printf("\033[7mbytes %d\033[m",READ_SIZE);
}

while( (c=getc(cmd)) != EOF ) // NEW: reads from tty
{
if ( c == 'q' ) // q -> N
return 0;
if ( c == ' ' ) // ' ' => next p
return PAGELEN; // how many to show
if ( c == '\n' ) // Enter key => 1 line
return 1;
}


}
  #2 (permalink)  
Old 10-16-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965

Code:
tcsetattr(0, TCSANOW, &tty);

Remember

fd | stream
=========
0 | stdin
1 | stdout
2 | stderr

If you are talking about output you should be refering to fd 1.

Two useful routines:

1. isatty()

2. cfmakeraw()
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0