• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************/
2 /*                                                                            */
3 /* Copyright (c) 2009 FUJITSU LIMITED                                         */
4 /*                                                                            */
5 /* This program is free software;  you can redistribute it and/or modify      */
6 /* it under the terms of the GNU General Public License as published by       */
7 /* the Free Software Foundation; either version 2 of the License, or          */
8 /* (at your option) any later version.                                        */
9 /*                                                                            */
10 /* This program is distributed in the hope that it will be useful,            */
11 /* but WITHOUT ANY WARRANTY;  without even the implied warranty of            */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See                  */
13 /* the GNU General Public License for more details.                           */
14 /*                                                                            */
15 /* You should have received a copy of the GNU General Public License          */
16 /* along with this program;  if not, write to the Free Software               */
17 /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    */
18 /*                                                                            */
19 /* Author: Miao Xie <miaox@cn.fujitsu.com>                                    */
20 /* Restructure for LTP: Shi Weihua <shiwh@cn.fujitsu.com>                     */
21 /*                                                                            */
22 /******************************************************************************/
23 /*++ simple_echo.c
24  *
25  * DESCRIPTION:
26  *	The command "echo" can't return the errno. So we write this program to
27  * instead of "echo".
28  *
29  * Author:
30  * -------
31  * 2008/04/17 created by Miao Xie@FNST
32  *
33  */
34 
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <libgen.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
main(int argc,char ** argv)46 int main(int argc, char **argv)
47 {
48 	int fd = 1;
49 
50 	if (argc != 2 && argc != 3) {
51 		fprintf(stderr, "usage: %s STRING [ostream]\n",
52 			basename(argv[0]));
53 		exit(1);
54 	}
55 
56 	if (argc == 3)
57 		if ((fd = open(argv[2], O_RDWR | O_SYNC)) == -1)
58 			err(errno, "%s", argv[2]);
59 
60 	if (write(fd, argv[1], strlen(argv[1])) == -1)
61 		err(errno, "write error");
62 
63 	if (fd != 1)
64 		if (close(fd) == -1)
65 			err(errno, "close error");
66 	return 0;
67 }
68