argv.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * argv.c
  3. *
  4. * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
  5. *
  6. * $Id$
  7. */
  8. #include "config.h"
  9. #include <ctype.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "argv.h"
  14. int
  15. argv_create(char *p, int argc, char *argv[])
  16. {
  17. int i;
  18. for (i = 0; i < argc - 1; i++) {
  19. while (*p != '\0' && isspace((int)*p))
  20. *p++ = '\0';
  21. if (*p == '\0')
  22. break;
  23. argv[i] = p;
  24. while (*p != '\0' && !isspace((int)*p))
  25. p++;
  26. }
  27. p[0] = '\0';
  28. argv[i] = NULL;
  29. return (i);
  30. }
  31. /* XXX - from tcpdump util.c. */
  32. char *
  33. argv_copy(char *argv[])
  34. {
  35. char **p, *buf, *src, *dst;
  36. int len = 0;
  37. p = argv;
  38. if (*p == 0)
  39. return (NULL);
  40. while (*p)
  41. len += strlen(*p++) + 1;
  42. if ((buf = (char *)malloc(len)) == NULL)
  43. return (NULL);
  44. p = argv;
  45. dst = buf;
  46. while ((src = *p++) != NULL) {
  47. while ((*dst++ = *src++) != '\0')
  48. ;
  49. dst[-1] = ' ';
  50. }
  51. dst[-1] = '\0';
  52. return (buf);
  53. }