]> git.sur5r.net Git - openldap/blob - contrib/whois++/util.c
SASL version check
[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 <stdio.h>
32 #include <signal.h>
33 #include <ctype.h>
34 #if defined(SYS5) || defined(XOS_2)
35 #include <termio.h>
36 #else
37 #include <sgtty.h>
38 #endif
39 #include <time.h>
40 #if defined(INTERNATIONAL)
41 #include <langinfo.h>
42 #include <locale.h>
43 #endif
44
45 static void     handler();
46
47 char    *lowerCase( string )
48 char    *string;
49
50 {
51         char    *s;
52
53         for ( s = string; s != NULL && *s != '\0'; s++ )
54                 if ( isupper( *s ) )
55                         *s = tolower( *s );
56         return string;
57 }
58
59 char    *convertTime( date, locale )
60 char    *date, *locale;
61
62 {
63         /*
64          * A quick hack to convert the time from the format Quipu uses into
65          * a more normal representation.
66          */
67         struct tm       *tm;
68         time_t          time;
69         static char     result[BUFSIZ];
70         int             UTCOffset;
71
72         /*
73          * Get local timezone information, we need to apply this to the 
74          * zulu time that Quipu uses later.
75          */
76         time = 0;
77         tm = localtime(&time);
78         UTCOffset = tm->tm_gmtoff;
79         sscanf( date, "%2d%2d%2d%2d%2d%2dZ",
80                 &tm->tm_year, &tm->tm_mon, &tm->tm_mday, 
81                 &tm->tm_hour, &tm->tm_min, &tm->tm_sec );
82         tm->tm_mon--;
83         tm->tm_isdst = 0;
84         tm->tm_gmtoff = 0;
85         time = mktime(tm);
86         time += UTCOffset;
87         tm = localtime(&time);
88 #if defined(INTERNATIONAL)
89         setlocale(LC_TIME, locale);
90         strftime(result, sizeof(result), nl_langinfo(D_T_FMT), tm);
91 #else
92         strftime(result, sizeof(result), "%c", tm);
93 #endif
94         return result;
95 }
96
97 static long     interrupt;
98
99 char    *getPassword( prompt )
100 char    *prompt;
101
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         void            handler();
111         unsigned long   flags;
112         int             c, idx;
113         static char     buffer[BUFSIZ + 1];
114
115         if ( ( input = fopen( "/dev/tty", "r" ) ) == NULL )
116                 input = stdin;
117         else
118                 setbuf( input, (char *) NULL );
119         vec.sv_handler = handler;
120         vec.sv_mask = 0;
121         vec.sv_flags = SV_INTERRUPT;
122         sigvec( SIGINT, &vec, &ovec );
123         interrupt = 0;
124 #if defined(SYS5) || defined(XOS_2)
125         ioctl( fileno( input ), TCGETS, &ttyb );
126         flags = ttyb.c_lflag;
127         ttyb.c_lflags &= ~ ( ECHO | ECHOE | ECHOK | ECHONL );
128         ioctl( fileno( input ), TCSETSF, &ttyb );
129 #else
130         ioctl( fileno( input ), TIOCGETP, &ttyb );
131         flags = ttyb.sg_flags;
132         ttyb.sg_flags &= ~ ECHO;
133         ioctl( fileno( input ), TIOCSETN, &ttyb );
134 #endif
135         fputs( prompt, stderr );
136         idx = 0;
137         while ( !interrupt && ( c = getc( input ) ) != EOF ) {
138                 if ( c == '\n' || c == '\r' )
139                         break;
140                 if ( idx < BUFSIZ )
141                         buffer[idx++] = c;
142         }
143         if ( interrupt )
144                 buffer[0] = '\0';
145         else
146                 buffer[idx] = '\0';
147 #if defined(SYS5) || defined(XOS_2)
148         ttyb.c_lflag = flags;
149         ioctl( fileno( input ), TCSETSW, &ttyb );
150 #else
151         ttyb.sg_flags = flags;
152         ioctl( fileno( input ), TIOCSETN, &ttyb );
153 #endif
154         putc( '\n', stderr );
155         sigvec( SIGINT, &ovec, (struct sigvec *) NULL );
156         if ( input != stdin )
157                 fclose( input );
158         if ( interrupt )
159                 kill( getpid(), SIGINT );
160         return buffer;
161 }
162
163 static void     handler()
164
165 {
166         ++interrupt;
167 }