]> asedeno.scripts.mit.edu Git - git.git/commitdiff
Rename safe_strncpy() to strlcpy().
authorPeter Eriksen <s022018@student.dtu.dk>
Sat, 24 Jun 2006 14:01:25 +0000 (16:01 +0200)
committerJunio C Hamano <junkio@cox.net>
Sun, 25 Jun 2006 06:16:25 +0000 (23:16 -0700)
This cleans up the use of safe_strncpy() even more.  Since it has the
same semantics as strlcpy() use this name instead.  Also move the
definition from inside path.c to its own file compat/strlcpy.c, and use
it conditionally at compile time, since some platforms already has
strlcpy().  It's included in the same way as compat/setenv.c.

Signed-off-by: Peter Eriksen <s022018@student.dtu.dk>
Signed-off-by: Junio C Hamano <junkio@cox.net>
12 files changed:
Makefile
builtin-log.c
builtin-tar-tree.c
cache.h
compat/strlcpy.c [new file with mode: 0644]
config.c
git-compat-util.h
http-fetch.c
http-push.c
ident.c
path.c
sha1_name.c

index e29e3fa381dd3b36bf235d304d99f254db702492..cde619c498da717ea665430f7d395358d0b1d06e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -26,6 +26,8 @@ all:
 #
 # Define NO_STRCASESTR if you don't have strcasestr.
 #
+# Define NO_STRLCPY if you don't have strlcpy.
+#
 # Define NO_SETENV if you don't have setenv in the C library.
 #
 # Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link.
@@ -240,9 +242,13 @@ LIBS = $(GITLIBS) -lz
 # because maintaining the nesting to match is a pain.  If
 # we had "elif" things would have been much nicer...
 
+ifeq ($(uname_S),Linux)
+       NO_STRLCPY = YesPlease
+endif
 ifeq ($(uname_S),Darwin)
        NEEDS_SSL_WITH_CRYPTO = YesPlease
        NEEDS_LIBICONV = YesPlease
+       NO_STRLCPY = YesPlease
        ## fink
        ifeq ($(shell test -d /sw/lib && echo y),y)
                ALL_CFLAGS += -I/sw/include
@@ -259,6 +265,7 @@ ifeq ($(uname_S),SunOS)
        NEEDS_NSL = YesPlease
        SHELL_PATH = /bin/bash
        NO_STRCASESTR = YesPlease
+       NO_STRLCPY = YesPlease
        ifeq ($(uname_R),5.8)
                NEEDS_LIBICONV = YesPlease
                NO_UNSETENV = YesPlease
@@ -276,6 +283,7 @@ ifeq ($(uname_O),Cygwin)
        NO_D_TYPE_IN_DIRENT = YesPlease
        NO_D_INO_IN_DIRENT = YesPlease
        NO_STRCASESTR = YesPlease
+       NO_STRLCPY = YesPlease
        NO_SYMLINK_HEAD = YesPlease
        NEEDS_LIBICONV = YesPlease
        # There are conflicting reports about this.
@@ -305,12 +313,14 @@ ifeq ($(uname_S),NetBSD)
 endif
 ifeq ($(uname_S),AIX)
        NO_STRCASESTR=YesPlease
+       NO_STRLCPY = YesPlease
        NEEDS_LIBICONV=YesPlease
 endif
 ifeq ($(uname_S),IRIX64)
        NO_IPV6=YesPlease
        NO_SETENV=YesPlease
        NO_STRCASESTR=YesPlease
+       NO_STRLCPY = YesPlease
        NO_SOCKADDR_STORAGE=YesPlease
        SHELL_PATH=/usr/gnu/bin/bash
        ALL_CFLAGS += -DPATH_MAX=1024
@@ -403,6 +413,10 @@ ifdef NO_STRCASESTR
        COMPAT_CFLAGS += -DNO_STRCASESTR
        COMPAT_OBJS += compat/strcasestr.o
 endif
+ifdef NO_STRLCPY
+       COMPAT_CFLAGS += -DNO_STRLCPY
+       COMPAT_OBJS += compat/strlcpy.o
+endif
 ifdef NO_SETENV
        COMPAT_CFLAGS += -DNO_SETENV
        COMPAT_OBJS += compat/setenv.o
