]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tinfo/lib_print.c
ncurses 6.2 - patch 20210227
[ncurses.git] / ncurses / tinfo / lib_print.c
index e5abd4de614533964c0b3c4e9d4b1a2e711f7c69..4accdf6786ea7a7701955d3b5f852e8e82165bff 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
 /****************************************************************************
- * Copyright (c) 1998-2006,2009 Free Software Foundation, Inc.              *
+ * Copyright 2018,2020 Thomas E. Dickey                                     *
+ * Copyright 1998-2011,2012 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            *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
 #define CUR SP_TERMTYPE
 #endif
 
 #define CUR SP_TERMTYPE
 #endif
 
-MODULE_ID("$Id: lib_print.c,v 1.19 2009/06/06 20:26:17 tom Exp $")
+MODULE_ID("$Id: lib_print.c,v 1.28 2020/08/29 16:22:03 juergen Exp $")
 
 NCURSES_EXPORT(int)
 NCURSES_SP_NAME(mcprint) (NCURSES_SP_DCLx char *data, int len)
 /* ship binary character data to the printer via mc4/mc5/mc5p */
 {
 
 NCURSES_EXPORT(int)
 NCURSES_SP_NAME(mcprint) (NCURSES_SP_DCLx char *data, int len)
 /* ship binary character data to the printer via mc4/mc5/mc5p */
 {
+    int result;
     char *mybuf, *switchon;
     char *mybuf, *switchon;
-    size_t onsize, offsize, res;
+    size_t onsize, offsize;
+    size_t need;
 
     errno = 0;
 
     errno = 0;
-    if (!HasTInfoTerminal(SP_PARM) || (!prtr_non && (!prtr_on || !prtr_off))) {
+    if (!HasTInfoTerminal(SP_PARM)
+       || len <= 0
+       || (!prtr_non && (!prtr_on || !prtr_off))) {
        errno = ENODEV;
        return (ERR);
     }
 
     if (prtr_non) {
        errno = ENODEV;
        return (ERR);
     }
 
     if (prtr_non) {
-       switchon = TPARM_1(prtr_non, len);
+       switchon = TIPARM_1(prtr_non, len);
        onsize = strlen(switchon);
        offsize = 0;
     } else {
        onsize = strlen(switchon);
        offsize = 0;
     } else {
@@ -64,16 +69,18 @@ NCURSES_SP_NAME(mcprint) (NCURSES_SP_DCLx char *data, int len)
        offsize = strlen(prtr_off);
     }
 
        offsize = strlen(prtr_off);
     }
 
+    need = onsize + (size_t) len + offsize;
+
     if (switchon == 0
     if (switchon == 0
-       || (mybuf = typeMalloc(char, onsize + len + offsize + 1)) == 0) {
+       || (mybuf = typeMalloc(char, need + 1)) == 0) {
        errno = ENOMEM;
        return (ERR);
     }
 
        errno = ENOMEM;
        return (ERR);
     }
 
-    (void) strcpy(mybuf, switchon);
-    memcpy(mybuf + onsize, data, (unsigned) len);
+    _nc_STRCPY(mybuf, switchon, need);
+    memcpy(mybuf + onsize, data, (size_t) len);
     if (offsize)
     if (offsize)
-       (void) strcpy(mybuf + onsize + len, prtr_off);
+       _nc_STRCPY(mybuf + onsize + len, prtr_off, need);
 
     /*
      * We're relying on the atomicity of UNIX writes here.  The
 
     /*
      * We're relying on the atomicity of UNIX writes here.  The
@@ -82,18 +89,18 @@ NCURSES_SP_NAME(mcprint) (NCURSES_SP_DCLx char *data, int len)
      * data has actually been shipped to the terminal.  If the write(2)
      * operation is truly atomic we're protected from this.
      */
      * data has actually been shipped to the terminal.  If the write(2)
      * operation is truly atomic we're protected from this.
      */
-    res = write(TerminalOf(SP_PARM)->Filedes, mybuf, onsize + len + offsize);
+    result = (int) write(TerminalOf(SP_PARM)->Filedes, mybuf, need);
 
     /*
      * By giving up our scheduler slot here we increase the odds that the
      * kernel will ship the contiguous clist items from the last write
      * immediately.
      */
 
     /*
      * By giving up our scheduler slot here we increase the odds that the
      * kernel will ship the contiguous clist items from the last write
      * immediately.
      */
-#ifndef __MINGW32__
+#ifndef _NC_WINDOWS
     (void) sleep(0);
 #endif
     free(mybuf);
     (void) sleep(0);
 #endif
     free(mybuf);
-    return (res);
+    return (result);
 }
 
 #if NCURSES_SP_FUNCS && !defined(USE_TERM_DRIVER)
 }
 
 #if NCURSES_SP_FUNCS && !defined(USE_TERM_DRIVER)