Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / core / misc.c
index e030a5147b9e744b42f84a4f6109d7c3b6a9b208..3dbc7bad0621797bde94f7d3405d4b67a4059e5a 100644 (file)
@@ -26,7 +26,6 @@
 #ifdef HAVE_REGEX_H
 #  include <regex.h>
 #endif
-#include <pwd.h>
 
 typedef struct {
        int condition;
@@ -174,37 +173,6 @@ int strarray_find(char **array, const char *item)
        return -1;
 }
 
-int execute(const char *cmd)
-{
-       char **args;
-#ifndef WIN32
-       int pid;
-#endif
-
-       g_return_val_if_fail(cmd != NULL, -1);
-
-#ifndef WIN32
-       pid = fork();
-       if (pid == -1) return FALSE;
-       if (pid != 0) {
-               pidwait_add(pid);
-               return pid;
-       }
-
-       args = g_strsplit(cmd, " ", -1);
-       execvp(args[0], args);
-       g_strfreev(args);
-
-       _exit(99);
-       return -1;
-#else
-       args = g_strsplit(cmd, " ", -1);
-       _spawnvp(_P_DETACH, args[0], args);
-       g_strfreev(args);
-       return 0;
-#endif
-}
-
 GSList *gslist_find_string(GSList *list, const char *key)
 {
        for (list = list; list != NULL; list = list->next)
@@ -449,25 +417,20 @@ int mkpath(const char *path, int mode)
        return 0;
 }
 
-/* Get home directory */
-const char *get_home_dir(void)
-{
-       struct passwd *pw = getpwuid(getuid());
-       if (!pw) {
-        if (g_getenv("HOME"))
-          return g_getenv("HOME");
-        else
-          return ".";
-       }
-       return pw->pw_dir;
-}
-
 /* convert ~/ to $HOME */
 char *convert_home(const char *path)
 {
-       return *path == '~' && (*(path+1) == '/' || *(path+1) == '\0') ?
-               g_strconcat((char *)get_home_dir(), path+1, NULL) :
-               g_strdup(path);
+       const char *home;
+
+       if (*path == '~' && (*(path+1) == '/' || *(path+1) == '\0')) {
+               home = g_get_home_dir();
+               if (home == NULL)
+                       home = ".";
+
+               return g_strconcat(home, path+1, NULL);
+       } else {
+               return g_strdup(path);
+       }
 }
 
 int g_istr_equal(gconstpointer v, gconstpointer v2)
@@ -598,6 +561,20 @@ int dec2octal(int decimal)
        return octal;
 }
 
+/* string -> uoff_t */
+uoff_t str_to_uofft(const char *str)
+{
+       uoff_t ret;
+
+       ret = 0;
+       while (*str != '\0') {
+               ret = ret*10 + (*str - '0');
+               str++;
+       }
+
+       return ret;
+}
+
 /* convert all low-ascii (<32) to ^<A..> combinations */
 char *show_lowascii(const char *channel)
 {
@@ -784,3 +761,165 @@ int expand_escape(const char **data)
                return strtol(digit, NULL, 8);
        }
 }
+
+/* Escape all '"', "'" and '\' chars with '\' */
+char *escape_string(const char *str)
+{
+       char *ret, *p;
+
+       p = ret = g_malloc(strlen(str)*2+1);
+       while (*str != '\0') {
+               if (*str == '"' || *str == '\'' || *str == '\\')
+                       *p++ = '\\';
+               *p++ = *str++;
+       }
+       *p = '\0';
+
+       return ret;
+}
+
+int strocpy(char *dest, const char *src, size_t dstsize)
+{
+       if (dstsize == 0)
+               return -1;
+
+       while (*src != '\0' && dstsize > 1) {
+               *dest++ = *src++;
+               dstsize--;
+       }
+
+       *dest++ = '\0';
+       return *src == '\0' ? 0 : -1;
+}
+
+int nearest_power(int num)
+{
+       int n = 1;
+
+       while (n < num) n <<= 1;
+       return n;
+}
+
+int parse_time_interval(const char *time, int *msecs)
+{
+       const char *desc;
+       int number, sign, len, ret;
+
+       *msecs = 0;
+
+       /* max. return value is around 24 days */
+       number = 0; sign = 1; ret = TRUE;
+       for (;;) {
+               if (*time == '-') {
+                       sign = -sign;
+                       time++;
+                       continue;
+               }
+
+               if (i_isdigit(*time)) {
+                       number = number*10 + (*time - '0');
+                       time++;
+                       continue;
+               }
+
+               /* skip punctuation */
+               while (*time != '\0' && i_ispunct(*time))
+                       time++;
+
+               /* get description */
+               for (len = 0, desc = time; i_isalpha(*time); time++)
+                       len++;
+
+               if (len == 0) {
+                       *msecs += number * 1000; /* assume seconds */
+                       *msecs *= sign;
+                       return TRUE;
+               }
+
+               if (g_strncasecmp(desc, "days", len) == 0) {
+                       if (number > 24) {
+                               /* would overflow */
+                               return FALSE;
+                       }
+                       *msecs += number * 1000*3600*24;
+               } else if (g_strncasecmp(desc, "hours", len) == 0)
+                       *msecs += number * 1000*3600;
+               else if (g_strncasecmp(desc, "minutes", len) == 0 ||
+                        g_strncasecmp(desc, "mins", len) == 0)
+                       *msecs += number * 1000*60;
+               else if (g_strncasecmp(desc, "seconds", len) == 0 ||
+                        g_strncasecmp(desc, "secs", len) == 0)
+                       *msecs += number * 1000;
+               else if (g_strncasecmp(desc, "milliseconds", len) == 0 ||
+                        g_strncasecmp(desc, "millisecs", len) == 0 ||
+                        g_strncasecmp(desc, "mseconds", len) == 0 ||
+                        g_strncasecmp(desc, "msecs", len) == 0)
+                       *msecs += number;
+               else {
+                       ret = FALSE;
+               }
+
+               /* skip punctuation */
+               while (*time != '\0' && i_ispunct(*time))
+                       time++;
+
+               if (*time == '\0')
+                       break;
+
+               number = 0;
+       }
+
+       *msecs *= sign;
+       return ret;
+}
+
+int parse_size(const char *size, int *bytes)
+{
+       const char *desc;
+       int number, len;
+
+       *bytes = 0;
+
+       /* max. return value is about 1.6 years */
+       number = 0;
+       while (*size != '\0') {
+               if (i_isdigit(*size)) {
+                       number = number*10 + (*size - '0');
+                       size++;
+                       continue;
+               }
+
+               /* skip punctuation */
+               while (*size != '\0' && i_ispunct(*size))
+                       size++;
+
+               /* get description */
+               for (len = 0, desc = size; i_isalpha(*size); size++)
+                       len++;
+
+               if (len == 0) {
+                       if (number == 0) {
+                               /* "0" - allow it */
+                               return TRUE;
+                       }
+
+                       *bytes += number*1024; /* assume kilobytes */
+                       return FALSE;
+               }
+
+               if (g_strncasecmp(desc, "gbytes", len) == 0)
+                       *bytes += number * 1024*1024*1024;
+               if (g_strncasecmp(desc, "mbytes", len) == 0)
+                       *bytes += number * 1024*1024;
+               if (g_strncasecmp(desc, "kbytes", len) == 0)
+                       *bytes += number * 1024;
+               if (g_strncasecmp(desc, "bytes", len) == 0)
+                       *bytes += number;
+
+               /* skip punctuation */
+               while (*size != '\0' && i_ispunct(*size))
+                       size++;
+       }
+
+       return TRUE;
+}