Figuring out a 'c_oflag'(man termios) that will help in stdout supression.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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...
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
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.
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
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.)
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
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
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
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)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.