From d680f769d003a19f51158a689e6039d650a3efe0 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 22 Apr 2009 21:56:24 +1000 Subject: update imported git files to adapt to new environment --- include/mingw.h | 17 +++++++++++++- libbb/git.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ libbb/git.h | 21 ++++++++++++++++++ libbb/mingw.c | 7 +++--- libbb/quote.c | 4 +++- libbb/run-command.c | 51 ++++-------------------------------------- libbb/setenv.c | 4 ++-- libbb/strbuf.c | 21 +++--------------- libbb/strlcpy.c | 4 ++-- libbb/trace.c | 5 +++-- libbb/usage.c | 3 ++- libbb/winansi.c | 2 +- libbb/write_or_die.c | 3 ++- 13 files changed, 124 insertions(+), 80 deletions(-) create mode 100644 libbb/git.c create mode 100644 libbb/git.h diff --git a/include/mingw.h b/include/mingw.h index fe84da7ad..3d252af5f 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -1,12 +1,25 @@ #include +#include /* * things that are not available in header files */ +typedef int gid_t; +typedef int uid_t; typedef int pid_t; #define hstrerror strerror +#define S_ISUID 04000 +#define S_ISGID 02000 +#define S_ISVTX 01000 +#ifndef S_IRWXU +#define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR) +#endif +#define S_IRWXG (S_IRWXU >> 3) +#define S_IRWXO (S_IRWXG >> 3) + +#define S_IFSOCK 0140000 #define S_IFLNK 0120000 /* Symbolic link */ #define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK) #define S_ISSOCK(x) 0 @@ -134,6 +147,7 @@ struct passwd *getpwuid(int uid); int setitimer(int type, struct itimerval *in, struct itimerval *out); int sigaction(int sig, struct sigaction *in, struct sigaction *out); int link(const char *oldpath, const char *newpath); +time_t tm_to_time_t(const struct tm *tm); /* * replacements of existing functions @@ -237,7 +251,7 @@ char **env_setenv(char **env, const char *name); /* * A replacement of main() that ensures that argv[0] has a path */ - +/* #define main(c,v) dummy_decl_mingw_main(); \ static int mingw_main(); \ int main(int argc, const char **argv) \ @@ -246,3 +260,4 @@ int main(int argc, const char **argv) \ return mingw_main(argc, argv); \ } \ static int mingw_main(c,v) +*/ diff --git a/libbb/git.c b/libbb/git.c new file mode 100644 index 000000000..3ca8a9424 --- /dev/null +++ b/libbb/git.c @@ -0,0 +1,62 @@ +#include "libbb.h" +#include "git.h" + +ssize_t write_in_full(int fd, const void *buf, size_t count) +{ + const char *p = buf; + ssize_t total = 0; + + while (count > 0) { + ssize_t written = xwrite(fd, p, count); + if (written < 0) + return -1; + if (!written) { + errno = ENOSPC; + return -1; + } + count -= written; + p += written; + total += written; + } + + return total; +} + + +void *xcalloc(size_t nmemb, size_t size) +{ + void *ret = calloc(nmemb, size); + if (!ret && (!nmemb || !size)) + ret = calloc(1, 1); + if (!ret) { + ret = calloc(nmemb, size); + if (!ret && (!nmemb || !size)) + ret = calloc(1, 1); + if (!ret) + die("Out of memory, calloc failed"); + } + return ret; +} + +/* + * This is like mktime, but without normalization of tm_wday and tm_yday. + */ +time_t tm_to_time_t(const struct tm *tm) +{ + static const int mdays[] = { + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 + }; + int year = tm->tm_year - 70; + int month = tm->tm_mon; + int day = tm->tm_mday; + + if (year < 0 || year > 129) /* algo only works for 1970-2099 */ + return -1; + if (month < 0 || month > 11) /* array bounds */ + return -1; + if (month < 2 || (year + 2) % 4) + day--; + return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + + tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; +} + diff --git a/libbb/git.h b/libbb/git.h new file mode 100644 index 000000000..3274af34d --- /dev/null +++ b/libbb/git.h @@ -0,0 +1,21 @@ +ssize_t write_in_full(int fd, const void *buf, size_t count); + +#define alloc_nr(x) (((x)+16)*3/2) + +#define ALLOC_GROW(x, nr, alloc) \ + do { \ + if ((nr) > alloc) { \ + if (alloc_nr(alloc) < (nr)) \ + alloc = (nr); \ + else \ + alloc = alloc_nr(alloc); \ + x = xrealloc((x), alloc * sizeof(*(x))); \ + } \ + } while(0) + +static inline int is_absolute_path(const char *path) +{ + return path[0] == '/' || has_dos_drive_prefix(path); +} + +#define NORETURN ATTRIBUTE_NORETURN diff --git a/libbb/mingw.c b/libbb/mingw.c index 1a9f35f4d..f90fbc1d2 100644 --- a/libbb/mingw.c +++ b/libbb/mingw.c @@ -1,6 +1,6 @@ -#include "../git-compat-util.h" +#include "libbb.h" #include "win32.h" -#include "../strbuf.h" +#include "strbuf.h" #include "run-command.h" unsigned int _CRT_fmode = _O_BINARY; @@ -1277,7 +1277,6 @@ char *strptime(const char *s, const char *format, struct tm *tm) { return NULL; } -void gitunsetenv(const char *env) +void unsetenv(const char *env) { } - diff --git a/libbb/quote.c b/libbb/quote.c index 7a49fcf69..129dea72f 100644 --- a/libbb/quote.c +++ b/libbb/quote.c @@ -1,5 +1,7 @@ -#include "cache.h" +#include "libbb.h" +#include "strbuf.h" #include "quote.h" +#include "git.h" int quote_path_fully = 1; diff --git a/libbb/run-command.c b/libbb/run-command.c index b05c734d0..d4d78e9bc 100644 --- a/libbb/run-command.c +++ b/libbb/run-command.c @@ -1,6 +1,6 @@ -#include "cache.h" +#include "libbb.h" #include "run-command.h" -#include "exec_cmd.h" +#include "git.h" static inline void close_pair(int fd[2]) { @@ -168,9 +168,11 @@ int start_command(struct child_process *cmd) env = env_setenv(env, *cmd->env); } +#if 0 if (cmd->git_cmd) { cmd->argv = prepare_git_cmd(cmd->argv); } +#endif cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env); @@ -352,48 +354,3 @@ int finish_async(struct async *async) #endif return ret; } - -int run_hook(const char *index_file, const char *name, ...) -{ - struct child_process hook; - const char **argv = NULL, *env[2]; - char index[PATH_MAX]; - va_list args; - int ret; - size_t i = 0, alloc = 0; - - if (access(git_path("hooks/%s", name), X_OK) < 0) - return 0; - - va_start(args, name); - ALLOC_GROW(argv, i + 1, alloc); - argv[i++] = git_path("hooks/%s", name); - while (argv[i-1]) { - ALLOC_GROW(argv, i + 1, alloc); - argv[i++] = va_arg(args, const char *); - } - va_end(args); - - memset(&hook, 0, sizeof(hook)); - hook.argv = argv; - hook.no_stdin = 1; - hook.stdout_to_stderr = 1; - if (index_file) { - snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); - env[0] = index; - env[1] = NULL; - hook.env = env; - } - - ret = start_command(&hook); - free(argv); - if (ret) { - warning("Could not spawn %s", argv[0]); - return ret; - } - ret = finish_command(&hook); - if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL) - warning("%s exited due to uncaught signal", argv[0]); - - return ret; -} diff --git a/libbb/setenv.c b/libbb/setenv.c index 3a22ea7b7..401d0cec5 100644 --- a/libbb/setenv.c +++ b/libbb/setenv.c @@ -1,6 +1,6 @@ -#include "../git-compat-util.h" +#include "libbb.h" -int gitsetenv(const char *name, const char *value, int replace) +int setenv(const char *name, const char *value, int replace) { int out; size_t namelen, valuelen; diff --git a/libbb/strbuf.c b/libbb/strbuf.c index a88496030..d98b0fd41 100644 --- a/libbb/strbuf.c +++ b/libbb/strbuf.c @@ -1,5 +1,6 @@ -#include "cache.h" -#include "refs.h" +#include "libbb.h" +#include "strbuf.h" +#include "git.h" int prefixcmp(const char *str, const char *prefix) { @@ -358,19 +359,3 @@ int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint) return len; } - -int strbuf_branchname(struct strbuf *sb, const char *name) -{ - int len = strlen(name); - if (interpret_branch_name(name, sb) == len) - return 0; - strbuf_add(sb, name, len); - return len; -} - -int strbuf_check_branch_ref(struct strbuf *sb, const char *name) -{ - strbuf_branchname(sb, name); - strbuf_splice(sb, 0, 0, "refs/heads/", 11); - return check_ref_format(sb->buf); -} diff --git a/libbb/strlcpy.c b/libbb/strlcpy.c index 4024c3603..30c9be4c1 100644 --- a/libbb/strlcpy.c +++ b/libbb/strlcpy.c @@ -1,6 +1,6 @@ -#include "../git-compat-util.h" +#include "libbb.h" -size_t gitstrlcpy(char *dest, const char *src, size_t size) +size_t strlcpy(char *dest, const char *src, size_t size) { size_t ret = strlen(src); diff --git a/libbb/trace.c b/libbb/trace.c index 4229ae123..dde0f6051 100644 --- a/libbb/trace.c +++ b/libbb/trace.c @@ -22,8 +22,9 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "cache.h" -#include "quote.h" +#include "libbb.h" +#include "strbuf.h" +#include "git.h" /* Get a trace file descriptor from GIT_TRACE env variable. */ static int get_trace_fd(int *need_close) diff --git a/libbb/usage.c b/libbb/usage.c index 820d09f92..86bf68e21 100644 --- a/libbb/usage.c +++ b/libbb/usage.c @@ -3,7 +3,8 @@ * * Copyright (C) Linus Torvalds, 2005 */ -#include "git-compat-util.h" +#include "libbb.h" +#include "git.h" static void report(const char *prefix, const char *err, va_list params) { diff --git a/libbb/winansi.c b/libbb/winansi.c index 44dc293ad..f3a304436 100644 --- a/libbb/winansi.c +++ b/libbb/winansi.c @@ -3,7 +3,7 @@ */ #include -#include "../git-compat-util.h" +#include "libbb.h" /* Functions to be wrapped: diff --git a/libbb/write_or_die.c b/libbb/write_or_die.c index 4c29255df..f97ab8ba6 100644 --- a/libbb/write_or_die.c +++ b/libbb/write_or_die.c @@ -1,4 +1,5 @@ -#include "cache.h" +#include "libbb.h" +#include "git.h" /* * Some cases use stdio, but want to flush after the write -- cgit v1.2.3-55-g6feb