#include <string.h>
#include <ctype.h>
#include <errno.h>
+#include <netdb.h>
#if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
# include <wchar.h>
gint max_tokens)
{
GSList *string_list = NULL, *slist;
- gchar **str_array, *s;
- guint i, n = 1;
+ gchar **str_array, *s, *new_str;
+ guint i, n = 1, len;
g_return_val_if_fail(str != NULL, NULL);
g_return_val_if_fail(delim != NULL, NULL);
guint delimiter_len = strlen(delim);
do {
- guint len;
- gchar *new_str;
-
len = s - str;
- new_str = g_new(gchar, len + 1);
- strncpy(new_str, str, len);
- new_str[len] = 0;
+ new_str = g_strndup(str, len);
+
+ if (new_str[0] == '\'' || new_str[0] == '\"') {
+ if (new_str[len - 1] == new_str[0]) {
+ new_str[len - 1] = '\0';
+ memmove(new_str, new_str + 1, len - 1);
+ }
+ }
string_list = g_slist_prepend(string_list, new_str);
n++;
str = s + delimiter_len;
}
if (*str) {
+ new_str = g_strdup(str);
+ if (new_str[0] == '\'' || new_str[0] == '\"') {
+ len = strlen(str);
+ if (new_str[len - 1] == new_str[0]) {
+ new_str[len - 1] = '\0';
+ memmove(new_str, new_str + 1, len - 1);
+ }
+ }
+ string_list = g_slist_prepend(string_list, new_str);
n++;
- string_list = g_slist_prepend(string_list, g_strdup(str));
}
str_array = g_new(gchar*, n);
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++;
+ *ap++ = *p++;
+ } else {
+ strcpy(ap, p);
+ return abbrev_group;
+ }
+ }
+
+ *ap = '\0';
+ return abbrev_group;
+}
+
GList *uri_list_extract_filenames(const gchar *uri_list)
{
GList *result = NULL;
return template_dir;
}
+gchar *get_header_cache_dir(void)
+{
+ static gchar *header_dir = NULL;
+
+ if (!header_dir)
+ header_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
+ HEADER_CACHE_DIR, NULL);
+
+ return header_dir;
+}
+
gchar *get_tmp_file(void)
{
static gchar *tmp_file = NULL;
gchar *get_domain_name(void)
{
static gchar *domain_name = NULL;
+ struct hostent *myfqdn = NULL;
if (!domain_name) {
gchar buf[BUFFSIZE] = "";
if (gethostname(buf, sizeof(buf)) < 0) {
perror("gethostname");
strcpy(buf, "unknown");
- }
+ } else {
+ myfqdn = gethostbyname(buf);
+ if (myfqdn != NULL) {
+ memset(buf, '\0', strlen(buf));
+ strcpy(buf, myfqdn->h_name);
+ } else {
+ perror("gethostbyname");
+ strcpy(buf, "unknown");
+ }
+ }
domain_name = g_strdup(buf);
}
return s.st_size;
}
+off_t get_file_size_as_crlf(const gchar *file)
+{
+ FILE *fp;
+ off_t size = 0;
+ gchar buf[BUFFSIZE];
+
+ if ((fp = fopen(file, "rb")) == NULL) {
+ FILE_OP_ERROR(file, "fopen");
+ return -1;
+ }
+
+ while (fgets(buf, sizeof(buf), fp) != NULL) {
+ strretchomp(buf);
+ size += strlen(buf) + 2;
+ }
+
+ if (ferror(fp)) {
+ FILE_OP_ERROR(file, "fgets");
+ size = -1;
+ }
+
+ fclose(fp);
+
+ return size;
+}
+
off_t get_left_file_size(FILE *fp)
{
glong pos;
gchar *dest_bak = NULL;
gboolean err = FALSE;
- if ((src_fp = fopen(src, "r")) == NULL) {
+ if ((src_fp = fopen(src, "rb")) == NULL) {
FILE_OP_ERROR(src, "fopen");
return -1;
}
}
}
- if ((dest_fp = fopen(dest, "w")) == NULL) {
+ if ((dest_fp = fopen(dest, "wb")) == NULL) {
FILE_OP_ERROR(dest, "fopen");
fclose(src_fp);
if (dest_bak) {
return tmpfile();
}
+FILE *str_open_as_stream(const gchar *str)
+{
+ FILE *fp;
+ size_t len;
+
+ g_return_val_if_fail(str != NULL, NULL);
+
+ fp = my_tmpfile();
+ if (!fp) {
+ FILE_OP_ERROR("str_open_as_stream", "my_tmpfile");
+ return NULL;
+ }
+
+ len = strlen(str);
+ if (len == 0) return fp;
+
+ if (fwrite(str, len, 1, fp) != 1) {
+ FILE_OP_ERROR("str_open_as_stream", "fwrite");
+ fclose(fp);
+ return NULL;
+ }
+
+ rewind(fp);
+ return fp;
+}
+
gint execute_async(gchar *const argv[])
{
pid_t pid;
gint execute_command_line(const gchar *cmdline, gboolean async)
{
gchar **argv;
- gint i;
gint ret;
argv = strsplit_with_quote(cmdline, " ", 0);
- for (i = 0; argv[i] != NULL; i++) {
- gchar *str = argv[i];
-
- if (str[0] == '\'' || str[0] == '\"') {
- gint len;
-
- len = strlen(str);
- if (str[len - 1] == str[0]) {
- str[len - 1] = '\0';
- memmove(str, str + 1, len - 1);
- }
- }
- }
-
if (async)
ret = execute_async(argv);
else
void set_log_file(const gchar *filename)
{
if (log_fp) return;
- log_fp = fopen(filename, "w");
+ log_fp = fopen(filename, "wb");
if (!log_fp)
FILE_OP_ERROR(filename, "fopen");
}