chiark / gitweb /
libudev: export udev_util_encode_string()
[elogind.git] / libudev / libudev-util.c
index 6c309afd0536798fe3a1e81dffb2b68d4bb7a591..e08349e0fa345d051a30d66d4d7a03f3b06592ea 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * libudev - interface to udev device information
  *
- * Copyright (C) 2008-2009 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2008-2011 Kay Sievers <kay.sievers@vrfy.org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
 #include <dirent.h>
 #include <ctype.h>
 #include <fcntl.h>
+#include <time.h>
 #include <sys/stat.h>
 
 #include "libudev.h"
 #include "libudev-private.h"
 
-static ssize_t get_sys_link(struct udev *udev, const char *slink, const char *syspath, char *value, size_t size)
+/**
+ * SECTION:libudev-util
+ * @short_description: utils
+ */
+
+ssize_t util_get_sys_core_link_value(struct udev *udev, const char *slink, const char *syspath, char *value, size_t size)
 {
        char path[UTIL_PATH_SIZE];
        char target[UTIL_PATH_SIZE];
@@ -43,16 +49,6 @@ static ssize_t get_sys_link(struct udev *udev, const char *slink, const char *sy
        return util_strscpy(value, size, pos);
 }
 
-ssize_t util_get_sys_subsystem(struct udev *udev, const char *syspath, char *subsystem, size_t size)
-{
-       return get_sys_link(udev, "subsystem", syspath, subsystem, size);
-}
-
-ssize_t util_get_sys_driver(struct udev *udev, const char *syspath, char *driver, size_t size)
-{
-       return get_sys_link(udev, "driver", syspath, driver, size);
-}
-
 int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size)
 {
        char link_target[UTIL_PATH_SIZE];
@@ -351,7 +347,7 @@ static int utf8_encoded_valid_unichar(const char *str)
        return len;
 }
 
-int udev_util_replace_whitespace(const char *str, char *to, size_t len)
+int util_replace_whitespace(const char *str, char *to, size_t len)
 {
        size_t i, j;
 
@@ -391,7 +387,7 @@ static int is_whitelisted(char c, const char *white)
 }
 
 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
-int udev_util_replace_chars(char *str, const char *white)
+int util_replace_chars(char *str, const char *white)
 {
        size_t i = 0;
        int replaced = 0;
@@ -434,18 +430,18 @@ int udev_util_replace_chars(char *str, const char *white)
 }
 
 /**
- * util_encode_string:
+ * udev_util_encode_string:
  * @str: input string to be encoded
  * @str_enc: output string to store the encoded input string
  * @len: maximum size of the output string, which may be
  *       four times as long as the input string
  *
  * Encode all potentially unsafe characters of a string to the
- * corresponding hex value prefixed by '\x'.
+ * corresponding 2 char hex value prefixed by '\x'.
  *
  * Returns: 0 if the entire string was copied, non-zero otherwise.
  **/
-int udev_util_encode_string(const char *str, char *str_enc, size_t len)
+UDEV_EXPORT int udev_util_encode_string(const char *str, char *str_enc, size_t len)
 {
        size_t i, j;
 
@@ -553,3 +549,15 @@ uint64_t util_string_bloom64(const char *str)
        bits |= 1LLU << ((hash >> 18) & 63);
        return bits;
 }
+
+#define USEC_PER_SEC  1000000ULL
+#define NSEC_PER_USEC 1000ULL
+unsigned long long now_usec(void)
+{
+       struct timespec ts;
+
+       if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
+               return 0;
+       return (unsigned long long) ts.tv_sec * USEC_PER_SEC +
+              (unsigned long long) ts.tv_nsec / NSEC_PER_USEC;
+}