VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin/echo.c@ 360

最後變更 在這個檔案從360是 360,由 bird 提交於 19 年 前

Ported to MSC

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 4.7 KB
 
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static char const copyright[] =
33"@(#) Copyright (c) 1989, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
35#endif /* not lint */
36
37#ifndef lint
38static char sccsid[] = "@(#)echo.c 8.1 (Berkeley) 5/31/93";
39#endif /* not lint */
40#endif
41#ifndef _MSC_VER
42#include <sys/cdefs.h>
43#endif
44//__FBSDID("$FreeBSD: src/bin/echo/echo.c,v 1.17 2004/04/06 20:06:46 markm Exp $");
45
46#include <sys/types.h>
47#ifndef _MSC_VER
48#include <sys/uio.h>
49#endif
50
51#include <stdio.h>
52#include <assert.h>
53#include <errno.h>
54#include <limits.h>
55#include <stdlib.h>
56#include <string.h>
57#ifndef _MSC_VER
58#include <unistd.h>
59#endif
60
61#ifndef IOV_MAX
62#define IOV_MAX 1024
63#endif
64
65#ifdef _MSC_VER
66#include <io.h>
67
68struct iovec {
69 char *iov_base;
70 size_t iov_len;
71};
72
73int writev(int fd, const struct iovec *vector, int count)
74{
75 int size = 0;
76 int i;
77 for (i = 0; i < count; i++)
78 {
79 int cb = write(fd, vector[i].iov_base, vector[i].iov_len);
80 if (cb < 0)
81 return -1;
82 size += cb;
83 }
84 return size;
85}
86
87#define STDERR_FILENO 2
88#define STDOUT_FILENO 1
89#endif /* _MSC_VER */
90
91/*
92 * Report an error and exit.
93 * Use it instead of err(3) to avoid linking-in stdio.
94 */
95static void
96errexit(const char *prog, const char *reason)
97{
98 char *errstr = strerror(errno);
99 write(STDERR_FILENO, prog, strlen(prog));
100 write(STDERR_FILENO, ": ", 2);
101 write(STDERR_FILENO, reason, strlen(reason));
102 write(STDERR_FILENO, ": ", 2);
103 write(STDERR_FILENO, errstr, strlen(errstr));
104 write(STDERR_FILENO, "\n", 1);
105}
106
107int
108kmk_builtin_echo(int argc, char *argv[])
109{
110 int nflag; /* if not set, output a trailing newline. */
111 int veclen; /* number of writev arguments. */
112 struct iovec *iov, *vp, *iovfree; /* Elements to write, current element. */
113 char space[] = " ";
114 char newline[] = "\n";
115 char *progname = argv[0];
116
117 /* This utility may NOT do getopt(3) option parsing. */
118 if (*++argv && !strcmp(*argv, "-n")) {
119 ++argv;
120 --argc;
121 nflag = 1;
122 } else
123 nflag = 0;
124
125 veclen = (argc >= 2) ? (argc - 2) * 2 + 1 : 0;
126
127 if ((iovfree = vp = iov = malloc((veclen + 1) * sizeof(struct iovec))) == NULL) {
128 errexit(progname, "malloc");
129 return 1;
130 }
131
132 while (argv[0] != NULL) {
133 size_t len;
134
135 len = strlen(argv[0]);
136
137 /*
138 * If the next argument is NULL then this is this
139 * the last argument, therefore we need to check
140 * for a trailing \c.
141 */
142 if (argv[1] == NULL) {
143 /* is there room for a '\c' and is there one? */
144 if (len >= 2 &&
145 argv[0][len - 2] == '\\' &&
146 argv[0][len - 1] == 'c') {
147 /* chop it and set the no-newline flag. */
148 len -= 2;
149 nflag = 1;
150 }
151 }
152 vp->iov_base = *argv;
153 vp++->iov_len = len;
154 if (*++argv) {
155 vp->iov_base = space;
156 vp++->iov_len = 1;
157 }
158 }
159 if (!nflag) {
160 veclen++;
161 vp->iov_base = newline;
162 vp++->iov_len = 1;
163 }
164 /* assert(veclen == (vp - iov)); */
165 while (veclen) {
166 int nwrite;
167
168 nwrite = (veclen > IOV_MAX) ? IOV_MAX : veclen;
169 if (writev(STDOUT_FILENO, iov, nwrite) == -1) {
170 errexit(progname, "write");
171 free(iovfree);
172 return 1;
173 }
174 iov += nwrite;
175 veclen -= nwrite;
176 }
177 free(iovfree);
178 return 0;
179}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette