diff -w -u -r /tmp/avrdude-5.1/ser_posix.c ./ser_posix.c
--- /tmp/avrdude-5.1/ser_posix.c	2005-08-29 18:30:05.000000000 -0700
+++ ./ser_posix.c	2006-04-03 09:08:25.000000000 -0700
@@ -65,6 +65,9 @@
   { 0,      0 }                 /* Terminator. */
 };
 
+static struct termios original_termios;
+static int saved_original_termios;
+
 static speed_t serial_baud_lookup(long baud)
 {
   struct baud_mapping *map = baud_lookup_table;
@@ -75,7 +78,7 @@
     map++;
   }
 
-  fprintf(stderr, "%s: serial_baud_lookup(): unknown baud rate: %ld", 
+  fprintf(stderr, "%s: serial_baud_lookup(): unknown baud rate: %ld\n", 
           progname, baud);
   exit(1);
 }
@@ -87,33 +90,38 @@
   speed_t speed = serial_baud_lookup (baud);
   
   if (!isatty(fd))
-    return -1;
+    return -ENOTTY;
   
   /*
    * initialize terminal modes
    */
   rc = tcgetattr(fd, &termios);
   if (rc < 0) {
-    fprintf(stderr, "%s: ser_setspeed(): tcgetattr() failed, %s", 
-            progname, strerror(errno));
+    fprintf(stderr, "%s: ser_setspeed(): tcgetattr() failed", 
+            progname);
     return -errno;
   }
 
-  termios.c_iflag = 0;
-  termios.c_oflag = 0;
-  termios.c_cflag = 0;
+  /*
+   * copy termios for ser_close if we haven't already
+   */
+  if (! saved_original_termios++) {
+    original_termios = termios;
+  }
+
+  cfmakeraw(&termios);
+  termios.c_cflag &= ~CSIZE;
   termios.c_cflag |=   (CS8 | CREAD | CLOCAL);
-  termios.c_lflag = 0;
   termios.c_cc[VMIN]  = 1;
   termios.c_cc[VTIME] = 0;
 
   cfsetospeed(&termios, speed);
   cfsetispeed(&termios, speed);
   
-  rc = tcsetattr(fd, TCSANOW, &termios);
+  rc = tcsetattr(fd, TCSANOW | TCSAFLUSH, &termios);
   if (rc < 0) {
-    fprintf(stderr, "%s: ser_setspeed(): tcsetattr() failed, %s", 
-            progname, strerror(errno));
+    fprintf(stderr, "%s: ser_setspeed(): tcsetattr() failed", 
+            progname);
     return -errno;
   }
 
@@ -137,7 +145,7 @@
   /*
    * open the serial port
    */
-  fd = open(port, O_RDWR | O_NOCTTY /*| O_NONBLOCK*/);
+  fd = open(port, O_RDWR | O_NOCTTY | O_NONBLOCK);
   if (fd < 0) {
     fprintf(stderr, "%s: ser_open(): can't open device \"%s\": %s\n",
             progname, port, strerror(errno));
@@ -150,8 +158,8 @@
   rc = ser_setspeed(fd, baud);
   if (rc) {
     fprintf(stderr, 
-            "%s: ser_open(): can't set attributes for device \"%s\"\n",
-            progname, port);
+            "%s: ser_open(): can't set attributes for device \"%s\": %s\n",
+            progname, port, strerror(-rc));
     exit(1);
   }
 
@@ -161,7 +169,18 @@
 
 static void ser_close(int fd)
 {
-  /* FIXME: Should really restore the terminal to original state here. */
+  /*
+   * restore original termios settings from ser_open
+   */
+  if (saved_original_termios) {
+    int rc = tcsetattr(fd, TCSANOW | TCSADRAIN, &original_termios);
+    if (rc) {
+      fprintf(stderr, 
+              "%s: ser_close(): can't reset attributes for device: %s\n",
+              progname, strerror(errno));
+    }
+    saved_original_termios = 0;
+  }
 
   close(fd);
 }
