4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* support.c - (non-GUI) useful routines */
31 #include <sys/param.h>
39 #include <libxml/parser.h>
52 static GHashTable
*uid_hash
= NULL
; /* UID -> User name */
53 static GHashTable
*gid_hash
= NULL
; /* GID -> Group name */
55 /* Static prototypes */
56 static void MD5Transform(guint32 buf
[4], guint32
const in
[16]);
58 /****************************************************************
59 * EXTERNAL INTERFACE *
60 ****************************************************************/
62 /* g_object_unref() the result! */
63 XMLwrapper
*xml_cache_load(const gchar
*pathname
)
65 static GFSCache
*xml_cache
= NULL
;
68 xml_cache
= g_fscache_new((GFSLoadFunc
) xml_new
, NULL
, NULL
);
69 return g_fscache_lookup(xml_cache
, pathname
);
72 /* Save doc as XML as filename, 0 on success or 1 on failure */
73 int save_xml_file(xmlDocPtr doc
, const gchar
*filename
)
75 #if LIBXML_VERSION > 20400
76 if (xmlSaveFormatFileEnc(filename
, doc
, NULL
, 1) < 0)
81 out
= fopen(filename
, "w");
85 xmlDocDump(out
, doc
); /* Some versions return void */
94 /* Create a new SOAP message and return the document and the (empty)
97 xmlDocPtr
soap_new(xmlNodePtr
*ret_body
)
103 doc
= xmlNewDoc("1.0");
104 root
= xmlNewDocNode(doc
, NULL
, "Envelope", NULL
);
105 xmlDocSetRootElement(doc
, root
);
107 env_ns
= xmlNewNs(root
, SOAP_ENV_NS
, "env");
108 xmlSetNs(root
, env_ns
);
110 *ret_body
= xmlNewTextChild(root
, env_ns
, "Body", NULL
);
111 xmlNewNs(*ret_body
, ROX_NS
, "rox");
116 /* Like g_strdup, but does realpath() too (if possible) */
117 char *pathdup(const char *path
)
119 char real
[MAXPATHLEN
];
121 g_return_val_if_fail(path
!= NULL
, NULL
);
123 if (realpath(path
, real
))
124 return g_strdup(real
);
126 return g_strdup(path
);
129 /* Join the path to the leaf (adding a / between them) and
130 * return a pointer to a static buffer with the result. Buffer is valid
131 * until the next call to make_path.
132 * The return value may be used as 'dir' for the next call.
134 const guchar
*make_path(const char *dir
, const char *leaf
)
136 static GString
*buffer
= NULL
;
139 buffer
= g_string_new(NULL
);
141 g_return_val_if_fail(dir
!= NULL
, buffer
->str
);
142 g_return_val_if_fail(leaf
!= NULL
, buffer
->str
);
144 if (buffer
->str
!= dir
)
145 g_string_assign(buffer
, dir
);
147 if (dir
[0] != '/' || dir
[1] != '\0')
148 g_string_append_c(buffer
, '/'); /* For anything except "/" */
150 g_string_append(buffer
, leaf
);
155 /* Return our complete host name for DND */
156 const char *our_host_name_for_dnd(void)
158 if (o_dnd_no_hostnames
.int_value
)
160 return our_host_name();
163 /* Return our complete host name, unconditionally */
164 const char *our_host_name(void)
166 static char *name
= NULL
;
172 if (gethostname(buffer
, 4096) == 0)
174 /* gethostname doesn't always return the full name... */
178 ent
= gethostbyname(buffer
);
179 name
= g_strdup(ent
? ent
->h_name
: buffer
);
183 g_warning("gethostname() failed - using localhost\n");
184 name
= g_strdup("localhost");
191 void debug_free_string(void *data
)
193 g_print("Freeing string '%s'\n", (char *) data
);
197 const char *user_name(uid_t uid
)
202 uid_hash
= g_hash_table_new(NULL
, NULL
);
204 retval
= g_hash_table_lookup(uid_hash
, GINT_TO_POINTER(uid
));
208 struct passwd
*passwd
;
210 passwd
= getpwuid(uid
);
211 retval
= passwd
? g_strdup(passwd
->pw_name
)
212 : g_strdup_printf("[%d]", (int) uid
);
213 g_hash_table_insert(uid_hash
, GINT_TO_POINTER(uid
),
220 const char *group_name(gid_t gid
)
225 gid_hash
= g_hash_table_new(NULL
, NULL
);
227 retval
= g_hash_table_lookup(gid_hash
, GINT_TO_POINTER(gid
));
233 group
= getgrgid(gid
);
234 retval
= group
? g_strdup(group
->gr_name
)
235 : g_strdup_printf("[%d]", (int) gid
);
236 g_hash_table_insert(gid_hash
, GINT_TO_POINTER(gid
),
243 /* Return a string in the form '23 M' in a static buffer valid until
246 const char *format_size(off_t size
)
248 static char *buffer
= NULL
;
251 if (size
>= PRETTY_SIZE_LIMIT
)
255 if (size
>= PRETTY_SIZE_LIMIT
)
259 if (size
>= PRETTY_SIZE_LIMIT
)
275 buffer
= g_strdup_printf("%" SIZE_FMT
" %s", size
, units
);
280 /* Return a string in the form '23M' in a static buffer valid until
281 * the next call. Aligned to the right (5 chars).
283 const char *format_size_aligned(off_t size
)
285 static char *buffer
= NULL
;
288 if (size
>= PRETTY_SIZE_LIMIT
)
292 if (size
>= PRETTY_SIZE_LIMIT
)
296 if (size
>= PRETTY_SIZE_LIMIT
)
312 buffer
= g_strdup_printf("%4" SIZE_FMT
"%c", size
, units
);
318 * Similar to format_size(), but this one uses a double argument since
319 * unsigned long isn't wide enough on all platforms and we must be able to
320 * sum sizes above 4 GB.
322 const gchar
*format_double_size(double size
)
324 static gchar
*buf
= NULL
;
327 if (size
>= PRETTY_SIZE_LIMIT
)
331 if (size
>= PRETTY_SIZE_LIMIT
)
335 if (size
>= PRETTY_SIZE_LIMIT
)
354 buf
= g_strdup_printf("%.0f %s", floor(size
), units
);
359 /* Fork and exec argv. Wait and return the child's exit status.
361 * Returns the error string from the command if any, or NULL on success.
362 * If the process returns a non-zero exit status without producing a message,
363 * a suitable message is created.
364 * g_free() the result.
366 char *fork_exec_wait(const char **argv
)
369 gchar
*errors
= NULL
;
370 GError
*error
= NULL
;
372 if (!g_spawn_sync(NULL
, (char **) argv
, NULL
,
373 G_SPAWN_SEARCH_PATH
| G_SPAWN_STDOUT_TO_DEV_NULL
,
375 NULL
, &errors
, &status
, &error
))
379 msg
= g_strdup(error
->message
);
384 if (errors
&& !*errors
)
385 null_g_free(&errors
);
387 if (!WIFEXITED(status
))
390 errors
= g_strdup("(Subprocess crashed?)");
392 else if (WEXITSTATUS(status
))
395 errors
= g_strdup(_("ERROR"));
404 /* If a file has this UID and GID, which permissions apply to us?
405 * 0 = User, 1 = Group, 2 = World
407 gint
applicable(uid_t uid
, gid_t gid
)
417 for (i
= 0; i
< ngroups
; i
++)
419 if (supplemental_groups
[i
] == gid
)
426 /* Converts a file's mode to a string. Result is a pointer
427 * to a static buffer, valid until the next call.
429 const char *pretty_permissions(mode_t m
)
431 static char buffer
[] = "rwx,rwx,rwx/UG"
437 buffer
[0] = m
& S_IRUSR
? 'r' : '-';
438 buffer
[1] = m
& S_IWUSR
? 'w' : '-';
439 buffer
[2] = m
& S_IXUSR
? 'x' : '-';
441 buffer
[4] = m
& S_IRGRP
? 'r' : '-';
442 buffer
[5] = m
& S_IWGRP
? 'w' : '-';
443 buffer
[6] = m
& S_IXGRP
? 'x' : '-';
445 buffer
[8] = m
& S_IROTH
? 'r' : '-';
446 buffer
[9] = m
& S_IWOTH
? 'w' : '-';
447 buffer
[10] = m
& S_IXOTH
? 'x' : '-';
449 buffer
[12] = m
& S_ISUID
? 'U' : '-';
450 buffer
[13] = m
& S_ISGID
? 'G' : '-';
452 buffer
[14] = m
& S_ISVTX
? 'T' : '-';
458 /* Gets the canonical name for address and compares to our_host_name() */
459 static gboolean
is_local_address(char *address
)
463 ent
= gethostbyname(address
);
465 return strcmp(our_host_name(), ent
? ent
->h_name
: address
) == 0;
468 /* Convert a URI to a local pathname (or NULL if it isn't local).
469 * The returned pointer needs to be passed to g_free() when done (if not NULL).
470 * THIS IS A CHANGE. The return path has been processed by unescape_uri().
477 char *get_local_path(const EscapedPath
*escaped_uri
)
479 const char *uri
= (char *) escaped_uri
;
483 char *path
, *uri_host
;
485 /* Just a local path - no host part */
487 return unescape_uri((EscapedPath
*) uri
);
489 path
= strchr(uri
+ 2, '/');
491 return NULL
; /* //something */
496 return unescape_uri((EscapedPath
*) path
);
499 uri_host
= g_strndup(uri
+ 2, path
- uri
- 2);
500 if (is_local_address(uri_host
))
504 return unescape_uri((EscapedPath
*) path
);
508 return NULL
; /* From a different host */
512 if (strncasecmp(uri
, "file:", 5))
513 return NULL
; /* Don't know this format */
518 return get_local_path((EscapedPath
*) uri
);
524 /* Set the close-on-exec flag for this FD.
525 * TRUE means that an exec()'d process will not get the FD.
527 void close_on_exec(int fd
, gboolean close
)
529 if (fcntl(fd
, F_SETFD
, close
))
530 g_warning("fcntl() failed: %s\n", g_strerror(errno
));
533 void set_blocking(int fd
, gboolean blocking
)
535 if (fcntl(fd
, F_SETFL
, blocking
? 0 : O_NONBLOCK
))
536 g_warning("fcntl() failed: %s\n", g_strerror(errno
));
539 /* Format this time nicely.
540 * g_free() the result.
542 char *pretty_time(const time_t *time
)
546 if (strftime(time_buf
, sizeof(time_buf
),
547 TIME_FORMAT
, localtime(time
)) == 0)
550 return to_utf8(time_buf
);
554 # define O_NOFOLLOW 0x0
557 /* 'from' and 'to' are complete pathnames of files (not dirs or symlinks).
558 * This spawns 'cp' to do the copy if lstat() succeeds, otherwise we
559 * do the copy manually using vfs.
561 * Returns an error string, or NULL on success. g_free() the result.
563 * XXX: This was only used for libvfs...
565 guchar
*copy_file(const guchar
*from
, const guchar
*to
)
567 const char *argv
[] = {"cp", "-pRf", NULL
, NULL
, NULL
};
572 return fork_exec_wait(argv
);
575 /* 'word' has all special characters escaped so that it may be inserted
576 * into a shell command.
577 * Eg: 'My Dir?' becomes 'My\ Dir\?'. g_free() the result.
579 guchar
*shell_escape(const guchar
*word
)
584 tmp
= g_string_new(NULL
);
588 if (strchr(" ?*['\"$~\\|();!`&", *word
))
589 g_string_append_c(tmp
, '\\');
590 g_string_append_c(tmp
, *word
);
595 g_string_free(tmp
, FALSE
);
599 /* TRUE iff `sub' is (or would be) an object inside the directory `parent',
600 * (or the two are the same item/directory).
601 * FALSE if parent doesn't exist.
603 gboolean
is_sub_dir(const char *sub_obj
, const char *parent
)
605 struct stat parent_info
;
608 if (mc_lstat(parent
, &parent_info
))
609 return FALSE
; /* Parent doesn't exist */
611 /* For checking Copy/Move operations do a realpath first on sub
612 * (the destination), since copying into a symlink is the same as
613 * copying into the thing it points to. Don't realpath 'parent' though;
614 * copying a symlink just makes a new symlink.
616 * When checking if an icon depends on a file (parent), use realpath on
617 * sub (the icon) too.
619 sub
= pathdup(sub_obj
);
626 if (mc_lstat(sub
, &info
) == 0)
628 if (info
.st_dev
== parent_info
.st_dev
&&
629 info
.st_ino
== parent_info
.st_ino
)
636 slash
= strrchr(sub
, '/');
655 /* True if the string 'list' contains 'item'.
656 * Eg ("close", "close, help") -> TRUE
658 gboolean
in_list(const guchar
*item
, const guchar
*list
)
666 if (strncmp(item
, list
, len
) == 0 &&
667 !g_ascii_isalpha(list
[len
]))
669 list
= strchr(list
, ',');
672 while (g_ascii_isspace(*++list
))
679 /* Split a path into its components. Eg:
681 * /bob/fred -> ["bob", "fred"]
682 * ///a//b// -> ["a", "b"]
685 * The array and the strings in it must be freed after use.
687 GPtrArray
*split_path(const guchar
*path
)
692 g_return_val_if_fail(path
!= NULL
, NULL
);
694 array
= g_ptr_array_new();
698 while (path
[0] == '/')
703 slash
= strchr(path
, '/');
706 g_ptr_array_add(array
, g_strndup(path
, slash
- path
));
710 g_ptr_array_add(array
, g_strdup(path
));
717 /* Return the shortest path from 'from' to 'to'.
718 * Eg: get_relative_path("/a/b/c", "a/d/e") -> "../d/e"
720 guchar
*get_relative_path(const guchar
*from
, const guchar
*to
)
724 GPtrArray
*src
, *dst
;
727 src
= split_path(from
);
728 dst
= split_path(to
);
730 /* The last component of src doesn't matter... */
733 g_free(src
->pdata
[src
->len
- 1]);
734 g_ptr_array_remove_index(src
, src
->len
- 1);
737 /* Strip off common path elements... */
739 while (i
< src
->len
&& i
< dst
->len
)
741 guchar
*a
= (guchar
*) src
->pdata
[i
];
742 guchar
*b
= (guchar
*) dst
->pdata
[i
];
744 if (strcmp(a
, b
) != 0)
749 /* Go up one dir for each element remaining in src */
750 path
= g_string_new(NULL
);
751 for (j
= i
; j
< src
->len
; j
++)
752 g_string_append(path
, "../");
754 /* Go down one dir for each element remaining in dst */
755 for (j
= i
; j
< dst
->len
; j
++)
757 g_string_append(path
, (guchar
*) dst
->pdata
[j
]);
758 g_string_append_c(path
, '/');
761 if (path
->str
[path
->len
- 1] == '/')
762 g_string_truncate(path
, path
->len
- 1);
764 g_string_assign(path
, ".");
766 /* Free the arrays */
767 for (i
= 0; i
< src
->len
; i
++)
768 g_free(src
->pdata
[i
]);
769 g_ptr_array_free(src
, TRUE
);
770 for (i
= 0; i
< dst
->len
; i
++)
771 g_free(dst
->pdata
[i
]);
772 g_ptr_array_free(dst
, TRUE
);
775 g_string_free(path
, FALSE
);
781 * Interperet text as a boolean value. Return defvalue if we don't
784 int text_to_boolean(const char *text
, int defvalue
)
786 if (g_strcasecmp(text
, "true")==0)
788 else if (g_strcasecmp(text
, "false")==0)
790 else if (g_strcasecmp(text
, "yes")==0)
792 else if (g_strcasecmp(text
, "no")==0)
794 else if (g_ascii_isdigit(text
[0]))
800 /* Return the pathname that this symlink points to.
801 * NULL on error (not a symlink, path too long) and errno set.
802 * g_free() the result.
804 char *readlink_dup(const char *source
)
806 char path
[MAXPATHLEN
+ 1];
809 got
= readlink(source
, path
, MAXPATHLEN
);
810 if (got
< 0 || got
> MAXPATHLEN
)
813 return g_strndup(path
, got
);
817 * This code implements the MD5 message-digest algorithm.
818 * The algorithm is due to Ron Rivest. The original code was
819 * written by Colin Plumb in 1993, and put in the public domain.
821 * Modified to use glib datatypes. Put under GPL to simplify
822 * licensing for ROX-Filer. Taken from Debian's dpkg package.
825 #define md5byte unsigned char
827 typedef struct _MD5Context MD5Context
;
835 #if G_BYTE_ORDER == G_BIG_ENDIAN
836 static void byteSwap(guint32
*buf
, unsigned words
)
838 md5byte
*p
= (md5byte
*)buf
;
841 *buf
++ = (guint32
)((unsigned)p
[3] << 8 | p
[2]) << 16 |
842 ((unsigned)p
[1] << 8 | p
[0]);
847 #define byteSwap(buf,words)
851 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
852 * initialization constants.
854 static void MD5Init(MD5Context
*ctx
)
856 ctx
->buf
[0] = 0x67452301;
857 ctx
->buf
[1] = 0xefcdab89;
858 ctx
->buf
[2] = 0x98badcfe;
859 ctx
->buf
[3] = 0x10325476;
866 * Update context to reflect the concatenation of another buffer full
869 static void MD5Update(MD5Context
*ctx
, md5byte
const *buf
, unsigned len
)
873 /* Update byte count */
876 if ((ctx
->bytes
[0] = t
+ len
) < t
)
877 ctx
->bytes
[1]++; /* Carry from low to high */
879 t
= 64 - (t
& 0x3f); /* Space available in ctx->in (at least 1) */
881 memcpy((md5byte
*)ctx
->in
+ 64 - t
, buf
, len
);
884 /* First chunk is an odd size */
885 memcpy((md5byte
*)ctx
->in
+ 64 - t
, buf
, t
);
886 byteSwap(ctx
->in
, 16);
887 MD5Transform(ctx
->buf
, ctx
->in
);
891 /* Process data in 64-byte chunks */
893 memcpy(ctx
->in
, buf
, 64);
894 byteSwap(ctx
->in
, 16);
895 MD5Transform(ctx
->buf
, ctx
->in
);
900 /* Handle any remaining bytes of data. */
901 memcpy(ctx
->in
, buf
, len
);
905 * Final wrapup - pad to 64-byte boundary with the bit pattern
906 * 1 0* (64-bit count of bits processed, MSB-first)
907 * Returns the newly allocated string of the hash.
909 static char *MD5Final(MD5Context
*ctx
)
913 int count
= ctx
->bytes
[0] & 0x3f; /* Number of bytes in ctx->in */
914 md5byte
*p
= (md5byte
*)ctx
->in
+ count
;
917 /* Set the first char of padding to 0x80. There is always room. */
920 /* Bytes of padding needed to make 56 bytes (-8..55) */
921 count
= 56 - 1 - count
;
923 if (count
< 0) { /* Padding forces an extra block */
924 memset(p
, 0, count
+ 8);
925 byteSwap(ctx
->in
, 16);
926 MD5Transform(ctx
->buf
, ctx
->in
);
927 p
= (md5byte
*)ctx
->in
;
931 byteSwap(ctx
->in
, 14);
933 /* Append length in bits and transform */
934 ctx
->in
[14] = ctx
->bytes
[0] << 3;
935 ctx
->in
[15] = ctx
->bytes
[1] << 3 | ctx
->bytes
[0] >> 29;
936 MD5Transform(ctx
->buf
, ctx
->in
);
938 byteSwap(ctx
->buf
, 4);
940 retval
= g_malloc(33);
941 bytes
= (guint8
*) ctx
->buf
;
942 for (i
= 0; i
< 16; i
++)
943 sprintf(retval
+ (i
* 2), "%02x", bytes
[i
]);
951 /* The four core functions - F1 is optimized somewhat */
953 /* #define F1(x, y, z) (x & y | ~x & z) */
954 #define F1(x, y, z) (z ^ (x & (y ^ z)))
955 #define F2(x, y, z) F1(z, x, y)
956 #define F3(x, y, z) (x ^ y ^ z)
957 #define F4(x, y, z) (y ^ (x | ~z))
959 /* This is the central step in the MD5 algorithm. */
960 #define MD5STEP(f,w,x,y,z,in,s) \
961 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
964 * The core of the MD5 algorithm, this alters an existing MD5 hash to
965 * reflect the addition of 16 longwords of new data. MD5Update blocks
966 * the data and converts bytes into longwords for this routine.
968 static void MD5Transform(guint32 buf
[4], guint32
const in
[16])
970 register guint32 a
, b
, c
, d
;
977 MD5STEP(F1
, a
, b
, c
, d
, in
[0] + 0xd76aa478, 7);
978 MD5STEP(F1
, d
, a
, b
, c
, in
[1] + 0xe8c7b756, 12);
979 MD5STEP(F1
, c
, d
, a
, b
, in
[2] + 0x242070db, 17);
980 MD5STEP(F1
, b
, c
, d
, a
, in
[3] + 0xc1bdceee, 22);
981 MD5STEP(F1
, a
, b
, c
, d
, in
[4] + 0xf57c0faf, 7);
982 MD5STEP(F1
, d
, a
, b
, c
, in
[5] + 0x4787c62a, 12);
983 MD5STEP(F1
, c
, d
, a
, b
, in
[6] + 0xa8304613, 17);
984 MD5STEP(F1
, b
, c
, d
, a
, in
[7] + 0xfd469501, 22);
985 MD5STEP(F1
, a
, b
, c
, d
, in
[8] + 0x698098d8, 7);
986 MD5STEP(F1
, d
, a
, b
, c
, in
[9] + 0x8b44f7af, 12);
987 MD5STEP(F1
, c
, d
, a
, b
, in
[10] + 0xffff5bb1, 17);
988 MD5STEP(F1
, b
, c
, d
, a
, in
[11] + 0x895cd7be, 22);
989 MD5STEP(F1
, a
, b
, c
, d
, in
[12] + 0x6b901122, 7);
990 MD5STEP(F1
, d
, a
, b
, c
, in
[13] + 0xfd987193, 12);
991 MD5STEP(F1
, c
, d
, a
, b
, in
[14] + 0xa679438e, 17);
992 MD5STEP(F1
, b
, c
, d
, a
, in
[15] + 0x49b40821, 22);
994 MD5STEP(F2
, a
, b
, c
, d
, in
[1] + 0xf61e2562, 5);
995 MD5STEP(F2
, d
, a
, b
, c
, in
[6] + 0xc040b340, 9);
996 MD5STEP(F2
, c
, d
, a
, b
, in
[11] + 0x265e5a51, 14);
997 MD5STEP(F2
, b
, c
, d
, a
, in
[0] + 0xe9b6c7aa, 20);
998 MD5STEP(F2
, a
, b
, c
, d
, in
[5] + 0xd62f105d, 5);
999 MD5STEP(F2
, d
, a
, b
, c
, in
[10] + 0x02441453, 9);
1000 MD5STEP(F2
, c
, d
, a
, b
, in
[15] + 0xd8a1e681, 14);
1001 MD5STEP(F2
, b
, c
, d
, a
, in
[4] + 0xe7d3fbc8, 20);
1002 MD5STEP(F2
, a
, b
, c
, d
, in
[9] + 0x21e1cde6, 5);
1003 MD5STEP(F2
, d
, a
, b
, c
, in
[14] + 0xc33707d6, 9);
1004 MD5STEP(F2
, c
, d
, a
, b
, in
[3] + 0xf4d50d87, 14);
1005 MD5STEP(F2
, b
, c
, d
, a
, in
[8] + 0x455a14ed, 20);
1006 MD5STEP(F2
, a
, b
, c
, d
, in
[13] + 0xa9e3e905, 5);
1007 MD5STEP(F2
, d
, a
, b
, c
, in
[2] + 0xfcefa3f8, 9);
1008 MD5STEP(F2
, c
, d
, a
, b
, in
[7] + 0x676f02d9, 14);
1009 MD5STEP(F2
, b
, c
, d
, a
, in
[12] + 0x8d2a4c8a, 20);
1011 MD5STEP(F3
, a
, b
, c
, d
, in
[5] + 0xfffa3942, 4);
1012 MD5STEP(F3
, d
, a
, b
, c
, in
[8] + 0x8771f681, 11);
1013 MD5STEP(F3
, c
, d
, a
, b
, in
[11] + 0x6d9d6122, 16);
1014 MD5STEP(F3
, b
, c
, d
, a
, in
[14] + 0xfde5380c, 23);
1015 MD5STEP(F3
, a
, b
, c
, d
, in
[1] + 0xa4beea44, 4);
1016 MD5STEP(F3
, d
, a
, b
, c
, in
[4] + 0x4bdecfa9, 11);
1017 MD5STEP(F3
, c
, d
, a
, b
, in
[7] + 0xf6bb4b60, 16);
1018 MD5STEP(F3
, b
, c
, d
, a
, in
[10] + 0xbebfbc70, 23);
1019 MD5STEP(F3
, a
, b
, c
, d
, in
[13] + 0x289b7ec6, 4);
1020 MD5STEP(F3
, d
, a
, b
, c
, in
[0] + 0xeaa127fa, 11);
1021 MD5STEP(F3
, c
, d
, a
, b
, in
[3] + 0xd4ef3085, 16);
1022 MD5STEP(F3
, b
, c
, d
, a
, in
[6] + 0x04881d05, 23);
1023 MD5STEP(F3
, a
, b
, c
, d
, in
[9] + 0xd9d4d039, 4);
1024 MD5STEP(F3
, d
, a
, b
, c
, in
[12] + 0xe6db99e5, 11);
1025 MD5STEP(F3
, c
, d
, a
, b
, in
[15] + 0x1fa27cf8, 16);
1026 MD5STEP(F3
, b
, c
, d
, a
, in
[2] + 0xc4ac5665, 23);
1028 MD5STEP(F4
, a
, b
, c
, d
, in
[0] + 0xf4292244, 6);
1029 MD5STEP(F4
, d
, a
, b
, c
, in
[7] + 0x432aff97, 10);
1030 MD5STEP(F4
, c
, d
, a
, b
, in
[14] + 0xab9423a7, 15);
1031 MD5STEP(F4
, b
, c
, d
, a
, in
[5] + 0xfc93a039, 21);
1032 MD5STEP(F4
, a
, b
, c
, d
, in
[12] + 0x655b59c3, 6);
1033 MD5STEP(F4
, d
, a
, b
, c
, in
[3] + 0x8f0ccc92, 10);
1034 MD5STEP(F4
, c
, d
, a
, b
, in
[10] + 0xffeff47d, 15);
1035 MD5STEP(F4
, b
, c
, d
, a
, in
[1] + 0x85845dd1, 21);
1036 MD5STEP(F4
, a
, b
, c
, d
, in
[8] + 0x6fa87e4f, 6);
1037 MD5STEP(F4
, d
, a
, b
, c
, in
[15] + 0xfe2ce6e0, 10);
1038 MD5STEP(F4
, c
, d
, a
, b
, in
[6] + 0xa3014314, 15);
1039 MD5STEP(F4
, b
, c
, d
, a
, in
[13] + 0x4e0811a1, 21);
1040 MD5STEP(F4
, a
, b
, c
, d
, in
[4] + 0xf7537e82, 6);
1041 MD5STEP(F4
, d
, a
, b
, c
, in
[11] + 0xbd3af235, 10);
1042 MD5STEP(F4
, c
, d
, a
, b
, in
[2] + 0x2ad7d2bb, 15);
1043 MD5STEP(F4
, b
, c
, d
, a
, in
[9] + 0xeb86d391, 21);
1051 # endif /* ASM_MD5 */
1053 char *md5_hash(const char *message
)
1058 MD5Update(&ctx
, message
, strlen(message
));
1059 return MD5Final(&ctx
);
1062 /* Convert string 'src' from the current locale to UTF-8 */
1063 gchar
*to_utf8(const gchar
*src
)
1070 retval
= g_locale_to_utf8(src
, -1, NULL
, NULL
, NULL
);
1072 retval
= g_convert_with_fallback(src
, -1, "utf-8", "iso-8859-1",
1073 "?", NULL
, NULL
, NULL
);
1075 return retval
? retval
: g_strdup(src
);
1078 /* Ensure string at 'sp' is UTF-8. g_free() and replace by
1079 * UTF-8 version if not.
1081 void ensure_utf8(gchar
**sp
)
1085 if (!g_utf8_validate(s
, -1, NULL
))
1092 /* Removes trailing / chars and converts a leading '~/' (if any) to
1093 * the user's home dir. g_free() the result.
1095 gchar
*expand_path(const gchar
*path
)
1100 g_return_val_if_fail(path
!= NULL
, NULL
);
1102 path_len
= strlen(path
);
1103 while (path_len
> 1 && path
[path_len
- 1] == '/')
1106 retval
= g_strndup(path
, path_len
);
1108 if (path
[0] == '~' && (path
[1] == '\0' || path
[1] == '/'))
1110 guchar
*tmp
= retval
;
1112 retval
= g_strconcat(home_dir
, retval
+ 1, NULL
);
1119 /* g_free() every element in the list, then free the list itself and
1120 * NULL the pointer to the list.
1122 void destroy_glist(GList
**list
)
1125 g_list_foreach(l
, (GFunc
) g_free
, NULL
);
1130 void null_g_free(gpointer p
)
1132 g_free(*(gpointer
*)p
);
1133 *(gpointer
*)p
= NULL
;
1136 typedef struct _CollatePart CollatePart
;
1138 struct _CollateKey
{
1143 struct _CollatePart
{
1144 guchar
*text
; /* NULL => end of list */
1148 /* Break 'name' (a UTF-8 string) down into a list of (text, number) pairs.
1149 * The text parts processed for collating. This allows any two names to be
1150 * quickly compared later for intelligent sorting (comparing names is
1153 CollateKey
*collate_key_new(const guchar
*name
)
1156 guchar
*to_free
= NULL
;
1162 g_return_val_if_fail(name
!= NULL
, NULL
);
1164 array
= g_array_new(FALSE
, FALSE
, sizeof(CollatePart
));
1166 /* Ensure valid UTF-8 */
1167 if (!g_utf8_validate(name
, -1, NULL
))
1169 to_free
= to_utf8(name
);
1173 retval
= g_new(CollateKey
, 1);
1174 retval
->caps
= g_unichar_isupper(g_utf8_get_char(name
));
1176 for (i
= name
; *i
; i
= g_utf8_next_char(i
))
1178 /* We're in a (possibly blank) text section starting at 'name'.
1179 * Find the end of it (the next digit, or end of string).
1181 if (g_ascii_isdigit(g_utf8_get_char(i
)))
1185 /* i -> first digit character */
1186 tmp
= g_utf8_strdown(name
, i
- name
);
1187 new.text
= g_utf8_collate_key(tmp
, -1);
1189 new.number
= strtol(i
, &endp
, 10);
1191 g_array_append_val(array
, new);
1193 g_return_val_if_fail(endp
> (char *) i
, NULL
);
1200 tmp
= g_utf8_strdown(name
, i
- name
);
1201 new.text
= g_utf8_collate_key(tmp
, -1);
1204 g_array_append_val(array
, new);
1207 g_array_append_val(array
, new);
1209 retval
->parts
= (CollatePart
*) array
->data
;
1210 g_array_free(array
, FALSE
);
1213 g_free(to_free
); /* Only taken for invalid UTF-8 */
1218 void collate_key_free(CollateKey
*key
)
1222 for (part
= key
->parts
; part
->text
; part
++)
1228 int collate_key_cmp(const CollateKey
*key1
, const CollateKey
*key2
,
1229 gboolean caps_first
)
1231 CollatePart
*n1
= key1
->parts
;
1232 CollatePart
*n2
= key2
->parts
;
1237 if (key1
->caps
&& !key2
->caps
)
1239 else if (key2
->caps
&& !key1
->caps
)
1246 return n2
->text
? -1 : 0;
1249 r
= strcmp(n1
->text
, n2
->text
);
1253 if (n1
->number
< n2
->number
)
1255 if (n1
->number
> n2
->number
)
1263 /* Returns TRUE if the object exists, FALSE if it doesn't.
1264 * For symlinks, the file pointed to must exist.
1266 gboolean
file_exists(const char *path
)
1270 return !mc_stat(path
, &info
);
1273 /* Escape path for future use in URI */
1274 EscapedPath
*escape_uri_path(const char *path
)
1276 const char *safe
= ":-_./"; /* Plus isalnum() */
1281 str
= g_string_sized_new(strlen(path
));
1283 for (s
= path
; *s
; s
++)
1285 if (!g_ascii_isalnum(*s
) && !strchr(safe
, *s
))
1286 g_string_append_printf(str
, "%%%02x", *s
);
1288 str
= g_string_append_c(str
, *s
);
1292 g_string_free(str
, FALSE
);
1294 return (EscapedPath
*) ans
;
1297 EscapedPath
*encode_path_as_uri(const guchar
*path
)
1299 gchar
*tpath
= (gchar
*) escape_uri_path(path
);
1302 uri
= g_strconcat("file://", our_host_name_for_dnd(), tpath
, NULL
);
1305 return (EscapedPath
*) uri
;
1308 gchar
*unescape_uri(const EscapedPath
*uri
)
1310 const char *uri_string
= (char *) uri
;
1315 tmp
= g_malloc(strlen(uri_string
) + 1);
1316 for (s
= uri_string
, d
= tmp
; *s
; s
++, d
++)
1318 /*printf("%s\n", s);*/
1319 if (*s
== '%' && g_ascii_isxdigit(s
[1]) &&
1320 g_ascii_isxdigit(s
[2]))
1327 c
= (int) strtol(buf
, NULL
, 16);
1339 /* Used as the sort function for sorting GPtrArrays */
1340 gint
strcmp2(gconstpointer a
, gconstpointer b
)
1342 const char *aa
= *(char **) a
;
1343 const char *bb
= *(char **) b
;
1345 return g_strcasecmp(aa
, bb
);
1348 /* Returns an array listing all the names in the directory 'path'.
1349 * The array is sorted.
1350 * '.' and '..' are skipped.
1351 * On error, the error is reported with g_warning and NULL is returned.
1353 GPtrArray
*list_dir(const guchar
*path
)
1356 GError
*error
= NULL
;
1360 dir
= g_dir_open(path
, 0, &error
);
1363 g_warning("Can't list directory:\n%s", error
->message
);
1364 g_error_free(error
);
1368 names
= g_ptr_array_new();
1370 while ((leaf
= g_dir_read_name(dir
))) {
1372 g_ptr_array_add(names
, g_strdup(leaf
));
1377 g_ptr_array_sort(names
, strcmp2
);
1382 int stat_with_timeout(const char *path
, struct stat
*info
)
1391 g_warning("stat_with_timeout: fork(): %s", g_strerror(errno
));
1399 _exit(mc_stat(path
, info
) ? 1 : 0);
1402 waitpid(child
, &status
, 0);
1405 retval
= mc_stat(path
, info
);