1 /** @file
2  *
3  * Copyright (c) 1999 - 2014, Intel Corporation. All rights reserved.<BR>
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  *    this list of conditions and the following disclaimer in the documentation
13  *    and/or other materials provided with the distribution.
14  *
15  * 3. All advertising materials mentioning features or use of this software must
16  *    display the following acknowledgement:
17  *
18  *    This product includes software developed by Intel Corporation and its
19  *    contributors.
20  *
21  * 4. Neither the name of Intel Corporation or its contributors may be used to
22  *    endorse or promote products derived from this software without specific
23  *    prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  * DISCLAIMED.  IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR
29  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
32  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 /*++
39 
40 Module Name:
41 
42     writev.c
43 
44 Abstract:
45 
46     Functions implementing the standard "writev" system call interface
47 
48 
49 Revision History
50 
51 --*/
52 #include  <LibConfig.h>
53 
54 #ifdef foo
55 #include <efi_interface.h>
56 #include <unistd.h>
57 #include <fcntl.h>
58 #define KERNEL
59 #include <errno.h>
60 #undef KERNEL
61 #include "./filedesc.h"
62 
63 #include <libc_debug.h>
64 #include <assert.h>
65 #endif
66 
67 #include <stdlib.h>
68 #include <unistd.h>
69 #include <sys/uio.h>
70 #include <string.h>
71 #ifndef KERNEL
72 #define KERNEL
73 #include <errno.h>
74 #undef KERNEL
75 #else
76 #include <errno.h>
77 #endif
78 
79 //
80 //  Name:
81 //      writev
82 //
83 //  Description:
84 //      BSD writev interface for libc
85 //
86 //  Arguments:
87 //      File Descriptor (index into file descriptor table)
88 //      iovec pointer
89 //      size of iovec array
90 //
91 //  Returns:
92 //      number of bytes written
93 //
94 
95 ssize_t
writev(int fd,const struct iovec * iov,int iovcnt)96 writev(
97     int fd,
98     const struct iovec *iov,
99     int iovcnt
100     )
101 {
102   const struct iovec   *pVecTmp;
103   char                 *pBuf;
104   size_t                TotalBytes;
105   size_t                i;
106   size_t                ret;
107 
108   //
109   //  See how much memory we'll need
110   //
111 
112   for (i = 0, TotalBytes = 0, pVecTmp = iov; i < (size_t)iovcnt; i++, pVecTmp++) {
113     TotalBytes += pVecTmp->iov_len;
114   }
115 
116   //
117   //  Allocate a contiguous buffer
118   //
119 
120   pBuf = (char*)malloc (TotalBytes);
121   if (pBuf == NULL) {
122     errno = ENOMEM;
123     return -1;
124   }
125 
126   //
127   //  Copy vectors to the buffer
128   //
129 
130   for (; iovcnt; iovcnt--) {
131     bcopy(iov->iov_base, pBuf, iov->iov_len);
132     pBuf += iov->iov_len;
133     iov++;
134   }
135 
136   //
137   //  Use standard write(2) then free buffer
138   //
139 
140   ret = write (fd, pBuf, TotalBytes);
141   free (pBuf);
142 
143   return (ret);
144 }
145