Find the answer to your Linux question:
Results 1 to 4 of 4
hi i am facing a situation like this. Its like i am writing a program to insert a user and his password. but i dont want the password to be ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141

    echo() & noecho()


    hi

    i am facing a situation like this. Its like i am writing a program to insert a user and his password. but i dont want the password to be echoed on the screen

    since i was familiar with ncurses i tried out this logic of using echo() and no echo() commands in the code like this

    Code:
    fprintf(handle->outFile,"Enter the password : ");
    noecho();
    fscanf(handle->inFile, "%s",firstpasswd);
    echo();
    fprintf(handle->outFile,"Confirm the password : ");
    noecho();
    fscanf(handle->inFile, "%s",secondpasswd);
    echo();
    i added the -lcurses option in the makefile

    But while compiling i got an error such as this,

    /usr/data/Common/include/dtype.h:74: error: syntax error before numeric constant

    where the code in that include file was like this
    Code:
    #ifndef _C_PLUS_PLUS_
    	#ifdef CORE_TYPES_H
    		#undef false
    		#undef true
    	#endif
    
    	typedef enum _Bool_E
    	{
    		false = 0,(line no:74)
    	//	FALSE = 0,
    		true = 1
    	//	TRUE = 1
    
    	} Bool_E;
    #else
    
    	typedef enum _Bool_E
    	{
    		FALSE = 0,
    		TRUE = 1
    	} Bool_E;
    #endif
    I guess this error is because curses has already defined values for false and true, and i am redefining it over here.

    I cannot afford to make a change in the .h file since if i do, i will have to change the code at a lot of places ( over a 500 as it is already existing code) which is not possible

    Is there any other method i can go about this issue?

    Regards

    Sharon

  2. #2
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    i solved the problem by working it out another way. but i think it is a roundabout way. but it is ok for now

    I wrote a function called getpasswd(char * passwd)

    Code:
    int getpasswd(char* passwd)
    {
    	int i=0;
    	int g=0;
    	char password[64];
    	struct termios org_opts, new_opts;
    	int res=0;
    
       while&#40;i<64&#41;
        &#123;
        	//-----  store old settings -----------
    		res=tcgetattr&#40;STDIN_FILENO, &org_opts&#41;;
    		assert&#40;res==0&#41;;
    		  //---- set new terminal parms --------
    		memcpy&#40;&new_opts, &org_opts, sizeof&#40;new_opts&#41;&#41;;
    		new_opts.c_lflag &= ~&#40;ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL&#41;;
    		tcsetattr&#40;STDIN_FILENO, TCSANOW, &new_opts&#41;;
    		g=getchar&#40;&#41;;
    		  //------  restore old settings ---------
    		res=tcsetattr&#40;STDIN_FILENO, TCSANOW, &org_opts&#41;;
    		assert&#40;res==0&#41;;
    
    		if&#40;g!='\n' && g !='\r'&#41;
    		&#123;
    			password&#91;i&#93;=g;
    			i++;
    		&#125;
    		else
    		&#123;
    			break;
    		&#125;
    	&#125;
    	password&#91;i&#93;='\0';
    	strcpy&#40;passwd,password&#41;;
    	return 0;
    
    &#125;
    and i just called this from where i wanted to get the password.
    fscanf(handle->inFile, "%s",firstpasswd);
    Instead i used

    Code:
    fprintf&#40;handle->outFile,"Enter the password &#58; "&#41;;
    getpasswd&#40;firstpasswd&#41;;
    fprintf&#40;handle->outFile,"\nConfirm the password &#58; "&#41;;
    getpasswd&#40;secondpasswd&#41;;
    if anyone knows of a better way to do this please let me know.
    ok

  3. #3
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    Gee. You went to a lot of work - getpass() in unistd.h is part of the standard library. It supresses echoing on a variety of terminal types.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    cool jim,

    i did not know about that function, anyway now i know thanks to u.

    Will implement that and see. since i read the man page and seems it will work for my program

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •