hurdchdir.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Change a port cell to a directory by looking up a name.
  2. Copyright (C) 1999-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <hurd.h>
  18. #include <hurd/port.h>
  19. #include <hurd/fd.h>
  20. #include <fcntl.h>
  21. #include <string.h>
  22. int
  23. _hurd_change_directory_port_from_name (struct hurd_port *portcell,
  24. const char *name)
  25. {
  26. size_t len;
  27. const char *lookup;
  28. file_t dir;
  29. /* Append trailing "/." to directory name to force ENOTDIR if it's not a
  30. directory and EACCES if we don't have search permission. */
  31. len = strlen (name);
  32. if (len >= 2 && name[len - 2] == '/' && name[len - 1] == '.')
  33. lookup = name;
  34. else if (len == 0)
  35. /* Special-case empty file name according to POSIX. */
  36. return __hurd_fail (ENOENT);
  37. else
  38. {
  39. char *n = alloca (len + 3);
  40. memcpy (n, name, len);
  41. n[len] = '/';
  42. n[len + 1] = '.';
  43. n[len + 2] = '\0';
  44. lookup = n;
  45. }
  46. dir = __file_name_lookup (lookup, 0, 0);
  47. if (dir == MACH_PORT_NULL)
  48. return -1;
  49. _hurd_port_set (portcell, dir);
  50. return 0;
  51. }