unistd.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2. /*
  3. * unistd function definitions for NOLIBC
  4. * Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu>
  5. */
  6. /* make sure to include all global symbols */
  7. #include "nolibc.h"
  8. #ifndef _NOLIBC_UNISTD_H
  9. #define _NOLIBC_UNISTD_H
  10. #include "std.h"
  11. #include "arch.h"
  12. #include "types.h"
  13. #include "sys.h"
  14. #define STDIN_FILENO 0
  15. #define STDOUT_FILENO 1
  16. #define STDERR_FILENO 2
  17. #define F_OK 0
  18. #define X_OK 1
  19. #define W_OK 2
  20. #define R_OK 4
  21. /*
  22. * int access(const char *path, int amode);
  23. * int faccessat(int fd, const char *path, int amode, int flag);
  24. */
  25. static __attribute__((unused))
  26. int sys_faccessat(int fd, const char *path, int amode, int flag)
  27. {
  28. return my_syscall4(__NR_faccessat, fd, path, amode, flag);
  29. }
  30. static __attribute__((unused))
  31. int faccessat(int fd, const char *path, int amode, int flag)
  32. {
  33. return __sysret(sys_faccessat(fd, path, amode, flag));
  34. }
  35. static __attribute__((unused))
  36. int access(const char *path, int amode)
  37. {
  38. return faccessat(AT_FDCWD, path, amode, 0);
  39. }
  40. static __attribute__((unused))
  41. int msleep(unsigned int msecs)
  42. {
  43. struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 };
  44. if (sys_select(0, NULL, NULL, NULL, &my_timeval) < 0)
  45. return (my_timeval.tv_sec * 1000) +
  46. (my_timeval.tv_usec / 1000) +
  47. !!(my_timeval.tv_usec % 1000);
  48. else
  49. return 0;
  50. }
  51. static __attribute__((unused))
  52. unsigned int sleep(unsigned int seconds)
  53. {
  54. struct timeval my_timeval = { seconds, 0 };
  55. if (sys_select(0, NULL, NULL, NULL, &my_timeval) < 0)
  56. return my_timeval.tv_sec + !!my_timeval.tv_usec;
  57. else
  58. return 0;
  59. }
  60. static __attribute__((unused))
  61. int usleep(unsigned int usecs)
  62. {
  63. struct timeval my_timeval = { usecs / 1000000, usecs % 1000000 };
  64. return sys_select(0, NULL, NULL, NULL, &my_timeval);
  65. }
  66. static __attribute__((unused))
  67. int tcsetpgrp(int fd, pid_t pid)
  68. {
  69. return ioctl(fd, TIOCSPGRP, &pid);
  70. }
  71. #endif /* _NOLIBC_UNISTD_H */