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 /*
28 * daytime inetd service - both stream and dgram based.
29 * Return human-readable time of day.
30 */
31
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <strings.h>
37 #include <netinet/in.h>
38 #include <inetsvc.h>
39
40
41 #define TIMEBUF_SIZE 26
42
43
44 static const char *
daytime(void)45 daytime(void)
46 {
47 time_t clock;
48 static char buf[TIMEBUF_SIZE];
49
50 clock = time(NULL);
51 (void) strlcpy(buf, ctime(&clock), sizeof (buf));
52 /*
53 * Format of ctime is "Fri Sep 13 00:00:00 1986\n\0". To conform to the
54 * required format as specified in RFCs 867 and 854 we replace the
55 * "\n\0" with "\r\n".
56 */
57 buf[TIMEBUF_SIZE - 2] = '\r';
58 buf[TIMEBUF_SIZE - 1] = '\n';
59
60 return (buf);
61 }
62
63 /* ARGSUSED3 */
64 static void
daytime_dg(int s,const struct sockaddr * sap,int sa_size,const void * buf,size_t sz)65 daytime_dg(int s, const struct sockaddr *sap, int sa_size, const void *buf,
66 size_t sz)
67 {
68 (void) safe_sendto(s, daytime(), TIMEBUF_SIZE, 0, sap, sa_size);
69 }
70
71 int
main(int argc,char * argv[])72 main(int argc, char *argv[])
73 {
74 opterr = 0; /* disable getopt error msgs */
75 switch (getopt(argc, argv, "ds")) {
76 case 'd':
77 dg_template(daytime_dg, STDIN_FILENO, NULL, 0);
78 break;
79 case 's':
80 (void) safe_write(STDIN_FILENO, daytime(), TIMEBUF_SIZE);
81 break;
82 default:
83 return (1);
84 }
85
86 return (0);
87 }
88