Procházet zdrojové kódy

Add sysdb module to faux

Serj Kalichev před 4 roky
rodič
revize
944e9c2eae
8 změnil soubory, kde provedl 345 přidání a 395 odebrání
  1. 5 6
      faux/Makefile.am
  2. 0 28
      faux/db.h
  3. 0 2
      faux/db/module.am
  4. 26 235
      faux/str.h
  5. 222 111
      faux/str/str.c
  6. 37 0
      faux/sysdb.h
  7. 2 0
      faux/sysdb/Makefile.am
  8. 53 13
      faux/sysdb/sysdb.c

+ 5 - 6
faux/Makefile.am

@@ -5,27 +5,26 @@ libfaux_la_LIBADD =
 nobase_include_HEADERS += \
 	faux/types.h \
 	faux/ctype.h \
-	faux/str.h
+	faux/str.h \
+	faux/sysdb.h
 
 #	faux/argv.h \
 #	faux/list.h \
-#	faux/c_decl.h \
 #	faux/dump.h \
 #	faux/system.h \
-#	faux/db.h \
 #	faux/ini.h \
 #	faux/log.h \
 #	faux/conv.h
 
 EXTRA_DIST += \
 	faux/ctype/Makefile.am \
-	faux/str/Makefile.am
+	faux/str/Makefile.am \
+	faux/sysdb/Makefile.am
 
 #	faux/argv/module.am \
 #	faux/list/module.am \
 #	faux/dump/module.am \
 #	faux/system/module.am \
-#	faux/db/module.am \
 #	faux/ini/module.am \
 #	faux/log/module.am \
 #	faux/conv/module.am \
@@ -33,12 +32,12 @@ EXTRA_DIST += \
 
 include $(top_srcdir)/faux/ctype/Makefile.am
 include $(top_srcdir)/faux/str/Makefile.am
+include $(top_srcdir)/faux/sysdb/Makefile.am
 
 #include $(top_srcdir)/faux/argv/module.am
 #include $(top_srcdir)/faux/list/module.am
 #include $(top_srcdir)/faux/dump/module.am
 #include $(top_srcdir)/faux/system/module.am
-#include $(top_srcdir)/faux/db/module.am
 #include $(top_srcdir)/faux/ini/module.am
 #include $(top_srcdir)/faux/log/module.am
 #include $(top_srcdir)/faux/conv/module.am

+ 0 - 28
faux/db.h

@@ -1,28 +0,0 @@
-#ifndef _lub_passwd_h
-#define _lub_passwd_h
-
-#include <stddef.h>
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif /* HAVE_CONFIG_H */
-
-#ifdef HAVE_PWD_H
-#include <pwd.h>
-#endif
-#ifdef HAVE_GRP_H
-#include <grp.h>
-#endif
-
-/* Wrappers for ugly getpwnam_r()-like functions */
-#ifdef HAVE_PWD_H
-struct passwd *lub_db_getpwnam(const char *name);
-struct passwd *lub_db_getpwuid(uid_t uid);
-#endif
-#ifdef HAVE_GRP_H
-struct group *lub_db_getgrnam(const char *name);
-struct group *lub_db_getgrgid(gid_t gid);
-#endif
-
-#endif
-

+ 0 - 2
faux/db/module.am

@@ -1,2 +0,0 @@
-liblub_la_SOURCES += lub/db/db.c
-

+ 26 - 235
faux/str.h

@@ -1,19 +1,6 @@
-/*
- * string.h
+/** @file str.h
+ * @brief Public interface for faux string functions.
  */
-/**
-\ingroup lub
-\defgroup lub_string string
-@{
-
-\brief This utility provides some simple string manipulation functions which
-augment those found in the standard ANSI-C library.
-
-As a rule of thumb if a function returns "char *" then the calling client becomes responsible for invoking 
-lub_string_free() to release the dynamically allocated memory.
-
-If a "const char *" is returned then the client has no responsiblity for releasing memory.
-*/
 
 #ifndef _faux_str_h
 #define _faux_str_h
@@ -28,190 +15,21 @@ If a "const char *" is returned then the client has no responsiblity for releasi
 #define UTF8_10   0x80 /* Next UTF8 bytes */
 
 C_DECL_BEGIN
-/**
- * This operation duplicates the specified string.
- *
- * \pre 
- * - none
- * 
- * \return 
- * A dynamically allocated string containing the same content as that specified.
- *
- * \post 
- * - The client is responsible for calling lub_string_free() with the
- *   returned string when they are finished using it.
- */
-char *lub_string_dup(
-	/** 
-         * The string to duplicate
-         */
-			    const char *string);
-/**
- * This operation concatinates the specified text onto an existing string.
- *
- * \pre 
- * - 'string_ptr' must contain reference to NULL or to a dynamically 
- *   allocated string.
- * 
- * \post 
- * - The old string referenced by 'string_ptr' will be automatically released
- * - 'string_ptr' will be updated to point to a dynamically allocated string 
- *   containing the concatinated text.
- * - If there is insufficient resource to extend the string then it will not
- *   be extended.
- * - The client maintains responsibility for releasing the string reference
- *   by string_ptr when they are finished using it.
- */
-void lub_string_cat(
-	/** 
-         * A pointer to the string to concatinate
-         */
-			   char **string_ptr,
-	/** 
-         * The text to be appended
-         */
-			   const char *text);
-/**
- * This operation concatinates a specified length of some text onto an
- * existing string.
- *
- * \pre 
- * - 'string_ptr' must contain reference to NULL or to a dynamically allocated
- *   string.
- * 
- * \post 
- * - The old string referenced by 'string_ptr' will be automatically
- *   released.
- * - 'string_ptr' will be updated to point to a dynamically allocated
- *   string containing the concatinated text.
- * - If there is insufficient resource to extend the string then it will not
- *   be extended.
- * - If there length passed in is greater than that of the specified 'text'
- *   then the length of the 'text' will be assumed.
- * - The client maintains responsibility for releasing the string reference
- *   by string_ptr when they are finished using it.
- */
-void lub_string_catn(
-	/** 
-         * A pointer to the string to concatinate
-         */
-			    char **string_ptr,
-	/** 
-         * The text to be appended
-         */
-			    const char *text,
-	/** 
-         * The length of text to be appended
-         */
-			    size_t length);
-/**
- * This operation dupicates a specified length of some text into a
- * new string.
- *
- * \pre 
- * - none
- * 
- * \return 
- * A dynamically allocated string containing the same content as that specified.
- *
- * \post 
- * - The client is responsible for calling lub_string_free() with the
- *   returned string when they are finished using it.
- */
-char *lub_string_dupn(
-	/** 
-         * The string containing the text to duplicate
-         */
-			     const char *string,
-	/** 
-         * The length of text to be duplicated
-         */
-			     unsigned length);
-/**
- * This operation returns a pointer to the last (space separated) word in the
- * specified string.
- *
- * \pre 
- * - none
- * 
- * \return 
- * A pointer to the last word in the string.
- *
- * \post 
- * - none
- */
-const char *lub_string_suffix(
-	/**
-         * The string from which to extract a suffix 
-         */
-				     const char *string);
 
-/**
- * This operation compares string cs to string ct in a case insensitive manner.
- *
- * \pre 
- * - none
- * 
- * \return 
- * - < 0 if cs < ct
- * -   0 if cs == ct
- * - > 0 if cs > ct
- *
- * \post 
- * - none
- */
-int lub_string_nocasecmp(
-	/**
-         * The first string for the comparison
-         */
-				const char *cs,
-	/**
-         * The second string for the comparison 
-         */
-				const char *ct);
-/**
- * This operation performs a case insensitive search for a substring within
- * another string.
- *
- * \pre 
- * - none
- * 
- * \return
- * pointer to first occurance of a case insensitive version of the string ct, 
- * or NULL if not present.
- *
- * \post 
- * - none
- */
-const char *lub_string_nocasestr(
-	/**
-         * The string within which to find a substring
-         */
-					const char *cs,
-	/**
-         * The substring for which to search
-         */
-					const char *ct);
+void faux_str_free(char **str);
 
