LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-08-2003, 08:29 PM   #1
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Rep: Reputation: 30
Figuring out a 'c_oflag'(man termios) that will help in stdout supression.


Hi everyone! :)
I'm trying to learn some low level stuff inspired by this post (post #5) , but i'm finding the man page a tad cryptic(man termios), is there a flag under 'c_oflag' that i could use to effect terminal output to stdout?
Thank's for any reply's.

Last edited by Tarts; 08-08-2003 at 08:47 PM.
 
Old 08-10-2003, 04:00 AM   #2
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Original Poster
Rep: Reputation: 30
Hi everyone, i got it(i think this thread was dead anyway).
Mabey this will be useful to someone.
this is how you would shutoff terminal output...
Code:
/*A terminal output suppressor:
Copyright (C) 2004 stratis aftousmis E-mail: jojotheracoon@yahoo.com

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.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.*/
#include <stdlib.h>
#include <stdio.h>
#include <termios.h>

struct termios save_attr;    //access the stuct termios
void set_terminal(void)
{
	struct termios set_attr;   //access the struct termios
	set_attr.c_lflag &= ~(ICANON|ECHO);  //in bold is the flags i 
 	tcgetattr(0, &save_attr);                              //i was asking about
	tcsetattr(0, TCSADRAIN, &set_attr); //use 'TCSADRAIN' when 
}                                                                //effecting output.

void reset_terminal(void)
{
	tcsetattr(0, TCSANOW, &save_attr);  //make changes now
}


int main()
{
	char string[100];
	printf("Please input your password...\n");
	set_terminal();
	fgets(string, sizeof(string), stdin);  
	reset_terminal();
	printf("This won't be seen, but the password is %s\n", string);
	return 0;
}
Here's a link to another example, don't use it though, it doesn't reset the terminal but it's got some stuff you might be able to learn from...GNU example , by the way, i'm not that good at programming, the example in the link above showed how to use 'c_lflag'.
{edit}
I do have one more question, how would i error check the 'fgets()' in the code above...and do i need to?
*cricket's*
:)
Thank's.

Last edited by Tarts; 09-14-2003 at 07:01 AM.
 
Old 08-10-2003, 06:45 AM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
fgets is much better than scanf. you should alwayd do error checking, i quote the following from the fgets manpage

<snip>
char *fgets(char *s, int size, FILE *stream);
<snip>
fgets() returns s on success, and NULL on error or when end of file occurs while no characters have been read.
 
Old 08-10-2003, 06:49 AM   #4
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Original Poster
Rep: Reputation: 30
Hi kev :)
Quote:
you should alwayd do error checking
That's the info i didn't know, i've seen alot of error checking in other code i've seen but i couldn't tell if was needed.
Is that what you ment by a better input method in the other thread when dealing with my login program?
{edit}
You left the forum...

Last edited by Tarts; 08-10-2003 at 07:07 AM.
 
Old 08-10-2003, 07:06 AM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
its not as good as it could be because you can still pipe into it but its 1000 times better than scanf. im in the middle of writing code to demonstrate the piping problem which i will post later

btw fgets stores the newline('\n') in the string so you might want to remove that before comparing to a password
 
Old 08-10-2003, 07:12 AM   #6
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by kev82 ]its not as good as it could be because you can still pipe into it but its 1000 times better than scanf. im in the middle of writing code to demonstrate the piping problem which i will post later
WOW, THANK'S!
Quote:
btw fgets stores the newline('\n') in the string so you might want to remove that before comparing to a password
If i can, i don't quite know how, but i would like to experiment and see if i can.
Thank's again kev.
 
Old 08-10-2003, 08:00 AM   #7
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
http://www.dur.ac.uk/k.a.martin/pipe_problem.txt

if ive done something wrong(more than likely) or if you dont understand some part of that please post. oh and lookup the isatty() function dont just copy my code, not that you would
 
Old 08-10-2003, 08:55 AM   #8
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Original Poster
Rep: Reputation: 30
Hello again kev!

Quote:
Originally posted by kev82

if ive done something wrong(more than likely) or if you dont understand some part of that please post. oh and lookup the isatty() function dont just copy my code, not that you would :)
Come now, how would i get better if i did. ;)

I will report any problem's i have, grasias.(i've said thank's in this thread far too many time's.)
 
Old 08-10-2003, 12:48 PM   #9
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Original Poster
Rep: Reputation: 30
Wow it worked, "Sorry is not a terminal", i see what your saying. :)
How on earth did you learn that ?!?

Any hint's on removing the '\n' password array, this problem is where my experience stop's.

Thank's for any reply's kev82.
 
Old 08-10-2003, 02:26 PM   #10
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
ok, think about it like this, you have an array of bytes(chars) the first n of them contain the ascii characters of what was typed in fgets, then '\n' for the new line and finally 0(c strings are zero terminated) so say the user entered sa55pqR the array would like

