]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/ditto.c
ncurses 5.5
[ncurses.git] / test / ditto.c
index 8ceafe5de11a05da49d2188c5bb58c71a67a1dcc..2a338d8b50f82e25d785099ed6c527c742df95c5 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998 Free Software Foundation, Inc.                        *
+ * Copyright (c) 1998-2001,2005 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -29,7 +29,7 @@
 /*
  * Author: Thomas E. Dickey <dickey@clark.net> 1998
  *
- * $Id: ditto.c,v 1.3 1998/08/15 23:39:34 tom Exp $
+ * $Id: ditto.c,v 1.5 2005/04/16 16:35:49 tom Exp $
  *
  * The program illustrates how to set up multiple screens from a single
  * program.  Invoke the program by specifying another terminal on the same
 #include <errno.h>
 
 typedef struct {
-       FILE *input;
-       FILE *output;
-       SCREEN *screen;
+    FILE *input;
+    FILE *output;
+    SCREEN *screen;
 } DITTO;
 
 static void
 failed(const char *s)
 {
-       perror(s);
-       exit(EXIT_FAILURE);
+    perror(s);
+    ExitProgram(EXIT_FAILURE);
 }
 
 static void
 usage(void)
 {
-       fprintf(stderr, "usage: ditto [terminal1 ...]\n");
-       exit(EXIT_FAILURE);
+    fprintf(stderr, "usage: ditto [terminal1 ...]\n");
+    ExitProgram(EXIT_FAILURE);
 }
 
 static FILE *
 open_tty(char *path)
 {
-       FILE *fp;
-       struct stat sb;
+    FILE *fp;
+    struct stat sb;
 
-       if (stat(path, &sb) < 0)
-               failed(path);
-       if ((sb.st_mode & S_IFMT) != S_IFCHR) {
-               errno = ENOTTY;
-               failed(path);
-       }
-       fp = fopen(path, "a+");
-       if (fp == 0)
-               failed(path);
-       printf("opened %s\n", path);
-       return fp;
+    if (stat(path, &sb) < 0)
+       failed(path);
+    if ((sb.st_mode & S_IFMT) != S_IFCHR) {
+       errno = ENOTTY;
+       failed(path);
+    }
+    fp = fopen(path, "a+");
+    if (fp == 0)
+       failed(path);
+    printf("opened %s\n", path);
+    return fp;
 }
 
 int
-main(
-       int argc GCC_UNUSED,
-       char *argv[] GCC_UNUSED)
+main(int argc GCC_UNUSED,
+     char *argv[]GCC_UNUSED)
 {
-       int j;
-       int active_tty = 0;
-       DITTO *data;
+    int j;
+    int active_tty = 0;
+    DITTO *data;
 
-       if (argc <= 1)
-               usage();
+    if (argc <= 1)
+       usage();
 
-       if ((data = (DITTO *)calloc(argc, sizeof(DITTO))) == 0)
-               failed("calloc data");
+    if ((data = (DITTO *) calloc((unsigned) argc, sizeof(DITTO))) == 0)
+       failed("calloc data");
 
-       data[0].input = stdin;
-       data[0].output = stdout;
-       for (j = 1; j < argc; j++) {
-               data[j].input =
-               data[j].output = open_tty(argv[j]);
-       }
+    data[0].input = stdin;
+    data[0].output = stdout;
+    for (j = 1; j < argc; j++) {
+       data[j].input =
+           data[j].output = open_tty(argv[j]);
+    }
 
-       /*
-        * If we got this far, we have open connection(s) to the terminal(s).
-        * Set up the screens.
-        */
-       for (j = 0; j < argc; j++) {
-               active_tty++;
-               data[j].screen = newterm(
-                       (char *)0,      /* assume $TERM is the same */
-                       data[j].output,
-                       data[j].input);
-               if (data[j].screen == 0)
-                       failed("newterm");
-               cbreak();
-               noecho();
-               scrollok(stdscr, TRUE);
-       }
+    /*
+     * If we got this far, we have open connection(s) to the terminal(s).
+     * Set up the screens.
+     */
+    for (j = 0; j < argc; j++) {
+       active_tty++;
+       data[j].screen = newterm((char *) 0,    /* assume $TERM is the same */
+                                data[j].output,
+                                data[j].input);
+       if (data[j].screen == 0)
+           failed("newterm");
+       cbreak();
+       noecho();
+       scrollok(stdscr, TRUE);
+    }
 
-       /*
-        * Loop, reading characters from any of the inputs and writing to all
-        * of the screens.
-        */
-       for(;;) {
-               int ch;
-               set_term(data[0].screen);
-               ch = getch();
-               if (ch == ERR)
-                       continue;
-               if (ch == 4)
-                       break;
-               for (j = 0; j < argc; j++) {
-                       set_term(data[j].screen);
-                       addch(ch);
-                       refresh();
-               }
+    /*
+     * Loop, reading characters from any of the inputs and writing to all
+     * of the screens.
+     */
+    for (;;) {
+       int ch;
+       set_term(data[0].screen);
+       ch = getch();
+       if (ch == ERR)
+           continue;
+       if (ch == 4)
+           break;
+       for (j = 0; j < argc; j++) {
+           set_term(data[j].screen);
+           addch(UChar(ch));
+           refresh();
        }
+    }
 
-       /*
-        * Cleanup and exit
-        */
-       for (j = argc-1; j >= 0; j--) {
-               set_term(data[j].screen);
-               endwin();
-       }
-       return EXIT_SUCCESS;
+    /*
+     * Cleanup and exit
+     */
+    for (j = argc - 1; j >= 0; j--) {
+       set_term(data[j].screen);
+       endwin();
+    }
+    ExitProgram(EXIT_SUCCESS);
 }