{"thread":{"id":"388","subject":"[PATCH]: first take at cleanup of #include, xmalloc / xrealloc, git status report usage.","startedAt":"2005-04-29T13:19:06Z","lastAt":"2005-04-29T17:07:43Z","messageCount":3,"participants":["Robert Sütterlin","Edgar Toernig","Klaus Robert Suetterlin"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"2128","messageId":"bfbb1f23220e1918afe70d4cf3de24f5@mpe.mpg.de","threadId":"388","inReplyTo":null,"subject":"[PATCH]: first take at cleanup of #include, xmalloc / xrealloc, git status report usage.","fromName":"Robert Sütterlin","fromEmail":"robert@mpe.mpg.de","sentAt":"2005-04-29T13:19:06Z","receivedAt":"2005-04-29T13:19:06Z","isPatch":true,"sender":{"key":"robert@mpe.mpg.de","avatar":null},"body":"This is a rather large monolithic or rather dualithic patch that tries \nto clean up #include usage in cache.h.\n\nIMHO including headers that include headers that include headers ... \nare we there, yet? is a BAD(tm) thing.  So I removed every #include \nfrom cache.h that was not necessary to define the contents of cache.h \nitself.  Then I entered only the required headers to each sourcefile.  \nThis will help in identifying which subsystems don't use the libgit \ninfrastructure, or which functionality needs to be added to libgit.\n\nAlso I added two report functions to usage (warning and message), \ntrtied to remove xmalloc and xrealloc whereever simply dying was IMHO \nwrong, added more diagnostic output and return value chacking to some \nfiles.\n\nThis is the beginning of a cleanup of git code.  I feel that (except \nmaybe for diff / merge) git has stabilised enough to start the \nrefactoring / renaming / api cleanup stuff.\n\nI also strongly believe that we should design a new internal memory \nbuffer.  The usage of malloc / realloc in git is not good at all.  \nEverybody and her dog seems to know how to best handle malloc, so there \nis a lot of code repetition and bad (i.e. no) api all over the place.  \nA new internal buffer concept would of course mean, that all of the \nalgorithms must be rewritten, but as Linus always says: \"that is just \ncoding.\"  Some not-so-core components already use strbuf --- which I do \nnot like that much really --- still I'd call this a beginning.\n\nFeel free to neglect any of my changes.\n\nsigned-off-by: Robert Suetterlin <robert@mpe.mpg.de>\n\nintermediate commit before cg-update continuing work to clean up \n#include, malloc / realloc and diagnostics messages.\n\n---\ncommit f03e0239ea6ae31d8f89a4a7965ed90832770c5b\ntree 86241f0cd550785e3cba0a235f7e423d0503be93\nparent 49612c471eebd26efe926a71752e254c1cdc382d\nauthor Robert Suetterlin <krs@robert3.mpe-garching.mpg.de> 1114763833 \n+0200\ncommitter Robert Suetterlin <krs@robert3.mpe-garching.mpg.de> \n1114763833 +0200\n\nIndex: Makefile\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/Makefile  (mode:100644 \nsha1:d73bea1cbb9451a89b03d6066bf2ed7fec32fd31)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/Makefile  (mode:100644 \nsha1:3c461e439a8731b688533dccbc5dafa1471a5b3b)\n@@ -73,7 +73,7 @@\n  \tLIB_OBJS += ppc/sha1.o ppc/sha1ppc.o\n  else\n  \tSHA1_HEADER=<openssl/sha.h>\n-\tLIBS += -lssl\n+\tLIBS += -lcrypto\n  endif\n  endif\n\nIndex: blob.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/blob.c  (mode:100644 \nsha1:3d99b93f020d84c5410c2b1056f2d7446b647d1e)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/blob.c  (mode:100644 \nsha1:037e7ebcf786efee5741398afa2144d6d168e9f3)\n@@ -1,20 +1,27 @@\n-#include \"blob.h\"\n-#include \"cache.h\"\n  #include <stdlib.h>\n+#include <string.h>\n+\n+#include \"cache.h\"\n+\n+#include \"blob.h\"\n\n  const char *blob_type = \"blob\";\n\n  struct blob *lookup_blob(unsigned char *sha1)\n  {\n  \tstruct object *obj = lookup_object(sha1);\n-\tif (!obj) {\n-\t\tstruct blob *ret = xmalloc(sizeof(struct blob));\n+\tif (NULL == obj) {\n+\t\tstruct blob *ret = malloc(sizeof(struct blob));\n+\t\tif (NULL == ret) {\n+\t\t\terror(\"Could not malloc(sizeof(struct blob))\");\n+\t\t\treturn NULL;\n+\t\t}\n  \t\tmemset(ret, 0, sizeof(struct blob));\n  \t\tcreated_object(sha1, &ret->object);\n  \t\tret->object.type = blob_type;\n  \t\treturn ret;\n  \t}\n-\tif (obj->parsed && obj->type != blob_type) {\n+\tif (obj->type != blob_type) {\n  \t\terror(\"Object %s is a %s, not a blob\",\n  \t\t      sha1_to_hex(sha1), obj->type);\n  \t\treturn NULL;\nIndex: cache.h\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/cache.h  (mode:100644 \nsha1:1052e17da7093024d34be3c939ab768e20cfa2a3)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/cache.h  (mode:100644 \nsha1:036d6b6897ef119b4de051d957b5886680bfffe3)\n@@ -1,19 +1,21 @@\n  #ifndef CACHE_H\n  #define CACHE_H\n\n-#include <unistd.h>\n-#include <stdio.h>\n+#include <sys/types.h>\n+#include <sys/mman.h>\n+#include <sys/param.h>\n  #include <sys/stat.h>\n+\n+#include <netinet/in.h>\n+\n+//#include <errno.h>\n  #include <fcntl.h>\n+#include <stdarg.h>\n  #include <stddef.h>\n  #include <stdlib.h>\n-#include <stdarg.h>\n-#include <string.h>\n+//#include <stdio.h>\n+//#include <string.h>\n  #include <unistd.h>\n-#include <errno.h>\n-#include <sys/mman.h>\n-#include <sys/param.h>\n-#include <netinet/in.h>\n\n  #include SHA1_HEADER\n  #include <zlib.h>\n@@ -25,6 +27,7 @@\n  /*\n   * Basic data structures for the directory cache\n   *\n+ * FIX this:  The Note below is outdated.\n   * NOTE NOTE NOTE! This is all in the native CPU byte format. It's\n   * not even trying to be portable. It's trying to be efficient. It's\n   * just a cache, after all.\n@@ -145,6 +148,8 @@\n  extern void usage(const char *err);\n  extern void die(const char *err, ...);\n  extern int error(const char *err, ...);\n+extern void warning(const char *err, ...);\n+extern void message(const char *msg, ...);\n\n  extern int cache_name_compare(const char *name1, int len1, const char \n*name2, int len2);\n\nIndex: cat-file.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/cat-file.c  (mode:100644 \nsha1:3c47d79a16305d326a65768fe9f37ee25928510b)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/cat-file.c  (mode:100644 \nsha1:568cfbc266416f8afa88995a90a3d17132e8db8b)\n@@ -3,6 +3,9 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <errno.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n\n  int main(int argc, char **argv)\nIndex: check-files.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/check-files.c  \n(mode:100644 sha1:7d16691aa9d51b5b4670d5837b3527ee7c7da79c)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/check-files.c  \n(mode:100644 sha1:438fa7fc084b94a5fe6dd0a4200b69d1ef147b75)\n@@ -6,6 +6,9 @@\n   *\n   * Copyright (C) 2005 Linus Torvalds\n   */\n+#include <errno.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n\n  static void check_file(const char *path)\nIndex: checkout-cache.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/checkout-cache.c  \n(mode:100644 sha1:b5ca20e8d0e25a688bcca3f0be39e9493dc947ce)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/checkout-cache.c  \n(mode:100644 sha1:c6576f35685a0a83627af2fa5edba742a103c2ad)\n@@ -32,34 +32,42 @@\n   * of \"-a\" causing problems (not possible in the above example,\n   * but get used to it in scripting!).\n   */\n+#include <sys/types.h>\n+#include <sys/stat.h>\n+\n+#include <errno.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n\n  static int force = 0, quiet = 0, not_new = 0;\n\n-static void create_directories(const char *path)\n+static int\n+create_directories(const char *path)\n  {\n-\tint len = strlen(path);\n-\tchar *buf = xmalloc(len + 1);\n-\tconst char *slash = path;\n+\tchar *buf = (char *)path;\n+\tchar *slash = buf;\n\n  \twhile ((slash = strchr(slash+1, '/')) != NULL) {\n-\t\tlen = slash - path;\n-\t\tmemcpy(buf, path, len);\n-\t\tbuf[len] = 0;\n-\t\tmkdir(buf, 0755);\n+\t\t*slash = '\\0';\n+\t\tif (0 != mkdir(buf, 0755))\n+\t\t\treturn error(\"Unable to mkdir(``%s'', 0755)\", buf);\n+\t\t*slash = '/';\n  \t}\n-\tfree(buf);\n+\n+\treturn 0;\n  }\n\n-static int create_file(const char *path, unsigned int mode)\n+static int\n+create_file(const char *path, unsigned int mode)\n  {\n  \tint fd;\n\n  \tmode = (mode & 0100) ? 0777 : 0666;\n  \tfd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);\n  \tif (fd < 0) {\n-\t\tif (errno == ENOENT) {\n-\t\t\tcreate_directories(path);\n+\t\tif (ENOENT == errno) {\n+\t\t\tif (0 != create_directories(path)) return -1;\n  \t\t\tfd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);\n  \t\t}\n  \t}\n@@ -110,7 +118,7 @@\n  \t\t\treturn 0;\n  \t\tif (!force) {\n  \t\t\tif (!quiet)\n-\t\t\t\tfprintf(stderr, \"checkout-cache: %s already exists\\n\", path);\n+\t\t\t\tmessage(\"checkout-cache: %s already exists\\n\", path);\n  \t\t\treturn 0;\n  \t\t}\n\n@@ -132,8 +140,7 @@\n  \tif (pos < 0) {\n  \t\tif (!quiet) {\n  \t\t\tpos = -pos - 1;\n-\t\t\tfprintf(stderr,\n-\t\t\t\t\"checkout-cache: %s is %s.\\n\",\n+\t\t\tmessage(\"checkout-cache: %s is %s.\\n\",\n  \t\t\t\tname,\n  \t\t\t\t(pos < active_nr &&\n  \t\t\t\t !strcmp(active_cache[pos]->name, name)) ?\nIndex: commit-tree.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/commit-tree.c  \n(mode:100644 sha1:23de13361944ad7ba7c5320cf7cdd04e81842c60)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/commit-tree.c  \n(mode:100644 sha1:e396ca80f6a87c09130b19fc5452605ac7d83861)\n@@ -3,13 +3,13 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n-#include \"cache.h\"\n-\n+#include <ctype.h>\n  #include <pwd.h>\n  #include <time.h>\n+#include <stdio.h>\n  #include <string.h>\n-#include <ctype.h>\n-#include <time.h>\n+\n+#include \"cache.h\"\n\n  #define BLOCKING (1ul << 14)\n\nIndex: convert-cache.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/convert-cache.c  \n(mode:100644 sha1:631d1aa910e7328c99642495f93908c749074f91)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/convert-cache.c  \n(mode:100644 sha1:e50248857b1b707a771a42fe4ce7f7dcc5fdc79f)\n@@ -1,6 +1,9 @@\n  #define _XOPEN_SOURCE /* glibc2 needs this */\n-#include <time.h>\n  #include <ctype.h>\n+#include <time.h>\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n\n  struct entry {\nIndex: diff-cache.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/diff-cache.c  (mode:100644 \nsha1:49f815cf732a32c87d965f22b00850643e7aab77)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/diff-cache.c  (mode:100644 \nsha1:1177977fba4d3d2fcd3cd6ec72451abb84d414a1)\n@@ -1,3 +1,6 @@\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n  #include \"diff.h\"\n\nIndex: diff-tree-helper.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/diff-tree-helper.c  \n(mode:100644 sha1:f3efc8a6f2d21c33bd6096dae3150df61800b015)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/diff-tree-helper.c  \n(mode:100644 sha1:4071871b6d3416d02d6203c47950d482d79c78f4)\n@@ -1,6 +1,9 @@\n  /*\n   * Copyright (C) 2005 Junio C Hamano\n   */\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n  #include \"strbuf.h\"\n  #include \"diff.h\"\nIndex: diff-tree.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/diff-tree.c  (mode:100644 \nsha1:5a1ad34652fcda63a37c3aeb55c0231f1be69bdd)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/diff-tree.c  (mode:100644 \nsha1:ac026816c32cb44d43025236409655f97197aff2)\n@@ -1,3 +1,6 @@\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n  #include \"diff.h\"\n\nIndex: diff.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/diff.c  (mode:100644 \nsha1:4cc41f097dfe1ade230d2d6e438b0249dfbc1a76)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/diff.c  (mode:100644 \nsha1:0326f0488d2c037f238f9012d8a007473b13b968)\n@@ -3,6 +3,11 @@\n   */\n  #include <sys/types.h>\n  #include <sys/wait.h>\n+\n+#include <errno.h>\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n  #include \"diff.h\"\n\nIndex: fsck-cache.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/fsck-cache.c  (mode:100644 \nsha1:f9b1431dd8f4f3b426a7e410de952277aaa11401)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/fsck-cache.c  (mode:100644 \nsha1:265319c97cc68313e3e62d304c38d074dc22abdb)\n@@ -1,11 +1,13 @@\n-#include \"cache.h\"\n-\n  #include <sys/types.h>\n+\n  #include <dirent.h>\n+#include <stdio.h>\n+#include <string.h>\n\n  #include \"commit.h\"\n  #include \"tree.h\"\n  #include \"blob.h\"\n+#include \"cache.h\"\n\n  #define REACHABLE 0x0001\n\n@@ -44,7 +46,7 @@\n  \tif (parse_tree(item))\n  \t\treturn -1;\n  \tif (item->has_full_path) {\n-\t\tfprintf(stderr, \"warning: fsck-cache: tree %s \"\n+\t\twarning(\"fsck-cache: tree %s \"\n  \t\t\t\"has full pathnames in it\\n\", sha1_to_hex(sha1));\n  \t}\n  \treturn 0;\n@@ -182,7 +184,7 @@\n  \t\t\tif (!fsck_name(name))\n  \t\t\t\tcontinue;\n  \t\t}\n-\t\tfprintf(stderr, \"bad sha1 file: %s/%s\\n\", path, de->d_name);\n+\t\terror(\"bad sha1 file: %s/%s\\n\", path, de->d_name);\n  \t}\n  \tclosedir(dir);\n  \treturn 0;\n@@ -238,10 +240,10 @@\n\n  \tif (!heads) {\n  \t\tif (show_unreachable) {\n-\t\t\tfprintf(stderr, \"unable to do reachability without a head\\n\");\n+\t\t\tmessage(\"unable to do reachability without a head\\n\");\n  \t\t\tshow_unreachable = 0;\n  \t\t}\n-\t\tfprintf(stderr, \"expect dangling commits - potential heads - due to \nlack of head information\\n\");\n+\t\tmessage(\"expect dangling commits - potential heads - due to lack of \nhead information\\n\");\n  \t}\n\n  \tcheck_connectivity();\nIndex: git-export.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/git-export.c  (mode:100644 \nsha1:9c867a285b7753859bb2cdcdb2615b386847db70)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/git-export.c  (mode:100644 \nsha1:c141f559b9f3e4812bd5a3f3f06df6e246f58142)\n@@ -1,3 +1,6 @@\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n  #include \"commit.h\"\n\nIndex: git-mktag.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/git-mktag.c  (mode:100644 \nsha1:5d2830dc2bdfa2e76afc3fd4687db8faffaefba2)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/git-mktag.c  (mode:100644 \nsha1:ae0d0c560a6da9ace877b25e7691745d660a9e3a)\n@@ -1,3 +1,6 @@\n+#include <string.h>\n+#include <stdio.h>\n+\n  #include \"cache.h\"\n\n  /*\nIndex: init-db.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/init-db.c  (mode:100644 \nsha1:83f95e8b926f4fd28e0db0ccfc4f040d4172ee8a)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/init-db.c  (mode:100644 \nsha1:e4ce5acea8efb8488a2557d022ca9673a6d5170a)\n@@ -3,13 +3,17 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <errno.h>\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n\n  void safe_create_dir(char *dir)\n  {\n  \tif (mkdir(dir, 0755) < 0) {\n  \t\tif (errno != EEXIST) {\n-\t\t\tperror(dir);\n+\t\t\terror(\"%s: %s\", dir, strerror(errno));\n  \t\t\texit(1);\n  \t\t}\n  \t}\n@@ -31,7 +35,7 @@\n  \tsha1_dir = getenv(DB_ENVIRONMENT);\n  \tif (!sha1_dir) {\n  \t\tsha1_dir = DEFAULT_DB_ENVIRONMENT;\n-\t\tfprintf(stderr, \"defaulting to local storage area\\n\");\n+\t\terror(\"defaulting to local storage area\");\n  \t}\n  \tlen = strlen(sha1_dir);\n  \tpath = xmalloc(len + 40);\nIndex: ls-tree.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/ls-tree.c  (mode:100644 \nsha1:60c169000bfaf7aa21406861e82140449f9a07ea)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/ls-tree.c  (mode:100644 \nsha1:94e3780d30bbdbb33693d2d767e81b26c83bd612)\n@@ -3,6 +3,9 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n\n  int line_termination = '\\n';\nIndex: merge-base.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/merge-base.c  (mode:100644 \nsha1:2c40881302e586366f03ae6ac6e7c0035847e2f0)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/merge-base.c  (mode:100644 \nsha1:ec9162900c34f45b2c736436554481d380ec0778)\n@@ -1,4 +1,6 @@\n  #include <stdlib.h>\n+#include <stdio.h>\n+\n  #include \"cache.h\"\n  #include \"commit.h\"\n\nIndex: merge-cache.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/merge-cache.c  \n(mode:100644 sha1:4902749fbb4230f218a350efa0ff786ded40337a)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/merge-cache.c  \n(mode:100644 sha1:8c944f383e699677e92d945d61e85ebf1a125321)\n@@ -1,6 +1,9 @@\n  #include <sys/types.h>\n  #include <sys/wait.h>\n\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n\n  static const char *pgm = NULL;\nIndex: read-cache.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/read-cache.c  (mode:100644 \nsha1:2112168477b8cc87970a94c584104d888ad0c067)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/read-cache.c  (mode:100644 \nsha1:2c2f85a3cdad584eddfa2a1db23d122630f14a75)\n@@ -3,7 +3,9 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n-#include <stdarg.h>\n+#include <errno.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n\n  struct cache_entry **active_cache = NULL;\nIndex: read-tree.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/read-tree.c  (mode:100644 \nsha1:1ad7ffc555b635fe57fa7834b12d71ff576be065)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/read-tree.c  (mode:100644 \nsha1:8b2de2a750328f82759c23c6ec35e35cfac42689)\n@@ -3,6 +3,9 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n\n  static int stage = 0;\nIndex: rev-list.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/rev-list.c  (mode:100644 \nsha1:77bfc29db1aad08ba9d7d87ce08d33d4a88e74e3)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/rev-list.c  (mode:100644 \nsha1:7cd56acf83c17e752f2cabd06efc4acc2f0b10ea)\n@@ -1,3 +1,4 @@\n+#include <stdio.h>\n  #include \"cache.h\"\n  #include \"commit.h\"\n\nIndex: rev-tree.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/rev-tree.c  (mode:100644 \nsha1:94d500ec091e8c1ac0bd4c68a7e39fd5213a79eb)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/rev-tree.c  (mode:100644 \nsha1:4a4d7c6b4baf8119c3f475eeb4c0302a978ff63c)\n@@ -1,7 +1,10 @@\n  #define _XOPEN_SOURCE /* glibc2 needs this */\n  #define _BSD_SOURCE /* for tm.tm_gmtoff */\n-#include <time.h>\n+\n  #include <ctype.h>\n+#include <stdio.h>\n+#include <string.h>\n+#include <time.h>\n\n  #include \"cache.h\"\n  #include \"commit.h\"\nIndex: rpush.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/rpush.c  (mode:100644 \nsha1:0293a1a46311d7e20b13177143741ab9d6d0d201)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/rpush.c  (mode:100644 \nsha1:83b197668b1b61ccbe73aa88084adcf8ee1e807e)\n@@ -1,7 +1,10 @@\n-#include \"cache.h\"\n-#include \"rsh.h\"\n+#include <sys/types.h>\n  #include <sys/socket.h>\n  #include <errno.h>\n+#include <string.h>\n+\n+#include \"cache.h\"\n+#include \"rsh.h\"\n\n  void service(int fd_in, int fd_out) {\n  \tssize_t size;\n@@ -14,10 +17,10 @@\n  \t\tdo {\n  \t\t\tsize = read(fd_in, sha1 + posn, 20 - posn);\n  \t\t\tif (size < 0) {\n-\t\t\t\tperror(\"rpush: read \");\n+\t\t\t\terror(\"rpush: read %s\", strerror(errno));\n  \t\t\t\treturn;\n  \t\t\t}\n-\t\t\tif (!size)\n+\t\t\tif (0 == size)\n  \t\t\t\treturn;\n  \t\t\tposn += size;\n  \t\t} while (posn < 20);\n@@ -26,7 +29,7 @@\n\n  \t\tbuf = map_sha1_file(sha1, &objsize);\n  \t\tif (!buf) {\n-\t\t\tfprintf(stderr, \"rpush: could not find %s\\n\",\n+\t\t\terror(\"rpush: could not find %s\\n\",\n  \t\t\t\tsha1_to_hex(sha1));\n  \t\t\treturn;\n  \t\t}\n@@ -34,10 +37,11 @@\n  \t\tdo {\n  \t\t\tsize = write(fd_out, buf + posn, objsize - posn);\n  \t\t\tif (size <= 0) {\n-\t\t\t\tif (!size) {\n-\t\t\t\t\tfprintf(stderr, \"rpush: write closed\");\n+\t\t\t\tif (0 == size) {\n+\t\t\t\t\terror(\"rpush: write closed\");\n  \t\t\t\t} else {\n-\t\t\t\t\tperror(\"rpush: write \");\n+\t\t\t\t\terror(\"rpush: write %s\",\n+\t\t\t\t\t     strerror(errno));\n  \t\t\t\t}\n  \t\t\t\treturn;\n  \t\t\t}\nIndex: rsh.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/rsh.c  (mode:100644 \nsha1:af2f47b174e3895e6c02c4cd8f16a89e3bacbbbb)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/rsh.c  (mode:100644 \nsha1:c1c1b2653460588cb1c76fffc68dce002ae3b6f9)\n@@ -1,10 +1,12 @@\n-#include \"rsh.h\"\n-\n-#include <string.h>\n  #include <sys/types.h>\n  #include <sys/socket.h>\n\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n+\n+#include \"rsh.h\"\n\n  #define COMMAND_SIZE 4096\n\nIndex: sha1_file.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/sha1_file.c  (mode:100644 \nsha1:2bc1590efa688e7a4d7b3a89d3bffb1482e7302a)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/sha1_file.c  (mode:100644 \nsha1:7b25a17e78fd114f0436bc48f4b52d3852266153)\n@@ -6,7 +6,16 @@\n   * This handles basic git sha1 object files - packing, unpacking,\n   * creation etc.\n   */\n-#include <stdarg.h>\n+#include <sys/types.h>\n+#include <sys/uio.h>\n+\n+#include <errno.h>\n+#include <fcntl.h>\n+//#include <stdarg.h>\n+#include <stdio.h>\n+#include <string.h>\n+#include <unistd.h>\n+\n  #include \"cache.h\"\n\n  const char *sha1_file_directory = NULL;\n@@ -64,6 +73,7 @@\n   * NOTE! This returns a statically allocated buffer, so you have to be\n   * careful about using it. Do a \"strdup()\" if you need to save the\n   * filename.\n+ * NOTE! This is not thread safe.\n   */\n  char *sha1_file_name(const unsigned char *sha1)\n  {\n@@ -71,7 +81,7 @@\n  \tstatic char *name, *base;\n\n  \tif (!base) {\n-\t\tchar *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : \nDEFAULT_DB_ENVIRONMENT;\n+\t\tchar *sha1_file_directory = get_object_directory();\n  \t\tint len = strlen(sha1_file_directory);\n  \t\tbase = xmalloc(len + 60);\n  \t\tmemcpy(base, sha1_file_directory, len);\n@@ -109,22 +119,27 @@\n  \tstruct stat st;\n  \tvoid *map;\n  \tint fd;\n+\tmode_t mode = O_RDONLY | sha1_file_open_flag;\n\n-\tfd = open(filename, O_RDONLY | sha1_file_open_flag);\n+\tfd = open(filename, mode);\n  \tif (fd < 0) {\n-\t\t/* See if it works without O_NOATIME */\n+\t\t/* See if it works without special flags */\n  \t\tswitch (sha1_file_open_flag) {\n  \t\tdefault:\n-\t\t\tfd = open(filename, O_RDONLY);\n+\t\t\tmode = O_RDONLY;\n+\t\t\tfd = open(filename, mode);\n  \t\t\tif (fd >= 0)\n  \t\t\t\tbreak;\n-\t\t/* Fallthrough */\n+\t\t\t/* FALLTHROUGH */\n  \t\tcase 0:\n-\t\t\tperror(filename);\n+\t\t\terror(\"Unable to open(%s, %o): %s\", filename, mode,\n+\t\t\t     strerror(errno));\n  \t\t\treturn NULL;\n  \t\t}\n\n-\t\t/* If it failed once, it will probably fail again. Stop using \nO_NOATIME */\n+\t\t/*\n+\t\t * If it failed once, it will probably fail again.\n+\t\t *  Stop using O_NOATIME */\n  \t\tsha1_file_open_flag = 0;\n  \t}\n  \tif (fstat(fd, &st) < 0) {\n@@ -144,7 +159,7 @@\n  \tint ret, bytes;\n  \tz_stream stream;\n  \tchar buffer[8192];\n-\tchar *buf;\n+\tchar *buf = NULL;\n\n  \t/* Get the data stream */\n  \tmemset(&stream, 0, sizeof(stream));\n@@ -154,23 +169,38 @@\n  \tstream.avail_out = sizeof(buffer);\n\n  \tinflateInit(&stream);\n-\tret = inflate(&stream, 0);\n-\tif (ret < Z_OK)\n-\t\treturn NULL;\n-\tif (sscanf(buffer, \"%10s %lu\", type, size) != 2)\n-\t\treturn NULL;\n+\tret = inflate(&stream, Z_SYNC_FLUSH);\n+\tif (ret < Z_OK) {\n+\t\terror(\"Error during initial inflate.\");\n+\t\tgoto end;\n+\t}\n+\tif (sscanf(buffer, \"%10s %lu\", type, size) != 2) {\n+\t\terror(\"Malformed object.\");\n+\t\tgoto end;\n+\t}\n\n  \tbytes = strlen(buffer) + 1;\n-\tbuf = xmalloc(*size);\n+\tbuf = malloc(*size);\n+\tif (NULL == buf) {\n+\t\terror(\"Unable to malloc(%d): %s\", *size, strerror(errno));\n+\t\tgoto end;\n+\t}\n\n  \tmemcpy(buf, buffer + bytes, stream.total_out - bytes);\n  \tbytes = stream.total_out - bytes;\n-\tif (bytes < *size && ret == Z_OK) {\n+\tif (bytes < *size && Z_OK == ret) {\n  \t\tstream.next_out = buf + bytes;\n  \t\tstream.avail_out = *size - bytes;\n-\t\twhile (inflate(&stream, Z_FINISH) == Z_OK)\n+\t\twhile (Z_OK == (ret = inflate(&stream, Z_FINISH)))\n  \t\t\t/* nothing */;\n  \t}\n+\n+\tif (Z_STREAM_END != ret) {\n+\t\tfree(buf);\n+\t\tbuf = NULL;\n+\t\terror(\"Error during inflate.\");\n+\t}\n+end:\n  \tinflateEnd(&stream);\n  \treturn buf;\n  }\n@@ -231,8 +261,8 @@\n\n  int write_sha1_file(char *buf, unsigned long len, const char *type, \nunsigned char *returnsha1)\n  {\n-\tint size;\n-\tchar *compressed;\n+\tint size, ret;\n+\tchar *compressed = NULL;\n  \tz_stream stream;\n  \tunsigned char sha1[20];\n  \tSHA_CTX c;\n@@ -249,14 +279,16 @@\n  \tSHA1_Update(&c, buf, len);\n  \tSHA1_Final(sha1, &c);\n\n-\tif (returnsha1)\n+\tif (NULL != returnsha1)\n  \t\tmemcpy(returnsha1, sha1, 20);\n\n  \tfilename = sha1_file_name(sha1);\n  \tfd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);\n  \tif (fd < 0) {\n-\t\tif (errno != EEXIST)\n-\t\t\treturn -1;\n+\t\tif (EEXIST != errno)\n+\t\t\treturn error(\n+\"Cannot open(%s, O_WRONLY | O_CREAT | O_EXCL, 0666):\\n%s\",\n+\t\t\t\t    filename, strerror(errno));\n\n  \t\t/*\n  \t\t * We might do collision checking here, but we'd need to\n@@ -267,9 +299,17 @@\n\n  \t/* Set it up */\n  \tmemset(&stream, 0, sizeof(stream));\n-\tdeflateInit(&stream, Z_BEST_COMPRESSION);\n+\tif(Z_OK != deflateInit(&stream, Z_BEST_COMPRESSION)) {\n+\t\tunlink(filename);\n+\t\tret = error(\"deflateInit(&stream, Z_BEST_COMPRESSION) failed\");\n+\t\tgoto end;\n+        }\n  \tsize = deflateBound(&stream, len+hdrlen);\n-\tcompressed = xmalloc(size);\n+\tcompressed = malloc(size);\n+\tif (NULL == compressed) {\n+\t\tret = error(\"Unable to malloc(%d):%s\", size, strerror(errno));\n+\t\tgoto end;\n+\t}\n\n  \t/* Compress it */\n  \tstream.next_out = compressed;\n@@ -278,23 +318,39 @@\n  \t/* First header.. */\n  \tstream.next_in = hdr;\n  \tstream.avail_in = hdrlen;\n-\twhile (deflate(&stream, 0) == Z_OK)\n+\twhile (Z_OK == (ret = deflate(&stream, Z_NO_FLUSH)))\n  \t\t/* nothing */\n+\tif (Z_STREAM_END != ret) {\n+\t\tret = error(\"Error deflating header\");\n+\t\tgoto end;\n+\t}\n\n  \t/* Then the data itself.. */\n  \tstream.next_in = buf;\n  \tstream.avail_in = len;\n-\twhile (deflate(&stream, Z_FINISH) == Z_OK)\n+\twhile (Z_OK == (ret = deflate(&stream, Z_FINISH)))\n  \t\t/* nothing */;\n-\tdeflateEnd(&stream);\n-\tsize = stream.total_out;\n+\tif (Z_STREAM_END != ret) {\n+\t\tret = error(\"Error deflating data\");\n+\t\tgoto end;\n+\t}\n\n-\tif (write(fd, compressed, size) != size)\n-\t\tdie(\"unable to write file\");\n+\tsize = write(fd, compressed, stream.total_out);\n+\tret = 0;\n+\tif (stream.total_out != size) {\n+\t\tif (0 > size)\n+\t\t\tret = error(\"Error writing to %s:%s\", filename,\n+\t\t\t\t   strerror(errno));\n+\t\telse\n+\t\t\tret = error(\"Unexpected short write to %s\", filename);\n+\t}\n+end:\n  \tfree(compressed);\n+\tdeflateEnd(&stream);\n  \tclose(fd);\n+\tif (0 != ret) unlink(filename);\n  \t\t\n-\treturn 0;\n+\treturn ret;\n  }\n\n  static inline int collision_check(char *filename, void *buf, unsigned \nint size)\n@@ -354,7 +410,6 @@\n  int write_sha1_from_fd(const unsigned char *sha1, int fd)\n  {\n  \tchar *filename = sha1_file_name(sha1);\n-\n  \tint local;\n  \tz_stream stream;\n  \tunsigned char real_sha1[20];\n@@ -366,7 +421,9 @@\n  \tlocal = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);\n\n  \tif (local < 0)\n-\t\treturn error(\"Couldn't open %s\\n\", filename);\n+\t\treturn error(\n+\"Error open(%s, O_WRONLY | O_CREAT | O_EXCL, 0666):\\n%s\",\n+\t\t\t    filename, strerror(errno));\n\n  \tmemset(&stream, 0, sizeof(stream));\n\n@@ -377,13 +434,12 @@\n  \tdo {\n  \t\tssize_t size;\n  \t\tsize = read(fd, buf, 4096);\n-\t\tif (size <= 0) {\n+\t\tif (0 >= size) {\n  \t\t\tclose(local);\n  \t\t\tunlink(filename);\n-\t\t\tif (!size)\n-\t\t\t\treturn error(\"Connection closed?\");\n-\t\t\tperror(\"Reading from connection\");\n-\t\t\treturn -1;\n+\t\t\tif (0 == size) return error(\"Connection closed?\");\n+\t\t\treturn error(\"Reading from connection: %s\",\n+\t\t\t\t    strerror(errno));\n  \t\t}\n  \t\twrite(local, buf, size);\n  \t\tstream.avail_in = size;\nIndex: show-diff.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/show-diff.c  (mode:100644 \nsha1:b13dbabcbd48b509d71bf36ff75ee569005e0d83)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/show-diff.c  (mode:100644 \nsha1:9c44d7b5e0c62f54cf59b50565837bc04e20a9e6)\n@@ -3,6 +3,10 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <errno.h>\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n  #include \"diff.h\"\n\n@@ -88,7 +92,7 @@\n  \t * Otherwise argv[1] .. argv[argc-1] have the explicit paths.\n  \t */\n  \tif (entries < 0) {\n-\t\tperror(\"read_cache\");\n+\t\terror(\"read_cache: %s\", strerror(errno));\n  \t\texit(1);\n  \t}\n\n@@ -113,7 +117,7 @@\n\n  \t\tif (stat(ce->name, &st) < 0) {\n  \t\t\tif (errno != ENOENT) {\n-\t\t\t\tperror(ce->name);\n+\t\t\t\terror(\"%s: %s\", ce->name, strerror(errno));\n  \t\t\t\tcontinue;\n  \t\t\t}\t\n  \t\t\tif (silent)\nIndex: show-files.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/show-files.c  (mode:100644 \nsha1:dd9ea231ef0e5af1d55f16add1d2f265a9bcc904)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/show-files.c  (mode:100644 \nsha1:12771b89e32c841dc2227fe624ab0ad1d623ef77)\n@@ -6,6 +6,8 @@\n   * Copyright (C) Linus Torvalds, 2005\n   */\n  #include <dirent.h>\n+#include <string.h>\n+#include <stdio.h>\n\n  #include \"cache.h\"\n\nIndex: tree.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/tree.c  (mode:100644 \nsha1:7c55bb9bfa1565dc9df5cab31207a02004d7fe10)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/tree.c  (mode:100644 \nsha1:9db7f13cda9e5adac67e5c258574adedc0e16e42)\n@@ -1,7 +1,10 @@\n+#include <stdlib.h>\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"tree.h\"\n  #include \"blob.h\"\n  #include \"cache.h\"\n-#include <stdlib.h>\n\n  const char *tree_type = \"tree\";\n\nIndex: unpack-file.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/unpack-file.c  \n(mode:100644 sha1:6ff3d51c182627f42b104932ecea9b95d6225a5d)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/unpack-file.c  \n(mode:100644 sha1:07f4d576b81d1abd0c906b68cc93c733cc84ccf6)\n@@ -1,3 +1,6 @@\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n\n  static char *create_temp_file(unsigned char *sha1)\nIndex: update-cache.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/update-cache.c  \n(mode:100644 sha1:30ab8972bd0d49fa0ae3fef33f68618c8b042acf)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/update-cache.c  \n(mode:100644 sha1:99b08762af96b0dd457abdf45b37b723a703b4af)\n@@ -3,7 +3,11 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <errno.h>\n  #include <signal.h>\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n\n  /*\nIndex: usage.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/usage.c  (mode:100644 \nsha1:e774d2ef32726af0707d817cdb63fc8751ddc9d8)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/usage.c  (mode:100644 \nsha1:58285fca616c970e2e76d4251197b5ee0e0baa3e)\n@@ -4,6 +4,7 @@\n   * Copyright (C) Linus Torvalds, 2005\n   */\n  #include <stdarg.h>\n+#include <stdio.h>\n  #include \"cache.h\"\n\n  static void report(const char *prefix, const char *err, va_list params)\n@@ -37,4 +38,28 @@\n  \treport(\"error: \", err, params);\n  \tva_end(params);\n  \treturn -1;\n+}\n+\n+void\n+warning(const char *err, ...)\n+{\n+\tva_list params;\n+\n+\tva_start(params, err);\n+\treport(\"warning: \", err, params);\n+\tva_end(params);\n+\n+\treturn;\n+}\n+\n+void\n+message(const char *msg, ...)\n+{\n+\tva_list params;\n+\n+\tva_start(params, msg);\n+\treport(\"\", msg, params);\n+\tva_end(params);\n+\n+\treturn;\n  }\nIndex: write-tree.c\n===================================================================\n--- c3aa1e6b53cc59d5fbe261f3f859584904ae3a63/write-tree.c  (mode:100644 \nsha1:168352853d37bdca71d68ad8312b87b84477dea1)\n+++ 86241f0cd550785e3cba0a235f7e423d0503be93/write-tree.c  (mode:100644 \nsha1:9349a974498bc9a52810d5c5203c0f27952ff14b)\n@@ -3,6 +3,10 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <errno.h>\n+#include <stdio.h>\n+#include <string.h>\n+\n  #include \"cache.h\"\n\n  static int check_valid_sha1(unsigned char *sha1)\n@@ -12,8 +16,8 @@\n\n  \t/* If we were anal, we'd check that the sha1 of the contents actually \nmatches */\n  \tret = access(filename, R_OK);\n-\tif (ret)\n-\t\tperror(filename);\n+\tif (0 != ret)\n+\t\terror(\"access(%s, R_OK): %s\", filename, strerror(errno));\n  \treturn ret;\n  }\n\n@@ -97,10 +101,10 @@\n  \t\tstruct cache_entry *ce = active_cache[i];\n  \t\tif (ntohs(ce->ce_flags) & ~CE_NAMEMASK) {\n  \t\t\tif (++unmerged > 10) {\n-\t\t\t\tfprintf(stderr, \"...\\n\");\n+\t\t\t\tmessage(\"...\");\n  \t\t\t\tbreak;\n  \t\t\t}\n-\t\t\tfprintf(stderr, \"%s: unmerged (%s)\\n\", ce->name, \nsha1_to_hex(ce->sha1));\n+\t\t\tmessage(\"%s: unmerged (%s)\\n\", ce->name, sha1_to_hex(ce->sha1));\n  \t\t}\n  \t}\n  \tif (unmerged)\n\n\n\f\n!-------------------------------------------------------------flip-\n\n\nAdded two report functions (warning, message) to usage.c.\nTried to correct all usages of xmalloc, xrealloc where simple dying\nwas not an option IMHO.\n\n---\ncommit c0dc464ff1c7c28e263fbccafa3e7965417b002b\ntree 8d544bed2cad3eb71e31dfee64544972beb49f3a\nparent f03e0239ea6ae31d8f89a4a7965ed90832770c5b\nauthor Robert Suetterlin <krs@robert3.mpe-garching.mpg.de> 1114778282 \n+0200\ncommitter Robert Suetterlin <krs@robert3.mpe-garching.mpg.de> \n1114778282 +0200\n\nIndex: Makefile\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/Makefile  (mode:100644 \nsha1:3c461e439a8731b688533dccbc5dafa1471a5b3b)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/Makefile  (mode:100644 \nsha1:6133db7c39995dee2dc4f4f1bfef88bc12b8f0c6)\n@@ -21,7 +21,7 @@\n\n  # DEFINES += -DNSEC\n\n-CFLAGS=-g -O2 -Wall\n+CFLAGS=-g -O -pipe -Wall -Werror\n  CFLAGS+=$(DEFINES)\n\n  # Should be changed to /usr/local\nIndex: cache.h\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/cache.h  (mode:100644 \nsha1:036d6b6897ef119b4de051d957b5886680bfffe3)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/cache.h  (mode:100644 \nsha1:5651f0b8e3181f9369467ee7c9203ce513c8cd79)\n@@ -1,24 +1,11 @@\n  #ifndef CACHE_H\n  #define CACHE_H\n\n-#include <sys/types.h>\n-#include <sys/mman.h>\n-#include <sys/param.h>\n-#include <sys/stat.h>\n-\n-#include <netinet/in.h>\n-\n-//#include <errno.h>\n-#include <fcntl.h>\n-#include <stdarg.h>\n-#include <stddef.h>\n-#include <stdlib.h>\n-//#include <stdio.h>\n-//#include <string.h>\n-#include <unistd.h>\n-\n-#include SHA1_HEADER\n-#include <zlib.h>\n+//FIX: these two includes are either unnecesary, or like stddef\n+// should be #included in the files that use the feature supplied\n+// by the header file.\n+#include <stddef.h> /* defines offsetof(), used in cache_entry_size()  \n*/\n+#include <stdlib.h> /* needed by inline functions xmalloc and xrealloc \n*/\n\n  #if ZLIB_VERNUM < 0x1200\n  #define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) \n+ 11)\nIndex: cat-file.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/cat-file.c  (mode:100644 \nsha1:568cfbc266416f8afa88995a90a3d17132e8db8b)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/cat-file.c  (mode:100644 \nsha1:b3edf2adc27e49d84cd1ca7a6d550d5b17d6b3e7)\n@@ -5,6 +5,7 @@\n   */\n  #include <errno.h>\n  #include <string.h>\n+#include <unistd.h>\n\n  #include \"cache.h\"\n\nIndex: check-files.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/check-files.c  \n(mode:100644 sha1:438fa7fc084b94a5fe6dd0a4200b69d1ef147b75)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/check-files.c  \n(mode:100644 sha1:cc336ff2fe90fc381de0d5b1eeccf9d8d9c41dec)\n@@ -6,7 +6,10 @@\n   *\n   * Copyright (C) 2005 Linus Torvalds\n   */\n+#include <sys/stat.h>\n+\n  #include <errno.h>\n+#include <fcntl.h>\n  #include <string.h>\n\n  #include \"cache.h\"\nIndex: checkout-cache.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/checkout-cache.c  \n(mode:100644 sha1:c6576f35685a0a83627af2fa5edba742a103c2ad)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/checkout-cache.c  \n(mode:100644 sha1:fd42b25c405c124d2c44a72de32061bf3fecbbd0)\n@@ -32,11 +32,14 @@\n   * of \"-a\" causing problems (not possible in the above example,\n   * but get used to it in scripting!).\n   */\n+#include <sys/param.h>\n  #include <sys/types.h>\n  #include <sys/stat.h>\n\n  #include <errno.h>\n+#include <fcntl.h>\n  #include <string.h>\n+#include <unistd.h>\n\n  #include \"cache.h\"\n\nIndex: commit-tree.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/commit-tree.c  \n(mode:100644 sha1:e396ca80f6a87c09130b19fc5452605ac7d83861)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/commit-tree.c  \n(mode:100644 sha1:c4c20dfd1129e13171af325ac1a076926aa45370)\n@@ -6,8 +6,10 @@\n  #include <ctype.h>\n  #include <pwd.h>\n  #include <time.h>\n-#include <stdio.h>\n+#include <stdarg.h> /* add_buffer */\n+#include <stdio.h> /* add_buffer, parse_rfc2822_date, main */\n  #include <string.h>\n+#include <unistd.h>\n\n  #include \"cache.h\"\n\n@@ -273,7 +275,7 @@\n  \t\tparents++;\n  \t}\n  \tif (!parents)\n-\t\tfprintf(stderr, \"Committing initial tree %s\\n\", argv[1]);\n+\t\tmessage(\"Committing initial tree %s\\n\", argv[1]);\n  \tpw = getpwuid(getuid());\n  \tif (!pw)\n  \t\tdie(\"You don't exist. Go away!\");\nIndex: convert-cache.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/convert-cache.c  \n(mode:100644 sha1:e50248857b1b707a771a42fe4ce7f7dcc5fdc79f)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/convert-cache.c  \n(mode:100644 sha1:be0bbb67d15a825568eee3fca38d68780bfa6f5c)\n@@ -1,4 +1,6 @@\n  #define _XOPEN_SOURCE /* glibc2 needs this */\n+#include <sys/stat.h>\n+\n  #include <ctype.h>\n  #include <time.h>\n  #include <stdio.h>\nIndex: diff-cache.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/diff-cache.c  (mode:100644 \nsha1:1177977fba4d3d2fcd3cd6ec72451abb84d414a1)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/diff-cache.c  (mode:100644 \nsha1:d821c62811c2c968ec81f0eba6794bfcab5ed8f0)\n@@ -1,3 +1,5 @@\n+#include <sys/stat.h>\n+\n  #include <stdio.h>\n  #include <string.h>\n\nIndex: diff-tree-helper.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/diff-tree-helper.c  \n(mode:100644 sha1:4071871b6d3416d02d6203c47950d482d79c78f4)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/diff-tree-helper.c  \n(mode:100644 sha1:d86042af7ad940ae20e3d24af2d688dde294a3a3)\n@@ -1,6 +1,8 @@\n  /*\n   * Copyright (C) 2005 Junio C Hamano\n   */\n+#include <sys/param.h>\n+\n  #include <stdio.h>\n  #include <string.h>\n\nIndex: diff-tree.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/diff-tree.c  (mode:100644 \nsha1:ac026816c32cb44d43025236409655f97197aff2)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/diff-tree.c  (mode:100644 \nsha1:4236f5baa23d158064544373d0d44984053899f5)\n@@ -1,3 +1,5 @@\n+#include <sys/stat.h>\n+\n  #include <stdio.h>\n  #include <string.h>\n\nIndex: diff.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/diff.c  (mode:100644 \nsha1:0326f0488d2c037f238f9012d8a007473b13b968)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/diff.c  (mode:100644 \nsha1:e6680ebb5ea4f6451f4dcd6ce8edadb768b96c0a)\n@@ -2,11 +2,14 @@\n   * Copyright (C) 2005 Junio C Hamano\n   */\n  #include <sys/types.h>\n+#include <sys/param.h>\n+#include <sys/stat.h>\n  #include <sys/wait.h>\n\n  #include <errno.h>\n  #include <stdio.h>\n  #include <string.h>\n+#include <unistd.h>\n\n  #include \"cache.h\"\n  #include \"diff.h\"\nIndex: fsck-cache.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/fsck-cache.c  (mode:100644 \nsha1:265319c97cc68313e3e62d304c38d074dc22abdb)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/fsck-cache.c  (mode:100644 \nsha1:7a16928924425b142d5350375e1c949bfee0066e)\n@@ -1,4 +1,5 @@\n  #include <sys/types.h>\n+#include <sys/mman.h> /* needed to munmap a map returned by \nmap_sha1_file */\n\n  #include <dirent.h>\n  #include <stdio.h>\n@@ -135,6 +136,7 @@\n  \treturn 0;\n  }\n\n+// FIX: uses map_sha1_file and unpack_sha1_file, never frees buffer\n  static int fsck_name(char *hex)\n  {\n  \tunsigned char sha1[20];\nIndex: git-mktag.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/git-mktag.c  (mode:100644 \nsha1:ae0d0c560a6da9ace877b25e7691745d660a9e3a)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/git-mktag.c  (mode:100644 \nsha1:72e1eb38fb562c0275b4903dcc23e05f14f1532c)\n@@ -1,3 +1,6 @@\n+#include <sys/types.h> /* only needed for mman.h */\n+#include <sys/mman.h> /*only needed for munmap */\n+\n  #include <string.h>\n  #include <stdio.h>\n\n@@ -25,6 +28,7 @@\n  /*\n   * We refuse to tag something we can't verify. Just because.\n   */\n+// FIX: uses map_sha1_file and unpack_sha1_file\n  static int verify_object(unsigned char *sha1, const char \n*expected_type)\n  {\n  \tint ret = -1;\n@@ -115,7 +119,7 @@\n  \t\tusage(\"cat <signaturefile> | git-mktag\");\n\n  \t// Read the signature\n-\tsize = read(0, buffer, MAXSIZE);\n+\tsize = fread(buffer, MAXSIZE, 1, stdin);\n\n  \t// Verify it for some basic sanity: it needs to start with \"object \n<sha1>\\ntag \"\n  \tif (verify_tag(buffer, size) < 0)\nIndex: http-pull.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/http-pull.c  (mode:100644 \nsha1:192dcc370dee47c52c72915394bb6f2a79f64e12)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/http-pull.c  (mode:100644 \nsha1:fba8940c5435eb05e1093172b7ebb20e1795761b)\n@@ -1,14 +1,17 @@\n+#include <errno.h>\n  #include <fcntl.h>\n-#include <unistd.h>\n-#include <string.h>\n  #include <stdlib.h>\n-#include \"cache.h\"\n-#include \"commit.h\"\n-#include <errno.h>\n  #include <stdio.h>\n+#include <string.h>\n+#include <unistd.h>\n\n  #include <curl/curl.h>\n  #include <curl/easy.h>\n+#include SHA1_HEADER\n+#include <zlib.h>\n+\n+#include \"cache.h\"\n+#include \"commit.h\"\n\n  static CURL *curl;\n\n@@ -93,7 +96,7 @@\n  \tclose(local);\n  \tinflateEnd(&stream);\n  \tSHA1_Final(real_sha1, &c);\n-\tif (zret != Z_STREAM_END) {\n+\tif (Z_STREAM_END != zret) {\n  \t\tunlink(filename);\n  \t\treturn error(\"File %s (%s) corrupt\\n\", hex, url);\n  \t}\nIndex: init-db.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/init-db.c  (mode:100644 \nsha1:e4ce5acea8efb8488a2557d022ca9673a6d5170a)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/init-db.c  (mode:100644 \nsha1:aba6b2630dfac2f8a3d58db776fe7c1c0535dd3a)\n@@ -3,6 +3,8 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <sys/stat.h>\n+\n  #include <errno.h>\n  #include <stdio.h>\n  #include <string.h>\nIndex: ls-tree.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/ls-tree.c  (mode:100644 \nsha1:94e3780d30bbdbb33693d2d767e81b26c83bd612)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/ls-tree.c  (mode:100644 \nsha1:ea2c7ce121d203711e54efb3bd41788c60c7f3ef)\n@@ -3,6 +3,8 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <sys/stat.h>\n+\n  #include <stdio.h>\n  #include <string.h>\n\nIndex: merge-cache.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/merge-cache.c  \n(mode:100644 sha1:8c944f383e699677e92d945d61e85ebf1a125321)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/merge-cache.c  \n(mode:100644 sha1:281ae0ab5da499a68f5072e197f2d38980b87b8a)\n@@ -1,8 +1,10 @@\n  #include <sys/types.h>\n+#include <sys/stat.h>\n  #include <sys/wait.h>\n\n  #include <stdio.h>\n  #include <string.h>\n+#include <unistd.h>\n\n  #include \"cache.h\"\n\nIndex: read-cache.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/read-cache.c  (mode:100644 \nsha1:2c2f85a3cdad584eddfa2a1db23d122630f14a75)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/read-cache.c  (mode:100644 \nsha1:0e99d46b859a13b9aaceb497488e6255a883663b)\n@@ -3,9 +3,16 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <sys/types.h>\n+#include <sys/mman.h>\n+#include <sys/stat.h>\n+\n  #include <errno.h>\n+#include <fcntl.h>\n  #include <string.h>\n+#include <unistd.h>\n\n+#include SHA1_HEADER\n  #include \"cache.h\"\n\n  struct cache_entry **active_cache = NULL;\nIndex: read-tree.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/read-tree.c  (mode:100644 \nsha1:8b2de2a750328f82759c23c6ec35e35cfac42689)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/read-tree.c  (mode:100644 \nsha1:d3dbbd2bc1f85554b6ea96edf7e7ffee73afd79e)\n@@ -3,8 +3,12 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <sys/param.h>\n+\n+#include <fcntl.h>\n  #include <stdio.h>\n  #include <string.h>\n+#include <unistd.h>\n\n  #include \"cache.h\"\n\nIndex: rpush.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/rpush.c  (mode:100644 \nsha1:83b197668b1b61ccbe73aa88084adcf8ee1e807e)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/rpush.c  (mode:100644 \nsha1:bb67e578915c571523d49d2b7ae6344ee45b2853)\n@@ -1,7 +1,9 @@\n  #include <sys/types.h>\n  #include <sys/socket.h>\n+\n  #include <errno.h>\n  #include <string.h>\n+#include <unistd.h>\n\n  #include \"cache.h\"\n  #include \"rsh.h\"\nIndex: rsh.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/rsh.c  (mode:100644 \nsha1:c1c1b2653460588cb1c76fffc68dce002ae3b6f9)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/rsh.c  (mode:100644 \nsha1:85f508a6ae6f9dfcc215a8468bc2cb337cf94c4f)\n@@ -3,6 +3,7 @@\n\n  #include <stdio.h>\n  #include <string.h>\n+#include <unistd.h>\n\n  #include \"cache.h\"\n\nIndex: sha1_file.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/sha1_file.c  (mode:100644 \nsha1:7b25a17e78fd114f0436bc48f4b52d3852266153)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/sha1_file.c  (mode:100644 \nsha1:0f07a82f7ab5a6bc89dec0d912cf5ce2d7fff64a)\n@@ -7,15 +7,19 @@\n   * creation etc.\n   */\n  #include <sys/types.h>\n-#include <sys/uio.h>\n+#include <sys/mman.h>\n+#include <sys/stat.h>\n+//#include <sys/uio.h>\n\n  #include <errno.h>\n  #include <fcntl.h>\n-//#include <stdarg.h>\n  #include <stdio.h>\n  #include <string.h>\n  #include <unistd.h>\n\n+#include SHA1_HEADER\n+#include <zlib.h>\n+\n  #include \"cache.h\"\n\n  const char *sha1_file_directory = NULL;\n@@ -319,9 +323,9 @@\n  \tstream.next_in = hdr;\n  \tstream.avail_in = hdrlen;\n  \twhile (Z_OK == (ret = deflate(&stream, Z_NO_FLUSH)))\n-\t\t/* nothing */\n-\tif (Z_STREAM_END != ret) {\n-\t\tret = error(\"Error deflating header\");\n+\t\t/* nothing */;\n+\tif (Z_STREAM_ERROR == ret) {\n+\t\tret = error(\"deflate(header): inconsistent stream state.\");\n  \t\tgoto end;\n  \t}\n\n@@ -457,7 +461,7 @@\n\n  \tclose(local);\n  \tSHA1_Final(real_sha1, &c);\n-\tif (ret != Z_STREAM_END) {\n+\tif (Z_STREAM_END != ret) {\n  \t\tunlink(filename);\n  \t\treturn error(\"File %s corrupted\", sha1_to_hex(sha1));\n  \t}\nIndex: show-diff.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/show-diff.c  (mode:100644 \nsha1:9c44d7b5e0c62f54cf59b50565837bc04e20a9e6)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/show-diff.c  (mode:100644 \nsha1:187cadd510e86018493061addd72465d9c920caa)\n@@ -3,6 +3,8 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <sys/stat.h>\n+\n  #include <errno.h>\n  #include <stdio.h>\n  #include <string.h>\nIndex: show-files.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/show-files.c  (mode:100644 \nsha1:12771b89e32c841dc2227fe624ab0ad1d623ef77)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/show-files.c  (mode:100644 \nsha1:c54ffdd14b1815381d1957cab9ce0499c9739691)\n@@ -5,6 +5,9 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <sys/param.h>\n+#include <sys/stat.h>\n+\n  #include <dirent.h>\n  #include <string.h>\n  #include <stdio.h>\nIndex: tree.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/tree.c  (mode:100644 \nsha1:9db7f13cda9e5adac67e5c258574adedc0e16e42)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/tree.c  (mode:100644 \nsha1:f9762bf82e528ad3842c8ada4d8ccaf5acce96c3)\n@@ -1,3 +1,5 @@\n+#include <sys/stat.h>\n+\n  #include <stdlib.h>\n  #include <stdio.h>\n  #include <string.h>\nIndex: unpack-file.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/unpack-file.c  \n(mode:100644 sha1:07f4d576b81d1abd0c906b68cc93c733cc84ccf6)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/unpack-file.c  \n(mode:100644 sha1:5a867509ba1f7276cdeec6ff46fd6fd0f89c80c8)\n@@ -1,5 +1,6 @@\n  #include <stdio.h>\n  #include <string.h>\n+#include <unistd.h>\n\n  #include \"cache.h\"\n\nIndex: update-cache.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/update-cache.c  \n(mode:100644 sha1:99b08762af96b0dd457abdf45b37b723a703b4af)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/update-cache.c  \n(mode:100644 sha1:78d4ba3fbbf548b85935a6fb83f445dcb5eec3c0)\n@@ -3,10 +3,20 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <sys/types.h> /* FIX: used in index_fd, should be part of \nsha1_file */\n+#include <sys/mman.h> /* FIX: used in index_fd, should be part of \nsha1_file */\n+#include <sys/param.h> /* only used in main for MAXPATHLEN */\n+#include <sys/stat.h>\n+\n  #include <errno.h>\n+#include <fcntl.h>\n  #include <signal.h>\n  #include <stdio.h>\n  #include <string.h>\n+#include <unistd.h>\n+\n+#include SHA1_HEADER /* FIX: used in index_fd, should be part of \nsha1_file */\n+#include <zlib.h> /* FIX: used in index_fd, should be part of \nsha1_file */\n\n  #include \"cache.h\"\n\n@@ -79,7 +89,7 @@\n  \tstream.next_in = in;\n  \tstream.avail_in = size;\n  \twhile (deflate(&stream, Z_FINISH) == Z_OK)\n-\t\t/*nothing */;\n+\t\t/* nothing */;\n\n  \tdeflateEnd(&stream);\n  \t\n@@ -115,7 +125,7 @@\n\n  \tfd = open(path, O_RDONLY);\n  \tif (fd < 0) {\n-\t\tif (errno == ENOENT) {\n+\t\tif (ENOENT == errno) {\n  \t\t\tif (allow_remove)\n  \t\t\t\treturn remove_file_from_cache(path);\n  \t\t}\n@@ -123,7 +133,7 @@\n  \t}\n  \tif (fstat(fd, &st) < 0) {\n  \t\tclose(fd);\n-\t\treturn -1;\n+\t\treturn error(\"fstat(\\\"%s\\\"): %s\", path, strerror(errno));\n  \t}\n  \tif (S_ISDIR(st.st_mode)) {\n  \t\tfprintf(stderr, \"'%s' is a directory, ignoring\\n\", path);\nIndex: write-tree.c\n===================================================================\n--- 86241f0cd550785e3cba0a235f7e423d0503be93/write-tree.c  (mode:100644 \nsha1:9349a974498bc9a52810d5c5203c0f27952ff14b)\n+++ 8d544bed2cad3eb71e31dfee64544972beb49f3a/write-tree.c  (mode:100644 \nsha1:1f32b3ab5b482b68934e1d8373b253750962a027)\n@@ -3,9 +3,12 @@\n   *\n   * Copyright (C) Linus Torvalds, 2005\n   */\n+#include <sys/stat.h>\n+\n  #include <errno.h>\n  #include <stdio.h>\n  #include <string.h>\n+#include <unistd.h>\n\n  #include \"cache.h\"\n\n\n\n\f\n!-------------------------------------------------------------flip-\n\n\n"},{"id":"2140","messageId":"20050429182407.5f6afd15.froese@gmx.de","threadId":"388","inReplyTo":"bfbb1f23220e1918afe70d4cf3de24f5@mpe.mpg.de","subject":"Re: [PATCH]: first take at cleanup of #include, xmalloc / xrealloc, git status report usage.","fromName":"Edgar Toernig","fromEmail":"froese@gmx.de","sentAt":"2005-04-29T16:24:07Z","receivedAt":"2005-04-29T16:24:07Z","isPatch":true,"sender":{"key":"froese@gmx.de","avatar":null},"body":"Robert Sütterlin wrote:\n>[...]\n> +static int\n> +create_directories(const char *path)\n>   {\n> -\tint len = strlen(path);\n> -\tchar *buf = xmalloc(len + 1);\n> -\tconst char *slash = path;\n> +\tchar *buf = (char *)path;\n> +\tchar *slash = buf;\n> \n>   \twhile ((slash = strchr(slash+1, '/')) != NULL) {\n> -\t\tlen = slash - path;\n> -\t\tmemcpy(buf, path, len);\n> -\t\tbuf[len] = 0;\n> -\t\tmkdir(buf, 0755);\n> +\t\t*slash = '\\0';\n> +\t\tif (0 != mkdir(buf, 0755))\n> +\t\t\treturn error(\"Unable to mkdir(``%s'', 0755)\", buf);\n> +\t\t*slash = '/';\n\nYou need the temp buffer.  Simply casting the const away may\nshut up the compiler but it's not correct.\n\n> -\t\tif (errno != EEXIST)\n> +\t\tif (EEXIST != errno)\n\nToo much Star Wars?  Joda-speak?\n\nCiao, ET.\n\n\nPS: the mkdir mode should be 0777 ...\n\n"},{"id":"2144","messageId":"20050429170743.GD93323@xdt04.mpe-garching.mpg.de","threadId":"388","inReplyTo":"20050429182407.5f6afd15.froese@gmx.de","subject":"Re: [PATCH]: first take at cleanup of #include, xmalloc / xrealloc, git status report usage.","fromName":"Klaus Robert Suetterlin","fromEmail":"robert@mpe.mpg.de","sentAt":"2005-04-29T17:07:43Z","receivedAt":"2005-04-29T17:07:43Z","isPatch":true,"sender":{"key":"robert@mpe.mpg.de","avatar":null},"body":"Thanks for reviewing this lengthy patch!\n\nOn Fri, Apr 29, 2005 at 06:24:07PM +0200, Edgar Toernig wrote:\n> Robert S?tterlin wrote:\n> >[...]\n> > +static int\n> > +create_directories(const char *path)\n> >   {\n> > -\tint len = strlen(path);\n> > -\tchar *buf = xmalloc(len + 1);\n> > -\tconst char *slash = path;\n> > +\tchar *buf = (char *)path;\n> > +\tchar *slash = buf;\n> > \n> >   \twhile ((slash = strchr(slash+1, '/')) != NULL) {\n> > -\t\tlen = slash - path;\n> > -\t\tmemcpy(buf, path, len);\n> > -\t\tbuf[len] = 0;\n> > -\t\tmkdir(buf, 0755);\n> > +\t\t*slash = '\\0';\n> > +\t\tif (0 != mkdir(buf, 0755))\n> > +\t\t\treturn error(\"Unable to mkdir(``%s'', 0755)\", buf);\n> > +\t\t*slash = '/';\n> \n> You need the temp buffer.  Simply casting the const away may\n> shut up the compiler but it's not correct.\n\nOk, I see!  Someone will pass a const char * (e.g. static string,\nreadonly mmap, ...).  In that case I would rather change the signature\nof the function ;).  My am I a smart ass, ain't I.\n\nWhat I wanted to achieve was getting rid of xmalloc that would just\ndie(\"horribly\") in case we cannot allocate the memory.  Or some\nother hacked up malloc / realloc alternative.\n\nAside: I really do not like the current habit of die(\"if anything does\n   not work out.\") in git code all that much.  People seem to take\n   the die() as cast in stone, and do not free resources in code\n   pathes that lead to a die(), currently.  Of course this die()\n   might change in the near future.  And we will have to examine\n   every die() very carefully to make sure Joe Lazy Programmer\n   didn't leave any garbage lying around.\n   \n   IMHO, the right thing for die() would be to dump core instead of just\n   exiting.  As die() should be called in the ``can't happen'' or rather\n   ``isn't resolved correctly'' cases only.  And programmers would be\n   able to use the core to identify these cases quickly.\n\nUnfortunately what You say is true and I cannot see a way around\nsome kind of working copy (except for forcing the caller to provide\na non-const char*).  Maybe I will put ``char scratchpath[MAXPATH + 1];''\nand ``#include <sys/param.h>'' in cache.h :).\n\n> \n> > -\t\tif (errno != EEXIST)\n> > +\t\tif (EEXIST != errno)\n> \n> Too much Star Wars?  Joda-speak?\n\nNo.  This is my prefered style for two reasons:\n\n1) Putting the non-l-value on the left hand side protects against\n   my most common typo: \"=\" instead of \"==\" or \"!=\".  No matter which\n   compiler or warning level.\n\n2) More often than not the variable part will be longer than\n   ``errno''.  So putting EEXIST and the comparator front helps seeing\n   the condition I test against.  Just compare:\n\nwhile (-1 != (ch = getopt(argc, argv, \"abcde:fg:h:ijkl:mnopq:r:stuvwx:y:z\")))\nwhile ((ch = getopt(argc, argv, \"abcde:fg:h:ijkl:mnopq:r:stuvwx:y:z\")) != -1)\n\n\n> \n> Ciao, ET.\n> \n> \n> PS: the mkdir mode should be 0777 ...\nThanks!  I did just a literal copy of what was there before.  I\nadded the check for the return value, to get the right diagnostic\noutput.\n\nKind regards,\n\n--Robert Suetterlin (robert@mpe.mpg.de)\nphone: (+49)89 / 30000-3546   fax: (+49)89 / 30000-3950\n"}]}