index 5a8a50b81dabea4ccd9b0ba0fcb5d925a00560d0..44d2d136f585a74e462556a4f756c7a297cadd51 100644 (file)
@@ -115,7 +115,7 @@ static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
        int len = 0;
 
        if (output_directory) {
-               safe_strncpy(filename, output_directory, 1010);
+               strlcpy(filename, output_directory, 1010);
                len = strlen(filename);
                if (filename[len - 1] != '/')
                        filename[len++] = '/';
index 39a61b629367df9dd274acf5fa310b45dd06f126..f2e48aae2a04822d26d255dddf36002f58f2093c 100644 (file)
@@ -233,8 +233,8 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
        /* XXX: should we provide more meaningful info here? */
        sprintf(header.uid, "%07o", 0);
        sprintf(header.gid, "%07o", 0);
-       safe_strncpy(header.uname, "git", sizeof(header.uname));
-       safe_strncpy(header.gname, "git", sizeof(header.gname));
+       strlcpy(header.uname, "git", sizeof(header.uname));
+       strlcpy(header.gname, "git", sizeof(header.gname));
        sprintf(header.devmajor, "%07o", 0);
        sprintf(header.devminor, "%07o", 0);
 
diff --git a/cache.h b/cache.h
index efeafea70f61a8496cfd6fd6405793459d3df7a9..14358a872cdb2d8d663dad389897cfa0731d821d 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -216,7 +216,6 @@ enum sharedrepo {
 int git_config_perm(const char *var, const char *value);
 int adjust_shared_perm(const char *path);
 int safe_create_leading_directories(char *path);
-size_t safe_strncpy(char *, const char *, size_t);
 char *enter_repo(char *path, int strict);
 
 /* Read and unpack a sha1 file into memory, write memory to a sha1 file */
diff --git a/compat/strlcpy.c b/compat/strlcpy.c
new file mode 100644 (file)
index 0000000..b66856a
--- /dev/null
@@ -0,0 +1,13 @@
+#include <string.h>
+
+size_t gitstrlcpy(char *dest, const char *src, size_t size)
+{
+       size_t ret = strlen(src);
+
+       if (size) {
+               size_t len = (ret >= size) ? size - 1 : ret;
+               memcpy(dest, src, len);
+               dest[len] = '\0';
+       }
+       return ret;
+}
index 3e077d4c6ca4f7162e3a3254e1b3e90db33270ff..ec44827da4afbcf89a5eed37c7f557a7ffdce35d 100644 (file)
--- a/config.c
+++ b/config.c
@@ -280,17 +280,17 @@ int git_default_config(const char *var, const char *value)
        }
 
        if (!strcmp(var, "user.name")) {
-               safe_strncpy(git_default_name, value, sizeof(git_default_name));
+               strlcpy(git_default_name, value, sizeof(git_default_name));
                return 0;
        }
 
        if (!strcmp(var, "user.email")) {
-               safe_strncpy(git_default_email, value, sizeof(git_default_email));
+               strlcpy(git_default_email, value, sizeof(git_default_email));
                return 0;
        }
 
        if (!strcmp(var, "i18n.commitencoding")) {
-               safe_strncpy(git_commit_encoding, value, sizeof(git_commit_encoding));
+               strlcpy(git_commit_encoding, value, sizeof(git_commit_encoding));
                return 0;
        }
 
index b3d4cf532e5728e6b962d0d65789f6e8e4bd7fe5..93f558056dec457570bf2867dc6c75cd5891d2f2 100644 (file)
@@ -79,6 +79,11 @@ extern void gitunsetenv(const char *);
 extern char *gitstrcasestr(const char *haystack, const char *needle);
 #endif
 
+#ifdef NO_STRLCPY
+#define strlcpy gitstrlcpy
+extern size_t gitstrlcpy(char *, const char *, size_t);
+#endif
+
 static inline void *xmalloc(size_t size)
 {
        void *ret = malloc(size);
index 2b63d89501cc8838cca96506b3532d33fc452a15..44eba5fd0df292255dce056ec5aa700186096b39 100644 (file)
@@ -584,8 +584,8 @@ static void process_alternates_response(void *callback_data)
                        // skip 'objects' at end
                        if (okay) {
                                target = xmalloc(serverlen + posn - i - 6);
-                               safe_strncpy(target, base, serverlen);
-                               safe_strncpy(target + serverlen, data + i, posn - i - 6);
+                               strlcpy(target, base, serverlen);
+                               strlcpy(target + serverlen, data + i, posn - i - 6);
                                if (get_verbosely)
                                        fprintf(stderr,
                                                "Also look at %s\n", target);
@@ -727,7 +727,7 @@ xml_cdata(void *userData, const XML_Char *s, int len)
        if (ctx->cdata)
                free(ctx->cdata);
        ctx->cdata = xmalloc(len + 1);
-       safe_strncpy(ctx->cdata, s, len + 1);
+       strlcpy(ctx->cdata, s, len + 1);
 }
 
 static int remote_ls(struct alt_base *repo, const char *path, int flags,
index 8d472f0202827db726ede451be3fd63940c38c96..3c89a17496dcc72fa8773a0308281364bbf647a8 100644 (file)
@@ -1271,7 +1271,7 @@ xml_cdata(void *userData, const XML_Char *s, int len)
        if (ctx->cdata)
                free(ctx->cdata);
        ctx->cdata = xmalloc(len + 1);
-       safe_strncpy(ctx->cdata, s, len + 1);
+       strlcpy(ctx->cdata, s, len + 1);
 }
 
 static struct remote_lock *lock_remote(char *path, long timeout)
@@ -1473,7 +1473,7 @@ static void process_ls_object(struct remote_ls_ctx *ls)
                return;
        path += 8;
        obj_hex = xmalloc(strlen(path));
-       safe_strncpy(obj_hex, path, 3);
+       strlcpy(obj_hex, path, 3);
        strcpy(obj_hex + 2, path + 3);
        one_remote_object(obj_hex);
        free(obj_hex);
@@ -2172,7 +2172,7 @@ static void fetch_symref(char *path, char **symref, unsigned char *sha1)
        /* If it's a symref, set the refname; otherwise try for a sha1 */
        if (!strncmp((char *)buffer.buffer, "ref: ", 5)) {
                *symref = xmalloc(buffer.posn - 5);
-               safe_strncpy(*symref, (char *)buffer.buffer + 5, buffer.posn - 5);
+               strlcpy(*symref, (char *)buffer.buffer + 5, buffer.posn - 5);
        } else {
                get_sha1_hex(buffer.buffer, sha1);
        }
diff --git a/ident.c b/ident.c
index 7b44cbd2cc54c049518b9fb1cb681a88c7aaa6c4..efec97ffd0a9f3aa6573f3476c749d38d6fc388c 100644 (file)
--- a/ident.c
+++ b/ident.c
@@ -71,9 +71,9 @@ int setup_ident(void)
                len = strlen(git_default_email);
                git_default_email[len++] = '.';
                if (he && (domainname = strchr(he->h_name, '.')))
-                       safe_strncpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
+                       strlcpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
                else
-                       safe_strncpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
+                       strlcpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
        }
        /* And set the default date */
        datestamp(git_default_date, sizeof(git_default_date));
diff --git a/path.c b/path.c
index 36972fd6df63b3c4eebdcf89238df8ade60d53c3..db8905f3c352592124b2f0bda18fc0ccd048528e 100644 (file)
--- a/path.c
+++ b/path.c
@@ -77,25 +77,12 @@ int git_mkstemp(char *path, size_t len, const char *template)
                pch += n;
        }
 
-       safe_strncpy(pch, template, len);
+       strlcpy(pch, template, len);
 
        return mkstemp(path);
 }
 
 
-size_t safe_strncpy(char *dest, const char *src, size_t size)
-{
-       size_t ret = strlen(src);
-
-       if (size) {
-               size_t len = (ret >= size) ? size - 1 : ret;
-               memcpy(dest, src, len);
-               dest[len] = '\0';
-       }
-       return ret;
-}
-
-
 int validate_symref(const char *path)
 {
        struct stat st;
index cd85d1fa0f9f2afb4e159cb635db435e75ef3e58..f2cbafa496231e7a9cdbbea2ab4e7d4c4ed761ab 100644 (file)
@@ -262,7 +262,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
                if (str[am] == '@' && str[am+1] == '{' && str[len-1] == '}') {
                        int date_len = len - am - 3;
                        char *date_spec = xmalloc(date_len + 1);
-                       safe_strncpy(date_spec, str + am + 2, date_len + 1);
+                       strlcpy(date_spec, str + am + 2, date_len + 1);
                        at_time = approxidate(date_spec);
                        free(date_spec);
                        len = am;