chiark / gitweb /
rule-generator: net - whitelist NICs that violate MAC local scheme
[elogind.git] / udev / lib / libudev-util.c
index 1cd18c890696968e8684dc0610486174b5277210..2b8cda59f6e30cb41b2d6c6bfee3732f5051fe45 100644 (file)
@@ -3,18 +3,10 @@
  *
  * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
  *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  */
 
 #include <stdio.h>
@@ -109,12 +101,12 @@ int util_log_priority(const char *priority)
        return 0;
 }
 
-size_t util_path_encode(char *s, size_t len)
+size_t util_path_encode(char *s, size_t size)
 {
-       char t[(len * 3)+1];
+       char t[(size * 4)+1];
        size_t i, j;
 
-       for (i = 0, j = 0; s[i] != '\0'; i++) {
+       for (i = 0, j = 0; s[i] != '\0' && i < size; i++) {
                if (s[i] == '/') {
                        memcpy(&t[j], "\\x2f", 4);
                        j += 4;
@@ -126,11 +118,12 @@ size_t util_path_encode(char *s, size_t len)
                        j++;
                }
        }
-       if (len == 0)
-               return j;
-       i = (j < len - 1) ? j : len - 1;
-       memcpy(s, t, i);
-       s[i] = '\0';
+       if (i >= size)
+               return 0;
+       if (j >= size)
+               return 0;
+       memcpy(s, t, j);
+       s[j] = '\0';
        return j;
 }
 
@@ -142,7 +135,7 @@ size_t util_path_decode(char *s)
                if (memcmp(&s[i], "\\x2f", 4) == 0) {
                        s[j] = '/';
                        i += 4;
-               }else if (memcmp(&s[i], "\\x5c", 4) == 0) {
+               } else if (memcmp(&s[i], "\\x5c", 4) == 0) {
                        s[j] = '\\';
                        i += 4;
                } else {
@@ -456,3 +449,28 @@ int udev_util_encode_string(const char *str, char *str_enc, size_t len)
 err:
        return -1;
 }
+
+void util_set_fd_cloexec(int fd)
+{
+       int flags;
+
+       flags = fcntl(fd, F_GETFD);
+       if (flags < 0)
+               flags = FD_CLOEXEC;
+       else
+               flags |= FD_CLOEXEC;
+       fcntl(fd, F_SETFD, flags);
+}
+
+unsigned int util_string_hash32(const char *str)
+{
+       unsigned int hash = 0;
+
+       while (str[0] != '\0') {
+               hash += str[0] << 4;
+               hash += str[0] >> 4;
+               hash *= 11;
+               str++;
+       }
+       return hash;
+}