[PATCH 2/3] libusbg: Add missing const qualifiers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Const qualifers should be placed in all functions which
don't need to modify provided buffers to allow caller to
pass literals or pointers to non-modifable space.

Signed-off-by: Krzysztof Opasiak <k.opasiak@xxxxxxxxxxx>
---
 include/usbg/usbg.h |   28 +++++++++++-------
 src/usbg.c          |   79 +++++++++++++++++++++++++++------------------------
 2 files changed, 59 insertions(+), 48 deletions(-)

diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
index 5509cdb..a44203a 100644
--- a/include/usbg/usbg.h
+++ b/include/usbg/usbg.h
@@ -240,7 +240,7 @@ extern const char *usbg_strerror(usbg_error e);
  * @param Pointer to be filled with pointer to usbg_state
  * @return 0 on success, usbg_error on error
  */
-extern int usbg_init(char *configfs_path, usbg_state **state);
+extern int usbg_init(const char *configfs_path, usbg_state **state);
 
 /**
  * @brief Clean up the libusbg library state
@@ -360,7 +360,7 @@ extern int usbg_rm_gadget_strs(usbg_gadget *g, int lang);
  * @param g Pointer to be filled with pointer to gadget
  * @return 0 on success usbg_error if error occurred
  */
-extern int usbg_create_gadget_vid_pid(usbg_state *s, char *name,
+extern int usbg_create_gadget_vid_pid(usbg_state *s, const char *name,
 		uint16_t idVendor, uint16_t idProduct, usbg_gadget **g);
 
 /**
@@ -374,8 +374,9 @@ extern int usbg_create_gadget_vid_pid(usbg_state *s, char *name,
  * @note Given strings are assumed to be in US English
  * @return 0 on success usbg_error if error occurred
  */
-extern int usbg_create_gadget(usbg_state *s, char *name,
-		usbg_gadget_attrs *g_attrs, usbg_gadget_strs *g_strs, usbg_gadget **g);
+extern int usbg_create_gadget(usbg_state *s, const char *name,
+		usbg_gadget_attrs *g_attrs, usbg_gadget_strs *g_strs,
+			      usbg_gadget **g);
 
 /**
  * @brief Set the USB gadget attributes
@@ -506,7 +507,8 @@ extern int usbg_set_gadget_strs(usbg_gadget *g, int lang,
  * @param ser Serial number
  * @return 0 on success usbg_error if error occurred
  */
-extern int usbg_set_gadget_serial_number(usbg_gadget *g, int lang, char *ser);
+extern int usbg_set_gadget_serial_number(usbg_gadget *g, int lang,
+					 const char *ser);
 
 /**
  * @brief Set the manufacturer name for a gadget
@@ -515,7 +517,8 @@ extern int usbg_set_gadget_serial_number(usbg_gadget *g, int lang, char *ser);
  * @param mnf Manufacturer
  * @return 0 on success usbg_error if error occurred
  */
-extern int usbg_set_gadget_manufacturer(usbg_gadget *g, int lang, char *mnf);
+extern int usbg_set_gadget_manufacturer(usbg_gadget *g, int lang,
+					const char *mnf);
 
 /**
  * @brief Set the product name for a gadget
@@ -524,7 +527,8 @@ extern int usbg_set_gadget_manufacturer(usbg_gadget *g, int lang, char *mnf);
  * @param prd Product
  * @return 0 on success usbg_error if error occurred
  */
-extern int usbg_set_gadget_product(usbg_gadget *g, int lang, char *prd);
+extern int usbg_set_gadget_product(usbg_gadget *g, int lang,
+				   const char *prd);
 
 /* USB function allocation and configuration */
 
@@ -539,7 +543,8 @@ extern int usbg_set_gadget_product(usbg_gadget *g, int lang, char *prd);
  * @return 0 on success usbg_error if error occurred
  */
 extern int usbg_create_function(usbg_gadget *g, usbg_function_type type,
-		char *instance, usbg_function_attrs *f_attrs, usbg_function **f);
+		 const char *instance, usbg_function_attrs *f_attrs,
+				usbg_function **f);
 
 /**
  * @brief Get function instance name length
@@ -663,7 +668,7 @@ extern int usbg_set_config_strs(usbg_config *c, int lang,
  * @param string Configuration description
  * @return 0 on success, usbg_error on failure.
  */
