]> git.sur5r.net Git - openldap/blob - contrib/whois++/util.c
5d7b8ebec48deb18763de080fb6e45fc72c6d6e0
[openldap] / contrib / whois++ / util.c
1 #if !defined(lint)
2 static char copyright[] = "Copyright 1992 The University of Adelaide";
3 #endif
4
5 /*
6  *                      U T I L
7  *
8  * Author:      Mark R. Prior
9  *              Communications and Systems Branch
10  *              Information Technology Division
11  *              The University of Adelaide
12  * E-mail:      mrp@itd.adelaide.edu.au
13  * Date:        November 1992
14  * Version:     1.7
15  * Description:
16  *              Some routines that I use in most my LDAP playthings :-)
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that the above copyright notice and this paragraph are
20  * duplicated in all such forms and that any documentation,
21  * advertising materials, and other materials related to such
22  * distribution and use acknowledge that the software was developed
23  * by the University of Adelaide. The name of the University may not
24  * be used to endorse or promote products derived from this software\
25  * without specific prior written permission.
26  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
28  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  */
30
31 #include "portable.h"
32
33 #include <stdio.h>
34 #include <signal.h>
35 #include <ctype.h>
36 #if defined(SYS5) || defined(XOS_2)
37 #include <termio.h>
38 #else
39 #include <sgtty.h>
40 #endif
41 #include <time.h>
42 #if defined(INTERNATIONAL)
43 #include <langinfo.h>
44 #include <locale.h>
45 #endif
46 #include <ac/unistd.h>
47
48 static void     handler(int sig);
49
50 char *
51 lowerCase( char *string )
52 {
53         char    *s;
54
55         for ( s = string; s != NULL && *s != '\0'; s++ )
56                 if ( isupper( *s ) )
57                         *s = tolower( *s );
58         return string;
59 }
60
61 char *
62 convertTime( char *date, char *locale )
63 {
64         /*
65          * A quick hack to convert the time from the format Quipu uses into
66          * a more normal representation.
67          */
68         struct tm       *tm;
69         time_t          time;
70         static char     result[BUFSIZ];
71         int             UTCOffset;
72
73         /*
74          * Get local timezone information, we need to apply this to the 
75          * zulu time that Quipu uses later.
76          */
77         time = 0;
78         tm = localtime(&time);
79         UTCOffset = tm->tm_gmtoff;
80         sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
81                 &tm->tm_year, &tm->tm_mon, &tm->tm_mday, 
82                 &tm->tm_hour, &tm->tm_min, &tm->tm_sec );
83         tm->tm_mon--;
84         tm->tm_isdst = 0;
85         tm->tm_gmtoff = 0;
86         time = mktime(tm);
87         time += UTCOffset;
88         tm = localtime(&time);
89 #if defined(INTERNATIONAL)
90         setlocale(LC_TIME, locale);
91         strftime(result, sizeof(result), nl_langinfo(D_T_FMT), tm);
92 #else
93         strftime(result, sizeof(result), "%c", tm);
94 #endif
95         return result;
96 }
97
98 static long     interrupt;
99
100 char *
101 getPassword( char *prompt )
102 {
103 #if defined(SYS5) || defined(XOS_2)
104         struct termios  ttyb;
105 #else
106         struct sgttyb   ttyb;
107 #endif
108         FILE            *input;
109         struct sigvec   ovec, vec;
110         unsigned long   flags;
111         int             c, idx;
112         static char     buffer[BUFSIZ + 1];
113
114         if ( ( input = fopen( "/dev/tty", "r" ) ) == NULL )
115                 input = stdin;
116         else
117                 setbuf( input, (char *) NULL );
118         vec.sv_handler = handler;
119         vec.sv_mask = 0;
120         vec.sv_flags = SV_INTERRUPT;
121         sigvec( SIGINT, &vec, &ovec );
122         interrupt = 0;
123 #if defined(SYS5) || defined(XOS_2)
124         ioctl( fileno( input ), TCGETS, &ttyb );
125         flags = ttyb.c_lflag;
126         ttyb.c_lflags &= ~ ( ECHO | ECHOE | ECHOK | ECHONL );
127         ioctl( fileno( input ), TCSETSF, &ttyb );
128 #else
129         ioctl( fileno( input ), TIOCGETP, &ttyb );
130         flags = ttyb.sg_flags;
131         ttyb.sg_flags &= ~ ECHO;
132         ioctl( fileno( input ), TIOCSETN, &ttyb );
133 #endif
134         fputs( prompt, stderr );
135         idx = 0;
136         while ( !interrupt && ( c = getc( input ) ) != EOF ) {
137                 if ( c == '\n' || c == '\r' )
138                         break;
139                 if ( idx < BUFSIZ )
140                         buffer[idx++] = c;
141         }
142         if ( interrupt )
143                 buffer[0] = '\0';
144         else
145                 buffer[idx] = '\0';
146 #if defined(SYS5) || defined(XOS_2)
147         ttyb.c_lflag = flags;
148         ioctl( fileno( input ), TCSETSW, &ttyb );
149 #else
150         ttyb.sg_flags = flags;
151         ioctl( fileno( input ), TIOCSETN, &ttyb );
152 #endif
153         putc( '\n', stderr );
154         sigvec( SIGINT, &ovec, (struct sigvec *) NULL );
155         if ( input != stdin )
156                 fclose( input );
157         if ( interrupt )
158                 kill( getpid(), SIGINT );
159         return buffer;
160 }
161
162 static void
163 handler( int sig )
164 {
165         ++interrupt;
166 }