Skip to content

Commit

Permalink
[PATCH] git: add git_mkstemp()
Browse files Browse the repository at this point in the history
Signed-off-by: Junio C Hamano <junkio@cox.net>
  • Loading branch information
Holger Eitzenberger authored and Junio C Hamano committed Aug 6, 2005
1 parent 51b0fca commit f2db68e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ extern char *sha1_file_name(const unsigned char *sha1);
extern char *sha1_pack_name(const unsigned char *sha1);
extern char *sha1_pack_index_name(const unsigned char *sha1);

int git_mkstemp(char *path, size_t n, const char *template);

int safe_create_leading_directories(char *path);
char *safe_strncpy(char *, const char *, size_t);

/* Read and unpack a sha1 file into memory, write memory to a sha1 file */
extern int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size);
Expand Down
26 changes: 26 additions & 0 deletions path.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,29 @@ char *git_path(const char *fmt, ...)
return bad_path;
return cleanup_path(pathname);
}


/* git_mkstemp() - create tmp file honoring TMPDIR variable */
int git_mkstemp(char *path, size_t len, const char *template)
{
char *env, *pch = path;

if ((env = getenv("TMPDIR")) == NULL) {
strcpy(pch, "/tmp/");
len -= 5;
} else
len -= snprintf(pch, len, "%s/", env);

safe_strncpy(pch, template, len);

return mkstemp(path);
}


char *safe_strncpy(char *dest, const char *src, size_t n)
{
strncpy(dest, src, n);
dest[n - 1] = '\0';

return dest;
}

0 comments on commit f2db68e

Please sign in to comment.