-extern int usbg_set_config_string(usbg_config *c, int lang, char *string);
+extern int usbg_set_config_string(usbg_config *c, int lang, const char *string);
 
 /**
  * @brief Add a function to a configuration
@@ -672,7 +677,8 @@ extern int usbg_set_config_string(usbg_config *c, int lang, char *string);
  * @param f Pointer to function
  * @return 0 on success, usbg_error on failure.
  */
-extern int usbg_add_config_function(usbg_config *c, char *name, usbg_function *f);
+extern int usbg_add_config_function(usbg_config *c, const char *name,
+				    usbg_function *f);
 
 /**
  * @brief Get target function of given binding
@@ -712,7 +718,7 @@ extern int usbg_get_udcs(struct dirent ***udc_list);
  * @param udc Name of UDC to enable gadget
  * @return 0 on success or usbg_error if error occurred.
  */
-extern int usbg_enable_gadget(usbg_gadget *g, char *udc);
+extern int usbg_enable_gadget(usbg_gadget *g, const char *udc);
 
 /**
  * @brief Disable a USB gadget device
diff --git a/src/usbg.c b/src/usbg.c
index 764b4b7..9e71067 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -269,7 +269,7 @@ const char *usbg_strerror(usbg_error e)
 	return ret;
 }
 
-static int usbg_lookup_function_type(char *name)
+static int usbg_lookup_function_type(const char *name)
 {
 	int i = 0;
 	int max = sizeof(function_names)/sizeof(char *);
@@ -378,7 +378,8 @@ static int file_select(const struct dirent *dent)
 		return 1;
 }
 
-static int usbg_read_buf(char *path, char *name, char *file, char *buf)
+static int usbg_read_buf(const char *path, const char *name, const char *file,
+			 char *buf)
 {
 	char p[USBG_MAX_PATH_LENGTH];
 	FILE *fp;
@@ -407,8 +408,8 @@ static int usbg_read_buf(char *path, char *name, char *file, char *buf)
 	return ret;
 }
 
-static int usbg_read_int(char *path, char *name, char *file, int base,
-		int *dest)
+static int usbg_read_int(const char *path, const char *name, const char *file,
+			 int base, int *dest)
 {
 	char buf[USBG_MAX_STR_LENGTH];
 	char *pos;
@@ -427,7 +428,8 @@ static int usbg_read_int(char *path, char *name, char *file, int base,
 #define usbg_read_dec(p, n, f, d)	usbg_read_int(p, n, f, 10, d)
 #define usbg_read_hex(p, n, f, d)	usbg_read_int(p, n, f, 16, d)
 
-static int usbg_read_string(char *path, char *name, char *file, char *buf)
+static int usbg_read_string(const char *path, const char *name,
+			    const char *file, char *buf)
 {
 	char *p = NULL;
 	int ret;
@@ -445,7 +447,8 @@ static int usbg_read_string(char *path, char *name, char *file, char *buf)
 	return ret;
 }
 
-static int usbg_write_buf(char *path, char *name, char *file, char *buf)
+static int usbg_write_buf(const char *path, const char *name, const char *file,
+			  const char *buf)
 {
 	char p[USBG_MAX_PATH_LENGTH];
 	FILE *fp;
@@ -475,8 +478,8 @@ static int usbg_write_buf(char *path, char *name, char *file, char *buf)
 	return ret;
 }
 
-static int usbg_write_int(char *path, char *name, char *file, int value,
-		char *str)
+static int usbg_write_int(const char *path, const char *name, const char *file,
+			  int value, const char *str)
 {
 	char buf[USBG_MAX_STR_LENGTH];
 	int nmb;
@@ -491,8 +494,8 @@ static int usbg_write_int(char *path, char *name, char *file, int value,
 #define usbg_write_hex16(p, n, f, v)	usbg_write_int(p, n, f, v, "0x%04x\n")
 #define usbg_write_hex8(p, n, f, v)	usbg_write_int(p, n, f, v, "0x%02x\n")
 
-static inline int usbg_write_string(char *path, char *name, char *file,
-		char *buf)
+static inline int usbg_write_string(const char *path, const char *name,
+				    const char *file, const char *buf)
 {
 	return usbg_write_buf(path, name, file, buf);
 }
@@ -557,7 +560,7 @@ static void usbg_free_state(usbg_state *s)
 	free(s);
 }
 
-static usbg_gadget *usbg_allocate_gadget(char *path, char *name,
+static usbg_gadget *usbg_allocate_gadget(const char *path, const char *name,
 		usbg_state *parent)
 {
 	usbg_gadget *g;
@@ -657,7 +660,7 @@ out:
 	return f;
 }
 
-static usbg_binding *usbg_allocate_binding(char *path, char *name,
+static usbg_binding *usbg_allocate_binding(const char *path, const char *name,
 		usbg_config *parent)
 {
 	usbg_binding *b;
@@ -679,7 +682,7 @@ static usbg_binding *usbg_allocate_binding(char *path, char *name,
 	return b;
 }
 
-static int ubsg_rm_file(char *path, char *name)
+static int ubsg_rm_file(const char *path, const char *name)
 {
 	int ret = USBG_SUCCESS;
 	int nmb;
@@ -697,7 +700,7 @@ static int ubsg_rm_file(char *path, char *name)
 	return ret;
 }
 
-static int usbg_rm_dir(char *path, char *name)
+static int usbg_rm_dir(const char *path, const char *name)
 {
 	int ret = USBG_SUCCESS;
 	int nmb;
@@ -715,7 +718,7 @@ static int usbg_rm_dir(char *path, char *name)
 	return ret;
 }
 
-static int usbg_rm_all_dirs(char *path)
+static int usbg_rm_all_dirs(const char *path)
 {
 	int ret = USBG_SUCCESS;
 	int n, i;
@@ -815,7 +818,7 @@ static int usbg_parse_function_attrs(usbg_function *f,
 	return ret;
 }
 
-static int usbg_parse_functions(char *path, usbg_gadget *g)
+static int usbg_parse_functions(const char *path, usbg_gadget *g)
 {
 	usbg_function *f;
 	int i, n;
@@ -860,7 +863,7 @@ out:
 	return ret;
 }
 
-static int usbg_parse_config_attrs(char *path, char *name,
+static int usbg_parse_config_attrs(const char *path, const char *name,
 		usbg_config_attrs *c_attrs)
 {
 	int buf, ret;
@@ -877,7 +880,7 @@ static int usbg_parse_config_attrs(char *path, char *name,
 	return ret;
 }
 
-static int usbg_parse_config_strs(char *path, char *name,
+static int usbg_parse_config_strs(const char *path, const char *name,
 		int lang, usbg_config_strs *c_strs)
 {
 	DIR *dir;
@@ -904,8 +907,7 @@ static int usbg_parse_config_strs(char *path, char *name,
 	return ret;
 }
 
-static int usbg_parse_config_binding(usbg_config *c, char *bpath,
-		int path_size)
+static int usbg_parse_config_binding(usbg_config *c, char *bpath, int path_size)
 {
 	int nmb;
 	int ret;
@@ -1019,7 +1021,7 @@ out:
 	return ret;
 }
 
-static int usbg_parse_configs(char *path, usbg_gadget *g)
+static int usbg_parse_configs(const char *path, usbg_gadget *g)
 {
 	int i, n;
 	int ret = USBG_SUCCESS;
@@ -1051,7 +1053,7 @@ out:
 	return ret;
 }
 
-static int usbg_parse_gadget_attrs(char *path, char *name,
+static int usbg_parse_gadget_attrs(const char *path, const char *name,
 		usbg_gadget_attrs *g_attrs)
 {
 	int buf, ret;
@@ -1110,7 +1112,7 @@ out:
 	return ret;
 }
 
-static int usbg_parse_gadget_strs(char *path, char *name, int lang,
+static int usbg_parse_gadget_strs(const char *path, const char *name, int lang,
 		usbg_gadget_strs *g_strs)
 {
 	int ret;
@@ -1166,7 +1168,7 @@ out:
 	return ret;
 }
 
-static int usbg_parse_gadgets(char *path, usbg_state *s)
+static int usbg_parse_gadgets(const char *path, usbg_state *s)
 {
 	usbg_gadget *g;
 	int i, n;
@@ -1220,7 +1222,7 @@ static int usbg_init_state(char *path, usbg_state *s)
  * User API
  */
 
