Make functions and structs dealing with string literals const correct.

This allows the compiler to check if someone writes into string
literals.

Signed-off-by: Stephan Gatzka <stephan.gatzka@gmail.com>
master
Stephan Gatzka 2014-02-07 17:52:03 +01:00 committed by Richard Cochran
parent 886112e202
commit 110849d422
16 changed files with 34 additions and 33 deletions

2
msg.c
View File

@ -379,7 +379,7 @@ int msg_pre_send(struct ptp_message *m)
return 0;
}
char *msg_type_string(int type)
const char *msg_type_string(int type)
{
switch (type) {
case SYNC:

2
msg.h
View File

@ -298,7 +298,7 @@ int msg_pre_send(struct ptp_message *m);
* @param type Value of the messageType field as returned by @ref msg_type().
* @return String describing the message type.
*/
char *msg_type_string(int type);
const char *msg_type_string(int type);
/**
* Print messages for debugging purposes.

5
pmc.c
View File

@ -109,7 +109,7 @@ struct management_id idtab[] = {
{ "PORT_DATA_SET_NP", PORT_DATA_SET_NP, do_set_action },
};
static char *action_string[] = {
static const char *action_string[] = {
"GET",
"SET",
"RESPONSE",
@ -711,7 +711,8 @@ static void usage(char *progname)
int main(int argc, char *argv[])
{
char *iface_name = NULL, *progname;
const char *iface_name = NULL;
char *progname;
int c, cnt, length, tmo = -1, batch_mode = 0, zero_datalen = 0;
char line[1024], *command = NULL;
enum transport_type transport_type = TRANS_UDP_IPV4;

View File

@ -61,7 +61,7 @@ struct pmc {
int zero_length_gets;
};
struct pmc *pmc_create(enum transport_type transport_type, char *iface_name,
struct pmc *pmc_create(enum transport_type transport_type, const char *iface_name,
UInteger8 boundary_hops, UInteger8 domain_number,
UInteger8 transport_specific, int zero_datalen)
{

View File

@ -25,7 +25,7 @@
struct pmc;
struct pmc *pmc_create(enum transport_type transport_type, char *iface_name,
struct pmc *pmc_create(enum transport_type transport_type, const char *iface_name,
UInteger8 boundary_hops, UInteger8 domain_number,
UInteger8 transport_specific, int zero_datalen);

4
raw.c
View File

@ -148,7 +148,7 @@ static int raw_close(struct transport *t, struct fdarray *fda)
unsigned char ptp_dst_mac[MAC_LEN] = { PTP_DST_MAC };
unsigned char p2p_dst_mac[MAC_LEN] = { P2P_DST_MAC };
static int open_socket(char *name, int event)
static int open_socket(const char *name, int event)
{
struct sockaddr_ll addr;
int fd, index;
@ -184,7 +184,7 @@ no_socket:
return -1;
}
static int raw_open(struct transport *t, char *name,
static int raw_open(struct transport *t, const char *name,
struct fdarray *fda, enum timestamp_type ts_type)
{
struct raw *raw = container_of(t, struct raw, t);

12
sk.c
View File

@ -40,7 +40,7 @@ int sk_check_fupsync;
/* private methods */
static int hwts_init(int fd, char *device, int rx_filter, int one_step)
static int hwts_init(int fd, const char *device, int rx_filter, int one_step)
{
struct ifreq ifreq;
struct hwtstamp_config cfg, req;
@ -77,7 +77,7 @@ static int hwts_init(int fd, char *device, int rx_filter, int one_step)
/* public methods */
int sk_interface_index(int fd, char *name)
int sk_interface_index(int fd, const char *name)
{
struct ifreq ifreq;
int err;
@ -102,7 +102,7 @@ int sk_general_init(int fd)
return 0;
}
int sk_get_ts_info(char *name, struct sk_ts_info *sk_info)
int sk_get_ts_info(const char *name, struct sk_ts_info *sk_info)
{
#ifdef ETHTOOL_GET_TS_INFO
struct ethtool_ts_info info;
@ -146,7 +146,7 @@ failed:
return -1;
}
int sk_interface_macaddr(char *name, unsigned char *mac, int len)
int sk_interface_macaddr(const char *name, unsigned char *mac, int len)
{
struct ifreq ifreq;
int err, fd;
@ -172,7 +172,7 @@ int sk_interface_macaddr(char *name, unsigned char *mac, int len)
return 0;
}
int sk_interface_addr(char *name, int family, uint8_t *addr, int len)
int sk_interface_addr(const char *name, int family, uint8_t *addr, int len)
{
struct ifaddrs *ifaddr, *i;
int copy_len, result = -1;
@ -285,7 +285,7 @@ int sk_receive(int fd, void *buf, int buflen,
return cnt;
}
int sk_timestamping_init(int fd, char *device, enum timestamp_type type,
int sk_timestamping_init(int fd, const char *device, enum timestamp_type type,
enum transport_type transport)
{
int err, filter1, filter2 = 0, flags, one_step;

10
sk.h
View File

@ -44,7 +44,7 @@ struct sk_ts_info {
* @param device The name of the network interface of interest.
* @return The result from the SIOCGIFINDEX ioctl.
*/
int sk_interface_index(int fd, char *device);
int sk_interface_index(int fd, const char *device);
/**
* Prepare a given socket for PTP "general" messages.
@ -59,7 +59,7 @@ int sk_general_init(int fd);
* @param info Struct containing obtained timestamping information.
* @return zero on success, negative on failure.
*/
int sk_get_ts_info(char *name, struct sk_ts_info *sk_info);
int sk_get_ts_info(const char *name, struct sk_ts_info *sk_info);
/**
* Obtain the MAC address of a network interface.
@ -68,7 +68,7 @@ int sk_get_ts_info(char *name, struct sk_ts_info *sk_info);
* @param len Length of 'mac'
* @return Zero on success, non-zero otherwise.
*/
int sk_interface_macaddr(char *name, unsigned char *mac, int len);
int sk_interface_macaddr(const char *name, unsigned char *mac, int len);
/**
* Obtains the first IP address assigned to a network interface.
@ -78,7 +78,7 @@ int sk_interface_macaddr(char *name, unsigned char *mac, int len);
* @param len Length of 'addr'
* @return The number of bytes written to addr on success, -1 otherwise.
*/
int sk_interface_addr(char *name, int family, uint8_t *addr, int len);
int sk_interface_addr(const char *name, int family, uint8_t *addr, int len);
/**
* Read a message from a socket.
@ -100,7 +100,7 @@ int sk_receive(int fd, void *buf, int buflen,
* @param transport The type of transport used.
* @return Zero on success, non-zero otherwise.
*/
int sk_timestamping_init(int fd, char *device, enum timestamp_type type,
int sk_timestamping_init(int fd, const char *device, enum timestamp_type type,
enum transport_type transport);
/**

View File

@ -29,7 +29,7 @@ int transport_close(struct transport *t, struct fdarray *fda)
return t->close(t, fda);
}
int transport_open(struct transport *t, char *name,
int transport_open(struct transport *t, const char *name,
struct fdarray *fda, enum timestamp_type tt)
{
return t->open(t, name, fda, tt);

View File

@ -64,7 +64,7 @@ struct transport;
int transport_close(struct transport *t, struct fdarray *fda);
int transport_open(struct transport *t, char *name,
int transport_open(struct transport *t, const char *name,
struct fdarray *fda, enum timestamp_type tt);
int transport_recv(struct transport *t, int fd,

View File

@ -30,7 +30,7 @@ struct transport {
int (*close)(struct transport *t, struct fdarray *fda);
int (*open)(struct transport *t, char *name, struct fdarray *fda,
int (*open)(struct transport *t, const char *name, struct fdarray *fda,
enum timestamp_type tt);
int (*recv)(struct transport *t, int fd, void *buf, int buflen,

4
udp.c
View File

@ -93,7 +93,7 @@ static int udp_close(struct transport *t, struct fdarray *fda)
return 0;
}
static int open_socket(char *name, struct in_addr mc_addr[2], short port)
static int open_socket(const char *name, struct in_addr mc_addr[2], short port)
{
struct sockaddr_in addr;
int fd, index, on = 1;
@ -148,7 +148,7 @@ enum { MC_PRIMARY, MC_PDELAY };
static struct in_addr mcast_addr[2];
static int udp_open(struct transport *t, char *name, struct fdarray *fda,
static int udp_open(struct transport *t, const char *name, struct fdarray *fda,
enum timestamp_type ts_type)
{
struct udp *udp = container_of(t, struct udp, t);

4
udp6.c
View File

@ -100,7 +100,7 @@ static int udp6_close(struct transport *t, struct fdarray *fda)
return 0;
}
static int open_socket_ipv6(char *name, struct in6_addr mc_addr[2], short port,
static int open_socket_ipv6(const char *name, struct in6_addr mc_addr[2], short port,
int *interface_index)
{
struct sockaddr_in6 addr;
@ -158,7 +158,7 @@ enum { MC_PRIMARY, MC_PDELAY };
static struct in6_addr mc6_addr[2];
static int udp6_open(struct transport *t, char *name, struct fdarray *fda,
static int udp6_open(struct transport *t, const char *name, struct fdarray *fda,
enum timestamp_type ts_type)
{
struct udp6 *udp6 = container_of(t, struct udp6, t);

2
uds.c
View File

@ -46,7 +46,7 @@ static int uds_close(struct transport *t, struct fdarray *fda)
return 0;
}
static int uds_open(struct transport *t, char *name, struct fdarray *fda,
static int uds_open(struct transport *t, const char *name, struct fdarray *fda,
enum timestamp_type tt)
{
int fd, err;

6
util.c
View File

@ -28,7 +28,7 @@
#define NS_PER_HOUR (3600 * NS_PER_SEC)
#define NS_PER_DAY (24 * NS_PER_HOUR)
char *ps_str[] = {
const char *ps_str[] = {
"NONE",
"INITIALIZING",
"FAULTY",
@ -42,7 +42,7 @@ char *ps_str[] = {
"GRAND_MASTER",
};
char *ev_str[] = {
const char *ev_str[] = {
"NONE",
"POWERUP",
"INITIALIZE",
@ -98,7 +98,7 @@ int str2pid(const char *s, struct PortIdentity *result)
return -1;
}
int generate_clock_identity(struct ClockIdentity *ci, char *name)
int generate_clock_identity(struct ClockIdentity *ci, const char *name)
{
unsigned char mac[6];
if (sk_interface_macaddr(name, mac, sizeof(mac)))

6
util.h
View File

@ -26,12 +26,12 @@
/**
* Table of human readable strings, one for each port state.
*/
extern char *ps_str[];
extern const char *ps_str[];
/**
* Table of human readable strings, one for each port event.
*/
extern char *ev_str[];
extern const char *ev_str[];
/**
* Convert a clock identity into a human readable string.
@ -64,7 +64,7 @@ char *pid2str(struct PortIdentity *id);
*/
int str2pid(const char *s, struct PortIdentity *result);
int generate_clock_identity(struct ClockIdentity *ci, char *name);
int generate_clock_identity(struct ClockIdentity *ci, const char *name);
/**
* Copies a PTPText to a static_ptp_text. This copies the text into