-/**
- * This operation releases the resources associated with a dynamically allocated
- * string.
- *
- * \pre 
- * - The calling client must have responsibility for the passed string.
- * 
- * \return
- * none
- *
- * \post 
- * - The string is no longer usable, any references to it must be discarded.
- */
-void lub_string_free(
-	/**
-         * The string to be released
-         */
-			    char *string);
+char *faux_str_dupn(const char *str, size_t n);
+char *faux_str_dup(const char *str);
+
+char *faux_str_catn(char **str, const char *text, size_t n);
+char *faux_str_cat(char **str, const char *text);
+
+char *faux_str_tolower(const char *str);
 
+
+//const char *faux_str_suffix(const char *string);
+//int faux_str_nocasecmp(const char *cs, const char *ct);
+//const char *faux_str_nocasestr(const char *cs, const char *ct);
 /*
  * These are the escape characters which are used by default when 
  * expanding variables. These characters will be backslash escaped
@@ -220,46 +38,19 @@ void lub_string_free(
  * This is a security feature to prevent users from arbitarily setting
  * parameters to contain special sequences.
  */
-extern const char *lub_string_esc_default;
-extern const char *lub_string_esc_regex;
-extern const char *lub_string_esc_quoted;
-
-/**
- * This operation decode the escaped string.
- *
- * \pre
- * - none
- *
- * \return
- * - The allocated string without escapes.
- *
- * \post
- * - The result string must be freed after using.
- */
-char *lub_string_decode(const char *string);
-char *lub_string_ndecode(const char *string, unsigned int len);
-
-/**
- * This operation encode the string using escape.
- *
- * \pre
- * - none
- *
- * \return
- * - The allocated string with escapes.
- *
- * \post
- * - The result string must be freed after using.
- */
-char *lub_string_encode(const char *string, const char *escape_chars);
+//extern const char *faux_str_esc_default;
+//extern const char *faux_str_esc_regex;
+//extern const char *faux_str_esc_quoted;
 
-char *lub_string_tolower(const char *str);
-unsigned int lub_string_equal_part(const char *str1, const char *str2,
-	bool_t utf8);
-const char *lub_string_nextword(const char *string,
-	size_t *len, size_t *offset, size_t *quoted);
-unsigned int lub_string_wordcount(const char *line);
+//char *faux_str_decode(const char *string);
+//char *faux_str_ndecode(const char *string, unsigned int len);
+//char *faux_str_encode(const char *string, const char *escape_chars);
+//unsigned int faux_str_equal_part(const char *str1, const char *str2,
+//	bool_t utf8);
+//const char *faux_str_nextword(const char *string,
+//	size_t *len, size_t *offset, size_t *quoted);
+//unsigned int faux_str_wordcount(const char *line);
 
 C_DECL_END
+
 #endif				/* _faux_str_h */
-/** @} */

+ 222 - 111
faux/str/str.c

@@ -12,10 +12,10 @@
 #include "faux/ctype.h"
 #include "faux/str.h"
 
