usershell.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* setusershell(), getusershell(), endusershell() for uClibc.
  2. *
  3. * Copyright (C) 2010 Bernhard Reutner-Fischer <uclibc@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in
  6. * this tarball.
  7. */
  8. /* My manpage reads:
  9. * The getusershell() function returns the next line from the file
  10. * /etc/shells, opening the file if necessary. The line should contain
  11. * the pathname of a valid user shell. If /etc/shells does not exist
  12. * or is unreadable, getusershell() behaves as if /bin/sh and /bin/csh
  13. * were listed in the file.
  14. * The getusershell() function returns a NULL pointer on end-of-file.
  15. */
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <paths.h>
  19. #include <string.h>
  20. #include "internal/parse_config.h"
  21. #if defined __USE_BSD || (defined __USE_XOPEN && !defined __USE_UNIX98)
  22. static const char * const defaultsh[] = { _PATH_BSHELL, _PATH_CSHELL, NULL};
  23. static char *shellb, **shells;
  24. static parser_t *shellp;
  25. void endusershell(void)
  26. {
  27. if (shellp) {
  28. shells = (char**) shellb;
  29. while (shells && *shells) {
  30. char*xxx = *shells++;
  31. free(xxx);
  32. }
  33. config_close(shellp);
  34. shellp = NULL;
  35. }
  36. free(shellb);
  37. shellb = NULL;
  38. shells = NULL;
  39. }
  40. void setusershell(void)
  41. {
  42. endusershell();
  43. shellp = config_open(_PATH_SHELLS);
  44. if (shellp == NULL)
  45. shells = (char **)defaultsh;
  46. else {
  47. char **shell = NULL;
  48. int pos = 0;
  49. while (config_read(shellp, &shell, 1, 1, "# \t", PARSE_NORMAL))
  50. {
  51. shellb = realloc(shellb, (pos + 2) * sizeof(char*));
  52. shells = (char**) shellb + pos++;
  53. *shells++ = strdup(*shell);
  54. *shells = NULL;
  55. }
  56. shells = (char **)shellb;
  57. }
  58. }
  59. char *getusershell(void)
  60. {
  61. char *sh;
  62. if (shells == NULL)
  63. setusershell();
  64. sh = *shells;
  65. if (sh)
  66. shells++;
  67. return sh;
  68. }
  69. #endif