{'s', 'a', '5', '5', 'p', 'q', 'R', '\n', 0, lots of unimportant junk}

now what you want to end up with is

{'s', 'a', '5', '5', 'p', 'q', 'R', 0, lots of unimportant junk}

just think for a while on the difference between the two and how you would turn one into the other.

Quote:
by Tarts
How on earth did you learn that ?!?
i enjoy and always have enjoyed the challenge of trying to break someone elses code ever since i was adding invincability and extra lives to my amstrad games(seems like a loong time ago). i guess you dont do that for years without learning a few things. i also have aspergers syndrome which gives me a natural aptitude for this sort of stuff
 
Old 08-11-2003, 04:29 AM   #11
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Original Poster
Rep: Reputation: 30
Sorry for the double post all, now i can't figure out how to delete thei post, oh we'll (ignore)...

Last edited by Tarts; 08-11-2003 at 06:24 AM.
 
Old 08-11-2003, 04:47 AM   #12
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by kev82
ok, think about it like this, you have an array of bytes(chars) the first n of them contain the ascii characters of what was typed in fgets, then '\n' for the new line and finally 0(c strings are zero terminated) so say the user entered sa55pqR the array would like

{'s', 'a', '5', '5', 'p', 'q', 'R', '\n', 0, lots of unimportant junk}

now what you want to end up with is

{'s', 'a', '5', '5', 'p', 'q', 'R', 0, lots of unimportant junk}

just think for a while on the difference between the two and how you would turn one into the other.
One has a '\n'.

'strcpy()' hmm, that won't work...

Code:
int main()
{
	char password[100], read[] = "stratis";
	printf("input please...\n");
	if (fscanf(stdin, "%s", &password) != 1)
        {
                printf("Aconverstion error has occured\n");
                return 0;
        }
	if (strcmp(password, read) == 0)
	{
		printf("Success\n");
		return 0;
	}
	else
	printf("Not successfull.\n");
	return 0;
}
Mabey i'm not looking at this the right way, how is 'fscanf()'(?) better than 'scanf()'(?), the man page show's they return the same way, does that mean there both un-usable
(Fun-kay code snippet)
Code:
int main()
{
        char password[100], read[] = "stratis";
        printf("Input password please...\n");
        fgets(password, sizeof(password), stdin);
        if (strcmp(password, read) != 0)
        {
                 char i;
                 password[i] -= '\n';
Ok i feel like a loser, i just can't figure out how to "extract" a newline from a string. We'll i tryed. :)
Thank's for any reply's.
:)
 
Old 08-11-2003, 04:55 AM   #13
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
Quote:
by Me
{'s', 'a', '5', '5', 'p', 'q', 'R', '\n', 0, lots of unimportant junk}

now what you want to end up with is

{'s', 'a', '5', '5', 'p', 'q', 'R', 0, lots of unimportant junk}
this should make it click - the arrays are declared like char x[100] so the size of the array is the same before and after meaning 'lots of unimportant junk' is a byte bigger in the second array(hence i havnt extracted, ive replaced).

<edit> i am a firm believer in 'if you couldnt do it yourself(with aid of a manual), dont ask someone else to' so how about to get more understanding of strings you try and implement your own string functions, just a few like strncat, strncpy, strlen, strcmp, strchr, and strstr.

as for the difference between scanf and fscanf, there arnt any, as far as i know scanf(...) evaluates to fscanf(stdin, ...) there are however LOTS of problems with scanf how about this one

Code:
char x[10];
scanf("%s", x);
what happens when i type more than 9 charcters? just because you only expect the user to type 9 characters doesnt mean they will.

if you want your programs to work well then, assume nothing about the input.

Last edited by kev82; 08-11-2003 at 05:16 AM.
 
Old 08-11-2003, 09:04 AM   #14
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Original Poster
Rep: Reputation: 30
Hi kev
Thank's for the reply, i got it.

Code:
include <stdlib.h>
#include <stdio.h>
#include <strings.h>

int main()
{
	char password[100], read[] = "stratis";
	do
	{
		printf("input please\n");
		fgets(password, sizeof(read), stdin);
		if (strcmp(password, read) == 0)
		{
			printf("Success!\n");
			return 0;
		}
		else
		printf("no go\n");
	}
	return 0;
}
Thank's for all your help!
 
  


Reply



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
gpg: figuring who something was encrypted for prell Linux - Software 0 01-05-2005 09:15 AM
man alsamixer not showing the man page nosaku Slackware 1 12-20-2004 08:52 AM
Having problems figuring out these commands Dandy Linux - Newbie 2 04-18-2004 10:21 PM
How to quit man (less) and keep man info on screen? peb Linux - Newbie 7 03-25-2004 10:02 PM
Compiling packages on RH 7.1 causes man files to be named man.gz mmboam Linux - General 0 05-09-2001 06:47 PM

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

All times are GMT -5. The time now is 12:30 PM.

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