_info 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "config.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. /**
  5. * pushpull - simple marshalling/unmarshalling routines
  6. *
  7. * This code lets you clearly add simple types into a buffer (the push
  8. * functions) and remove them (the pull functions). The buffer stores
  9. * the values as little-endian for machine portability. The pull functions
  10. * don't need to be checked on every call, but error state is kept so you
  11. * can check if there was an error at the end.
  12. *
  13. * The normal way to use this is to create your own higher-level marshal
  14. * and unmarshal functions in terms of these.
  15. *
  16. * Author: Rusty Russell <rusty@rustcorp.com.au>
  17. * License: CC0 (Public domain)
  18. *
  19. * Example:
  20. * #include <ccan/pushpull/push.h>
  21. * #include <ccan/pushpull/pull.h>
  22. * #include <ccan/err/err.h>
  23. * #include <string.h>
  24. * #include <stdio.h>
  25. * #include <unistd.h>
  26. *
  27. * int main(int argc, char *argv[])
  28. * {
  29. * if (argv[1] && !strcmp(argv[1], "push")) {
  30. * int i;
  31. * char *buf = malloc(1);
  32. * size_t len = 0;
  33. *
  34. * // We ignore allocation failure!
  35. * for (i = 2; i < argc; i++)
  36. * push_u32(&buf, &len, atol(argv[i]));
  37. *
  38. * write(STDOUT_FILENO, buf, len);
  39. * } else if (argc == 2 && !strcmp(argv[1], "pull")) {
  40. * int r, max = 32;
  41. * size_t len = 0;
  42. * char *buf = malloc(max);
  43. * const char *p;
  44. * uint32_t val;
  45. *
  46. * while ((r = read(STDIN_FILENO, buf+len, max-len)) > 0) {
  47. * len += r;
  48. * if (len == max) {
  49. * max *= 2;
  50. * // We crash on allocation failure
  51. * buf = realloc(buf, max);
  52. * }
  53. * }
  54. *
  55. * p = buf;
  56. * while (pull_u32(&p, &len, &val))
  57. * printf("%u ", val);
  58. * } else
  59. * errx(1, "Usage: %s [push|pull] [<number>...]", argv[0]);
  60. * return 0;
  61. * }
  62. */
  63. int main(int argc, char *argv[])
  64. {
  65. /* Expect exactly one argument */
  66. if (argc != 2)
  67. return 1;
  68. if (strcmp(argv[1], "depends") == 0) {
  69. printf("ccan/endian\n");
  70. return 0;
  71. }
  72. return 1;
  73. }