(get_new_private_charset_id): Don't limit CHARSET_ID by WIDTH.
[bpt/emacs.git] / lib-src / b2m.c
index 526d173..4607dce 100644 (file)
 /* Made conformant to the GNU coding standards January, 1995
    by Francesco Potorti` <pot@cnuce.cnr.it>. */
 
-#include <stdio.h>
-#include <time.h>
-#include <sys/types.h>
-#ifdef MSDOS
-#include <fcntl.h>
-#endif
-
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 /* On some systems, Emacs defines static as nothing for the sake
 #undef static
 #endif
 
+#include <stdio.h>
+#include <time.h>
+#include <sys/types.h>
+#include <getopt.h>
+#ifdef MSDOS
+#include <fcntl.h>
+#endif
+
 #undef TRUE
 #define TRUE   1
 #undef FALSE
@@ -64,7 +65,7 @@ struct linebuffer
 
 extern char *strtok();
 
-char *xmalloc (), *xrealloc ();
+long *xmalloc (), *xrealloc ();
 char *concat ();
 long readline ();
 void fatal ();
@@ -78,6 +79,16 @@ void fatal ();
 
 char *progname;
 
+struct option longopts[] =
+{
+  { "help",                    no_argument,       NULL,     'h'   },
+  { "version",                 no_argument,       NULL,     'V'   },
+  { 0 }
+};
+
+extern int optind;
+
+int
 main (argc, argv)
      int argc;
      char **argv;
@@ -89,16 +100,44 @@ main (argc, argv)
 
 #ifdef MSDOS
   _fmode = O_BINARY;           /* all of files are treated as binary files */
+#if __DJGPP__ > 1
+  if (!isatty (fileno (stdout)))
+    setmode (fileno (stdout), O_BINARY);
+  if (!isatty (fileno (stdin)))
+    setmode (fileno (stdin), O_BINARY);
+#else /* not __DJGPP__ > 1 */
   (stdout)->_flag &= ~_IOTEXT;
   (stdin)->_flag &= ~_IOTEXT;
+#endif /* not __DJGPP__ > 1 */
 #endif
-  if (argc != 1)
+  progname = argv[0];
+
+  while (1)
+    {
+      int opt = getopt_long (argc, argv, "hV", longopts, 0);
+      if (opt == EOF)
+       break;
+
+      switch (opt)
+       {
+       case 'V':
+         printf ("%s (GNU Emacs %s)\n", "b2m", VERSION);
+         puts ("b2m is in the public domain.");
+         exit (GOOD);
+
+       case 'h':
+         fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
+         exit (GOOD);
+       }
+    }
+
+  if (optind != argc)
     {
       fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
       exit (GOOD);
     }
+
   labels_saved = printing = header = FALSE;
-  progname = argv[0];
   ltoday = time (0);
   today = ctime (&ltoday);
   data.size = 200;
@@ -113,7 +152,7 @@ main (argc, argv)
       if (streq (data.buffer, "*** EOOH ***") && !printing)
        {
          printing = header = TRUE;
-         printf ("From Babyl to mail by %s %s", progname, today);
+         printf ("From \"Babyl to mail by %s\" %s", progname, today);
          continue;
        }
 
@@ -203,12 +242,13 @@ readline (linebuffer, stream)
        }
       if (c == EOF)
        {
+         *p = '\0';
          chars_deleted = 0;
          break;
        }
       if (c == '\n')
        {
-         if (p[-1] == '\r' && p > buffer)
+         if (p > buffer && p[-1] == '\r')
            {
              *--p = '\0';
              chars_deleted = 2;
@@ -229,22 +269,22 @@ readline (linebuffer, stream)
 /*
  * Like malloc but get fatal error if memory is exhausted.
  */
-char *
+long *
 xmalloc (size)
      unsigned int size;
 {
-  char *result = (char *) malloc (size);
+  long *result = (long *) malloc (size);
   if (result == NULL)
     fatal ("virtual memory exhausted");
   return result;
 }
 
-char *
+long *
 xrealloc (ptr, size)
      char *ptr;
      unsigned int size;
 {
-  char *result = (char *) realloc (ptr, size);
+  long *result = (long *) realloc (ptr, size);
   if (result == NULL)
     fatal ("virtual memory exhausted");
   return result;
@@ -252,6 +292,7 @@ xrealloc (ptr, size)
 
 void
 fatal (message)
+     char *message;
 {
   fprintf (stderr, "%s: %s\n", progname, message);
   exit (BAD);