summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrzysztof Opasiak <k.opasiak@samsung.com>2015-12-22 15:05:51 +0100
committerKrzysztof Opasiak <k.opasiak@samsung.com>2015-12-23 00:49:03 +0100
commitb7a6f9f7fa1476dc6c48488ad47e73cfe93b1763 (patch)
tree6e59ae9a7057434698e503a99379238c63850b7e
parent0565d795b3137af9a5c01a4fb1b40296ed4b1fdb (diff)
downloadlibusbg-b7a6f9f7fa1476dc6c48488ad47e73cfe93b1763.tar.gz
libusbg-b7a6f9f7fa1476dc6c48488ad47e73cfe93b1763.tar.bz2
libusbg-b7a6f9f7fa1476dc6c48488ad47e73cfe93b1763.zip
libusbgx: Replace *_cpy_*() functions with *_s() convention
Instead of having _cpy_ and _len function for getting each string value let's use *_s() which behave like snprintf() family. Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
-rw-r--r--include/usbg/usbg.h127
-rw-r--r--src/usbg.c82
2 files changed, 66 insertions, 143 deletions
diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
index 502997d..42158e3 100644
--- a/include/usbg/usbg.h
+++ b/include/usbg/usbg.h
@@ -262,20 +262,15 @@ extern void usbg_cleanup(usbg_state *s);
extern const char *usbg_get_configfs_path(usbg_state *s);
/**
- * @brief Get ConfigFS path length
- * @param s Pointer to state
- * @return Length of path or usbg_error if error occurred.
- */
-extern size_t usbg_get_configfs_path_len(usbg_state *s);
-
-/**
- * @brief Copy ConfigFS path to buffer
- * @param s Pointer to state
- * @param buf Buffer where path should be copied
- * @param len Length of given buffer
- * @return 0 on success or usbg_error if error occurred.
+ * @brief Get ConfigFS path into user buffer
+ * @param[in] s Pointer to state
+ * @param[out] buf Place where path should be stored
+ * @param[in] len Size of buffer
+ * @return Number of bytes placed into buf (excluding '\0') or the number of
+ * characters which would have been written to the buffer if enough
+ * space had been available. (just like snprintf() family).
*/
-extern int usbg_cpy_configfs_path(usbg_state *s, char *buf, size_t len);
+extern int usbg_get_configfs_path_s(usbg_state *s, char *buf, int len);
/* USB gadget queries */
@@ -478,20 +473,15 @@ extern int usbg_get_gadget_attrs(usbg_gadget *g, usbg_gadget_attrs *g_attrs);
extern const char *usbg_get_gadget_name(usbg_gadget *g);
/**
- * @brief Get gadget name length
- * @param g Gadget which name length should be returned
- * @return Length of name string or usbg_error if error occurred.
- */
-extern size_t usbg_get_gadget_name_len(usbg_gadget *g);
-
-/**
- * @brief Copy gadget name
- * @param g Pointer to gadget
- * @param buf Buffer where name should be copied
- * @param len Length of given buffer
- * @return 0 on success or usbg_error if error occurred.
+ * @brief Get gadget name into user buffer
+ * @param[in] g Pointer to state
+ * @param[out] buf Place where name should be stored
+ * @param[in] len Size of buffer
+ * @return Number of bytes placed into buf (excluding '\0') or the number of
+ * characters which would have been written to the buffer if enough
+ * space had been available. (just like snprintf() family).
*/
-extern int usbg_cpy_gadget_name(usbg_gadget *g, char *buf, size_t len);
+extern int usbg_get_gadget_name_s(usbg_gadget *g, char *buf, int len);
/**
* @brief Set the USB gadget vendor id
@@ -649,20 +639,15 @@ extern int usbg_create_function(usbg_gadget *g, usbg_function_type type,
extern const char *usbg_get_function_instance(usbg_function *f);
/**
- * @brief Get function instance name length
- * @param f function which name length should be returned
- * @return Length of name string or usbg_error if error occurred.
- */
-extern size_t usbg_get_function_instance_len(usbg_function *f);
-
-/**
- * @brief Copy function instance name
- * @param f Pointer to function
- * @param buf Buffer where instance name should be copied
- * @param len Length of given buffer
- * @return 0 on success or usbg_error if error occurred.
+ * @brief Get function instance name into user buffer
+ * @param[in] f Pointer to function
+ * @param[out] buf Place where instance name should be stored
+ * @param[in] len Size of buffer
+ * @return Number of bytes placed into buf (excluding '\0') or the number of
+ * characters which would have been written to the buffer if enough
+ * space had been available. (just like snprintf() family).
*/
-extern int usbg_cpy_function_instance(usbg_function *f, char *buf, size_t len);
+extern int usbg_get_function_instance_s(usbg_function *f, char *buf, int len);
/**
* @brief Get function type as a string
@@ -744,20 +729,15 @@ extern int usbg_create_config(usbg_gadget *g, int id, const char *label,
extern const char *usbg_get_config_label(usbg_config *c);
/**
- * @brief Get config label length
- * @param c Config which label length should be returned
- * @return Length of label or usbg_error if error occurred.
+ * @brief Get config label into user buffer
+ * @param[in] c Pointer to config
+ * @param[out] buf Place where label should be stored
+ * @param[in] len Size of buffer
+ * @return Number of bytes placed into buf (excluding '\0') or the number of
+ * characters which would have been written to the buffer if enough
+ * space had been available. (just like snprintf() family).
*/
-extern size_t usbg_get_config_label_len(usbg_config *c);
-
-/**
- * @brief Copy config label
- * @param c Pointer to config
- * @param buf Buffer where label should be copied
- * @param len Length of given buffer
- * @return 0 on success or usbg_error if error occurred.
- */
-extern int usbg_cpy_config_label(usbg_config *c, char *buf, size_t len);
+extern int usbg_get_config_label_s(usbg_config *c, char *buf, int len);
/**
* @brief Get config id
@@ -856,20 +836,15 @@ extern usbg_function *usbg_get_binding_target(usbg_binding *b);
extern const char *usbg_get_binding_name(usbg_binding *b);
/**
- * @brief Get binding name length
- * @param b Binding which name length should be returned
- * @return Length of name string or usbg_error if error occurred.
+ * @brief Get binding name into user buffer
+ * @param[in] b Pointer to binding
+ * @param[out] buf Place where binding name should be stored
+ * @param[in] len Size of buffer
+ * @return Number of bytes placed into buf (excluding '\0') or the number of
+ * characters which would have been written to the buffer if enough
+ * space had been available. (just like snprintf() family).
*/
-extern size_t usbg_get_binding_name_len(usbg_binding *b);
-
-/**
- * @brief Copy binding name
- * @param b Pointer to binding
- * @param buf Buffer where name should be copied
- * @param len Length of given buffer
- * @return 0 on success or usbg_error if error occurred.
- */
-extern int usbg_cpy_binding_name(usbg_binding *b, char *buf, size_t len);
+extern int usbg_get_binding_name_s(usbg_binding *b, char *buf, int len);
/* USB gadget setup and teardown */
@@ -900,21 +875,15 @@ extern int usbg_disable_gadget(usbg_gadget *g);
extern const char *usbg_get_udc_name(usbg_udc *u);
/**
- * @brief Get gadget name length
- * @param g Gadget which name length should be returned
- * @return Length of name string or usbg_error if error occurred.
- * @note If gadget isn't enabled on any udc returned size is 0.
- */
-extern size_t usbg_get_gadget_udc_len(usbg_gadget *g);
-
-/**
- * @brief Copy name of udc
- * @param u Pointer to udc
- * @param buf Buffer where udc name should be copied
- * @param len Length of given buffer
- * @return 0 on success or usbg_error if error occurred.
+ * @brief Get udc name into user buffer
+ * @param[in] u Pointer to udc
+ * @param[out] buf Place where udc name should be stored
+ * @param[in] len Size of buffer
+ * @return Number of bytes placed into buf (excluding '\0') or the number of
+ * characters which would have been written to the buffer if enough
+ * space had been available. (just like snprintf() family).
*/
-extern int usbg_cpy_udc_name(usbg_udc *u, char *buf, size_t len);
+extern int usbg_get_udc_name_s(usbg_udc *u, char *buf, int len);
/**
* @brief Get udc to which gadget is bound
diff --git a/src/usbg.c b/src/usbg.c
index 636c52f..d99e8e2 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -987,20 +987,12 @@ const char *usbg_get_configfs_path(usbg_state *s)
return s ? s->configfs_path : NULL;
}
-size_t usbg_get_configfs_path_len(usbg_state *s)
+int usbg_get_configfs_path_s(usbg_state *s, char *buf, int len)
{
- return s ? strlen(s->configfs_path) : USBG_ERROR_INVALID_PARAM;
-}
-
-int usbg_cpy_configfs_path(usbg_state *s, char *buf, size_t len)
-{
- if (!s || !buf || len == 0)
+ if (!s || !buf)
return USBG_ERROR_INVALID_PARAM;
- buf[--len] = '\0';
- strncpy(buf, s->configfs_path, len);
-
- return USBG_SUCCESS;
+ return snprintf(buf, len, "%s", s->configfs_path);
}
usbg_gadget *usbg_get_gadget(usbg_state *s, const char *name)
@@ -1425,20 +1417,12 @@ const char *usbg_get_gadget_name(usbg_gadget *g)
return g ? g->name : NULL;
}
-size_t usbg_get_gadget_name_len(usbg_gadget *g)
-{
- return g ? strlen(g->name) : USBG_ERROR_INVALID_PARAM;
-}
-
-int usbg_cpy_gadget_name(usbg_gadget *g, char *buf, size_t len)
+int usbg_get_gadget_name_s(usbg_gadget *g, char *buf, int len)
{
- if (!g || !buf || len == 0)
+ if (!g || !buf)
return USBG_ERROR_INVALID_PARAM;
- buf[--len] = '\0';
- strncpy(buf, g->name, len);
-
- return USBG_SUCCESS;
+ return snprintf(buf, len, "%s", g->name);
}
const char *usbg_get_udc_name(usbg_udc *u)
@@ -1446,20 +1430,13 @@ const char *usbg_get_udc_name(usbg_udc *u)
return u ? u->name : NULL;
}
-size_t usbg_get_udc_name_len(usbg_udc *u)
+int usbg_get_udc_name_s(usbg_udc *u, char *buf, int len)
{
- return u ? strlen(u->name) : USBG_ERROR_INVALID_PARAM;
-}
-
-int usbg_cpy_udc_name(usbg_udc *u, char *buf, size_t len)
-{
- if (!u || !buf || len == 0)
+ if (!u || !buf)
return USBG_ERROR_INVALID_PARAM;
- buf[--len] = '\0';
- strncpy(buf, u->name, len);
+ return snprintf(buf, len, "%s", u->name);
- return USBG_SUCCESS;
}
int usbg_set_gadget_attr(usbg_gadget *g, usbg_gadget_attr attr, int val)
@@ -1940,20 +1917,13 @@ const char *usbg_get_config_label(usbg_config *c)
return c ? c->label : NULL;
}
-size_t usbg_get_config_label_len(usbg_config *c)
+int usbg_get_config_label_s(usbg_config *c, char *buf, int len)
{
- return c ? strlen(c->label) : USBG_ERROR_INVALID_PARAM;
-}
-
-int usbg_cpy_config_label(usbg_config *c, char *buf, size_t len)
-{
- if (!c || !buf || len == 0)
+ if (!c || !buf)
return USBG_ERROR_INVALID_PARAM;
- buf[--len] = '\0';
- strncpy(buf, c->label, len);
+ return snprintf(buf, len, "%s", c->label);
- return USBG_SUCCESS;
}
int usbg_get_config_id(usbg_config *c)
@@ -1966,20 +1936,12 @@ const char *usbg_get_function_instance(usbg_function *f)
return f ? f->instance : NULL;
}
-size_t usbg_get_function_instance_len(usbg_function *f)
-{
- return f ? strlen(f->instance) : USBG_ERROR_INVALID_PARAM;
-}
-
-int usbg_cpy_function_instance(usbg_function *f, char *buf, size_t len)
+int usbg_get_function_instance_s(usbg_function *f, char *buf, int len)
{
- if (!f || !buf || len == 0)
+ if (!f || !buf)
return USBG_ERROR_INVALID_PARAM;
- buf[--len] = '\0';
- strncpy(buf, f->instance, len);
-
- return USBG_SUCCESS;
+ return snprintf(buf, len, "%s", f->instance);
}
int usbg_set_config_attrs(usbg_config *c, const usbg_config_attrs *c_attrs)
@@ -2142,20 +2104,12 @@ const char *usbg_get_binding_name(usbg_binding *b)
return b ? b->name : NULL;
}
-size_t usbg_get_binding_name_len(usbg_binding *b)
+int usbg_get_binding_name_s(usbg_binding *b, char *buf, int len)
{
- return b ? strlen(b->name) : USBG_ERROR_INVALID_PARAM;
-}
-
-int usbg_cpy_binding_name(usbg_binding *b, char *buf, size_t len)
-{
- if (!b || !buf || len == 0)
+ if (!b || !buf)
return USBG_ERROR_INVALID_PARAM;
- buf[--len] = '\0';
- strncpy(buf, b->name, len);
-
- return USBG_SUCCESS;
+ return snprintf(buf, len, "%s", b->name);
}
int usbg_enable_gadget(usbg_gadget *g, usbg_udc *udc)