2N/A/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2N/A/*
2N/A * lib/krb5/os/net_write.c
2N/A *
2N/A * Copyright 1987, 1988, 1990, 2009 by the Massachusetts Institute of Technology.
2N/A * All Rights Reserved.
2N/A *
2N/A * Export of this software from the United States of America may
2N/A * require a specific license from the United States Government.
2N/A * It is the responsibility of any person or organization contemplating
2N/A * export to obtain such a license before exporting.
2N/A *
2N/A * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
2N/A * distribute this software and its documentation for any purpose and
2N/A * without fee is hereby granted, provided that the above copyright
2N/A * notice appear in all copies and that both that copyright notice and
2N/A * this permission notice appear in supporting documentation, and that
2N/A * the name of M.I.T. not be used in advertising or publicity pertaining
2N/A * to distribution of the software without specific, written prior
2N/A * permission. Furthermore if you modify this software you must label
2N/A * your software as modified software and not distribute it in such a
2N/A * fashion that it might be confused with the original M.I.T. software.
2N/A * M.I.T. makes no representations about the suitability of
2N/A * this software for any purpose. It is provided "as is" without express
2N/A * or implied warranty.
2N/A *
2N/A */
2N/A
2N/A#include "k5-int.h"
2N/A#include "os-proto.h"
2N/A
2N/A/*
2N/A * krb5_net_write() writes "len" bytes from "buf" to the file
2N/A * descriptor "fd". It returns the number of bytes written or
2N/A * a write() error. (The calling interface is identical to
2N/A * write(2).)
2N/A *
2N/A * XXX must not use non-blocking I/O
2N/A */
2N/A
2N/Aint
2N/Akrb5_net_write(krb5_context context, int fd, const char *buf, int len)
2N/A{
2N/A sg_buf sg;
2N/A SG_SET(&sg, buf, len);
2N/A return krb5int_net_writev(context, fd, &sg, 1);
2N/A}
2N/A
2N/Aint
2N/Akrb5int_net_writev(krb5_context context, int fd, sg_buf *sgp, int nsg)
2N/A{
2N/A int cc, len = 0;
2N/A SOCKET_WRITEV_TEMP tmp;
2N/A
2N/A while (nsg > 0) {
2N/A /* Skip any empty data blocks. */
2N/A if (SG_LEN(sgp) == 0) {
2N/A sgp++, nsg--;
2N/A continue;
2N/A }
2N/A cc = SOCKET_WRITEV((SOCKET)fd, sgp, nsg, tmp);
2N/A if (cc < 0) {
2N/A if (SOCKET_ERRNO == SOCKET_EINTR)
2N/A continue;
2N/A
2N/A /* XXX this interface sucks! */
2N/A errno = SOCKET_ERRNO;
2N/A return -1;
2N/A }
2N/A len += cc;
2N/A while (cc > 0) {
2N/A if ((unsigned)cc < SG_LEN(sgp)) {
2N/A SG_ADVANCE(sgp, (unsigned)cc);
2N/A cc = 0;
2N/A } else {
2N/A cc -= SG_LEN(sgp);
2N/A sgp++, nsg--;
2N/A assert(nsg > 0 || cc == 0);
2N/A }
2N/A }
2N/A }
2N/A return len;
2N/A}