-int usbg_init(char *configfs_path, usbg_state **state)
+int usbg_init(const char *configfs_path, usbg_state **state)
 {
 	int ret = USBG_SUCCESS;
 	DIR *dir;
@@ -1542,7 +1544,8 @@ int usbg_rm_gadget_strs(usbg_gadget *g, int lang)
 }
 
 
-static int usbg_create_empty_gadget(usbg_state *s, char *name, usbg_gadget **g)
+static int usbg_create_empty_gadget(usbg_state *s, const char *name,
+				    usbg_gadget **g)
 {
 	char gpath[USBG_MAX_PATH_LENGTH];
 	int nmb;
@@ -1581,7 +1584,7 @@ out:
 	return ret;
 }
 
-int usbg_create_gadget_vid_pid(usbg_state *s, char *name,
+int usbg_create_gadget_vid_pid(usbg_state *s, const char *name,
 		uint16_t idVendor, uint16_t idProduct, usbg_gadget **g)
 {
 	int ret;
@@ -1615,8 +1618,9 @@ int usbg_create_gadget_vid_pid(usbg_state *s, char *name,
 	return ret;
 }
 
-int usbg_create_gadget(usbg_state *s, char *name,
-		usbg_gadget_attrs *g_attrs, usbg_gadget_strs *g_strs, usbg_gadget **g)
+int usbg_create_gadget(usbg_state *s, const char *name,
+		       usbg_gadget_attrs *g_attrs, usbg_gadget_strs *g_strs,
+		       usbg_gadget **g)
 {
 	usbg_gadget *gad;
 	int ret;
@@ -1790,7 +1794,7 @@ int usbg_get_gadget_strs(usbg_gadget *g, int lang,
 			g_strs)	: USBG_ERROR_INVALID_PARAM;
 }
 
-static int usbg_check_dir(char *path)
+static int usbg_check_dir(const char *path)
 {
 	int ret = USBG_SUCCESS;
 	DIR *dir;
@@ -1839,7 +1843,7 @@ out:
 	return ret;
 }
 
-int usbg_set_gadget_serial_number(usbg_gadget *g, int lang, char *serno)
+int usbg_set_gadget_serial_number(usbg_gadget *g, int lang, const char *serno)
 {
 	int ret = USBG_ERROR_INVALID_PARAM;
 
@@ -1860,7 +1864,7 @@ int usbg_set_gadget_serial_number(usbg_gadget *g, int lang, char *serno)
 	return ret;
 }
 
-int usbg_set_gadget_manufacturer(usbg_gadget *g, int lang, char *mnf)
+int usbg_set_gadget_manufacturer(usbg_gadget *g, int lang, const char *mnf)
 {
 	int ret = USBG_ERROR_INVALID_PARAM;
 
@@ -1881,7 +1885,7 @@ int usbg_set_gadget_manufacturer(usbg_gadget *g, int lang, char *mnf)
 	return ret;
 }
 
-int usbg_set_gadget_product(usbg_gadget *g, int lang, char *prd)
+int usbg_set_gadget_product(usbg_gadget *g, int lang, const char *prd)
 {
 	int ret = USBG_ERROR_INVALID_PARAM;
 
@@ -1903,7 +1907,8 @@ int usbg_set_gadget_product(usbg_gadget *g, int lang, char *prd)
 }
 
 int usbg_create_function(usbg_gadget *g, usbg_function_type type,
-		char *instance, usbg_function_attrs *f_attrs, usbg_function **f)
+			 const char *instance, usbg_function_attrs *f_attrs,
+			 usbg_function **f)
 {
 	char fpath[USBG_MAX_PATH_LENGTH];
 	usbg_function *func;
@@ -2118,7 +2123,7 @@ int usbg_set_config_strs(usbg_config *c, int lang,
 	return usbg_set_config_string(c, lang, c_strs->configuration);
 }
 
-int usbg_set_config_string(usbg_config *c, int lang, char *str)
+int usbg_set_config_string(usbg_config *c, int lang, const char *str)
 {
 	int ret = USBG_ERROR_INVALID_PARAM;
 
@@ -2139,7 +2144,7 @@ int usbg_set_config_string(usbg_config *c, int lang, char *str)
 	return ret;
 }
 
-int usbg_add_config_function(usbg_config *c, char *name, usbg_function *f)
+int usbg_add_config_function(usbg_config *c, const char *name, usbg_function *f)
 {
 	char bpath[USBG_MAX_PATH_LENGTH];
 	char fpath[USBG_MAX_PATH_LENGTH];
@@ -2245,7 +2250,7 @@ int usbg_get_udcs(struct dirent ***udc_list)
 	return ret;
 }
 
-int usbg_enable_gadget(usbg_gadget *g, char *udc)
+int usbg_enable_gadget(usbg_gadget *g, const char *udc)
 {
 	char gudc[USBG_MAX_STR_LENGTH];
 	struct dirent **udc_list;
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux