aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2023-04-29 22:14:41 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2023-04-29 22:16:42 +0200
commitb58e8bfe04f4f8bcbb18dd6cb7c01f4d44b1f9ec (patch)
tree4c7afa46057e73d111e3ef9004a95205f66b0179 /src
parentdedupe command line routines (diff)
downloadcimple-b58e8bfe04f4f8bcbb18dd6cb7c01f4d44b1f9ec.tar.gz
cimple-b58e8bfe04f4f8bcbb18dd6cb7c01f4d44b1f9ec.zip
cmd_line: read executable name from /proc/self/exe
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/client_main.c4
-rw-r--r--src/cmd_line.c51
-rw-r--r--src/cmd_line.h4
-rw-r--r--src/file.c34
-rw-r--r--src/file.h2
-rw-r--r--src/server_main.c6
-rw-r--r--src/worker_main.c4
8 files changed, 91 insertions, 16 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 30fb3fd..6f2cc6b 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -36,6 +36,7 @@ generate_sql_header(sqlite)
add_my_executable(server server_main.c server.c
ci_queue.c
cmd_line.c
+ file.c
msg.c
net.c
signal.c
@@ -49,6 +50,7 @@ target_include_directories(server PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
add_my_executable(client client_main.c client.c
cmd_line.c
+ file.c
msg.c
net.c)
diff --git a/src/client_main.c b/src/client_main.c
index 4574e44..a895ccf 100644
--- a/src/client_main.c
+++ b/src/client_main.c
@@ -39,7 +39,7 @@ static int parse_settings(struct settings *settings, int argc, char *argv[])
while ((opt = getopt_long(argc, argv, "hVH:p:", long_options, &longind)) != -1) {
switch (opt) {
case 'h':
- exit_with_usage(0, argv[0]);
+ exit_with_usage(0);
break;
case 'V':
exit_with_version();
@@ -51,7 +51,7 @@ static int parse_settings(struct settings *settings, int argc, char *argv[])
settings->port = optarg;
break;
default:
- exit_with_usage(1, argv[0]);
+ exit_with_usage(1);
break;
}
}
diff --git a/src/cmd_line.c b/src/cmd_line.c
index 9527d6f..fee18d4 100644
--- a/src/cmd_line.c
+++ b/src/cmd_line.c
@@ -6,29 +6,68 @@
*/
#include "cmd_line.h"
+#include "file.h"
+#include "log.h"
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
-void exit_with_usage(int ec, const char *argv0)
+static char *get_current_binary_path()
{
- FILE *dest = stdout;
+ return my_readlink("/proc/self/exe");
+}
+
+static char *get_current_binary_name()
+{
+ char *path, *name, *result;
+
+ path = get_current_binary_path();
+ if (!path)
+ return NULL;
+
+ name = basename(path);
+
+ result = strdup(name);
+ if (!result) {
+ log_errno("strdup");
+ goto free_path;
+ }
+
+free_path:
+ free(path);
+
+ return result;
+}
+
+void exit_with_usage(int ec)
+{
+ char *binary;
+ FILE *dest;
+
+ dest = stdout;
if (ec)
dest = stderr;
- fprintf(dest, "usage: %s %s\n", argv0, get_usage_string());
+ binary = get_current_binary_name();
+
+ fprintf(dest, "usage: %s %s\n", binary ?: "prog", get_usage_string());
+ free(binary);
exit(ec);
}
-void exit_with_usage_err(const char *argv0, const char *msg)
+void exit_with_usage_err(const char *msg)
{
if (msg)
fprintf(stderr, "usage error: %s\n", msg);
- exit_with_usage(1, argv0);
+ exit_with_usage(1);
}
void exit_with_version()
{
- printf("%s\n", VERSION);
+ char *binary = get_current_binary_name();
+
+ printf("%s %s\n", binary ?: "prog", VERSION);
+ free(binary);
exit(0);
}
diff --git a/src/cmd_line.h b/src/cmd_line.h
index a661825..58cdb9a 100644
--- a/src/cmd_line.h
+++ b/src/cmd_line.h
@@ -10,8 +10,8 @@
const char *get_usage_string();
-void exit_with_usage(int ec, const char *argv0);
-void exit_with_usage_err(const char *argv0, const char *msg);
+void exit_with_usage(int ec);
+void exit_with_usage_err(const char *msg);
void exit_with_version();
#endif
diff --git a/src/file.c b/src/file.c
index 55e1691..bc7af8e 100644
--- a/src/file.c
+++ b/src/file.c
@@ -63,6 +63,40 @@ free_old:
return ret;
}
+char *my_readlink(const char *path)
+{
+ size_t current_size = 256;
+ char *buf = NULL;
+
+ while (1) {
+ buf = realloc(buf, current_size);
+ if (!buf) {
+ log_errno("realloc");
+ goto free;
+ }
+
+ ssize_t res = readlink(path, buf, current_size);
+ if (res < 0) {
+ log_errno("readlink");
+ goto free;
+ }
+
+ if ((size_t)res == current_size) {
+ current_size *= 2;
+ continue;
+ }
+
+ break;
+ }
+
+ return buf;
+
+free:
+ free(buf);
+
+ return NULL;
+}
+
int file_exists(const char *path)
{
struct stat stat;
diff --git a/src/file.h b/src/file.h
index a68f628..ec3701c 100644
--- a/src/file.h
+++ b/src/file.h
@@ -13,9 +13,9 @@
int rm_rf(const char *dir);
int my_chdir(const char *dir, char **old);
+char *my_readlink(const char *path);
int file_exists(const char *path);
-
int file_read(int fd, char **output, size_t *len);
#endif
diff --git a/src/server_main.c b/src/server_main.c
index 02304b2..cdd73b3 100644
--- a/src/server_main.c
+++ b/src/server_main.c
@@ -39,7 +39,7 @@ static int parse_settings(struct settings *settings, int argc, char *argv[])
while ((opt = getopt_long(argc, argv, "hVp:s:", long_options, &longind)) != -1) {
switch (opt) {
case 'h':
- exit_with_usage(0, argv[0]);
+ exit_with_usage(0);
break;
case 'V':
exit_with_version();
@@ -51,13 +51,13 @@ static int parse_settings(struct settings *settings, int argc, char *argv[])
settings->sqlite_path = optarg;
break;
default:
- exit_with_usage(1, argv[0]);
+ exit_with_usage(1);
break;
}
}
if (!settings->sqlite_path) {
- exit_with_usage_err(argv[0], "must specify the path to a SQLite database");
+ exit_with_usage_err("must specify the path to a SQLite database");
return -1;
}
diff --git a/src/worker_main.c b/src/worker_main.c
index 9e9a0b9..540eea1 100644
--- a/src/worker_main.c
+++ b/src/worker_main.c
@@ -40,7 +40,7 @@ static int parse_settings(struct settings *settings, int argc, char *argv[])
while ((opt = getopt_long(argc, argv, "hVH:p:", long_options, &longind)) != -1) {
switch (opt) {
case 'h':
- exit_with_usage(0, argv[0]);
+ exit_with_usage(0);
break;
case 'V':
exit_with_version();
@@ -52,7 +52,7 @@ static int parse_settings(struct settings *settings, int argc, char *argv[])
settings->port = optarg;
break;
default:
- exit_with_usage(1, argv[0]);
+ exit_with_usage(1);
break;
}
}