my_getug.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 2004 by Tito Ragusa <farmatito@tiscali.it>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /*
  22. *
  23. * if bufsize is > 0 char *buffer can not be set to NULL.
  24. * If idname is not NULL it is written on the static allocated buffer
  25. * (and a pointer to it is returned).
  26. * if idname is NULL, id as string is written to the static allocated buffer
  27. * and NULL is returned.
  28. * if bufsize is = 0 char *buffer can be set to NULL.
  29. * If idname exists a pointer to it is returned,
  30. * else NULL is returned.
  31. * if bufsize is < 0 char *buffer can be set to NULL.
  32. * If idname exists a pointer to it is returned,
  33. * else an error message is printed and the program exits.
  34. */
  35. #include <stdio.h>
  36. #include <assert.h>
  37. #include "libbb.h"
  38. /* internal function for my_getpwuid and my_getgrgid */
  39. char * my_getug(char *buffer, char *idname, long id, int bufsize, char prefix)
  40. {
  41. if(bufsize > 0 ) {
  42. assert(buffer!=NULL);
  43. if(idname) {
  44. return safe_strncpy(buffer, idname, bufsize);
  45. }
  46. snprintf(buffer, bufsize, "%ld", id);
  47. } else if(bufsize < 0 && !idname) {
  48. bb_error_msg_and_die("unknown %cid %ld", prefix, id);
  49. }
  50. return idname;
  51. }
  52. /* END CODE */
  53. /*
  54. Local Variables:
  55. c-file-style: "linux"
  56. c-basic-offset: 4
  57. tab-width: 4
  58. End:
  59. */