-/* TODO: Are that vars erally needed? */
-const char *lub_string_esc_default = "`|$<>&()#;\\\"!";
-const char *lub_string_esc_regex = "^$.*+[](){}";
-const char *lub_string_esc_quoted = "\\\"";
+/* TODO: Are that vars really needed? */
+//const char *lub_string_esc_default = "`|$<>&()#;\\\"!";
+//const char *lub_string_esc_regex = "^$.*+[](){}";
+//const char *lub_string_esc_quoted = "\\\"";
 
 
 /** @brief Free the memory allocated for the string.
@@ -36,30 +36,170 @@ void faux_str_free(char **str) {
 }
 
 
-/*--------------------------------------------------------- */
-char *lub_string_dup(const char *string)
-{
-	if (!string)
+/** @brief Duplicates the string.
+ *
+ * Duplicates the string. Same as standard strdup() function. Allocates
+ * memory with malloc(). Checks for NULL pointer.
+ *
+ * @warning Resulting string must be freed by faux_str_free().
+ *
+ * @param [in] str String to duplicate.
+ * @return Pointer to allocated string or NULL.
+ */
+char *faux_str_dup(const char *str) {
+
+	if (!str)
 		return NULL;
-	return strdup(string);
+	return strdup(str);
 }
 
-/*--------------------------------------------------------- */
-char *lub_string_dupn(const char *string, unsigned int len)
-{
+
+/** @brief Duplicates the first n bytes of the string.
+ *
+ * Duplicates at most n bytes of the string. Allocates
+ * memory with malloc(). Checks for NULL pointer. Function will allocate
+ * n + 1 bytes to store string and terminating null byte.
+ *
+ * @warning Resulting string must be freed by faux_str_free().
+ *
+ * @param [in] str String to duplicate.
+ * @param [in] n Number of bytes to copy.
+ * @return Pointer to allocated string or NULL.
+ */
+char *faux_str_dupn(const char *str, size_t n) {
+
 	char *res = NULL;
+	size_t len = 0;
 
-	if (!string)
-		return res;
+	if (!str)
+		return NULL;
+	len = strlen(str);
+	len = (len < n) ? len : n;
 	res = malloc(len + 1);
-	strncpy(res, string, len);
+	if (!res)
+		return NULL;
+	strncpy(res, str, len);
 	res[len] = '\0';
 
 	return res;
 }
 
 
-/*--------------------------------------------------------- */
+/** @brief Generates lowercase copy of input string.
+ *
+ * Allocates the copy of input string and convert that copy to lowercase.
+ *
+ * @warning Resulting string must be freed by faux_str_free().
+ *
+ * @param [in] str String to convert.
+ * @return Pointer to lowercase string copy or NULL.
+ */
+char *faux_str_tolower(const char *str)
+{
+	char *res = faux_str_dup(str);
+	char *p = res;
+
+	if (!res)
+		return NULL;
+
+	while (*p) {
+		*p = faux_ctype_tolower(*p);
+		p++;
+	}
+
+	return res;
+}
+
+
+/** @brief Generates uppercase copy of input string.
+ *
+ * Allocates the copy of input string and convert that copy to uppercase.
+ *
+ * @warning Resulting string must be freed by faux_str_free().
+ *
+ * @param [in] str String to convert.
+ * @return Pointer to lowercase string copy or NULL.
+ */
+char *faux_str_toupper(const char *str)
+{
+	char *res = faux_str_dup(str);
+	char *p = res;
+
+	if (!res)
+		return NULL;
+
+	while (*p) {
+		*p = faux_ctype_toupper(*p);
+		p++;
+	}
+
+	return res;
+}
+
+
+/** @brief Add n bytes of text to existent string.
+ *
+ * Concatenate two strings. Add n bytes of second string to the end of the
+ * first one. The first argument is address of string pointer. The pointer
+ * can be changed due to realloc() features. The first pointer can be NULL.
+ * In this case the memory will be malloc()-ed and stored to the first pointer.
+ *
+ * @param [in,out] str Address of first string pointer.
+ * @param [in] text Text to add to the first string.
+ * @param [in] n Number of bytes to add.
+ * @return Pointer to resulting string or NULL.
+ */
+char *faux_str_catn(char **str, const char *text, size_t n) {
+
+	size_t str_len = 0;
+	size_t text_len = 0;
+	char *res = NULL;
+	char *p = NULL;
+
+	if (!text)
+		return *str;
+
+	str_len = (*str) ? strlen(*str) : 0;
+	text_len = strlen(text);
+	text_len = (text_len < n) ? text_len : n;
+
+	res = realloc(*str, str_len + text_len + 1);
+	if (!res)
+		return NULL;
+	p = res + str_len;
+	strncpy(p, text, text_len);
+	p[text_len] = '\0';
+	*str = res;
+
+	return res;
+}
+
+
+/** @brief Add some text to existent string.
+ *
+ * Concatenate two strings. Add second string to the end of the first one.
+ * The first argument is address of string pointer. The pointer can be
+ * changed due to realloc() features. The first pointer can be NULL. In this
+ * case the memory will be malloc()-ed and stored to the first pointer.
+ *
+ * @param [in,out] str Address of first string pointer.
+ * @param [in] text Text to add to the first string.
+ * @return Pointer to resulting string or NULL.
+ */
+char *faux_str_cat(char **str, const char *text) {
+
+	size_t len = 0;
+
+	if (!text)
+		return *str;
+	len = strlen(text);
+
+	return faux_str_catn(str, text, len);
+}
+
+
+// TODO: Is it needed?
+/*
 char *lub_string_ndecode(const char *string, unsigned int len)
 {
 	const char *s = string;
@@ -69,7 +209,6 @@ char *lub_string_ndecode(const char *string, unsigned int len)
 	if (!string)
 		return NULL;
 
-	/* Allocate enough memory for result */
 	p = res = malloc(len + 1);
 
 	while (*s && (s < (string +len))) {
@@ -79,19 +218,19 @@ char *lub_string_ndecode(const char *string, unsigned int len)
 			else
 				*p = *s;
 		} else {
-/*			switch (*s) {
-			case 'r':
-			case 'n':
-				*p = '\n';
-				break;
-			case 't':
-				*p = '\t';
-				break;
-			default:
-				*p = *s;
-				break;
-			}
-*/			*p = *s;
+//			switch (*s) {
+//			case 'r':
+//			case 'n':
+//				*p = '\n';
+//				break;
+//			case 't':
+//				*p = '\t';
+//				break;
+//			default:
+//				*p = *s;
+//				break;
+//			}
+//			*p = *s;
 			esc = 0;
 		}
 		if (!esc)
@@ -102,18 +241,23 @@ char *lub_string_ndecode(const char *string, unsigned int len)
 
 	return res;
 }
+*/
 
-/*--------------------------------------------------------- */
+// TODO: Is it needed?
+/*
 inline char *lub_string_decode(const char *string)
 {
 	return lub_string_ndecode(string, strlen(string));
 }
+*/
 
+// TODO: Is it needed?
 /*----------------------------------------------------------- */
 /*
  * This needs to escape any dangerous characters within the command line
  * to prevent gaining access to the underlying system shell.
  */
+/*
 char *lub_string_encode(const char *string, const char *escape_chars)
 {
 	char *result = NULL;
@@ -121,11 +265,11 @@ char *lub_string_encode(const char *string, const char *escape_chars)
 
 	if (!escape_chars)
 		return lub_string_dup(string);
-	if (string && !(*string)) /* Empty string */
+	if (string && !(*string)) // Empty string
 		return lub_string_dup(string);
 
 	for (p = string; p && *p; p++) {
-		/* find any special characters and prefix them with '\' */
+		// find any special characters and prefix them with '\'
 		size_t len = strcspn(p, escape_chars);
 		lub_string_catn(&result, p, len);
 		p += len;
@@ -138,86 +282,41 @@ char *lub_string_encode(const char *string, const char *escape_chars)
 	}
 	return result;
 }
