File: pipe.c

package info (click to toggle)
obs-studio 30.2.3%2Bdfsg-3.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,928 kB
  • sloc: ansic: 202,137; cpp: 112,403; makefile: 868; python: 599; sh: 275; javascript: 19
file content (76 lines) | stat: -rw-r--r-- 2,223 bytes parent folder | download | duplicates (2)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright (c) 2023 Dennis Sädtler <dennis@obsproject.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "pipe.h"
#include "darray.h"
#include "dstr.h"

struct os_process_args {
	DARRAY(char *) arguments;
};

struct os_process_args *os_process_args_create(const char *executable)
{
	struct os_process_args *args = bzalloc(sizeof(struct os_process_args));
	char *str = bstrdup(executable);
	da_push_back(args->arguments, &str);
	/* Last item in argv must be NULL. */
	char *terminator = NULL;
	da_push_back(args->arguments, &terminator);
	return args;
}

void os_process_args_add_arg(struct os_process_args *args, const char *arg)
{
	char *str = bstrdup(arg);
	/* Insert before NULL list terminator. */
	da_insert(args->arguments, args->arguments.num - 1, &str);
}

void os_process_args_add_argf(struct os_process_args *args, const char *format,
			      ...)
{
	va_list va_args;
	struct dstr tmp = {0};

	va_start(va_args, format);
	dstr_vprintf(&tmp, format, va_args);
	da_insert(args->arguments, args->arguments.num - 1, &tmp.array);
	va_end(va_args);
}

size_t os_process_args_get_argc(struct os_process_args *args)
{
	/* Do not count terminating NULL. */
	return args->arguments.num - 1;
}

char **os_process_args_get_argv(const struct os_process_args *args)
{
	return args->arguments.array;
}

void os_process_args_destroy(struct os_process_args *args)
{
	if (!args)
		return;

	for (size_t idx = 0; idx < args->arguments.num; idx++)
		bfree(args->arguments.array[idx]);

	da_free(args->arguments);
	bfree(args);
}