Add makefile
[minish.git] / src / minish-once.c
blobb71529f67e8d193026277433560c96b797f9e097
1 #define _POSIX_C_SOURCE 200809L
3 #include <errno.h> // ENOMEM,errno
4 #include <locale.h> // LC_ALL,setlocale
5 #include <signal.h> // SIGHUP,raise
6 #include <stdio.h> // ferror,fputs,getline,perror,ssize_t,stderr,stdin
7 #include <stdlib.h> // EXIT_FAILURE,NULL,malloc,size_t
8 #include <unistd.h> // execv
10 int main(void) {
11 setlocale(LC_ALL, "");
12 char* line = NULL;
13 size_t size = 0u;
14 ssize_t len = getline(&line, &size, stdin);
15 if (len == -1) {
16 if (ferror(stdin)) {
17 perror("minish-once");
19 raise(SIGHUP);
20 return EXIT_FAILURE;
22 // NOTE len cannot be 0
23 if (line[len - 1] == '\n') {
24 line[--len] = '\0';
26 size_t words = 1u;
27 for (char* s = line; s < line + len; ++s) {
28 if (!*s) {
29 ++words;
32 char** const argv = malloc(words * sizeof(char*));
33 if (!argv) {
34 errno = ENOMEM;
35 perror("minish-once: malloc()");
36 return EXIT_FAILURE;
38 argv[0] = line;
39 size_t i = 1u;
40 for (char* s = line; s < line + len; ++s) {
41 if (!*s) {
42 argv[i++] = s + 1;
45 argv[i] = NULL;
46 execv(line, argv);
47 fputs("minish-once: ", stderr);
48 perror(line);
49 return EXIT_FAILURE;