summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJilles Tjoelker <jilles@irssi.org>2007-07-06 21:54:32 +0000
committerjilles <jilles@dbcabf3a-b0e7-0310-adc4-f8d773084564>2007-07-06 21:54:32 +0000
commit3f84c9d19d34f1f5018f0419af0c002ec0bc43c1 (patch)
tree866e06152c38eba21ba4577ef64fa9e2a25a15b5 /src
parent8312144547de98710859e8c44227ee894d724b01 (diff)
downloadirssi-3f84c9d19d34f1f5018f0419af0c002ec0bc43c1.zip
/lastlog -file: use stdio instead of two write(2) calls per line
git-svn-id: http://svn.irssi.org/repos/irssi/trunk@4573 dbcabf3a-b0e7-0310-adc4-f8d773084564
Diffstat (limited to 'src')
-rw-r--r--src/fe-text/lastlog.c46
1 files changed, 28 insertions, 18 deletions
diff --git a/src/fe-text/lastlog.c b/src/fe-text/lastlog.c
index c692f31f..5157a0dd 100644
--- a/src/fe-text/lastlog.c
+++ b/src/fe-text/lastlog.c
@@ -75,7 +75,7 @@ int cmd_options_get_level(const char *cmd, GHashTable *optlist)
}
static void show_lastlog(const char *searchtext, GHashTable *optlist,
- int start, int count, int fhandle)
+ int start, int count, FILE *fhandle)
{
WINDOW_REC *window;
LINE_REC *startline;
@@ -157,7 +157,7 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
return;
}
- if (len > MAX_LINES_WITHOUT_FORCE && fhandle == -1 &&
+ if (len > MAX_LINES_WITHOUT_FORCE && fhandle == NULL &&
g_hash_table_lookup(optlist, "force") == NULL) {
printformat_window(active_win,
MSGLEVEL_CLIENTNOTICE|MSGLEVEL_LASTLOG,
@@ -166,7 +166,7 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
return;
}
- if (fhandle == -1 && g_hash_table_lookup(optlist, "-") == NULL)
+ if (fhandle == NULL && g_hash_table_lookup(optlist, "-") == NULL)
printformat(NULL, NULL, MSGLEVEL_LASTLOG, TXT_LASTLOG_START);
line = g_string_new(NULL);
@@ -176,8 +176,8 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
if (rec == NULL) {
if (tmp->next == NULL)
break;
- if (fhandle != -1) {
- write(fhandle, "--\n", 3);
+ if (fhandle != NULL) {
+ fwrite("--\n", 3, 1, fhandle);
} else {
printformat_window(active_win,
MSGLEVEL_LASTLOG,
@@ -188,7 +188,7 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
}
/* get the line text */
- textbuffer_line2text(rec, fhandle == -1, line);
+ textbuffer_line2text(rec, fhandle == NULL, line);
if (!settings_get_bool("timestamps")) {
struct tm *tm = localtime(&rec->info.time);
char timestamp[10];
@@ -200,9 +200,9 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
}
/* write to file/window */
- if (fhandle != -1) {
- write(fhandle, line->str, line->len);
- write(fhandle, "\n", 1);
+ if (fhandle != NULL) {
+ fwrite(line->str, line->len, 1, fhandle);
+ fputc('\n', fhandle);
} else {
printtext_window(active_win, MSGLEVEL_LASTLOG,
"%s", line->str);
@@ -213,7 +213,7 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
}
g_string_free(line, TRUE);
- if (fhandle == -1 && g_hash_table_lookup(optlist, "-") == NULL)
+ if (fhandle == NULL && g_hash_table_lookup(optlist, "-") == NULL)
printformat(NULL, NULL, MSGLEVEL_LASTLOG, TXT_LASTLOG_END);
textbuffer_view_set_bookmark_bottom(WINDOW_GUI(window)->view,
@@ -232,7 +232,8 @@ static void cmd_lastlog(const char *data)
GHashTable *optlist;
char *text, *countstr, *start, *fname;
void *free_arg;
- int count, fhandle;
+ int count, fd;
+ FILE *fhandle;
g_return_if_fail(data != NULL);
@@ -251,22 +252,31 @@ static void cmd_lastlog(const char *data)
if (count == 0) count = -1;
/* target where to print it */
- fhandle = -1;
+ fhandle = NULL;
fname = g_hash_table_lookup(optlist, "file");
if (fname != NULL) {
fname = convert_home(fname);
- fhandle = open(fname, O_WRONLY | O_APPEND | O_CREAT,
- octal2dec(settings_get_int("log_create_mode")));
+ fd = open(fname, O_WRONLY | O_APPEND | O_CREAT,
+ octal2dec(settings_get_int("log_create_mode")));
+ if (fd != -1) {
+ fhandle = fdopen(fd, "a");
+ if (fhandle == NULL)
+ close(fd);
+ }
g_free(fname);
}
- if (fname != NULL && fhandle == -1) {
+ if (fname != NULL && fhandle == NULL) {
printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
- "%s", g_strerror(errno));
+ "Could not open lastlog: %s", g_strerror(errno));
} else {
show_lastlog(text, optlist, atoi(start), count, fhandle);
- if (fhandle != -1)
- close(fhandle);
+ if (fhandle != NULL) {
+ if (ferror(fhandle))
+ printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
+ "Could not write lastlog: %s", g_strerror(errno));
+ fclose(fhandle);
+ }
}
cmd_params_free(free_arg);