Try to move system dependent stuff to system dependent files.
[unix-history] / usr / src / usr.bin / telnet / utilities.c
CommitLineData
788caa15
GM
1#define TELOPTS
2#include <arpa/telnet.h>
115a5494 3#include <sys/types.h>
788caa15
GM
4
5#include <ctype.h>
6
115a5494
GM
7#include "ring.h"
8
788caa15
GM
9#include "externs.h"
10
11FILE *NetTrace = 0; /* Not in bss, since needs to stay */
12
13/*
14 * upcase()
15 *
16 * Upcase (in place) the argument.
17 */
18
19void
20upcase(argument)
21register char *argument;
22{
23 register int c;
24
25 while ((c = *argument) != 0) {
26 if (islower(c)) {
27 *argument = toupper(c);
28 }
29 argument++;
30 }
31}
32
33/*
34 * SetSockOpt()
35 *
36 * Compensate for differences in 4.2 and 4.3 systems.
37 */
38
39int
40SetSockOpt(fd, level, option, yesno)
41int
42 fd,
43 level,
44 option,
45 yesno;
46{
47#ifndef NOT43
48 return setsockopt(fd, level, option,
49 (char *)&yesno, sizeof yesno);
50#else /* NOT43 */
51 if (yesno == 0) { /* Can't do that in 4.2! */
52 fprintf(stderr, "Error: attempt to turn off an option 0x%x.\n",
53 option);
54 return -1;
55 }
56 return setsockopt(fd, level, option, 0, 0);
57#endif /* NOT43 */
58}
59\f
60/*
61 * The following are routines used to print out debugging information.
62 */
63
64
65void
66Dump(direction, buffer, length)
67char direction;
68char *buffer;
69int length;
70{
71# define BYTES_PER_LINE 32
72# define min(x,y) ((x<y)? x:y)
73 char *pThis;
74 int offset;
75
76 offset = 0;
77
78 while (length) {
79 /* print one line */
80 fprintf(NetTrace, "%c 0x%x\t", direction, offset);
81 pThis = buffer;
82 buffer = buffer+min(length, BYTES_PER_LINE);
83 while (pThis < buffer) {
84 fprintf(NetTrace, "%.2x", (*pThis)&0xff);
85 pThis++;
86 }
87 fprintf(NetTrace, "\n");
88 length -= BYTES_PER_LINE;
89 offset += BYTES_PER_LINE;
90 if (length < 0) {
91 return;
92 }
93 /* find next unique line */
94 }
95}
96
97
98/*VARARGS*/
99void
100printoption(direction, fmt, option, what)
101 char *direction, *fmt;
102 int option, what;
103{
104 if (!showoptions)
105 return;
106 fprintf(NetTrace, "%s ", direction+1);
107 if (fmt == doopt)
108 fmt = "do";
109 else if (fmt == dont)
110 fmt = "dont";
111 else if (fmt == will)
112 fmt = "will";
113 else if (fmt == wont)
114 fmt = "wont";
115 else
116 fmt = "???";
117 if (option < (sizeof telopts/sizeof telopts[0]))
118 fprintf(NetTrace, "%s %s", fmt, telopts[option]);
119 else
120 fprintf(NetTrace, "%s %d", fmt, option);
121 if (*direction == '<') {
122 fprintf(NetTrace, "\r\n");
123 return;
124 }
125 fprintf(NetTrace, " (%s)\r\n", what ? "reply" : "don't reply");
126}
127
128void
129printsub(direction, pointer, length)
130char *direction, /* "<" or ">" */
131 *pointer; /* where suboption data sits */
132int length; /* length of suboption data */
133{
134 if (showoptions) {
135 fprintf(NetTrace, "%s suboption ",
136 (direction[0] == '<')? "Received":"Sent");
137 switch (pointer[0]) {
138 case TELOPT_TTYPE:
139 fprintf(NetTrace, "Terminal type ");
140 switch (pointer[1]) {
141 case TELQUAL_IS:
142 {
143 char tmpbuf[sizeof subbuffer];
144 int minlen = min(length, sizeof tmpbuf);
145
146 memcpy(tmpbuf, pointer+2, minlen);
147 tmpbuf[minlen-1] = 0;
148 fprintf(NetTrace, "is %s.\n", tmpbuf);
149 }
150 break;
151 case TELQUAL_SEND:
152 fprintf(NetTrace, "- request to send.\n");
153 break;
154 default:
155 fprintf(NetTrace,
156 "- unknown qualifier %d (0x%x).\n", pointer[1]);
157 }
158 break;
159 default:
160 fprintf(NetTrace, "Unknown option %d (0x%x)\n",
161 pointer[0], pointer[0]);
162 }
163 }
164}