argv__get_argv.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * argv__get_argv.c
  3. */
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include <string.h>
  7. #include "lub/string.h"
  8. #include "private.h"
  9. /*--------------------------------------------------------- */
  10. char *lub_argv__get_line(const lub_argv_t * this)
  11. {
  12. int space = 0;
  13. const char *p;
  14. unsigned i;
  15. char *line = NULL;
  16. for (i = 0; i < this->argc; i++) {
  17. if (i != 0)
  18. lub_string_cat(&line, " ");
  19. space = 0;
  20. /* Search for spaces */
  21. for (p = this->argv[i].arg; *p; p++) {
  22. if (isspace(*p)) {
  23. space = 1;
  24. break;
  25. }
  26. }
  27. if (space)
  28. lub_string_cat(&line, "\"");
  29. lub_string_cat(&line, this->argv[i].arg);
  30. if (space)
  31. lub_string_cat(&line, "\"");
  32. }
  33. return line;
  34. }
  35. /*--------------------------------------------------------- */
  36. char **lub_argv__get_argv(const lub_argv_t * this, const char *argv0)
  37. {
  38. char **result = NULL;
  39. unsigned i;
  40. unsigned a = 0;
  41. if (argv0)
  42. a = 1;
  43. result = malloc(sizeof(char *) * (this->argc + 1 + a));
  44. if (argv0)
  45. result[0] = strdup(argv0);
  46. for (i = 0; i < this->argc; i++)
  47. result[i + a] = strdup(this->argv[i].arg);
  48. result[i + a] = NULL;
  49. return result;
  50. }
  51. /*--------------------------------------------------------- */
  52. void lub_argv__free_argv(char **argv)
  53. {
  54. unsigned i;
  55. if (!argv)
  56. return;
  57. for (i = 0; argv[i]; i++)
  58. free(argv[i]);
  59. free(argv);
  60. }
  61. /*--------------------------------------------------------- */