#include "intl.h"
#include "utils.h"
+#include "socket.h"
#include "statusbar.h"
#include "logwindow.h"
return TRUE;
}
-gint get_quote_level(const gchar *str)
+gint get_quote_level(const gchar *str, const gchar *quote_chars)
{
const gchar *first_pos;
const gchar *last_pos;
gint quote_level = -1;
/* speed up line processing by only searching to the last '>' */
- if ((first_pos = strchr(str, '>')) != NULL) {
+ if ((first_pos = line_has_quote_char(str, quote_chars)) != NULL) {
/* skip a line if it contains a '<' before the initial '>' */
if (memchr(str, '<', first_pos - str) != NULL)
return -1;
- last_pos = strrchr(first_pos, '>');
+ last_pos = line_has_quote_char_last(first_pos, quote_chars);
} else
return -1;
break;
}
- if (*p == '>')
+ if (strchr(quote_chars, *p))
quote_level++;
else if (*p != '-' && !isspace(*p) && p <= last_pos) {
/* any characters are allowed except '-' and space */
- while (*p != '-' && *p != '>' && !isspace(*p) &&
- p < last_pos)
+ while (*p != '-'
+ && !strchr(quote_chars, *p)
+ && !isspace(*p)
+ && p < last_pos)
p++;
- if (*p == '>')
+ if (strchr(quote_chars, *p))
quote_level++;
else
break;
return quote_level;
}
+const gchar * line_has_quote_char(const gchar * str, const gchar *quote_chars)
+{
+ gchar * position = NULL;
+ gchar * tmp_pos = NULL;
+ int i;
+
+ if (quote_chars == NULL)
+ return FALSE;
+
+ for (i = 0; i < strlen(quote_chars); i++) {
+ tmp_pos = strchr (str, quote_chars[i]);
+ if(position == NULL
+ || (tmp_pos != NULL && position >= tmp_pos) )
+ position = tmp_pos;
+ }
+ return position;
+}
+
+const gchar * line_has_quote_char_last(const gchar * str, const gchar *quote_chars)
+{
+ gchar * position = NULL;
+ gchar * tmp_pos = NULL;
+ int i;
+
+ if (quote_chars == NULL)
+ return FALSE;
+
+ for (i = 0; i < strlen(quote_chars); i++) {
+ tmp_pos = strrchr (str, quote_chars[i]);
+ if(position == NULL
+ || (tmp_pos != NULL && position <= tmp_pos) )
+ position = tmp_pos;
+ }
+ return position;
+}
+
gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
{
register guint haystack_len, needle_len;
return NULL;
}
-/* this fuction was taken from gstrfuncs.c in glib. */
+gchar *strchr_parenthesis_close(const gchar *str, gchar op, gchar cl)
+{
+ const gchar *p;
+ gchar quote_chr = '"';
+ gint in_brace;
+ gboolean in_quote = FALSE;
+
+ p = str;
+
+ if ((p = strchr_with_skip_quote(p, quote_chr, op))) {
+ p++;
+ in_brace = 1;
+ while (*p) {
+ if (*p == op && !in_quote)
+ in_brace++;
+ else if (*p == cl && !in_quote)
+ in_brace--;
+ else if (*p == quote_chr)
+ in_quote ^= TRUE;
+
+ if (in_brace == 0)
+ return (gchar *)p;
+
+ p++;
+ }
+ }
+
+ return NULL;
+}
+
+gchar **strsplit_parenthesis(const gchar *str, gchar op, gchar cl,
+ gint max_tokens)
+{
+ GSList *string_list = NULL, *slist;
+ gchar **str_array;
+ const gchar *s_op, *s_cl;
+ guint i, n = 1;
+
+ g_return_val_if_fail(str != NULL, NULL);
+
+ if (max_tokens < 1)
+ max_tokens = G_MAXINT;
+
+ s_op = strchr_with_skip_quote(str, '"', op);
+ if (!s_op) return NULL;
+ str = s_op;
+ s_cl = strchr_parenthesis_close(str, op, cl);
+ if (s_cl) {
+ do {
+ guint len;
+ gchar *new_string;
+
+ str++;
+ len = s_cl - str;
+ new_string = g_new(gchar, len + 1);
+ strncpy(new_string, str, len);
+ new_string[len] = 0;
+ string_list = g_slist_prepend(string_list, new_string);
+ n++;
+ str = s_cl + 1;
+
+ while (*str && isspace(*str)) str++;
+ if (*str != op) {
+ string_list = g_slist_prepend(string_list,
+ g_strdup(""));
+ n++;
+ s_op = strchr_with_skip_quote(str, '"', op);
+ if (!--max_tokens || !s_op) break;
+ str = s_op;
+ } else
+ s_op = str;
+ s_cl = strchr_parenthesis_close(str, op, cl);
+ } while (--max_tokens && s_cl);
+ }
+
+ str_array = g_new(gchar*, n);
+
+ i = n - 1;
+
+ str_array[i--] = NULL;
+ for (slist = string_list; slist; slist = slist->next)
+ str_array[i--] = slist->data;
+
+ g_slist_free(string_list);
+
+ return str_array;
+}
+
gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
gint max_tokens)
{
return str_array;
}
+gchar *get_abbrev_newsgroup_name(const gchar *group)
+{
+ gchar *abbrev_group;
+ gchar *ap;
+ const gchar *p = group;
+
+ abbrev_group = ap = g_malloc(strlen(group) + 1);
+
+ while (*p) {
+ while (*p == '.')
+ *ap++ = *p++;
+ if (strchr(p, '.')) {
+ *ap++ = *p++;
+ while (*p != '.') p++;
+ } else {
+ strcpy(ap, p);
+ return abbrev_group;
+ }
+ }
+
+ *ap = '\0';
+ return abbrev_group;
+}
+
+gchar *trim_string(const gchar *str, gint len)
+{
+ const gchar *p = str;
+ gint mb_len;
+ gchar *new_str;
+ gint new_len = 0;
+
+ if (!str) return NULL;
+ if (strlen(str) <= len)
+ return g_strdup(str);
+
+ while (*p != '\0') {
+ mb_len = mblen(p, MB_LEN_MAX);
+ if (mb_len == 0)
+ break;
+ else if (mb_len < 0)
+ return g_strdup(str);
+ else if (new_len + mb_len > len)
+ break;
+ else
+ new_len += mb_len;
+ p += mb_len;
+ }
+
+ Xstrndup_a(new_str, str, new_len, return g_strdup(str));
+ return g_strconcat(new_str, "...", NULL);
+}
+
GList *uri_list_extract_filenames(const gchar *uri_list)
{
GList *result = NULL;
static gchar *domain_name = NULL;
if (!domain_name) {
- gchar buf[BUFFSIZE] = "";
+ gchar buf[128] = "";
+ struct hostent *hp;
if (gethostname(buf, sizeof(buf)) < 0) {
perror("gethostname");
- strcpy(buf, "unknown");
+ domain_name = "unknown";
+ } else {
+ buf[sizeof(buf) - 1] = '\0';
+ if ((hp = my_gethostbyname(buf)) == NULL) {
+ perror("gethostbyname");
+ domain_name = g_strdup(buf);
+ } else {
+ domain_name = g_strdup(hp->h_name);
+ }
}
- domain_name = g_strdup(buf);
+ debug_print("domain name = %s\n", domain_name);
}
return domain_name;
return FALSE;
}
+gboolean is_file_entry_exist(const gchar *file)
+{
+ struct stat s;
+
+ if (stat(file, &s) < 0) {
+ if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
gint change_dir(const gchar *dir)
{
gchar *prevdir = NULL;
return 0;
}
+gint make_dir(const gchar *dir)
+{
+ if (mkdir(dir, S_IRWXU) < 0) {
+ FILE_OP_ERROR(dir, "mkdir");
+ return -1;
+ }
+ if (chmod(dir, S_IRWXU) < 0)
+ FILE_OP_ERROR(dir, "chmod");
+
+ return 0;
+}
+
gint make_dir_hier(const gchar *dir)
{
gchar *parent_dir;
parent_dir = g_strndup(dir, p - dir);
if (*parent_dir != '\0') {
if (!is_dir_exist(parent_dir)) {
- if (mkdir(parent_dir, S_IRWXU) < 0) {
- FILE_OP_ERROR(parent_dir, "mkdir");
+ if (make_dir(parent_dir) < 0) {
g_free(parent_dir);
return -1;
}
- if (chmod(parent_dir, S_IRWXU) < 0)
- FILE_OP_ERROR(parent_dir, "chmod");
}
}
g_free(parent_dir);
}
+
if (!is_dir_exist(dir)) {
- if (mkdir(dir, S_IRWXU) < 0) {
- FILE_OP_ERROR(dir, "mkdir");
+ if (make_dir(dir) < 0)
return -1;
- }
- if (chmod(dir, S_IRWXU) < 0)
- FILE_OP_ERROR(dir, "chmod");
}
return 0;
while ((d = readdir(dp)) != NULL) {
fileno = to_number(d->d_name);
if (fileno >= 0 && first <= fileno && fileno <= last) {
+ if (is_dir_exist(d->d_name))
+ continue;
if (unlink(d->d_name) < 0)
FILE_OP_ERROR(d->d_name, "unlink");
}
return remove_numbered_files(dir, 0, UINT_MAX);
}
+gint remove_expired_files(const gchar *dir, guint hours)
+{
+ DIR *dp;
+ struct dirent *d;
+ struct stat s;
+ gchar *prev_dir;
+ gint fileno;
+ time_t mtime, now, expire_time;
+
+ prev_dir = g_get_current_dir();
+
+ if (chdir(dir) < 0) {
+ FILE_OP_ERROR(dir, "chdir");
+ return -1;
+ }
+
+ if ((dp = opendir(".")) == NULL) {
+ FILE_OP_ERROR(dir, "opendir");
+ return -1;
+ }
+
+ now = time(NULL);
+ expire_time = hours * 60 * 60;
+
+ while ((d = readdir(dp)) != NULL) {
+ fileno = to_number(d->d_name);
+ if (fileno >= 0) {
+ if (stat(d->d_name, &s) < 0) {
+ FILE_OP_ERROR(d->d_name, "stat");
+ continue;
+ }
+ if (S_ISDIR(s.st_mode))
+ continue;
+ mtime = MAX(s.st_mtime, s.st_atime);
+ if (now - mtime > expire_time) {
+ if (unlink(d->d_name) < 0)
+ FILE_OP_ERROR(d->d_name, "unlink");
+ }
+ }
+ }
+
+ closedir(dp);
+
+ if (chdir(prev_dir) < 0) {
+ FILE_OP_ERROR(prev_dir, "chdir");
+ g_free(prev_dir);
+ return -1;
+ }
+
+ g_free(prev_dir);
+
+ return 0;
+}
+
gint remove_dir_recursive(const gchar *dir)
{
struct stat s;
return 0;
}
+/* convert line endings into CRLF. If the last line doesn't end with
+ * linebreak, add it.
+ */
+gint canonicalize_file(const gchar *src, const gchar *dest)
+{
+ FILE *src_fp, *dest_fp;
+ gchar buf[BUFFSIZE];
+ gint len;
+ gboolean err = FALSE;
+ gboolean last_linebreak = FALSE;
+
+ if ((src_fp = fopen(src, "rb")) == NULL) {
+ FILE_OP_ERROR(src, "fopen");
+ return -1;
+ }
+
+ if ((dest_fp = fopen(dest, "wb")) == NULL) {
+ FILE_OP_ERROR(dest, "fopen");
+ fclose(src_fp);
+ return -1;
+ }
+
+ if (change_file_mode_rw(dest_fp, dest) < 0) {
+ FILE_OP_ERROR(dest, "chmod");
+ g_warning("can't change file mode\n");
+ }
+
+ while (fgets(buf, sizeof(buf), src_fp) != NULL) {
+ gint r = 0;
+
+ len = strlen(buf);
+ if (len == 0) break;
+ last_linebreak = FALSE;
+
+ if (buf[len - 1] != '\n') {
+ last_linebreak = TRUE;
+ r = fputs(buf, dest_fp);
+ } else if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
+ r = fputs(buf, dest_fp);
+ } else {
+ if (len > 1) {
+ r = fwrite(buf, len - 1, 1, dest_fp);
+ if (r != 1)
+ r = EOF;
+ }
+ if (r != EOF)
+ r = fputs("\r\n", dest_fp);
+ }
+
+ if (r == EOF) {
+ g_warning("writing to %s failed.\n", dest);
+ fclose(dest_fp);
+ fclose(src_fp);
+ unlink(dest);
+ return -1;
+ }
+ }
+
+ if (last_linebreak == TRUE) {
+ if (fputs("\r\n", dest_fp) == EOF)
+ err = TRUE;
+ }
+
+ if (ferror(src_fp)) {
+ FILE_OP_ERROR(src, "fread");
+ err = TRUE;
+ }
+ fclose(src_fp);
+ if (fclose(dest_fp) == EOF) {
+ FILE_OP_ERROR(dest, "fclose");
+ err = TRUE;
+ }
+
+ if (err) {
+ unlink(dest);
+ return -1;
+ }
+
+ return 0;
+}
+
+gint canonicalize_file_replace(const gchar *file)
+{
+ gchar *tmp_file;
+
+ tmp_file = get_tmp_file();
+
+ if (canonicalize_file(file, tmp_file) < 0)
+ return -1;
+
+ unlink(file);
+ if (rename(tmp_file, file) < 0) {
+ FILE_OP_ERROR(file, "rename");
+ unlink(tmp_file);
+ return -1;
+ }
+
+ return 0;
+}
+
gint change_file_mode_rw(FILE *fp, const gchar *file)
{
#if HAVE_FCHMOD
fputs(buf, stdout);
}
+#define TIME_LEN 11
+
void log_print(const gchar *format, ...)
{
va_list args;
- gchar buf[BUFFSIZE];
- gchar *logbuf;
- gchar timestr[6];
+ gchar buf[BUFFSIZE + TIME_LEN];
time_t t;
+ time(&t);
+ strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
+
va_start(args, format);
- g_vsnprintf(buf, sizeof(buf), format, args);
+ g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args);
va_end(args);
-
- time(&t);
- strftime(timestr, 6, "%H:%M", localtime(&t));
- logbuf = g_strdup_printf("[%s] %s", timestr, buf);
- if (debug_mode) fputs(logbuf, stdout);
- log_window_append(logbuf, LOG_NORMAL);
+ if (debug_mode) fputs(buf, stdout);
+ log_window_append(buf, LOG_NORMAL);
if (log_fp) {
- fputs(logbuf, log_fp);
+ fputs(buf, log_fp);
fflush(log_fp);
}
if (log_verbosity_count)
- statusbar_puts_all(buf);
- g_free(logbuf);
+ statusbar_puts_all(buf + TIME_LEN);
}
void log_message(const gchar *format, ...)
{
va_list args;
- gchar buf[BUFFSIZE];
+ gchar buf[BUFFSIZE + TIME_LEN];
+ time_t t;
+
+ time(&t);
+ strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
va_start(args, format);
- g_vsnprintf(buf, sizeof(buf), format, args);
+ g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args);
va_end(args);
- if (debug_mode) g_message("%s", buf);
- log_window_append(buf, LOG_MSG);
+ if (debug_mode) g_message("%s", buf + TIME_LEN);
+ log_window_append(buf + TIME_LEN, LOG_MSG);
if (log_fp) {
- fputs("message: ", log_fp);
- fputs(buf, log_fp);
+ fwrite(buf, TIME_LEN, 1, log_fp);
+ fputs("* message: ", log_fp);
+ fputs(buf + TIME_LEN, log_fp);
fflush(log_fp);
}
- statusbar_puts_all(buf);
+ statusbar_puts_all(buf + TIME_LEN);
}
void log_warning(const gchar *format, ...)
{
va_list args;
- gchar buf[BUFFSIZE];
+ gchar buf[BUFFSIZE + TIME_LEN];
+ time_t t;
+
+ time(&t);
+ strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
va_start(args, format);
- g_vsnprintf(buf, sizeof(buf), format, args);
+ g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args);
va_end(args);
g_warning("%s", buf);
- log_window_append(buf, LOG_WARN);
+ log_window_append(buf + TIME_LEN, LOG_WARN);
if (log_fp) {
- fputs("*** warning: ", log_fp);
- fputs(buf, log_fp);
+ fwrite(buf, TIME_LEN, 1, log_fp);
+ fputs("** warning: ", log_fp);
+ fputs(buf + TIME_LEN, log_fp);
fflush(log_fp);
}
}
void log_error(const gchar *format, ...)
{
va_list args;
- gchar buf[BUFFSIZE];
+ gchar buf[BUFFSIZE + TIME_LEN];
+ time_t t;
+
+ time(&t);
+ strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
va_start(args, format);
- g_vsnprintf(buf, sizeof(buf), format, args);
+ g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args);
va_end(args);
g_warning("%s", buf);
- log_window_append(buf, LOG_ERROR);
+ log_window_append(buf + TIME_LEN, LOG_ERROR);
if (log_fp) {
+ fwrite(buf, TIME_LEN, 1, log_fp);
fputs("*** error: ", log_fp);
- fputs(buf, log_fp);
+ fputs(buf + TIME_LEN, log_fp);
fflush(log_fp);
}
}