getusershell.c 585 B

123456789101112131415161718192021222324252627282930313233
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. static const char defshells[] = "/bin/sh\n/bin/csh\n";
  6. static char *line;
  7. static size_t linesize;
  8. static FILE *f;
  9. void endusershell(void)
  10. {
  11. if (f) fclose(f);
  12. f = 0;
  13. }
  14. void setusershell(void)
  15. {
  16. if (!f) f = fopen("/etc/shells", "rbe");
  17. if (!f) f = fmemopen((void *)defshells, sizeof defshells - 1, "rb");
  18. }
  19. char *getusershell(void)
  20. {
  21. ssize_t l;
  22. if (!f) setusershell();
  23. if (!f) return 0;
  24. l = getline(&line, &linesize, f);
  25. if (l <= 0) return 0;
  26. if (line[l-1]=='\n') line[l-1]=0;
  27. return line;
  28. }