file-io-posix.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2009 Openmoko Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <fcntl.h>
  18. #include <unistd.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <wikilib.h>
  22. #include <file-io.h>
  23. #include <msg.h>
  24. #include <wl-time.h>
  25. int wl_open(const char *filename, int flags)
  26. {
  27. int f = 0;
  28. switch (flags) {
  29. case WL_O_RDONLY:
  30. f = O_RDONLY;
  31. break;
  32. case WL_O_WRONLY:
  33. f = O_WRONLY;
  34. break;
  35. case WL_O_RDWR:
  36. f = O_RDWR;
  37. break;
  38. case WL_O_CREATE:
  39. f = O_WRONLY | O_TRUNC | O_CREAT;
  40. break;
  41. }
  42. return open(filename, f, S_IRWXU);
  43. }
  44. void wl_close(int fd)
  45. {
  46. close(fd);
  47. }
  48. int wl_read(int fd, void *buf, unsigned int count)
  49. {
  50. return read(fd, buf, count);
  51. }
  52. int wl_write(int fd, void *buf, unsigned int count)
  53. {
  54. return write(fd, buf, count);
  55. }
  56. int wl_seek(int fd, unsigned int pos)
  57. {
  58. return lseek(fd, pos, SEEK_SET);
  59. }
  60. int wl_fsize(int fd, unsigned int *size)
  61. {
  62. int ret;
  63. struct stat stat_buf;
  64. ret = fstat(fd, &stat_buf);
  65. if (ret < 0)
  66. return ret;
  67. *size = (unsigned int) stat_buf.st_size;
  68. return 0;
  69. }
  70. unsigned int wl_tell(int fd)
  71. {
  72. return lseek(fd, 0, SEEK_CUR);
  73. }