--- adserve.inc.o	2008-01-24 23:18:08.000000000 +0100
+++ adserve.inc	2008-02-29 12:07:52.000000000 +0100
@@ -265,7 +265,7 @@
       $values = $_GET;
     }
 
-    $variables->ad_dir = getcwd();
+    $variables->ad_dir = adserve_getcwd();
     // 'debug' is an integer.
     $variables->debug = isset($values['debug']) ? (int)$values['debug'] : 0;
     // Cache types are comprised of only letters.
@@ -477,7 +477,7 @@
   // Optionally set DRUPAL_ROOT at the top of this file.
   if (DRUPAL_ROOT && is_dir(DRUPAL_ROOT) &&
       file_exists(DRUPAL_ROOT .'/includes/bootstrap.inc')) {
-    chdir(DRUPAL_ROOT);
+    $cwd = (DRUPAL_ROOT);
   }
   else if (DRUPAL_ROOT && DRUPAL_ROOT != 'DRUPAL_ROOT' &&
            adserve_variable('debug')) {
@@ -485,15 +485,18 @@
   }
   else {
     // Move process to top level Drupal directory so we can perform a bootstrap.
+    $cwd = adserve_getcwd();
+    
     for ($i = 1; $i <= 20; $i++) {
-      if (file_exists('./includes/bootstrap.inc')) {
+      if (file_exists($cwd . '/includes/bootstrap.inc')) {
         break;
       }
-      chdir('..');
+      $cwd = dirname($cwd);
     }
   }
-  adserve_variable('root_dir', getcwd());
-  require_once getcwd() .'/includes/bootstrap.inc';
+  adserve_variable('root_dir', $cwd);
+  chdir($cwd);
+  require_once $cwd .'/includes/bootstrap.inc';
 }
 
 /**
@@ -640,3 +643,7 @@
 
   return $filter;
 }
+
+function adserve_getcwd() {
+  return (dirname($_SERVER['SCRIPT_FILENAME']));
+}
\ No newline at end of file
