I am writing a program for my college class :eek: It has to record a password, but it cant display it on the screen. It is a unix based enviroment. Does this look correct to you guys?

main()
{
  int  i;
  char buffer[80];            
  initscr();                

  printw("Please enter a password => ");    
  refresh();                
  noecho();                
                    
  while((buffer[i] = getch()) != '\n') i++;

  printw("\nPassword is %s - press return to continue.",buffer);
  refresh();
  getch();

  endwin();                 }

Dani AI

Generated

A few concise, practical fixes and a safe pattern to use with curses. — congrats on the 97% — and thanks to and for the pointer directions. The common runtime traps to avoid are an uninitialised index, failing to null-terminate, not handling backspace, and printing the password back to the screen. The snippet below shows a robust loop that captures characters, echoes asterisks, guards the buffer, and clears sensitive memory before exit.

int main(void) {
  int ch, i = 0;
  char buffer[80];
  initscr();
  cbreak();
  noecho();
  keypad(stdscr, TRUE);

  while ((ch = getch()) != '\n' && ch != EOF && i < (int)sizeof buffer - 1) {
    if (ch == KEY_BACKSPACE || ch == 127) {
      if (i > 0) {
        i--;
        int y, x;
        getyx(stdscr, y, x);
        mvaddch(y, x - 1, ' ');
        move(y, x - 1);
      }
    } else {
      buffer[i++] = (char)ch;
      addch('*');
    }
    refresh();
  }
  buffer[i] = '\0';
  endwin();
  memset(buffer, 0, sizeof buffer); /* wipe sensitive data */
  return 0;
}

Security and portability notes: do not print the captured password in any final submission. If the assignment allows, prefer platform helpers (e.g., getpass on some Unixes) or higher-level input helpers rather than rolling your own for production code. Always compile/link with the curses library (commonly -lncurses), treat getch() return as int, and clear the buffer immediately after use. Finally, confirm with your instructor whether non-ANSI libraries are permitted before handing in.

Recommended Answers

All 7 Replies

I see the program must be using curses?? Are you allowed to use non-ansi standard functions?

>>Does this look correct to you guys
How it "looks" is unimportant. Does your program compile with 0 errors and does it work ok when you run and test it?

Yes it compiles with 0 errors. I just wanted to make sure it was the correct way to do this so i can get full credit.

Yes it uses curses.

I see the program must be using curses?? Are you allowed to use non-ansi standard functions?

>>Does this look correct to you guys
How it "looks" is unimportant. Does your program compile with 0 errors and does it work ok when you run and test it?

>>Are you allowed to use non-ansi standard functions?

You didn't answer that question. If not then you may not get any credit at all. Of course if your class is studying curses then the question is moot.

Not being familiar with whatever curses is I still have a couple concerns with the code as posted.

First, main() should be declared with a return type, just like any other function. That return type should be type int to be standard, though implementations of some compilers allow you to use void as a return type.

Second, C style strings are null terminated char arrays. To my knowledge, getch() doesn't automatically append a terminating null char to buffers like scanf() or >> or getline() will. Therefore I am concerned that the char array you call buffer is not truly a string, but instead remains a routine array of char, until you specifically add the null terminating char.

most programs that use passwords display an asterisk for each character typed. Your program probably should do that too, and it can be done within that while loop. If you look at the curses library you might find there it already a function that will do it for you. That loop also needs to check for buffer overflow, do not allow i to exceed the size of input buffer.

Thanks for all the help I got a 97% on the project. :)

Thanks for all the help I got a 97% on the project. :)

Congratulations, just keep on putting effort in your work and the rewards will definately come back to you. And if you encounter any other problem just dont hesitate to ask it.

Hope you are enjoying your stay at the community.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.