This commit is contained in:
qwjyh 2024-10-10 20:53:37 +09:00
commit 9f1862f150
8 changed files with 56 additions and 0 deletions

3
.clang-format Normal file
View file

@ -0,0 +1,3 @@
BasedOnStyle: LLVM
IndentWidth: 4

2
.clangd Normal file
View file

@ -0,0 +1,2 @@
CompileFlags:
Add: [-std=gnu23]

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
main

8
Makefile Normal file
View file

@ -0,0 +1,8 @@
CFLAGS := -Wall -Wextra
main: main.c
gcc ${CFLAGS} -o $@ $^
.PHONY: clean
clean:
-rm main

3
README.adoc Normal file
View file

@ -0,0 +1,3 @@
= Cからシェルスクリプトを呼ぶテスト
* 作業ディレクトリが変わらないので注意
* 適切にオプションを設定しないとエラー検知できないので注意

23
main.c Normal file
View file

@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
const auto cmd = "./subdir/entry.sh";
FILE *f = popen(cmd, "r");
if (f == NULL) {
perror("popen failed");
return EXIT_FAILURE;
}
char buf[1024];
while (fgets(buf, sizeof(buf), f)) {
printf("read: %s", buf);
}
int ret = pclose(f);
if (ret == -1) {
perror("pclose failed");
return EXIT_FAILURE;
};
printf("Return code: %d", ret);
return EXIT_SUCCESS;
}

5
subdir/called.sh Executable file
View file

@ -0,0 +1,5 @@
echo "Hi, this is called"
echo "\${0}: ${0}"
pwd
echo "Good bye"
exit 1

11
subdir/entry.sh Executable file
View file

@ -0,0 +1,11 @@
#!/bin/env bash
set -euo pipefail
echo "Hi, this is entry.sh"
echo "pwd:"
pwd
echo "\${0}: ${0}"
cd "$(dirname "${0}")"
./called.sh
echo $?
echo "returned"
exit 0