usershell.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* vi: set sw=4 ts=4: */
  2. /* getusershell and friends for uClibc
  3. *
  4. * Copyright (C) 2002 by Robert Griebl <griebl@gmx.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU Library General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. /* Rely on the .bss to zero these */
  23. static FILE *fp;
  24. static int isopen;
  25. static char **cursh;
  26. static char *validsh [] = { "/bin/sh", "/bin/csh", 0 };
  27. void endusershell ( void )
  28. {
  29. if ( fp && isopen )
  30. fclose ( fp );
  31. isopen = 0;
  32. }
  33. void setusershell ( void )
  34. {
  35. if ( isopen )
  36. endusershell ( );
  37. fp = fopen ( "/etc/shells", "r" );
  38. cursh = validsh;
  39. isopen = 1;
  40. }
  41. char *getusershell ( void )
  42. {
  43. char line [BUFSIZ];
  44. if ( !isopen )
  45. setusershell ( );
  46. if ( fp ) {
  47. return fgets ( line, sizeof( line ) - 1, fp );
  48. } else {
  49. char *result = *cursh;
  50. if ( result )
  51. cursh++;
  52. return result;
  53. }
  54. }