xref: /titanic_50/usr/src/cmd/cmd-inet/usr.lib/in.daytimed/in.daytimed.c (revision 174bc6499d233e329ecd3d98a880a7b07df16bfa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * daytime inetd service - both stream and dgram based.
31  * Return human-readable time of day.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <unistd.h>
37 #include <stdio.h>
38 #include <strings.h>
39 #include <netinet/in.h>
40 #include <inetsvc.h>
41 
42 
43 #define	TIMEBUF_SIZE	26
44 
45 
46 static const char *
47 daytime(void)
48 {
49 	time_t		clock;
50 	static char	buf[TIMEBUF_SIZE];
51 
52 	clock = time(NULL);
53 	(void) strlcpy(buf, ctime(&clock), sizeof (buf));
54 	/*
55 	 * Format of ctime is "Fri Sep 13 00:00:00 1986\n\0". To conform to the
56 	 * required format as specified in RFCs 867 and 854 we replace the
57 	 * "\n\0" with "\r\n".
58 	 */
59 	buf[TIMEBUF_SIZE - 2] = '\r';
60 	buf[TIMEBUF_SIZE - 1] = '\n';
61 
62 	return (buf);
63 }
64 
65 /* ARGSUSED3 */
66 static void
67 daytime_dg(int s, const struct sockaddr *sap, int sa_size, const void *buf,
68     size_t sz)
69 {
70 	(void) safe_sendto(s, daytime(), TIMEBUF_SIZE, 0, sap, sa_size);
71 }
72 
73 int
74 main(int argc, char *argv[])
75 {
76 	opterr = 0;	/* disable getopt error msgs */
77 	switch (getopt(argc, argv, "ds")) {
78 	case 'd':
79 		dg_template(daytime_dg, STDIN_FILENO, NULL, 0);
80 		break;
81 	case 's':
82 		(void) safe_write(STDIN_FILENO, daytime(), TIMEBUF_SIZE);
83 		break;
84 	default:
85 		return (1);
86 	}
87 
88 	return (0);
89 }
90