2 * $Id: util.c,v 1.3 2004/10/28 00:21:48 paulus Exp $
4 * Copyright (C) 1995,1996,1997 Lars Fenneberg
6 * Copyright 1992 Livingston Enterprises, Inc.
8 * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
9 * and Merit Network, Inc. All Rights Reserved
11 * See the file COPYRIGHT for the respective terms and conditions.
12 * If the file is missing contact me at lf@elemental.net
13 * and I'll send you a copy.
19 #include <radiusclient.h>
24 * Purpose: Turns printable string into correct tm struct entries.
28 static const char * months[] =
30 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
31 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
34 void rc_str2tm (char *valstr, struct tm *tm)
39 for (i = 0; i < 12; i++)
41 if (strncmp (months[i], valstr, 3) == 0)
49 tm->tm_mday = atoi (&valstr[4]);
52 tm->tm_year = atoi (&valstr[7]) - 1900;
56 * Function: rc_getifname
58 * Purpose: get the network interface name associated with this tty
62 char *rc_getifname(char *tty)
64 #if defined(BSD4_4) || defined(linux)
65 static char name[512];
68 if ((fd = open(tty, O_RDWR|O_NDELAY)) < 0) {
69 rc_log(LOG_ERR, "rc_getifname: can't open %s: %s", tty, strerror(errno));
75 strcpy(name,ttyname(fd));
76 if (strlen(name) < 1) {
77 rc_log(LOG_ERR, "rc_getifname: can't get attached interface of %s: %s", tty, strerror(errno));
82 if (ioctl(fd, SIOCGIFNAME, name) < 0) {
83 rc_log(LOG_ERR, "rc_getifname: can't ioctl %s: %s", tty, strerror(errno));
91 #if defined(BSD4_4) || defined(linux)
100 * Purpose: Reads in a string from the user (with or witout echo)
104 char *rc_getstr (char *prompt, int do_echo)
110 struct termios term_old, term_new;
111 static char buf[GETSTR_LENGTH];
112 int is_term, flags, old_flags;
117 out = fileno(stdout);
119 (void) sigemptyset (&newset);
120 (void) sigaddset (&newset, SIGINT);
121 (void) sigaddset (&newset, SIGTSTP);
122 (void) sigaddset (&newset, SIGQUIT);
124 (void) sigprocmask (SIG_BLOCK, &newset, &oldset);
126 if ((is_term = isatty(in)))
129 (void) tcgetattr (in, &term_old);
132 term_new.c_lflag |= ECHO;
134 term_new.c_lflag &= ~ECHO;
136 if (tcsetattr (in, TCSAFLUSH, &term_new) == 0)
143 if ((flags = fcntl(in, F_GETFL, 0)) >= 0) {
147 fcntl(in, F_SETFL, flags);
149 while (read(in, &c, 1) > 0)
152 fcntl(in, F_SETFL, old_flags);
158 write(out, prompt, strlen(prompt));
160 /* well, this looks ugly, but it handles the following end of line
161 markers: \r \r\0 \r\n \n \n\r, at least at a second pass */
166 if (read(in, &c, 1) <= 0)
169 if (!flushed && ((c == '\0') || (c == '\r') || (c == '\n'))) {
174 if ((c == '\r') || (c == '\n'))
179 if (p < buf + GETSTR_LENGTH)
181 if (do_echo && !is_term)
189 if (!do_echo || !is_term) write(out, "\r\n", 2);
192 tcsetattr (in, TCSAFLUSH, &term_old);
194 if ((flags = fcntl(in, F_GETFL, 0)) >= 0) {
198 fcntl(in, F_SETFL, flags);
200 while (read(in, &c, 1) > 0)
203 fcntl(in, F_SETFL, old_flags);
207 (void) sigprocmask (SIG_SETMASK, &oldset, NULL);
212 void rc_mdelay(int msecs)
216 tv.tv_sec = (int) msecs / 1000;
217 tv.tv_usec = (msecs % 1000) * 1000;
219 select(0,(fd_set *)NULL,(fd_set *)NULL,(fd_set *)NULL, &tv);
225 * Purpose: generate a quite unique string
227 * Remarks: not that unique at all...
235 static unsigned short int cnt = 0;
236 sprintf (buf, "%08lX%04X%02hX",
237 (unsigned long int) time (NULL),
238 (unsigned int) getpid (),