Index: xcvs-generate-passwd.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cvslog/xcvs/xcvs-generate-passwd.php,v
retrieving revision 1.1
diff -u -F^f -r1.1 xcvs-generate-passwd.php
--- xcvs-generate-passwd.php	4 Sep 2007 18:25:44 -0000	1.1
+++ xcvs-generate-passwd.php	18 Oct 2008 17:29:44 -0000
@@ -40,15 +40,47 @@
  */
 define('DRUPAL_SITE', '');
 
+/**
+ * general error function
+ */
+function error_message($msg = '') {
+  $stderr = fopen('php://stderr', 'w');
+  fwrite($stderr, "ERROR: " . basename($argv[0]) . ":" . $msg . "\n");
+  fclose($stderr);
+}
+
+/**
+ * Database error function
+ */
+function database_error_check($msg = '') {
+  $errno = mysql_errno();
+  $error = mysql_error();
+  if ($errno) {
+    error_message($msg);
+    error_message("More info: $errno:$error");
+    exit(1);
+  }
+}
+
+if ($argc == 1) {
+  $passwd = $argv[1];
+  $passwd_new = $argv[1] . '.new';
+}
+else {
+  error_message('Usage error: required argument missing: full path name for CVSROOT/passwd file');
+  exit(2);
+}
 
 // Connect to the right database.
 $connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
 mysql_select_db(DB_NAME);
+database_error_check('Could not connect to database.');
 
 // Generate a login for the anonymous user:
 $output = "anonymous:QO9EJSIJc3kwg:". CVS_USER ."\n\n";
 
 $result = mysql_query("SELECT * FROM cvs_accounts WHERE pass != '' AND status = 1 ORDER BY cvs_user");
+database_error_check('SELECT query failed.');
 while ($account = mysql_fetch_object($result)) {
   if ($account->uid) {
     $output .= "# http://". DRUPAL_SITE ."/user/$account->uid\n";
@@ -59,5 +91,14 @@
   $output .= "$account->cvs_user:$account->pass:". CVS_USER ."\n\n";
 }
 
-print $output;
-?>
+// Write the results out to the new file
+if (!file_put_contents($passwd_new, $output)) {
+  error_message('Writing to new passwd file failed!');
+  exit(3);
+}
+
+// Rename the file
+if (!rename($passwd_new, $passwd)) {
+  error_message('Renaming new passwd file failed!');
+  exit(4);
+}
