File: packet_send.c

package info (click to toggle)
tinyssh 20250501-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,388 kB
  • sloc: ansic: 20,245; sh: 1,582; python: 1,449; makefile: 913
file content (42 lines) | stat: -rw-r--r-- 946 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
20140120
20241211 - reformated using clang-format
Jan Mojzis
Public domain.
*/

#include <unistd.h>
#include "writeall.h"
#include "e.h"
#include "byte.h"
#include "purge.h"
#include "packet.h"

int packet_sendisready(void) { return (packet.sendbuf.len > 0); }

int packet_send(void) {

    struct buf *sendbuf = &packet.sendbuf;
    long long w;

    if (sendbuf->len <= 0) return 1;
    w = write(1, sendbuf->buf, sendbuf->len);
    if (w == -1) {
        if (errno == EINTR) return 1;
        if (errno == EAGAIN) return 1;
        if (errno == EWOULDBLOCK) return 1;
        return 0;
    }
    byte_copy(sendbuf->buf, sendbuf->len - w, sendbuf->buf + w);
    sendbuf->len -= w;
    purge(sendbuf->buf + sendbuf->len, w);
    return 1;
}

int packet_sendall(void) {

    if (writeall(1, packet.sendbuf.buf, packet.sendbuf.len) == -1) return 0;
    purge(packet.sendbuf.buf, packet.sendbuf.len);
    packet.sendbuf.len = 0;
    return 1;
}