From 9f1862f150514d1b4db77e4b4ff39246629fc1f5 Mon Sep 17 00:00:00 2001 From: qwjyh Date: Thu, 10 Oct 2024 20:53:37 +0900 Subject: [PATCH] init --- .clang-format | 3 +++ .clangd | 2 ++ .gitignore | 1 + Makefile | 8 ++++++++ README.adoc | 3 +++ main.c | 23 +++++++++++++++++++++++ subdir/called.sh | 5 +++++ subdir/entry.sh | 11 +++++++++++ 8 files changed, 56 insertions(+) create mode 100644 .clang-format create mode 100644 .clangd create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.adoc create mode 100644 main.c create mode 100755 subdir/called.sh create mode 100755 subdir/entry.sh diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..8ceb7ca --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +BasedOnStyle: LLVM +IndentWidth: 4 + diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..de383d6 --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Add: [-std=gnu23] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +main diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d7ab6e6 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +CFLAGS := -Wall -Wextra + +main: main.c + gcc ${CFLAGS} -o $@ $^ + +.PHONY: clean +clean: + -rm main diff --git a/README.adoc b/README.adoc new file mode 100644 index 0000000..1a79888 --- /dev/null +++ b/README.adoc @@ -0,0 +1,3 @@ += Cからシェルスクリプトを呼ぶテスト +* 作業ディレクトリが変わらないので注意 +* 適切にオプションを設定しないとエラー検知できないので注意 diff --git a/main.c b/main.c new file mode 100644 index 0000000..005aee9 --- /dev/null +++ b/main.c @@ -0,0 +1,23 @@ +#include +#include + +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; +} diff --git a/subdir/called.sh b/subdir/called.sh new file mode 100755 index 0000000..0c5cbc5 --- /dev/null +++ b/subdir/called.sh @@ -0,0 +1,5 @@ +echo "Hi, this is called" +echo "\${0}: ${0}" +pwd +echo "Good bye" +exit 1 diff --git a/subdir/entry.sh b/subdir/entry.sh new file mode 100755 index 0000000..4e7b013 --- /dev/null +++ b/subdir/entry.sh @@ -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