-
-/*--------------------------------------------------------- */
-void lub_string_catn(char **string, const char *text, size_t len)
-{
-	if (text) {
-		char *q;
-		size_t length, initlen, textlen = strlen(text);
-
-		/* make sure the client cannot give us duff details */
-		len = (len < textlen) ? len : textlen;
-
-		/* remember the size of the original string */
-		initlen = *string ? strlen(*string) : 0;
-
-		/* account for '\0' */
-		length = initlen + len + 1;
-
-		/* allocate the memory for the result */
-		q = realloc(*string, length);
-		if (NULL != q) {
-			*string = q;
-			/* move to the end of the initial string */
-			q += initlen;
-
-			while (len--) {
-				*q++ = *text++;
-			}
-			*q = '\0';
-		}
-	}
-}
-
-/*--------------------------------------------------------- */
-void lub_string_cat(char **string, const char *text)
-{
-	size_t len = text ? strlen(text) : 0;
-	lub_string_catn(string, text, len);
-}
+*/
 
 
+// TODO: Is it needed?
 /*--------------------------------------------------------- */
+/*
 int lub_string_nocasecmp(const char *cs, const char *ct)
 {
 	int result = 0;
 	while ((0 == result) && *cs && *ct) {
-		/*lint -e155 Ignoring { }'ed sequence within an expression, 0 assumed 
-		 * MACRO implementation uses braces to prevent multiple increments
-		 * when called.
-		 */
+		//lint -e155 Ignoring { }'ed sequence within an expression, 0 assumed 
+		// MACRO implementation uses braces to prevent multiple increments
+		// when called.
+		//
 		int s = faux_ctype_tolower(*cs++);
 		int t = faux_ctype_tolower(*ct++);
 
 		result = s - t;
 	}
-	/*lint -e774 Boolean within 'if' always evealuates to True 
-	 * not the case because of tolower() evaluating to 0 under lint
-	 * (see above)
-	 */
+	//lint -e774 Boolean within 'if' always evealuates to True 
+	// not the case because of tolower() evaluating to 0 under lint
+	// (see above)
+	//
 	if (0 == result) {
-		/* account for different string lengths */
+		// account for different string lengths
 		result = *cs - *ct;
 	}
 	return result;
 }
+*/
 
-/*--------------------------------------------------------- */
-char *lub_string_tolower(const char *str)
-{
-	char *tmp = strdup(str);
-	char *p = tmp;
-
-	while (*p) {
-		*p = tolower(*p);
-		p++;
-	}
-
-	return tmp;
-}
+// TODO: Is it needed?
 
 /*--------------------------------------------------------- */
+/*
 const char *lub_string_nocasestr(const char *cs, const char *ct)
 {
 	const char *p = NULL;
@@ -227,14 +326,14 @@ const char *lub_string_nocasestr(const char *cs, const char *ct)
 		const char *q = cs;
 
 		p = ct;
-		/*lint -e155 Ignoring { }'ed sequence within an expression, 0 assumed 
-		 * MACRO implementation uses braces to prevent multiple increments
-		 * when called.
-		 */
-		/*lint -e506 Constant value Boolean
-		 * not the case because of tolower() evaluating to 0 under lint
-		 * (see above)
-		 */
+		//lint -e155 Ignoring { }'ed sequence within an expression, 0 assumed 
+		// MACRO implementation uses braces to prevent multiple increments
+		// when called.
+		//
+		//lint -e506 Constant value Boolean
+		// not the case because of tolower() evaluating to 0 under lint
+		// (see above)
+		//
 		while (*p && *q
 		       && (faux_ctype_tolower(*p) == faux_ctype_tolower(*q))) {
 			p++, q++;
@@ -245,13 +344,16 @@ const char *lub_string_nocasestr(const char *cs, const char *ct)
 		cs++;
 	}
 	if (p && !*p) {
-		/* we've found the first match of ct within cs */
+		// we've found the first match of ct within cs
 		result = cs;
 	}
 	return result;
 }
+*/
 
+// TODO: Is it needed?
 /*--------------------------------------------------------- */
+/*
 unsigned int lub_string_equal_part(const char *str1, const char *str2,
 	bool_t utf8)
 {
@@ -269,14 +371,18 @@ unsigned int lub_string_equal_part(const char *str1, const char *str2,
 	if (!utf8)
 		return cnt;
 
-	/* UTF8 features */
+	// UTF8 features
 	if (cnt && (UTF8_11 == (*(str1 - 1) & UTF8_MASK)))
 		cnt--;
 
 	return cnt;
 }
+*/
+
+// TODO: Is it needed?
 
 /*--------------------------------------------------------- */
+/*
 const char *lub_string_suffix(const char *string)
 {
 	const char *p1, *p2;
@@ -290,8 +396,11 @@ const char *lub_string_suffix(const char *string)
 	}
 	return p2;
 }
+*/
 
+// TODO: Is it needed?
 /*--------------------------------------------------------- */
+/*
 const char *lub_string_nextword(const char *string,
 	size_t *len, size_t *offset, size_t *quoted)
 {
@@ -299,12 +408,12 @@ const char *lub_string_nextword(const char *string,
 
 	*quoted = 0;
 
-	/* Find the start of a word (not including an opening quote) */
+	// Find the start of a word (not including an opening quote)
 	while (*string && isspace(*string)) {
 		string++;
 		(*offset)++;
 	}
