aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2023-11-20 10:05:48 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2023-11-20 10:05:48 -0500
commit243d8540b3869f9ace2baf8b00f9cb86e8d84d52 (patch)
treedbb3566b4d1e750008ba40c0800d9bd55a9774cc
parentd377b6fd45fbffe3d57ce3486853938e45bebaf4 (diff)
downloadstasis-243d8540b3869f9ace2baf8b00f9cb86e8d84d52.tar.gz
Fix warnings and add xmkstemp
* Reduce stack usage for status variable in msg() * Bugfix: write OMC_COLOR_RESET to the requested stream instead of stdout * Add helper function xmkstemp to avoid clutter when using disposable files
-rw-r--r--include/utils.h9
-rw-r--r--src/utils.c27
2 files changed, 30 insertions, 6 deletions
diff --git a/include/utils.h b/include/utils.h
index 15684e8..f815c12 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -45,5 +45,14 @@ char *git_describe(const char *path);
void msg(unsigned type, char *fmt, ...);
void debug_shell();
+/**
+ * Creates a temporary file returning an open file pointer via @a fp, and the
+ * path to the file. The caller is responsible for closing @a fp and
+ * free()ing the returned file path.
+ * @param fp pointer to FILE (to be initialized)
+ * @return system path to the temporary file
+ * @return NULL on failure
+ */
+char *xmkstemp(FILE **fp);
#endif //OMC_UTILS_H
diff --git a/src/utils.c b/src/utils.c
index 417b0cc..802d397 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -4,8 +4,9 @@
extern struct OMC_GLOBAL globals;
char *dirstack[1024];
-const size_t dirstack_max = sizeof(dirstack) / sizeof(dirstack[0]);
-size_t dirstack_len = 0;
+const ssize_t dirstack_max = sizeof(dirstack) / sizeof(dirstack[0]);
+ssize_t dirstack_len = 0;
+
int pushd(const char *path) {
if (dirstack_len + 1 > dirstack_max) {
return -1;
@@ -309,7 +310,6 @@ int touch(const char *filename) {
perror(filename);
return 1;
}
- fprintf(stderr, "");
fclose(fp);
return 0;
}
@@ -375,7 +375,7 @@ char *git_describe(const char *path) {
void msg(unsigned type, char *fmt, ...) {
FILE *stream = NULL;
char header[255];
- char status[255];
+ char status[20];
if (type & OMC_MSG_NOP) {
// quiet mode
@@ -418,8 +418,7 @@ void msg(unsigned type, char *fmt, ...) {
fprintf(stream, "%s", header);
vfprintf(stream, fmt, args);
- printf("%s", OMC_COLOR_RESET);
- printf("%s", OMC_COLOR_RESET);
+ fprintf(stream, "%s", OMC_COLOR_RESET);
va_end(args);
}
@@ -429,3 +428,19 @@ void debug_shell() {
msg(OMC_MSG_L1 | OMC_MSG_WARN, "EXITING OMC DEBUG SHELL\n" OMC_COLOR_RESET);
exit(255);
}
+
+char *xmkstemp(FILE **fp) {
+ char t_name[PATH_MAX];
+ strcpy(t_name, "/tmp/OMC.XXXXXX");
+ int fd = mkstemp(t_name);
+
+ *fp = fdopen(fd, "w");
+ if (!*fp) {
+ if (fd > 0)
+ close(fd);
+ *fp = NULL;
+ return NULL;
+ }
+ char *path = strdup(t_name);
+ return path;
+} \ No newline at end of file