You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.1 KiB

  1. /* LINTLIBRARY */
  2. #include <stdio.h>
  3. #include "util.h"
  4. #ifdef BSD
  5. #include <pwd.h>
  6. #endif
  7. char *
  8. util_tilde_expand(char const *fname)
  9. {
  10. #ifdef BSD
  11. struct passwd *userRecord;
  12. char username[256], *filename;
  13. register int i, j;
  14. filename = ALLOC(char, strlen(fname) + 256);
  15. /* Clear the return string */
  16. i = 0;
  17. filename[0] = '\0';
  18. /* Tilde? */
  19. if (fname[0] == '~') {
  20. j = 0;
  21. i = 1;
  22. while ((fname[i] != '\0') && (fname[i] != '/')) {
  23. username[j++] = fname[i++];
  24. }
  25. username[j] = '\0';
  26. if (username[0] == '\0') {
  27. /* ~/ resolves to home directory of current user */
  28. if ((userRecord = getpwuid(getuid())) != 0) {
  29. (void) strcat(filename, userRecord->pw_dir);
  30. } else {
  31. i = 0;
  32. }
  33. } else {
  34. /* ~user/ resolves to home directory of 'user' */
  35. if ((userRecord = getpwnam(username)) != 0) {
  36. (void) strcat(filename, userRecord->pw_dir);
  37. } else {
  38. i = 0;
  39. }
  40. }
  41. }
  42. /* Concantenate remaining portion of file name */
  43. (void) strcat(filename, fname + i);
  44. return filename;
  45. #else
  46. return strsav(fname);
  47. #endif
  48. }