*
* 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>
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;
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;
}
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 {
}
/* if space is allowed, replace whitespace with ordinary space */
- if (isspace(str[i]) && strchr(white, ' ') != NULL) {
+ if (isspace(str[i]) && white != NULL && strchr(white, ' ') != NULL) {
str[i] = ' ';
i++;
replaced++;
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;
+}