_info 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "config.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. /**
  5. * foreach - macro for simple iteration of arrays
  6. *
  7. * The foreach_int and foreach_ptr macros allow simple iteration of
  8. * static arrays. This is very efficient with good compiler support
  9. * (ie. gcc), and not too bad (ie. a few compares and mallocs) for
  10. * other compilers.
  11. *
  12. * License: LGPL (3 or any later version)
  13. * Author: Rusty Russell <rusty@rustcorp.com.au>
  14. *
  15. * Example:
  16. * // Figure out how to get usage: message out of a program.
  17. * #include <ccan/foreach/foreach.h>
  18. * #include <stdio.h>
  19. * #include <stdlib.h>
  20. * #include <string.h>
  21. * #include <err.h>
  22. *
  23. * // Returns true if program outputs "usage:"
  24. * static bool try_usage(const char *prog, const char *arg)
  25. * {
  26. * char *command;
  27. * FILE *in;
  28. * int status;
  29. *
  30. * command = malloc(strlen(prog) + 1 + strlen(arg) + 1 +
  31. * sizeof("</dev/null 2>&1 | grep -qiw usage:"));
  32. * sprintf(command, "%s %s </dev/null 2>&1 | grep -qiw usage:",
  33. * prog, arg);
  34. * in = popen(command, "r");
  35. * if (!in)
  36. * err(2, "running '%s'", command);
  37. * status = pclose(in);
  38. * free(command);
  39. * return (WIFEXITED(status) && WEXITSTATUS(status) == 0);
  40. * }
  41. *
  42. * int main(int argc, char *argv[])
  43. * {
  44. * const char *arg;
  45. *
  46. * if (argc != 2)
  47. * errx(1, "Usage: %s <progname>\n"
  48. * "Figures out how to get usage message", argv[0]);
  49. * foreach_ptr(arg, "--help", "-h", "--usage", "-?", "") {
  50. * if (try_usage(argv[1], arg)) {
  51. * printf("%s %s\n", argv[1], arg);
  52. * exit(0);
  53. * }
  54. * }
  55. * printf("%s is unhelpful\n", argv[1]);
  56. * exit(1);
  57. * }
  58. */
  59. int main(int argc, char *argv[])
  60. {
  61. if (argc != 2)
  62. return 1;
  63. if (strcmp(argv[1], "depends") == 0) {
  64. #if !HAVE_COMPOUND_LITERALS || !HAVE_FOR_LOOP_DECLARATION
  65. printf("ccan/list\n");
  66. #endif
  67. return 0;
  68. }
  69. return 1;
  70. }