argv__get_argv.c 1.4 KB

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