aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs6f/s6f_equote.c
blob: a4c41bbac865f609b214ae08e65ed1d3edf31247 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* ISC license. */

#include <string.h>

#include "s6f.h"

size_t s6f_equote_space (char const *const *argv, unsigned int argc, char const *prefix)
{
  size_t l = (prefix ? strlen(prefix) + 3 : 2) * argc ;
  for (unsigned int i = 0 ; i < argc ; i++) l += strlen(argv[i]) ;
  return l ;
}

unsigned int s6f_equote (char const **newargv, char const *const *argv, unsigned int argc, char const *prefix, char *s)
{
  size_t prefixlen = prefix ? strlen(prefix) : 0 ;
  for (unsigned int i = 0 ; i < argc ; i++)
  {
    newargv[i] = s ;
    *s++ = ' ' ;
    if (prefix)
    {
      memcpy(s, prefix, prefixlen) ;
      *s++ = '/' ;
    }
    s = stpcpy(s, argv[i]) + 1 ;
  }
  newargv[argc] = "" ;
  return argc + 1 ;
}