-	/* Is this the start of a quoted string ? */
+	// Is this the start of a quoted string ?
 	if (*string == '"') {
 		*quoted = 1;
 		string++;
@@ -312,7 +421,7 @@ const char *lub_string_nextword(const char *string,
 	word = string;
 	*len = 0;
 
-	/* Find the end of the word */
+	// Find the end of the word
 	while (*string) {
 		if (*string == '\\') {
 			string++;
@@ -323,11 +432,11 @@ const char *lub_string_nextword(const char *string,
 			}
 			continue;
 		}
-		/* End of word */
+		// End of word
 		if (!*quoted && isspace(*string))
 			break;
 		if (*string == '"') {
-			/* End of a quoted string */
+			// End of a quoted string
 			*quoted = 2;
 			break;
 		}
@@ -337,8 +446,11 @@ const char *lub_string_nextword(const char *string,
 
 	return word;
 }
+*/
 
+// TODO: Is it needed?
 /*--------------------------------------------------------- */
+/*
 unsigned int lub_string_wordcount(const char *line)
 {
 	const char *word;
@@ -349,12 +461,11 @@ unsigned int lub_string_wordcount(const char *line)
 	for (word = lub_string_nextword(line, &len, &offset, &quoted);
 		*word || quoted;
 		word = lub_string_nextword(word + len, &len, &offset, &quoted)) {
-		/* account for the terminating quotation mark */
+		// account for the terminating quotation mark
 		len += quoted ? quoted - 1 : 0;
 		result++;
 	}
 
 	return result;
 }
-
-/*--------------------------------------------------------- */
+*/

+ 37 - 0
faux/sysdb.h

@@ -0,0 +1,37 @@
+/** @file sysdb.h
+ * @brief Public interface for faux system database (passwd, group etc) functions.
+ */
+
+#ifndef _faux_sysdb_h
+#define _faux_sysdb_h
+
+#include <stddef.h>
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+#ifdef HAVE_PWD_H
+#include <pwd.h>
+#endif
+#ifdef HAVE_GRP_H
+#include <grp.h>
+#endif
+
+#include "faux/types.h"
+
+C_DECL_BEGIN
+
+// Wrappers for ugly getpwnam_r()-like functions
+#ifdef HAVE_PWD_H
+struct passwd *faux_sysdb_getpwnam(const char *name);
+struct passwd *faux_sysdb_getpwuid(uid_t uid);
+#endif
+#ifdef HAVE_GRP_H
+struct group *faux_sysdb_getgrnam(const char *name);
+struct group *faux_sysdb_getgrgid(gid_t gid);
+#endif
+
+C_DECL_END
+
+#endif

+ 2 - 0
faux/sysdb/Makefile.am

@@ -0,0 +1,2 @@
+libfaux_la_SOURCES += \
+	faux/sysdb/sysdb.c

+ 53 - 13
faux/db/db.c → faux/sysdb/sysdb.c

@@ -1,5 +1,11 @@
-/* It must be here to include config.h before another headers */
-#include "lub/db.h"
+/** @file sysdb.c
+ * @brief Wrappers for system database functions like getpwnam(), getgrnam().
+ */
+
+// It must be here to include config.h before another headers
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
 
 #include <stdlib.h>
 #include <errno.h>
@@ -8,13 +14,23 @@
 #include <grp.h>
 #include <unistd.h>
 
+#include "faux/sysdb.h"
+
 #define DEFAULT_GETPW_R_SIZE_MAX 1024
 
-struct passwd *lub_db_getpwnam(const char *name)
-{
+/** @brief Wrapper for ugly getpwnam_r() function.
+ *
+ * Gets passwd structure by user name. Easy to use.
+ *
+ * @param [in] name User name.
+ * @return Pointer to allocated passwd structure.
+ * @warning The resulting pointer (return value) must be freed by free().
+ */
+struct passwd *faux_sysdb_getpwnam(const char *name) {
+
 	long int size;
 	char *buf;
-	struct passwd *pwbuf; 
+	struct passwd *pwbuf;
 	struct passwd *pw = NULL;
 	int res = 0;
 
@@ -28,7 +44,7 @@ struct passwd *lub_db_getpwnam(const char *name)
 	if (!pwbuf)
 		return NULL;
 	buf = (char *)pwbuf + sizeof(*pwbuf);
-	
+
 	res = getpwnam_r(name, pwbuf, buf, size, &pw);
 
 	if (res || !pw) {
@@ -43,8 +59,16 @@ struct passwd *lub_db_getpwnam(const char *name)
 	return pwbuf;
 }
 
-struct passwd *lub_db_getpwuid(uid_t uid)
-{
+/** @brief Wrapper for ugly getpwuid_r() function.
+ *
+ * Gets passwd structure by UID. Easy to use.
+ *
+ * @param [in] uid UID.
+ * @return Pointer to allocated passwd structure.
+ * @warning The resulting pointer (return value) must be freed by free().
+ */
+struct passwd *faux_sysdb_getpwuid(uid_t uid) {
+
 	long int size;
 	char *buf;
 	struct passwd *pwbuf; 
@@ -61,7 +85,7 @@ struct passwd *lub_db_getpwuid(uid_t uid)
 	if (!pwbuf)
 		return NULL;
 	buf = (char *)pwbuf + sizeof(*pwbuf);
-	
+
 	res = getpwuid_r(uid, pwbuf, buf, size, &pw);
 
 	if (NULL == pw) {
@@ -76,8 +100,16 @@ struct passwd *lub_db_getpwuid(uid_t uid)
 	return pwbuf;
 }
 
-struct group *lub_db_getgrnam(const char *name)
-{
+/** @brief Wrapper for ugly getgrnam_r() function.
+ *
+ * Gets group structure by group name. Easy to use.
+ *
+ * @param [in] name Group name.
+ * @return Pointer to allocated group structure.
+ * @warning The resulting pointer (return value) must be freed by free().
+ */
+struct group *faux_sysdb_getgrnam(const char *name) {
+
 	long int size;
 	char *buf;
 	struct group *grbuf; 
@@ -109,8 +141,16 @@ struct group *lub_db_getgrnam(const char *name)
 	return grbuf;
 }
 
-struct group *lub_db_getgrgid(gid_t gid)
-{
+/** @brief Wrapper for ugly getgrgid_r() function.
+ *
+ * Gets group structure by GID. Easy to use.
+ *
+ * @param [in] gid GID.
+ * @return Pointer to allocated group structure.
+ * @warning The resulting pointer (return value) must be freed by free().
+ */
+struct group *faux_sysdb_getgrgid(gid_t gid) {
+
 	long int size;
 	char *buf;
 	struct group *grbuf;