small crc32 calculator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

args.c 817B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <stddef.h>
  2. static int argc, argi;
  3. static char **argv, *flag;
  4. static char *
  5. nextarg()
  6. {
  7. char *temp = flag;
  8. flag = NULL;
  9. if (temp[1])
  10. return temp + 1;
  11. if (++argi == argc)
  12. return NULL;
  13. return argv[argi];
  14. }
  15. static void
  16. args_parse(int argc_, char **argv_,
  17. void flagfn(char, char*()), void plainfn(char*))
  18. {
  19. argc = argc_;
  20. argv = argv_;
  21. for (argi = 1; argi < argc; argi++) {
  22. char *arg = argv[argi];
  23. if (!arg[0])
  24. continue;
  25. /* if ... || arg == "-" */
  26. if (arg[0] != '-' || !arg[1]) {
  27. if (plainfn)
  28. plainfn(arg);
  29. continue;
  30. }
  31. /* if arg == "--" */
  32. if (arg[1] == '-' && !arg[2]) {
  33. argi++;
  34. break;
  35. }
  36. for (flag = arg + 1; *flag; flag++) {
  37. flagfn(*flag, nextarg);
  38. if (!flag)
  39. break;
  40. }
  41. }
  42. for (; argi < argc; argi++)
  43. if (plainfn)
  44. plainfn(argv[argi]);
  45. }