чување f5745bcd7bc2691e5b0dfb438f22a1b905da9ca4
родитељ 5a103eaadcbd5615148b49c5819a4310fc0c8fb9
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Sun, 30 Jun 2024 16:15:48 +0200
Use variables instead of #ifdef macros; refactor backup/symlink code
Diffstat:
измењених датотека: 8, додавања: 189(+), брисања: 165(-)
diff --git a/README b/README
@@ -56,12 +56,12 @@ A: You don't. If you want customization/configuration, clone the git repo,
Q: Why am I getting a "Permission denied" when trying to edit a file?
A: You either can't access the directory for writing and search (execute bit)
- and haven't disabled the creation of backups nor set BACKUP_FAIL_OK, or can't
- access the file itself for writing.
+ and haven't disabled the creation of backups nor set backup_fail to 0, or
+ can't access the file itself for writing.
Q: How to get crontab -e to work?
-A: Uncomment the BACKUP_FAIL_OK define in config.h. If that fails, comment out
- the CREATE_BACKUPS define in that file.
+A: Set the backup_fail to 0 in config.h. If that fails, set create_backups to 0
+ in that file.
License
diff --git a/config.def.h b/config.def.h
@@ -2,22 +2,7 @@
* any later version. Copyright (C) 2022-2024 Страхиња Радић.
* See the file LICENSE for exact copyright and license details. */
-/* Comment out to not create backups */
-#define CREATE_BACKUPS
-
-/* Remove comment to not fail with error if creating backups is not possible
- * due to file and directory permissions */
-/*#define BACKUP_FAIL_OK*/
-
-/* Comment out to not wrap lines */
-#define ENABLE_WRAP
-
-#define WRAP_WIDTH 72
-
#ifndef ONLY_DEFS
-#ifdef SLED_C_GLOBALS
-const char* backup_suffix = "~";
-#endif /* SLED_C_GLOBALS */
#define DLG_FG TB_BLACK
#define DLG_BG TB_WHITE
@@ -29,6 +14,8 @@ const char* backup_suffix = "~";
#define INPUT_BG TB_WHITE
#define INPUT_EDIT_FG TB_WHITE
#define INPUT_EDIT_BG TB_BLACK
+#define LINENO_FG TB_BLACK
+#define LINENO_BG TB_BLACK | TB_BRIGHT
#define PROMPT_FG TB_YELLOW | TB_BOLD
#define PROMPT_BG TB_MAGENTA
#define SEARCH_FG TB_WHITE | TB_BOLD
@@ -50,9 +37,30 @@ const char* backup_suffix = "~";
#define TITLE_FG TB_BLACK
#define TITLE_BG TB_WHITE
+extern const int backup_fail;
+extern const char* backup_suffix;
+extern const int create_backups;
+extern const int enable_wrap;
+extern const int line_numbers;
+extern const int line_numbers_width;
+extern const int wrap_width;
+
#ifdef SLED_C_GLOBALS
/* clang-format off */
+/* Fail with error if creating backups is not possible due to file and
+ * directory permissions? */
+const int backup_fail = 1;
+const char* backup_suffix = "~";
+/* Comment out to not create backups */
+const int create_backups = 1;
+const int enable_wrap = 1;
+const int line_numbers = 1; /* draw line numbers? */
+const int line_numbers_width = 6; /* number of glyphs for line numbers */
+/* - ideally, wrap_width + line_numbers_width <= 80 */
+/* - wrap width of 72 is recommended on https://useplaintext.email/ */
+const int wrap_width = 72;
+
/* Always processed */
static const struct Key top_level_keys[] = {
{ .ev = { .key = TB_KEY_ESC }, .callback = cancel_close },
diff --git a/config.mk b/config.mk
@@ -1,5 +1,5 @@
#CC = cc
-CFLAGS = -Os -Wall -pedantic -std=c99
+CFLAGS = -Os -Wextra -Wall -pedantic -std=c99
CPPFLAGS = -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=200809L \
-D_XOPEN_SOURCE=700
# OpenBSD
diff --git a/draw.c b/draw.c
@@ -78,7 +78,6 @@ const char* actions[] = {
const char* go_to_dialog_title = "Go to line";
const char* replace_dialog_title = "Replace with";
const char* search_dialog_title = "Search text";
-const char* program_name = "sled";
const char* prompt_dirty = "File changed, save [y/n/ESC]?";
const char* prompt_overwrite = "Not empty, overwrite? [y/n/ESC]?";
const char* prompt_reread_dirty = "File changed, lose changes [y/n/ESC]?";
@@ -593,6 +592,7 @@ free_drawstate(struct DrawState* state, int skip_paste_free)
free_bufferline(state->buffer + row, 1);
free(state->backup_filename);
free(state->real_filename);
+ free(state->orig_real_filename);
free(state->buffer);
if (!skip_paste_free)
{
@@ -627,16 +627,17 @@ void
init_drawstate(struct DrawState* state, char* error, char* action,
char* filename, char* prompt, int skip_paste_and_mode_init)
{
- state->error = error;
- state->action = action;
- state->filename = filename;
- state->real_filename = NULL;
- state->backup_filename = NULL;
- state->rfn_len = 0;
- state->input = NULL;
- state->search = NULL;
- state->chain_dialog = 0;
- state->buffer = NULL;
+ state->error = error;
+ state->action = action;
+ state->filename = filename;
+ state->real_filename = NULL;
+ state->orig_real_filename = NULL;
+ state->backup_filename = NULL;
+ state->rfn_len = 0;
+ state->input = NULL;
+ state->search = NULL;
+ state->chain_dialog = 0;
+ state->buffer = NULL;
init_coord(&state->coord, 0, 0);
state->display_column = 0;
init_coord(&state->first_shown, 0, 0);
diff --git a/draw.h b/draw.h
@@ -54,7 +54,8 @@ struct DrawState {
char* prompt;
char* action;
char* filename;
- char* real_filename; /* what symlink points to */
+ char* orig_real_filename; /* what symlink points to */
+ char* real_filename; /* directory holding the symlink prepended to it */
char* backup_filename;
ssize_t rfn_len;
uint32_t* input;
diff --git a/sled.1.in b/sled.1.in
@@ -210,6 +210,6 @@ runtime.
This is a design decision.
.It
If backup creation is enabled
-.Pq Dv CREATE_BACKUPS No in Pa config.h ,
+.Pq Dv create_backups No in Pa config.h ,
the directory of the edited file must be writable by the user to open the file.
.El
diff --git a/sled.c b/sled.c
@@ -26,15 +26,16 @@
" termbox developers,\n" \
" which is released under the terms of Expat license, see the file" \
" LICENSE.termbox.\n")
-#define SLED_C_GLOBALS
+#define SLED_C_GLOBALS
#define ONLY_DEFS
-#include "draw.h"
-#include "sled.h"
#include "config.h"
-#include "version.h"
+#include "draw.h"
#include "limit.h"
+#include "sled.h"
#include "util.h"
+#include "version.h"
+
#undef ONLY_DEFS
#include "config.h"
@@ -59,7 +60,6 @@ enum {
SAVE_ERR_CANT_UNLINK = 7
};
-extern const char* backup_suffix;
extern const char* errors[];
extern const char* actions[];
extern const char* mode_labels[];
@@ -69,7 +69,6 @@ extern const char* search_dialog_title;
extern const char* prompt_dirty;
extern const char* prompt_overwrite;
extern const char* prompt_reread_dirty;
-extern const char* program_name;
int
cancel_close(struct DrawState* state)
@@ -1062,12 +1061,14 @@ insert_char(struct DrawState* state, uint32_t ch)
position_to(state, state->coord.row, state->coord.col + 1);
buffer_line->text[buffer_line->length] = 0;
-#ifdef ENABLE_WRAP
- while (state->display_column >= WRAP_WIDTH)
+ if (!enable_wrap)
+ goto insert_skip_wrap;
+
+ while (state->display_column >= wrap_width)
{
int saved_column = state->coord.col;
buffer_line = state->buffer + state->coord.row;
- state->coord.col = buffer_length(buffer_line->text, WRAP_WIDTH);
+ state->coord.col = buffer_length(buffer_line->text, wrap_width);
/* clang-format off */
while (state->coord.col > 0
&& !u32_is_word_boundary(
@@ -1084,8 +1085,8 @@ insert_char(struct DrawState* state, uint32_t ch)
position_to(state, state->coord.row + 1,
saved_column - state->coord.col);
}
-#endif /* ENABLE_WRAP */
+insert_skip_wrap:
state->dirty = 1;
return 0;
@@ -1106,12 +1107,14 @@ insert_line(struct DrawState* state)
clear_selection(state);
}
-#ifdef ENABLE_WRAP
- while (state->display_column >= WRAP_WIDTH)
+ if (!enable_wrap)
+ goto insert_skip_wrap;
+
+ while (state->display_column >= wrap_width)
{
int saved_column = state->coord.col;
buffer_line = state->buffer + state->coord.row;
- state->coord.col = buffer_length(buffer_line->text, WRAP_WIDTH);
+ state->coord.col = buffer_length(buffer_line->text, wrap_width);
while (state->coord.col > 0
&& !u32_is_word_boundary(buffer_line->text[state->coord.col
- 1],
@@ -1127,7 +1130,8 @@ insert_line(struct DrawState* state)
position_to(state, state->coord.row + 1,
saved_column - state->coord.col);
}
-#endif /* ENABLE_WRAP */
+
+insert_skip_wrap:
simple_insert_line(state);
state->saved_column = -1;
position_to(state, state->coord.row + 1, 0);
@@ -1260,7 +1264,8 @@ load_file(struct DrawState* state)
state->buffer_size = ALLOC_DELTA;
state->rows_count = 0;
- if (!(input = fopen(state->filename, "rt")))
+ input = fopen(state->filename, "rt");
+ if (!input)
return LOAD_ERR_CANT_OPEN_FILE;
while (!feof(input))
@@ -1832,7 +1837,7 @@ print_error(const int code, const char* msg, ...)
va_start(args, msg);
vsnprintf(buf, LINE_DEFAULT, msg, args);
va_end(args);
- fprintf(stderr, "%s: %s\n", program_name, buf);
+ fprintf(stderr, "%s: %s\n", PROGRAM_NAME, buf);
return code;
}
@@ -1915,9 +1920,7 @@ save_file(struct DrawState* state)
char* buf = NULL;
char* pbuf = NULL;
ssize_t bufsize = LINE_DEFAULT;
-#ifdef CREATE_BACKUPS
struct stat st;
-#endif
int result = SAVE_ERR_NONE;
char* temp = NULL;
@@ -1928,38 +1931,28 @@ save_file(struct DrawState* state)
redraw_status(state);
errno = 0;
-#ifdef CREATE_BACKUPS
- if (state->new_file)
+ if (!create_backups || state->new_file)
+ goto save_skip_backup_copy;
+
+ if (!backup_fail && !state->backup_possible)
goto save_skip_backup_copy;
-#ifdef BACKUP_FAIL_OK
- if (state->backup_possible)
- {
- if (unlink(state->backup_filename) < 0 && errno != ENOENT)
- {
- result = SAVE_ERR_CANT_COPY_FILE;
- goto save_file_cleanup;
- }
- errno = 0;
- cp(state->rfn_len > 0 ? state->real_filename : state->filename,
- state->backup_filename);
- errno = 0; /* ignore errors */
- }
-#else /* !BACKUP_FAIL_OK */
if (unlink(state->backup_filename) < 0 && errno != ENOENT)
{
- result = SAVE_ERR_CANT_COPY_FILE;
+ result = SAVE_ERR_CANT_UNLINK;
goto save_file_cleanup;
}
-
if (cp(state->rfn_len > 0 ? state->real_filename : state->filename,
state->backup_filename))
{
- result = SAVE_ERR_CANT_COPY_FILE;
- goto save_file_cleanup;
+ if (backup_fail)
+ {
+ result = SAVE_ERR_CANT_COPY_FILE;
+ goto save_file_cleanup;
+ }
+ else
+ errno = 0; /* ignore errors */
}
-#endif /* BACKUP_FAIL_OK */
-#endif /* CREATE_BACKUPS */
save_skip_backup_copy:
if (!(output = fopen(state->rfn_len > 0 ? state->real_filename
@@ -1970,8 +1963,7 @@ save_skip_backup_copy:
goto save_file_cleanup;
}
-#ifdef CREATE_BACKUPS
- if (state->new_file)
+ if (!create_backups || state->new_file)
goto save_skip_backup_symlink;
if (lstat(state->rfn_len > 0 ? state->real_filename : state->filename,
@@ -1981,27 +1973,10 @@ save_skip_backup_copy:
result = SAVE_ERR_CANT_STAT_FILE;
goto save_file_cleanup;
}
-#ifdef BACKUP_FAIL_OK
- if (state->backup_possible)
- {
- if (state->rfn_len > 0)
- {
- if (unlink(state->filename) == -1)
- {
- result = SAVE_ERR_CANT_UNLINK;
- goto save_file_cleanup;
- }
- if (symlink(state->real_filename, state->filename) == -1)
- {
- result = SAVE_ERR_CANT_SYMLINK;
- goto save_file_cleanup;
- }
- }
- chmod(state->rfn_len > 0 ? state->real_filename
- : state->filename,
- st.st_mode);
- }
-#else /* !BACKUP_FAIL_OK */
+
+ if (!backup_fail && !state->backup_possible)
+ goto save_skip_backup_symlink;
+
if (state->rfn_len > 0)
{
if (unlink(state->filename) == -1)
@@ -2009,7 +1984,7 @@ save_skip_backup_copy:
result = SAVE_ERR_CANT_UNLINK;
goto save_file_cleanup;
}
- if (symlink(state->real_filename, state->filename) == -1)
+ if (symlink(state->orig_real_filename, state->filename) == -1)
{
result = SAVE_ERR_CANT_SYMLINK;
goto save_file_cleanup;
@@ -2017,8 +1992,6 @@ save_skip_backup_copy:
}
chmod(state->rfn_len > 0 ? state->real_filename : state->filename,
st.st_mode);
-#endif /* BACKUP_FAIL_OK */
-#endif /* CREATE_BACKUPS */
save_skip_backup_symlink:
if (state->rows_count == 1 && state->buffer[0].length == 0)
@@ -2460,12 +2433,11 @@ toggle_selection(struct DrawState* state)
static int
wrap_para(struct DrawState* state)
{
- char* temp = NULL;
-#ifdef ENABLE_WRAP
struct BufferLine* current = NULL;
struct BufferLine* next = NULL;
uint32_t* ptext = NULL;
- int sel = SEL_NOT_EMPTY(state);
+ char* temp = NULL;
+ int sel;
int start_row;
int end_row;
int cur_row;
@@ -2473,6 +2445,11 @@ wrap_para(struct DrawState* state)
int dlen;
int col;
+ if (!enable_wrap)
+ goto wrap_not_enabled;
+
+ sel = SEL_NOT_EMPTY(state);
+
MEMCCPY(state->action, actions[ACT_WRAPPING], LINE_DEFAULT, temp);
redraw_status(state);
@@ -2508,11 +2485,10 @@ wrap_next_line:
state->coord.col = 0;
current = &state->buffer[cur_row];
next = &state->buffer[cur_row + 1];
- bcol = buffer_length(current->text, WRAP_WIDTH);
state->coord.row = cur_row;
dlen = display_length(current->text, current->length);
- while (dlen < WRAP_WIDTH && u32_has_nonblanks(next->text))
+ while (dlen < wrap_width && u32_has_nonblanks(next->text))
{
/* Insert space to avoid joining ending and beginning words */
if (!u32_is_word_boundary(current->text[current->length - 1], 1))
@@ -2537,9 +2513,9 @@ wrap_next_line:
dlen = display_length(current->text, current->length);
}
- while (dlen >= WRAP_WIDTH)
+ while (dlen >= wrap_width)
{
- bcol = buffer_length(current->text, WRAP_WIDTH);
+ bcol = buffer_length(current->text, wrap_width);
while (bcol > 0
&& !u32_is_word_boundary(current->text[bcol - 1], 1))
bcol--;
@@ -2575,11 +2551,11 @@ wrap_next_line:
if (cur_row == state->rows_count)
break;
- /* If the line in block is < WRAP_WIDTH, join it with next */
+ /* If the line in block is < wrap_width, join it with next */
next = &state->buffer[cur_row + 1];
if (u32_has_nonblanks(next->text)
&& display_length(current->text, current->length)
- < WRAP_WIDTH)
+ < wrap_width)
{
/* Add space if needed to avoid joining words */
if (!u32_is_word_boundary(current->text[current->length
@@ -2610,7 +2586,7 @@ wrap_next_line:
if (cur_row == MIN((end_row + 1), state->rows_count))
break;
- bcol = buffer_length(current->text, WRAP_WIDTH);
+ bcol = buffer_length(current->text, wrap_width);
while (bcol > 0
&& !u32_is_word_boundary(current->text[bcol - 1], 1))
bcol--;
@@ -2628,10 +2604,12 @@ wrap_next_line:
position_to(state, cur_row, 0);
*state->action = 0;
+ goto wrap_success;
-#else
+wrap_not_enabled:
MEMCCPY(state->error, errors[ERR_NOT_IMP], LINE_DEFAULT, temp);
-#endif
+
+wrap_success:
return 0;
wrap_alloc_error:
@@ -2697,6 +2675,7 @@ main(int argc, char** argv)
char prompt[LINE_DEFAULT];
struct DrawState state;
char* filename = NULL;
+ char* wd = NULL;
ssize_t filename_size = 0;
struct stat st;
int result;
@@ -2714,7 +2693,7 @@ main(int argc, char** argv)
#ifdef __OpenBSD__
if (pledge("stdio tty unveil rpath wpath cpath fattr", NULL) < 0)
{
- perror("sled: pledge");
+ perror(PROGRAM_NAME ": pledge");
exit(1);
}
#endif
@@ -2727,35 +2706,27 @@ main(int argc, char** argv)
"-v |"
" --version\n"
"\t%s filename\n",
- program_name, program_name);
+ PROGRAM_NAME, PROGRAM_NAME);
return 0;
}
else if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))
{
- printf("%s %s, committed on %s\n", program_name, VERSION, DATE);
+ printf("%s %s, committed on %s\n", PROGRAM_NAME, VERSION, DATE);
return 0;
}
else if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--full-version"))
{
- printf("%s %s, committed on %s\n", program_name, VERSION, DATE);
+ printf("%s %s, committed on %s\n", PROGRAM_NAME, VERSION, DATE);
puts(COPYRIGHT);
printf("Features set in config.h:\n");
-#ifdef CREATE_BACKUPS
- printf(" CREATE_BACKUPS:\tset\n");
-#else
- printf(" CREATE_BACKUPS:\tunset\n");
-#endif
-#ifdef BACKUP_FAIL_OK
- printf(" BACKUP_FAIL_OK:\tset\n");
-#else
- printf(" BACKUP_FAIL_OK:\tunset\n");
-#endif
-#ifdef ENABLE_WRAP
- printf(" ENABLE_WRAP:\t\tset\n");
-#else
- printf(" ENABLE_WRAP:\t\tunset\n");
-#endif
- printf(" WRAP_WIDTH:\t\t%d\n", WRAP_WIDTH);
+ printf(" backup_fail:\t\t%s\n", backup_fail ? "set" : "unset");
+ printf(" create_backups:\t%s\n",
+ create_backups ? "set" : "unset");
+ printf(" enable_wrap:\t\t%s\n", enable_wrap ? "set" : "unset");
+ printf(" line_numbers:\t\t%s\n",
+ line_numbers ? "set" : "unset");
+ printf(" line_numbers_width:\t%d\n", line_numbers_width);
+ printf(" wrap_width:\t\t%d\n", wrap_width);
return 0;
}
@@ -2772,7 +2743,7 @@ main(int argc, char** argv)
(void)dirname(dir, argv[1], filename_size);
if (init_limits(dir) < 0)
{
- perror("sled: init_limits");
+ perror(PROGRAM_NAME ": init_limits");
free(dir);
exit(1);
}
@@ -2798,7 +2769,7 @@ main(int argc, char** argv)
#ifdef __OpenBSD__
if (unveil(state.filename, "rwc") < 0)
{
- perror("sled: unveil");
+ perror(PROGRAM_NAME ": unveil");
free(filename);
free(dir);
exit(1);
@@ -2808,13 +2779,41 @@ main(int argc, char** argv)
#ifdef __OpenBSD__
if (unveil(dir, "rwc") < 0)
{
- perror("sled: unveil");
+ perror(PROGRAM_NAME ": unveil");
+ free(filename);
+ free(dir);
+ exit(1);
+ }
+#endif
+
+ if (!create_backups)
+ goto main_skip_backups;
+
+ if (!(wd = malloc(limits[L_PATH_MAX])))
+ {
+ perror(PROGRAM_NAME ": malloc");
+ free(filename);
+ free(dir);
+ exit(1);
+ }
+ if (!getcwd(wd, limits[L_PATH_MAX]))
+ {
+ perror(PROGRAM_NAME ": getcwd");
+ free(wd);
+ free(filename);
+ free(dir);
+ exit(1);
+ }
+#ifdef __OpenBSD__
+ if (unveil(wd, "rwc") < 0)
+ {
+ perror(PROGRAM_NAME ": unveil");
free(filename);
free(dir);
exit(1);
}
#endif
-#ifdef CREATE_BACKUPS
+
errno = 0;
if (lstat(state.filename, &st) != -1)
{
@@ -2824,9 +2823,8 @@ main(int argc, char** argv)
{
free(filename);
free(dir);
- return print_error(1,
- "Allocation failed (out of "
- "memory?)");
+ perror(PROGRAM_NAME ": malloc");
+ exit(1);
}
if (!(state.real_filename = malloc(limits[L_PATH_MAX])))
@@ -2834,9 +2832,8 @@ main(int argc, char** argv)
free(filename);
free(dir);
free(rfn);
- return print_error(1,
- "Allocation failed (out of "
- "memory?)");
+ perror(PROGRAM_NAME ": malloc");
+ exit(1);
}
state.rfn_len = readlink(state.filename, rfn,
@@ -2847,10 +2844,24 @@ main(int argc, char** argv)
rfn[state.rfn_len] = 0;
if (*rfn == '/') /* rfn is absolute */
+ {
MEMCCPY(state.real_filename, rfn,
limits[L_PATH_MAX], temp);
+ state.rfn_len = strlen(rfn);
+ }
else /* rfn is relative */
{
+ state.orig_real_filename
+ = strndup(rfn, state.rfn_len);
+ if (!state.orig_real_filename)
+ {
+ free(filename);
+ free(dir);
+ free(rfn);
+ perror(PROGRAM_NAME ": strndup");
+ exit(1);
+ }
+
MEMCCPY(state.real_filename, dir,
limits[L_PATH_MAX], temp);
if (!temp)
@@ -2869,7 +2880,7 @@ main(int argc, char** argv)
#ifdef __OpenBSD__
if (unveil(state.real_filename, "rwc") < 0)
{
- perror("sled: unveil");
+ perror(PROGRAM_NAME ": unveil");
free(filename);
exit(1);
}
@@ -2908,20 +2919,20 @@ main(int argc, char** argv)
#ifdef __OpenBSD__
if (unveil(state.backup_filename, "rwc") < 0)
{
- perror("sled: unveil");
+ perror(PROGRAM_NAME ": unveil");
free(filename);
free(dir);
exit(1);
}
#endif
-#endif /* CREATE_BACKUPS */
+main_skip_backups:
if (state.rfn_len > 0)
{
if (!(pdir = realloc(dir, limits[L_PATH_MAX])))
{
result = 1;
- print_error(1, "Allocation failed (out of memory?)");
+ perror(PROGRAM_NAME ": realloc");
goto general_error;
}
dir = pdir;
@@ -2930,7 +2941,7 @@ main(int argc, char** argv)
#ifdef __OpenBSD__
if (unveil(dir, "rwc") < 0)
{
- perror("sled: unveil");
+ perror(PROGRAM_NAME ": unveil");
free(filename);
free(dir);
exit(1);
@@ -2938,13 +2949,25 @@ main(int argc, char** argv)
#endif
}
+#ifdef __OpenBSD__
+ if (unveil(NULL, NULL) < 0)
+ {
+ perror(PROGRAM_NAME ": unveil");
+ free(filename);
+ free(dir);
+ free(rfn);
+ exit(1);
+ }
+#endif
+
dir_access = access(dir, W_OK | X_OK);
dir_access_errno = errno;
-#ifdef BACKUP_FAIL_OK
- if (state.backup_possible)
+
+ if (!backup_fail && state.backup_possible)
state.backup_possible = dir_access != -1;
-#endif /* BACKUP_FAIL_OK */
- if (stat(state.filename, &st) == -1)
+
+ result = stat(state.filename, &st);
+ if (result == -1)
{
if (errno != ENOENT || dir_access == -1)
{
@@ -2969,17 +2992,6 @@ main(int argc, char** argv)
goto general_error;
}
-#ifdef __OpenBSD__
- if (unveil(NULL, NULL) < 0)
- {
- perror("sled: unveil");
- free(filename);
- free(dir);
- free(rfn);
- exit(1);
- }
-#endif
-
if (access(state.filename, F_OK | R_OK | W_OK) == -1)
goto file_error;
@@ -3003,12 +3015,13 @@ main(int argc, char** argv)
file_error:
result = errno;
- perror(program_name);
+ perror(PROGRAM_NAME);
/* FALLTHROUGH */
general_error:
free(state.real_filename);
free(state.backup_filename);
+ free(wd);
free(filename);
free(dir);
free(rfn);
@@ -3046,7 +3059,7 @@ init_termbox:
if (state.error && *state.error)
{
if (errno != 0)
- perror(program_name);
+ perror(PROGRAM_NAME);
free(filename);
return print_error(1, state.error);
}
diff --git a/version.h.in b/version.h.in
@@ -2,5 +2,6 @@
* any later version. Copyright (C) 2022-2024 Страхиња Радић.
* See the file LICENSE for exact copyright and license details. */
-#define VERSION "%VERSION%"
-#define DATE "%DATE%"
+#define PROGRAM_NAME "sled"
+#define DATE "%DATE%"
+#define VERSION "%VERSION%"