getpasswd.c 654 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <../boot/boot.h>
  4. void
  5. getpasswd(char *p, int len)
  6. {
  7. char c;
  8. int i, n, fd;
  9. fd = open("#c/consctl", OWRITE);
  10. if(fd < 0)
  11. fatal("can't open consctl; please reboot");
  12. write(fd, "rawon", 5);
  13. Prompt:
  14. print("password: ");
  15. n = 0;
  16. for(;;){
  17. do{
  18. i = read(0, &c, 1);
  19. if(i < 0)
  20. fatal("can't read cons; please reboot");
  21. }while(i == 0);
  22. switch(c){
  23. case '\n':
  24. p[n] = '\0';
  25. close(fd);
  26. print("\n");
  27. return;
  28. case '\b':
  29. if(n > 0)
  30. n--;
  31. break;
  32. case 'u' - 'a' + 1: /* cntrl-u */
  33. print("\n");
  34. goto Prompt;
  35. default:
  36. if(n < len - 1)
  37. p[n++] = c;
  38. break;
  39. }
  40. }
  41. }