]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/lib_print.c
ncurses 4.1
[ncurses.git] / ncurses / lib_print.c
1
2 /***************************************************************************
3 *                            COPYRIGHT NOTICE                              *
4 ****************************************************************************
5 *                ncurses is copyright (C) 1992-1995                        *
6 *                          Zeyd M. Ben-Halim                               *
7 *                          zmbenhal@netcom.com                             *
8 *                          Eric S. Raymond                                 *
9 *                          esr@snark.thyrsus.com                           *
10 *                                                                          *
11 *        Permission is hereby granted to reproduce and distribute ncurses  *
12 *        by any means and for any fee, whether alone or as part of a       *
13 *        larger distribution, in source or in binary form, PROVIDED        *
14 *        this notice is included with any such distribution, and is not    *
15 *        removed from any of its header files. Mention of ncurses in any   *
16 *        applications linked with it is highly appreciated.                *
17 *                                                                          *
18 *        ncurses comes AS IS with no warranty, implied or expressed.       *
19 *                                                                          *
20 ***************************************************************************/
21
22
23 #include <curses.priv.h>
24
25 #include <term.h>
26
27 MODULE_ID("$Id: lib_print.c,v 1.8 1996/12/21 14:24:06 tom Exp $")
28
29 int mcprint(char *data, int len)
30 /* ship binary character data to the printer via mc4/mc5/mc5p */
31 {
32     char        *mybuf, *switchon;
33     size_t      onsize, offsize, res;
34
35     errno = 0;
36     if (!prtr_non && (!prtr_on || !prtr_off))
37     {
38         errno = ENODEV;
39         return(ERR);
40     }
41
42     if (prtr_non)
43     {
44         switchon = tparm(prtr_non, len);
45         onsize = strlen(switchon);
46         offsize = 0;
47     }
48     else
49     {
50         switchon = prtr_on;
51         onsize = strlen(prtr_on);
52         offsize = strlen(prtr_off);
53     }
54
55     if ((mybuf = malloc(onsize + len + offsize + 1)) == (char *)NULL)
56     {
57         errno = ENOMEM;
58         return(ERR);
59     }
60
61     (void) strcpy(mybuf, switchon);
62     memcpy(mybuf + onsize, data, len);
63     if (offsize)
64       (void) strcpy(mybuf + onsize + len, prtr_off);
65
66     /*
67      * We're relying on the atomicity of UNIX writes here.  The
68      * danger is that output from a refresh() might get interspersed
69      * with the printer data after the write call returns but before the
70      * data has actually been shipped to the terminal.  If the write(2)
71      * operation is truly atomic we're protected from this.
72      */
73     res = write(cur_term->Filedes, mybuf, onsize + len + offsize);
74
75     /*
76      * By giving up our scheduler slot here we increase the odds that the
77      * kernel will ship the contiguous clist items from the last write
78      * immediately.
79      */
80     (void) sleep(0);
81
82     free(mybuf);
83     return(res);
84 }