Ignore:
Timestamp:
Dec 22, 2012, 5:19:41 PM (10 years ago)
Author:
sam
Message:

neercs: fix our unread() function so that it stops losing characters.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/neercs/term/pty.cpp

    r2090 r2159  
    114114{
    115115#if defined HAVE_FORKPTY
     116    size_t sent = 0;
     117
    116118    /* Do we have data from previous call? */
    117119    if (m_unread_len)
    118120    {
    119         /* FIXME: check that m_unread_len < maxlen */
    120         memcpy(data, m_unread_data, m_unread_len);
    121 
    122         data += m_unread_len;
    123         maxlen -= m_unread_len;
    124 
    125         delete[] m_unread_data;
    126         m_unread_data = 0;
    127         m_unread_len = 0;
     121        size_t tocopy = min(maxlen, m_unread_len);
     122
     123        memcpy(data, m_unread_data, tocopy);
     124
     125        data += tocopy;
     126        sent += tocopy;
     127        maxlen -= tocopy;
     128
     129        if (tocopy < m_unread_len)
     130        {
     131            m_unread_len -= tocopy;
     132            memmove(m_unread_data, m_unread_data + tocopy, m_unread_len);
     133        }
     134        else
     135        {
     136            delete[] m_unread_data;
     137            m_unread_data = 0;
     138            m_unread_len = 0;
     139        }
    128140    }
    129141
     
    160172                if (nr <= 0)
    161173                    m_eof = true;
    162 
    163                 if (nr >= 0)
    164                     return nr;
     174                else
     175                    sent += nr;
     176
     177                if (sent >= 0)
     178                    return sent;
    165179            }
    166180        }
     
    174188{
    175189#if defined HAVE_FORKPTY
    176     char *new_data;
    177 
     190    /* Prepare unread buffer */
    178191    if (m_unread_data)
    179192    {
    180         new_data = new char[m_unread_len + len];
    181         memcpy(new_data + len, m_unread_data, m_unread_len);
     193        char *tmp = new char[m_unread_len + len];
     194        memcpy(tmp + len, m_unread_data, m_unread_len);
    182195        delete[] m_unread_data;
     196        m_unread_data = tmp;
     197        m_unread_len += len;
    183198    }
    184199    else
    185200    {
    186         new_data = new char[len];
    187     }
    188 
    189     memcpy(new_data, data, len);
    190     m_unread_data = new_data;
     201        m_unread_data = new char[len];
     202        m_unread_len = len;
     203    }
     204
     205    /* Copy data to the unread buffer */
     206    memcpy(m_unread_data, data, len);
    191207#endif
    192208}
Note: See TracChangeset for help on using the changeset viewer.