summaryrefslogtreecommitdiffstats
path: root/backend/util.c
blob: 57d0294bfe66594fed3f6a1ac0f019b31bfae4c7 (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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 *   Copyright (C) 2010 Michael Buesch <m@bues.ch>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 */

#include "util.h"
#include "log.h"

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>


void msleep(unsigned int msecs)
{
	int err;
	struct timespec time;

	time.tv_sec = 0;
	while (msecs >= 1000) {
		time.tv_sec++;
		msecs -= 1000;
	}
	time.tv_nsec = msecs;
	time.tv_nsec *= 1000000;
	do {
		err = nanosleep(&time, &time);
	} while (err && errno == EINTR);
	if (err) {
		fprintf(stderr, "nanosleep() failed with: %s\n",
			strerror(errno));
	}
}

char * string_strip(char *str)
{
	char *start = str;
	size_t len;

	if (!str)
		return NULL;
	while (*start != '\0' && isspace(*start))
		start++;
	len = strlen(start);
	while (len && isspace(start[len - 1])) {
		start[len - 1] = '\0';
		len--;
	}

	return start;
}

char * string_split(char *str, int (*sep_match)(int c))
{
	char c;

	if (!str)
		return NULL;
	for (c = *str; c != '\0'; c = *str) {
		if (sep_match(c)) {
			*str = '\0';
			return str + 1;
		}
		str++;
	}

	return NULL;
}

uint_fast8_t tiny_hash(const char *str)
{
	uint8_t c, hash = 21;

	while ((c = *str++) != '\0')
		hash = ((hash << 3) + hash) + c;

	return hash;
}

pid_t subprocess_exec(const char *_command)
{
	char command_buf[4096] = { };
	char *command;
	char *argv[32] = { };
	unsigned int i;
	long err = 0;
	pid_t pid;

	if (strlen(_command) >= sizeof(command_buf) - 1) {
		logerr("subprocess_exec: Command too long");
		err = -E2BIG;
		goto out;
	}
	strcpy(command_buf, _command);
	command = command_buf;

	for (i = 0; i < ARRAY_SIZE(argv) - 1; i++) {
		argv[i] = command;
		command = string_split(command, isspace);
		if (strempty(argv[i])) {
			/* Skip empty elements */
			argv[i] = NULL;
			i--;
		}
		if (!command)
			break;
	}
	if (command) {
		logerr("subprocess_exec: Too many arguments: '%s'\n", _command);
		err = -EINVAL;
		goto out;
	}
	if (!argv[0]) {
		logerr("subprocess_exec: Too few arguments\n");
		err = -EINVAL;
		goto out;
	}

	pid = vfork();
	if (pid < 0) {
		logerr("subprocess_exec: Failed to fork (%s)\n", strerror(errno));
		err = -errno;
		goto out;
	}
	if (pid == 0) { /* Child */
		execv(argv[0], argv);
		logerr("subprocess_exec: Failed to exec '%s': %s\n",
		       _command, strerror(errno));
		exit(0);
		while (1);
	}
	err = pid;
out:
	return err;
}
bues.ch cgit interface