return itos_buf(nstr, n);
}
+#define divide(num,divisor,i,d) \
+{ \
+ register int tmp; \
+ i = num/divisor; \
+ tmp = i*divisor; \
+ d = num-tmp; \
+ if (d > 1000) d /= 1000; \
+ else if (d > 100) d /= 100; \
+ else if (d > 10) d /= 10; \
+}
+
gchar *to_human_readable(off_t size)
{
static gchar str[14];
-
- if (size < 1024)
- g_snprintf(str, sizeof(str), _("%dB"), (gint)size);
- else if (size >> 10 < 1024)
- g_snprintf(str, sizeof(str), _("%.1fKB"), (gfloat)size / (1 << 10));
- else if (size >> 20 < 1024)
- g_snprintf(str, sizeof(str), _("%.2fMB"), (gfloat)size / (1 << 20));
- else
- g_snprintf(str, sizeof(str), _("%.2fGB"), (gfloat)size / (1 << 30));
-
- return str;
+ static gchar *b_format = NULL, *kb_format = NULL,
+ *mb_format = NULL, *gb_format = NULL;
+ register int t = 0, r = 0;
+ if (b_format == NULL) {
+ b_format = _("%dB");
+ kb_format = _("%d.%dKB");
+ mb_format = _("%.2fMB");
+ gb_format = _("%.2fGB");
+ }
+
+ if (size < 1024) {
+ g_snprintf(str, sizeof(str), b_format, (gint)size);
+ return str;
+ } else if (size >> 10 < 1024) {
+ divide(size, (1 << 10), t, r);
+ g_snprintf(str, sizeof(str), kb_format, t, r);
+ return str;
+ } else if (size >> 20 < 1024) {
+ g_snprintf(str, sizeof(str), mb_format, (gfloat)size / (1 << 20));
+ return str;
+ } else {
+ g_snprintf(str, sizeof(str), gb_format, (gfloat)size / (1 << 30));
+ return str;
+ }
}
/* strcmp with NULL-checking */
gchar *fname;
gint fd;
FILE *fp;
+ gchar buf[2]="\0";
tmpdir = get_tmp_dir();
tmplen = strlen(tmpdir);
#ifndef G_OS_WIN32
g_unlink(fname);
+
+ /* verify that we can write in the file after unlinking */
+ if (write(fd, buf, 1) < 0) {
+ close(fd);
+ return tmpfile();
+ }
+
#endif
fp = fdopen(fd, "w+b");
if (!fp)
close(fd);
- else
+ else {
+ rewind(fp);
return fp;
+ }
+
#endif /* HAVE_MKSTEMP || G_OS_WIN32 */
return tmpfile();
return result;
}
-static gchar *mailcap_get_command_in_file(const gchar *path, const gchar *type)
+static gchar *mailcap_get_command_in_file(const gchar *path, const gchar *type, const gchar *file_to_open)
{
FILE *fp = fopen(path, "rb");
gchar buf[BUFFSIZE];
if (!fp)
return NULL;
while (fgets(buf, sizeof (buf), fp) != NULL) {
- gchar **parts = g_strsplit(buf, ";", -1);
+ gchar **parts = g_strsplit(buf, ";", 3);
gchar *trimmed = parts[0];
while (trimmed[0] == ' ')
trimmed++;
trimmed[strlen(trimmed)-1] = '\0';
if (!strcmp(trimmed, type)) {
+ gboolean needsterminal = FALSE;
+ if (parts[2] && strstr(parts[2], "needsterminal")) {
+ needsterminal = TRUE;
+ }
+ if (parts[2] && strstr(parts[2], "test=")) {
+ gchar *orig_testcmd = g_strdup(strstr(parts[2], "test=")+5);
+ gchar *testcmd = orig_testcmd;
+ if (strstr(testcmd,";"))
+ *(strstr(testcmd,";")) = '\0';
+ while (testcmd[0] == ' ')
+ testcmd++;
+ while (testcmd[strlen(testcmd)-1] == '\n')
+ testcmd[strlen(testcmd)-1] = '\0';
+ while (testcmd[strlen(testcmd)-1] == '\r')
+ testcmd[strlen(testcmd)-1] = '\0';
+ while (testcmd[strlen(testcmd)-1] == ' ')
+ testcmd[strlen(testcmd)-1] = '\0';
+
+ if (strstr(testcmd, "%s")) {
+ gchar *tmp = g_strdup_printf(testcmd, file_to_open);
+ gint res = system(tmp);
+ g_free(tmp);
+ g_free(orig_testcmd);
+
+ if (res != 0) {
+ g_strfreev(parts);
+ continue;
+ }
+ } else {
+ gint res = system(testcmd);
+ g_free(orig_testcmd);
+
+ if (res != 0) {
+ g_strfreev(parts);
+ continue;
+ }
+ }
+ }
+
trimmed = parts[1];
while (trimmed[0] == ' ')
trimmed++;
- while (trimmed[strlen(trimmed)-1] == ' ')
- trimmed[strlen(trimmed)-1] = '\0';
while (trimmed[strlen(trimmed)-1] == '\n')
trimmed[strlen(trimmed)-1] = '\0';
while (trimmed[strlen(trimmed)-1] == '\r')
trimmed[strlen(trimmed)-1] = '\0';
+ while (trimmed[strlen(trimmed)-1] == ' ')
+ trimmed[strlen(trimmed)-1] = '\0';
result = g_strdup(trimmed);
g_strfreev(parts);
fclose(fp);
- if (strstr(result, "%s") && !strstr(result, "'%s'")) {
+ /* if there are no single quotes around %s, add them.
+ * '.*%s.*' is ok, as in display 'png:%s'
+ */
+ if (strstr(result, "%s")
+ && !(strstr(result, "'") < strstr(result,"%s") &&
+ strstr(strstr(result,"%s"), "'"))) {
gchar *start = g_strdup(result);
gchar *end = g_strdup(strstr(result, "%s")+2);
gchar *tmp;
g_free(result);
result = tmp;
}
+ if (needsterminal) {
+ gchar *tmp = g_strdup_printf("xterm -e %s", result);
+ g_free(result);
+ result = tmp;
+ }
return result;
}
g_strfreev(parts);
fclose(fp);
return NULL;
}
-gchar *mailcap_get_command_for_type(const gchar *type)
+gchar *mailcap_get_command_for_type(const gchar *type, const gchar *file_to_open)
{
gchar *result = NULL;
gchar *path = NULL;
+ if (type == NULL)
+ return NULL;
path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".mailcap", NULL);
- result = mailcap_get_command_in_file(path, type);
+ result = mailcap_get_command_in_file(path, type, file_to_open);
g_free(path);
if (result)
return result;
- result = mailcap_get_command_in_file("/etc/mailcap", type);
+ result = mailcap_get_command_in_file("/etc/mailcap", type, file_to_open);
return result;
}
+void mailcap_update_default(const gchar *type, const gchar *command)
+{
+ gchar *path = NULL, *outpath = NULL;
+ path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".mailcap", NULL);
+ outpath = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".mailcap.new", NULL);
+ FILE *fp = fopen(path, "rb");
+ FILE *outfp = fopen(outpath, "wb");
+ gchar buf[BUFFSIZE];
+
+ if (!fp) {
+ g_free(path);
+ g_free(outpath);
+ return;
+ }
+ if (!outfp) {
+ g_free(path);
+ g_free(outpath);
+ fclose(fp);
+ return;
+ }
+ while (fgets(buf, sizeof (buf), fp) != NULL) {
+ gchar **parts = g_strsplit(buf, ";", 3);
+ gchar *trimmed = parts[0];
+ while (trimmed[0] == ' ')
+ trimmed++;
+ while (trimmed[strlen(trimmed)-1] == ' ')
+ trimmed[strlen(trimmed)-1] = '\0';
+
+ if (!strcmp(trimmed, type)) {
+ g_strfreev(parts);
+ continue;
+ }
+ else {
+ fputs(buf, outfp);
+ }
+ g_strfreev(parts);
+ }
+ fprintf(outfp, "%s; %s\n", type, command);
+ fclose(fp);
+ fclose(outfp);
+ g_rename(outpath, path);
+}
+
gint copy_dir(const gchar *src, const gchar *dst)
{
GDir *dir;