1
0
mirror of https://github.com/irssi/irssi.git synced 2024-12-04 14:46:39 -05:00

/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
This commit is contained in:
Jilles Tjoelker 2007-07-06 21:54:32 +00:00 committed by jilles
parent 8312144547
commit 3f84c9d19d

View File

@ -75,7 +75,7 @@ int cmd_options_get_level(const char *cmd, GHashTable *optlist)
} }
static void show_lastlog(const char *searchtext, 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; WINDOW_REC *window;
LINE_REC *startline; LINE_REC *startline;
@ -157,7 +157,7 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
return; return;
} }
if (len > MAX_LINES_WITHOUT_FORCE && fhandle == -1 && if (len > MAX_LINES_WITHOUT_FORCE && fhandle == NULL &&
g_hash_table_lookup(optlist, "force") == NULL) { g_hash_table_lookup(optlist, "force") == NULL) {
printformat_window(active_win, printformat_window(active_win,
MSGLEVEL_CLIENTNOTICE|MSGLEVEL_LASTLOG, MSGLEVEL_CLIENTNOTICE|MSGLEVEL_LASTLOG,
@ -166,7 +166,7 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
return; 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); printformat(NULL, NULL, MSGLEVEL_LASTLOG, TXT_LASTLOG_START);
line = g_string_new(NULL); line = g_string_new(NULL);
@ -176,8 +176,8 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
if (rec == NULL) { if (rec == NULL) {
if (tmp->next == NULL) if (tmp->next == NULL)
break; break;
if (fhandle != -1) { if (fhandle != NULL) {
write(fhandle, "--\n", 3); fwrite("--\n", 3, 1, fhandle);
} else { } else {
printformat_window(active_win, printformat_window(active_win,
MSGLEVEL_LASTLOG, MSGLEVEL_LASTLOG,
@ -188,7 +188,7 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
} }
/* get the line text */ /* get the line text */
textbuffer_line2text(rec, fhandle == -1, line); textbuffer_line2text(rec, fhandle == NULL, line);
if (!settings_get_bool("timestamps")) { if (!settings_get_bool("timestamps")) {
struct tm *tm = localtime(&rec->info.time); struct tm *tm = localtime(&rec->info.time);
char timestamp[10]; char timestamp[10];
@ -200,9 +200,9 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
} }
/* write to file/window */ /* write to file/window */
if (fhandle != -1) { if (fhandle != NULL) {
write(fhandle, line->str, line->len); fwrite(line->str, line->len, 1, fhandle);
write(fhandle, "\n", 1); fputc('\n', fhandle);
} else { } else {
printtext_window(active_win, MSGLEVEL_LASTLOG, printtext_window(active_win, MSGLEVEL_LASTLOG,
"%s", line->str); "%s", line->str);
@ -213,7 +213,7 @@ static void show_lastlog(const char *searchtext, GHashTable *optlist,
} }
g_string_free(line, TRUE); 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); printformat(NULL, NULL, MSGLEVEL_LASTLOG, TXT_LASTLOG_END);
textbuffer_view_set_bookmark_bottom(WINDOW_GUI(window)->view, textbuffer_view_set_bookmark_bottom(WINDOW_GUI(window)->view,
@ -232,7 +232,8 @@ static void cmd_lastlog(const char *data)
GHashTable *optlist; GHashTable *optlist;
char *text, *countstr, *start, *fname; char *text, *countstr, *start, *fname;
void *free_arg; void *free_arg;
int count, fhandle; int count, fd;
FILE *fhandle;
g_return_if_fail(data != NULL); g_return_if_fail(data != NULL);
@ -251,22 +252,31 @@ static void cmd_lastlog(const char *data)
if (count == 0) count = -1; if (count == 0) count = -1;
/* target where to print it */ /* target where to print it */
fhandle = -1; fhandle = NULL;
fname = g_hash_table_lookup(optlist, "file"); fname = g_hash_table_lookup(optlist, "file");
if (fname != NULL) { if (fname != NULL) {
fname = convert_home(fname); fname = convert_home(fname);
fhandle = open(fname, O_WRONLY | O_APPEND | O_CREAT, fd = open(fname, O_WRONLY | O_APPEND | O_CREAT,
octal2dec(settings_get_int("log_create_mode"))); octal2dec(settings_get_int("log_create_mode")));
if (fd != -1) {
fhandle = fdopen(fd, "a");
if (fhandle == NULL)
close(fd);
}
g_free(fname); g_free(fname);
} }
if (fname != NULL && fhandle == -1) { if (fname != NULL && fhandle == NULL) {
printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
"%s", g_strerror(errno)); "Could not open lastlog: %s", g_strerror(errno));
} else { } else {
show_lastlog(text, optlist, atoi(start), count, fhandle); show_lastlog(text, optlist, atoi(start), count, fhandle);
if (fhandle != -1) if (fhandle != NULL) {
close(fhandle); if (ferror(fhandle))
printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
"Could not write lastlog: %s", g_strerror(errno));
fclose(fhandle);
}
} }
cmd_params_free(free_arg); cmd_params_free(free_arg);