getpasswd.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <../boot/boot.h>
  12. void
  13. getpasswd(char *p, int len)
  14. {
  15. char c;
  16. int i, n, fd;
  17. fd = open("#c/consctl", OWRITE);
  18. if(fd < 0)
  19. fatal("can't open consctl; please reboot");
  20. write(fd, "rawon", 5);
  21. Prompt:
  22. print("password: ");
  23. n = 0;
  24. for(;;){
  25. do{
  26. i = read(0, &c, 1);
  27. if(i < 0)
  28. fatal("can't read cons; please reboot");
  29. }while(i == 0);
  30. switch(c){
  31. case '\n':
  32. p[n] = '\0';
  33. close(fd);
  34. print("\n");
  35. return;
  36. case '\b':
  37. if(n > 0)
  38. n--;
  39. break;
  40. case 'u' - 'a' + 1: /* cntrl-u */
  41. print("\n");
  42. goto Prompt;
  43. default:
  44. if(n < len - 1)
  45. p[n++] = c;
  46. break;
  47. }
  48. }
  49. }