/src/zziplib/zzip/plugin.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 90 lines · 49 code · 12 blank · 29 comment · 6 complexity · f9b1678285179b27e7bdb630bae296e5 MD5 · raw file

  1. /*
  2. * Author:
  3. * Guido Draheim <guidod@gmx.de>
  4. * Mike Nordell <tamlin-@-algonet-se>
  5. *
  6. * Copyright (c) 2002,2003 Guido Draheim
  7. * All rights reserved,
  8. * use under the restrictions of the
  9. * Lesser GNU General Public License
  10. * or alternatively the restrictions
  11. * of the Mozilla Public License 1.1
  12. */
  13. #include <zzip/lib.h>
  14. #include <zzip/plugin.h>
  15. #include <string.h>
  16. #include <sys/stat.h>
  17. #include <errno.h>
  18. #include <stdlib.h>
  19. #ifdef DEBUG
  20. #include <stdio.h>
  21. #endif
  22. #include <zzip/file.h>
  23. #include <zzip/format.h>
  24. zzip_off_t
  25. zzip_filesize(int fd)
  26. {
  27. struct stat st;
  28. if (fstat(fd, &st) < 0)
  29. return -1;
  30. # if defined DEBUG && ! defined _WIN32
  31. if (! st.st_size && st.st_blocks > 1) /* seen on some darwin 10.1 machines */
  32. fprintf(stderr, "broken fstat(2) ?? st_size=%ld st_blocks=%ld\n",
  33. (long) st.st_size, (long) st.st_blocks);
  34. # endif
  35. return st.st_size;
  36. }
  37. static const struct zzip_plugin_io default_io = {
  38. &open,
  39. &close,
  40. &_zzip_read,
  41. &_zzip_lseek,
  42. &zzip_filesize,
  43. 1, 1,
  44. &_zzip_write
  45. };
  46. /** => zzip_init_io
  47. * This function returns a zzip_plugin_io_t handle to static defaults
  48. * wrapping the posix io file functions for actual file access. The
  49. * returned structure is shared by all threads in the system.
  50. */
  51. zzip_plugin_io_t
  52. zzip_get_default_io(void)
  53. {
  54. return (zzip_plugin_io_t) & default_io;
  55. }
  56. /**
  57. * This function initializes the users handler struct to default values
  58. * being the posix io functions in default configured environments.
  59. *
  60. * Note that the target io_handlers_t structure should be static or
  61. * atleast it should be kept during the lifetime of zzip operations.
  62. */
  63. int
  64. zzip_init_io(zzip_plugin_io_handlers_t io, int flags)
  65. {
  66. if (! io)
  67. {
  68. return ZZIP_ERROR;
  69. }
  70. memcpy(io, &default_io, sizeof(default_io));
  71. io->fd.sys = flags;
  72. return 0;
  73. }
  74. /*
  75. * Local variables:
  76. * c-file-style: "stroustrup"
  77. * End:
  78. */