X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Futil.c;h=f784f0399fd6d7b10c5962f88052958529b38c74;hb=71d7c22f54ae32d15133571e09ddf7ab435e8afa;hp=1aa827145323b472fd497c5a6e6c23ca17ca0cae;hpb=e868fb3d322f5c46385f1fc6db5bb1ab33f90305;p=sliver-openvswitch.git diff --git a/lib/util.c b/lib/util.c index 1aa827145..f784f0399 100644 --- a/lib/util.c +++ b/lib/util.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, 2010 Nicira Networks. + * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -149,6 +149,28 @@ ovs_strlcpy(char *dst, const char *src, size_t size) } } +/* Copies 'src' to 'dst'. Reads no more than 'size - 1' bytes from 'src'. + * Always null-terminates 'dst' (if 'size' is nonzero), and writes a zero byte + * to every otherwise unused byte in 'dst'. + * + * Except for performance, the following call: + * ovs_strzcpy(dst, src, size); + * is equivalent to these two calls: + * memset(dst, '\0', size); + * ovs_strlcpy(dst, src, size); + * + * (Thus, ovs_strzcpy() is similar to strncpy() without some of the pitfalls.) + */ +void +ovs_strzcpy(char *dst, const char *src, size_t size) +{ + if (size > 0) { + size_t len = strnlen(src, size - 1); + memcpy(dst, src, len); + memset(dst + len, '\0', size - len); + } +} + void ovs_fatal(int err_no, const char *format, ...) {