чување efb1258027fccf440261d04a15e6312c20d7a6c5
родитељ 265ff0d0c5684139e4cabd9da8732e393936c2eb
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Sun, 13 Feb 2022 17:59:16 +0100
Correct error reporting when directory passed as argument
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | TODO | | | 4 | ++-- |
| M | config.h | | | 2 | +- |
| M | poe.c | | | 71 | ++++++++++++++++++++++++++++++++++++++--------------------------------- |
измењених датотека: 3, додавања: 41(+), брисања: 36(-)
diff --git a/TODO b/TODO
@@ -1,9 +1,9 @@
TODO
====
- [ ] Tidy up basic logic in poe.c
+ [~] Tidy up basic logic in poe.c
- [ ] Investigate supplying a directory as an argument
+ [x] Investigate supplying a directory as an argument
< > Wrap long lines? Not if it would complicate the code too much.
diff --git a/config.h b/config.h
@@ -1,6 +1,6 @@
/* See the file LICENSE for copyright and license details. */
-#define SAVE_WRAP_WIDTH 70
+#define SAVE_WRAP_WIDTH 0
#define WRAP_FIRST_MSGSTR 0
#define WARN_COUNT_DOTS 0
diff --git a/poe.c b/poe.c
@@ -3,12 +3,15 @@
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
+#include <fcntl.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/stat.h>
#include <time.h>
+#include <unistd.h>
#include "version.h"
#include "termbox.h"
@@ -201,7 +204,7 @@ quit_callback(struct DrawState* state, struct tb_event* ev)
strcpy(state->error, errors[ERR_CANT_SAVE]);
break;
default:
- state->error[0] = 0;
+ *state->error = 0;
}
}
state->dirty = 0;
@@ -573,7 +576,7 @@ show_search(struct DrawState* state)
state->search_first_shown_column = 0;
if (!state->search)
state->search = calloc(MAXSEARCH, sizeof(uint32_t));
- state->search[0] = 0;
+ *state->search = 0;
tb_set_cursor((state->maxx - state->search_width + 2) / 2,
state->maxy - 1 - SEARCH_HEIGHT + 1);
}
@@ -589,7 +592,7 @@ void
cancel_search(struct DrawState* state)
{
state->show_search = 0;
- state->search[0] = 0;
+ *state->search = 0;
tb_set_cursor(TB_HIDE_CURSOR, TB_HIDE_CURSOR);
}
@@ -1117,7 +1120,7 @@ next_match(struct DrawState* state)
if (state->msgid_count == 0 || !state->search || !*state->search)
return;
- state->error[0] = 0;
+ *state->error = 0;
struct PoEntry* current = NULL;
for (size_t e = state->msgid_number; e < state->msgid_count; e++)
{
@@ -1140,7 +1143,7 @@ prev_match(struct DrawState* state)
if (state->msgid_count == 0 || !state->search || !*state->search)
return;
- state->error[0] = 0;
+ *state->error = 0;
struct PoEntry* current = NULL;
for (size_t e = state->msgid_number - 1; e > 0; e--)
{
@@ -1163,7 +1166,7 @@ next_untranslated(struct DrawState* state)
if (state->msgid_count == 0)
return;
- state->error[0] = 0;
+ *state->error = 0;
struct PoEntry* current = NULL;
for (size_t e = state->msgid_number; e < state->msgid_count; e++)
@@ -1200,7 +1203,7 @@ next_fuzzy(struct DrawState* state)
if (state->msgid_count == 0)
return;
- state->error[0] = 0;
+ *state->error = 0;
struct PoEntry* current = NULL;
for (size_t e = state->msgid_number; e < state->msgid_count; e++)
@@ -1564,7 +1567,7 @@ move_list_up(struct DrawState* state)
{
if (state->msgid_number > 1)
{
- state->error[0] = 0;
+ *state->error = 0;
state->msgid_number--;
if (state->msgid_number < state->first_shown_msgid)
state->first_shown_msgid--;
@@ -1576,7 +1579,7 @@ move_list_down(struct DrawState* state)
{
if (state->msgid_number < state->msgid_count)
{
- state->error[0] = 0;
+ *state->error = 0;
state->msgid_number++;
if (state->msgid_number - state->first_shown_msgid
> state->maxy - 2)
@@ -1587,7 +1590,7 @@ move_list_down(struct DrawState* state)
void
move_list_start(struct DrawState* state)
{
- state->error[0] = 0;
+ *state->error = 0;
state->msgid_number = state->msgid_count > 0 ? 1 : 0;
state->first_shown_msgid = state->msgid_number;
}
@@ -1595,7 +1598,7 @@ move_list_start(struct DrawState* state)
void
move_list_end(struct DrawState* state)
{
- state->error[0] = 0;
+ *state->error = 0;
state->msgid_number = state->msgid_count;
state->first_shown_msgid = state->msgid_count > 0
? MAX(1, state->msgid_number - (state->maxy - 2))
@@ -1605,7 +1608,7 @@ move_list_end(struct DrawState* state)
void
move_list_page_up(struct DrawState* state)
{
- state->error[0] = 0;
+ *state->error = 0;
if (state->msgid_number > state->maxy - 2)
{
state->msgid_number -= state->maxy - 2;
@@ -1619,7 +1622,7 @@ move_list_page_up(struct DrawState* state)
void
move_list_page_down(struct DrawState* state)
{
- state->error[0] = 0;
+ *state->error = 0;
if (state->msgid_number < state->msgid_count - state->maxy - 2)
{
state->msgid_number += state->maxy - 2;
@@ -2026,12 +2029,12 @@ handle_key_event(struct tb_event* ev, struct DrawState* state)
handle_search_key_event(ev, state);
else if (ev->key == TB_KEY_ESC)
{
- if (state->error[0])
- state->error[0] = 0;
+ if (*state->error)
+ *state->error = 0;
else if (state->in_prompt)
{
state->in_prompt = 0;
- state->prompt[0] = 0;
+ *state->prompt = 0;
return;
}
else if (state->show_help)
@@ -2058,7 +2061,7 @@ handle_key_event(struct tb_event* ev, struct DrawState* state)
}
else
state->running = 0;
- state->error[0] = 0;
+ *state->error = 0;
}
else if (state->show_help)
strcpy(state->error, errors[ERR_DLG_OPEN]);
@@ -2092,7 +2095,7 @@ handle_key_event(struct tb_event* ev, struct DrawState* state)
break;
case SAVE_ERR_NONE:
default:
- state->error[0] = 0;
+ *state->error = 0;
}
state->dirty = 0;
}
@@ -2113,7 +2116,7 @@ handle_key_event(struct tb_event* ev, struct DrawState* state)
else if (ev->key == TB_KEY_F1 || ev->ch == 'H')
{
state->show_help = 1;
- state->error[0] = 0;
+ *state->error = 0;
}
else if (ev->key == TB_KEY_F3 || ev->ch == 'n')
next_match(state);
@@ -2183,12 +2186,12 @@ main(int argc, char** argv)
char error[MAXBUFLINE];
char prompt[MAXBUFLINE];
struct DrawState state;
- FILE* input = NULL;
char filename[MAXPATH];
+ struct stat st;
- error[0] = 0;
- prompt[0] = 0;
- filename[0] = 0;
+ *error = 0;
+ *prompt = 0;
+ *filename = 0;
if (argc == 1)
return print_error(1, "No filename given");
@@ -2201,16 +2204,18 @@ main(int argc, char** argv)
{
strncpy(filename, argv[1], MAXPATH - 1);
filename[MAXPATH - 1] = 0;
- if (!filename[0])
- print_error(EINVAL, "Invalid argument");
+ if (!*filename)
+ print_error(EINVAL, "No filename given");
}
- if (!filename[0])
- return print_error(1, "No filename given");
+ if (stat(filename, &st) == -1 || access(filename, R_OK | W_OK) == -1)
+ {
+ perror(program_name);
+ return errno;
+ }
- if (!(input = fopen(filename, "rt")))
- return print_error(ENOENT, "No such file `%s'", filename);
- fclose(input);
+ if (!S_ISREG(st.st_mode))
+ return print_error(EINVAL, "Is not a regular file");
init_drawstate(&state, error, filename, prompt);
@@ -2232,7 +2237,7 @@ main(int argc, char** argv)
return print_error(result, "%s:%ld:%ld: Syntax error", filename,
lineno, col);
case LOAD_ERR_NONE:
- if (error[0] != 0)
+ if (*error)
return print_error(1, error);
break;
default:
@@ -2242,7 +2247,7 @@ main(int argc, char** argv)
state.first_shown_msgid = state.msgid_number;
tb_init();
- error[0] = 0;
+ *error = 0;
state.maxx = 0;
while (state.running)
@@ -2272,7 +2277,7 @@ main(int argc, char** argv)
}
}
tb_shutdown();
- if (state.error && state.error[0])
+ if (state.error && *state.error)
return print_error(1, state.error);
free_drawstate(&state);