f994dfe4a8e6 — Sean Farley 12 years ago
fast-common: factor out common functions
2 files changed, 60 insertions(+), 0 deletions(-)

A => fast-hg-common.c
A => fast-hg-common.h
A => fast-hg-common.c +43 -0
@@ 0,0 1,43 @@ 
+/*
+ * Copyright 2012 Sean Farley
+ *
+ * This guy called Sean wrote this sweet code. You are hereby granted
+ * permission to do whatever you feel like doing with it on the understanding
+ * that he's not responsible for anything that results from your use of this
+ * sweet code.
+ */
+
+#include "fast-hg-common.h"
+
+void go_up_one_dir(char* dir_path) {
+  size_t k = 0;
+
+  /* if we get here then the file doesn't exist, so try the next directory up */
+  for (k = strlen(dir_path)-1; k+1; k--) {
+    if (dir_path[k] == '/') { /* obviously won't work on windows ... but what does? */
+      dir_path[k] = 0;        /* cheap hack in c: set an element of the
+                                 character array to the null-terminator '\0'
+                                 to mark the end of a new string */
+      break;
+    }
+  }
+}
+
+int map(int (*apply)(const char*)) {
+  char pwd[PATH_MAX+1], hg_path[PATH_MAX+1];
+  size_t l=0;
+  int denied=0;
+
+  getcwd(pwd, PATH_MAX+1);
+
+  while ((l = strlen(pwd)) > 0) {
+    sprintf(hg_path, "%s/.hg", pwd);
+    denied = access(hg_path, R_OK);
+
+    if (!denied) return apply(pwd);
+
+    go_up_one_dir(pwd);
+  }
+
+  return 0;
+}

          
A => fast-hg-common.h +17 -0
@@ 0,0 1,17 @@ 
+/*
+ * Copyright 2012 Sean Farley
+ *
+ * This guy called Sean wrote this sweet code. You are hereby granted
+ * permission to do whatever you feel like doing with it on the understanding
+ * that he's not responsible for anything that results from your use of this
+ * sweet code.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <limits.h>
+
+void go_up_one_dir(char* dir_path);
+int map(int (*apply)(const char*));