dir-lookup.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <gnu/syscall.h>
  21. #include <string.h>
  22. #include <sys/stat.h>
  23. struct mach_msg_string_int_int
  24. {
  25. mach_msg_header_t header;
  26. mach_msg_type_t type_one; string_t one;
  27. mach_msg_type_t type_two; int two;
  28. mach_msg_type_t type_three; int three;
  29. };
  30. struct mach_msg_int_int_string_int
  31. {
  32. mach_msg_header_t header;
  33. mach_msg_type_t type_one; int one;
  34. mach_msg_type_t type_two; int two;
  35. mach_msg_type_t type_three; string_t three;
  36. mach_msg_type_t type_four; int four;
  37. };
  38. mach_msg_type_t mach_msg_type_file_name =
  39. {
  40. /* msgt_name = */ (unsigned char) MACH_MSG_TYPE_STRING_C,
  41. /* msgt_size = */ 8,
  42. /* msgt_number = */ 1024,
  43. /* msgt_inline = */ 1,
  44. /* msgt_longform = */ 0,
  45. /* msgt_deallocate = */ 0,
  46. /* msgt_unused = */ 0
  47. };
  48. kern_return_t __dir_lookup (file_t start_dir, string_t file_name, int flags, mode_t mode, retry_type *do_retry, string_t retry_name, mach_port_t *port)
  49. {
  50. union message
  51. {
  52. struct mach_msg_string_int_int request;
  53. struct mach_msg_int_int_string_int reply;
  54. };
  55. union message message = {0};
  56. message.request.header.msgh_size = sizeof (message.request);
  57. message.request.type_one = mach_msg_type_file_name;
  58. strcpy (message.request.one, file_name);
  59. message.request.type_two = mach_msg_type_int32;
  60. message.request.two = flags;
  61. message.request.type_three = mach_msg_type_int32;
  62. message.request.three = mode;
  63. kern_return_t result = __syscall_get (start_dir, SYS__dir_lookup,
  64. &message.request.header,
  65. sizeof (message.reply));
  66. if (message.reply.one != KERN_SUCCESS)
  67. return message.reply.one;
  68. *port = message.reply.four;
  69. return result;
  70. }