]> git.sur5r.net Git - openldap/blob - clients/ud/main.c
Apply patches from Hallvard and others to remove lint.
[openldap] / clients / ud / main.c
1 /*
2  * Copyright (c) 1991, 1992, 1993 
3  * Regents of the University of Michigan.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  *
12  * The University of Michigan would like to thank the following people for
13  * their contributions to this piece of software:
14  *
15  *      Robert Urquhart    <robert@sfu.ca>
16  *      Simon Fraser University, Academic Computing Services
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <setjmp.h>
24
25 #ifdef HAVE_PWD_H
26 #include <pwd.h>
27 #endif
28
29 #include <ac/signal.h>
30 #include <ac/string.h>
31 #include <ac/ctype.h>
32 #include <ac/termios.h>
33 #include <ac/time.h>
34 #include <ac/unistd.h>
35
36 #ifdef HAVE_SYS_FILE_H
37 #include <sys/file.h>
38 #endif
39
40 #include <lber.h>
41 #include <ldap.h>
42 #include <ldapconfig.h>
43 #include "ud.h"
44
45 #ifndef lint
46 char copyright[] =
47 "@(#) Copyright (c) 1991, 1992, 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
48 #endif
49
50 /*
51  *  Used with change_base() to indicate which base we are changing.
52  */
53 #define BASE_SEARCH     0
54 #define BASE_GROUPS     1
55
56 #define iscom(x)        (!strncasecmp(x, cmd, strlen(cmd)))
57
58 static char *server = NULL;
59 static char *config_file = UD_CONFIG_FILE;
60 static char *filter_file = FILTERFILE;
61 static int ldap_port = LDAP_PORT;
62 static int dereference = TRUE;
63
64 char *default_bind_object = UD_BINDDN;
65
66 char *bound_dn;                 /* bound user's Distinguished Name */
67 char *group_base;               /* place in LDAP tree where groups are */
68 char *search_base;              /* place in LDAP tree where searches start */
69
70 static jmp_buf env;             /* spot to jump to on an interrupt */
71
72 int lpp;                        /* lines per page */
73 int verbose;                    /* 1 if verbose mode on */
74 int col_size;                   /* characters across on the screen */
75 int bind_status;                /* user's bind status */
76
77 LDAP *ld;                       /* LDAP descriptor */
78 LDAPFiltDesc *lfdp;             /* LDAP filter descriptor */
79
80 #ifdef DEBUG
81 int debug;                      /* debug flag */
82 #endif
83
84
85 int
86 main( int argc, char **argv )
87 {
88         register int c;                         /* for parsing argv */
89         register char *cp;                      /* for parsing Version */
90
91         verbose = 1;
92
93         /*  handle argument list */
94         while ((c = getopt(argc, argv, "c:d:Df:l:p:s:u:vV")) != -1) {
95                 switch (c) {
96                 case 'l' :
97 #ifdef LDAP_DEBUG
98                         ldap_debug = (int) strtol(optarg, (char **) NULL, 0);
99                         lber_debug = ldap_debug;
100 #endif
101                         break;
102                 case 'd' :
103 #ifdef DEBUG
104                         debug = (int) strtol(optarg, (char **) NULL, 0);
105 #endif
106                         break;
107                 case 's' :
108                         server = strdup(optarg);
109                         break;
110                 case 'c' :
111                         filter_file = strdup(optarg);
112                         break;
113                 case 'f' :
114                         config_file = optarg;
115                         break;
116                 case 'p' :
117                         ldap_port = atoi(optarg);
118                         break;
119                 case 'u' :
120                         default_bind_object = strdup(optarg);
121                         break;
122                 case 'v' :
123                         verbose = 1;    /* this is the default anyways... */
124                         break;
125                 case 'V' :
126                         verbose = 0;
127                         break;
128                 case 'D' :
129                         printf("\n\n  Debug flag values\n\n");
130                         printf("    1  function trace\n");
131                         printf("    2  find() information\n");
132                         printf("    4  group information\n");
133                         printf("    8  mod() information\n");
134                         printf("   16  parsing information\n");
135                         printf("   32  output information\n");
136                         printf("   64  authentication information\n");
137                         printf("  128  initialization information\n\n");
138                         format("These are masks, and may be added to form multiple debug levels.  For example, '-d 35' would perform a function trace, print out information about the find() function, and would print out information about the output routines too.", 75, 2);
139                         exit(0);
140                 default:
141                         fprintf(stderr, "Usage: %s [-c filter-config-file] [-d debug-level] [-l ldap-debug-level] [-s server] [-p port] [-V]\n", argv[0]);
142                         exit(-1);
143                         /* NOTREACHED */
144                 }
145         }
146
147         /* just print the first line of Version[] */
148         cp = strchr(Version, '\t');
149         if (cp != NULL)
150                 *cp = '\0';
151         printf(Version);
152         fflush( stdout );
153
154         initialize_client();
155         initialize_attribute_strings();
156
157         /* now tackle the user's commands */
158         do_commands();
159         /* NOTREACHED */
160
161         return 0;
162 }
163
164 void
165 do_commands( void )
166 {
167         LDAPMessage *mp;                        /* returned by find() */
168         register char *cp;                      /* misc char pointer */
169         register char *ap;                      /* misc char pointer */
170         static char buf[MED_BUF_SIZE];          /* for prompting */
171         static char cmd[MED_BUF_SIZE];          /* holds the command */
172         static char input[MED_BUF_SIZE];        /* buffer for input */
173
174 #ifdef DEBUG
175         if (debug & D_TRACE)
176                 printf("->do_commands()\n");
177 #endif
178         if (verbose) 
179                 printf("\n  Enter a command.  If you need help, type 'h' or '?' and hit RETURN.\n\n");
180         /* jump here on an interrupt */
181         (void) setjmp(env);
182         for (;;) {
183                 printf("* ");
184                 fflush(stdout);
185                 cp = input;
186 /* Temporary kludge - if cp is null, dumps core under Solaris */
187                 if (cp == NULL)
188                         break;
189                 fetch_buffer(input, sizeof(input), stdin);
190                 if (*input == '\0') {
191                         putchar('\n');
192                         continue;
193                 }
194                 while (isspace(*cp))
195                         cp++;   
196                 ap = cmd;
197                 if (memset(cmd, '\0', sizeof(cmd)) == NULL)
198                         fatal("memset");
199                 while (!isspace(*cp) && (*cp != '\0'))
200                         *ap++ = *cp++;
201                 if (iscom("status"))
202                         status();
203                 else if (iscom("stop") || iscom("quit"))
204                         break;
205                 else if (iscom("cb") || iscom("cd") || iscom("moveto")) {
206                         while (isspace(*cp) && (*cp != '\0')) 
207                                 cp++;
208                         if (!strncasecmp(cp, "base", 4))
209                                 cp += 4;
210                         change_base(BASE_SEARCH, &search_base, nextstr(cp));
211                 }
212                 else if (iscom("memberships"))
213                         (void) list_memberships(nextstr(cp));
214                 else if (iscom("list"))
215                         (void) list_groups(nextstr(cp));
216                 else if (iscom("groupbase"))
217                         change_base(BASE_GROUPS, &group_base, nextstr(cp));
218                 else if (iscom("find") || iscom("display") || iscom("show")) {
219                         cp = nextstr(cp);
220                         if ((mp = find(cp, FALSE)) != NULL) {
221                                 parse_answer(mp);
222                                 print_an_entry();
223                                 ldap_msgfree(mp);
224                         }
225                         else
226                                 printf(" Could not find \"%s\".\n", cp);
227                 }
228 #ifdef UOFM
229                 else if (iscom("vedit") && isatty( 1 )) {
230 #else
231                 else if (iscom("vedit")) {
232 #endif
233                         (void) edit(nextstr(cp));
234                 }
235                 else if (iscom("modify") || iscom("change") || iscom("alter"))
236                         (void) modify(nextstr(cp));
237                 else if (iscom("bind") || iscom("iam"))
238                         (void) auth(nextstr(cp), 0);
239                 else if ((cmd[0] == '?') || iscom("help"))
240                         print_help(nextstr(cp));
241                 else if (iscom("join") || iscom("subscribe")) 
242                         (void) x_group(G_JOIN, nextstr(cp));
243                 else if (iscom("resign") || iscom("unsubscribe"))
244                         (void) x_group(G_RESIGN, nextstr(cp));
245                 else if (!strncasecmp("create", cmd, strlen(cmd)))
246                         add_group(nextstr(cp));
247                 else if (!strncasecmp("remove", cmd, strlen(cmd)))
248                         remove_group(nextstr(cp));
249                 else if (!strncasecmp("purge", cmd, strlen(cmd)))
250                         purge_group(nextstr(cp));
251                 else if (!strncasecmp("verbose", cmd, strlen(cmd))) {
252                         verbose = 1 - verbose;
253                         if (verbose)
254                                 printf("  Verbose mode has been turned on.\n");
255                 }
256                 else if (!strncasecmp("dereference", cmd, strlen(cmd))) {
257                         dereference = 1 - dereference;
258                         if (dereference == 1)
259                                 ld->ld_deref = LDAP_DEREF_ALWAYS;
260                         else
261                                 ld->ld_deref = LDAP_DEREF_NEVER;
262                 }
263                 else if (!strncasecmp("tidy", cmd, strlen(cmd)))
264                         tidy_up();
265                 else if (cmd[0] == '\0')
266                         putchar('\n');
267                 else
268                         printf("  Invalid command.  Type \"help commands.\"\n");
269         }
270         printf(" Thank you!\n");
271         
272         ldap_unbind(ld);
273 #ifdef HAVE_KERBEROS
274         destroy_tickets();
275 #endif
276         exit(0);
277         /* NOTREACHED */
278 }
279
280 void
281 status( void )
282 {
283         register char **rdns;
284
285 #ifdef DEBUG
286         if (debug & D_TRACE)
287                 printf("->status()\n");
288 #endif
289         printf("  Current server is %s", server);
290         if ( ld != NULL && ld->ld_host != NULL && strcasecmp( ld->ld_host,
291             server ) != 0 )
292                 printf( " (%s)", ld->ld_host );
293         putchar( '\n' );
294         printbase("  Search base is ", search_base);
295         printbase("  Group  base is ", group_base);
296         if ( bound_dn != NULL ) {
297                 rdns = ldap_explode_dn(bound_dn, TRUE);
298                 printf("  Bound as \"%s\"\n", *rdns);
299                 ldap_value_free(rdns);
300         } else {
301                 printf("  Bound as Nobody\n" );
302         }
303         printf( "  Verbose mode is %sabled\n", ( verbose ? "en" : "dis" ));
304         if ( ld != NULL ) {
305                 printf( "  Aliases are %sbeing dereferenced\n", ( ld->ld_deref == LDAP_DEREF_ALWAYS ) ? "" : "not" );
306         }
307 }
308
309 void
310 change_base( int type, char **base, char *s )
311 {
312         register char *cp;                      /* utility pointers */
313         char **rdns;                            /* for parsing */
314         char *output_string;                    /* for nice output */
315         int num_picked;                         /* # of selected base */
316         int j;                                  /* used with num_picked */
317         int i = 1;                              /* index into choices array */
318         int matches;                            /* # of matches found */
319         int rest = 1;                           /* # left to display */
320         char tmp[MED_BUF_SIZE];                 /* temporary buffer */
321         static char *choices[MED_BUF_SIZE];     /* bases from which to choose */
322         static char resp[SMALL_BUF_SIZE];       /* for prompting user */
323         static char buf[MED_BUF_SIZE];
324         static char *attrs[] = { "objectClass", NULL };
325         LDAPMessage *mp;                        /* results from a search */
326         LDAPMessage *ep;                        /* for going thru bases */
327
328 #ifdef DEBUG
329         if (debug & D_TRACE)
330                 printf("->change_base(%s, %s)\n", s, s);
331 #endif
332         /*
333          *  If s is NULL we need to prompt the user for an argument.
334          */
335         while (s == NULL) {
336                 if (verbose) {
337                         printf("  You need to specify how the base is to be changed.  Valid choices are:\n");
338                         printf("     ?       - list the choices immediately below this level\n");
339                         printf("     ..      - move up one level in the Directory tree\n");
340                         printf("     root    - move to the root of the Directory tree\n");
341                         printf("     default - move to the default level built into this program\n");
342                         printf("     <entry> - move to the entry specified\n");
343                 }
344                 printf("  Change base to? ");
345                 fflush(stdout);
346                 fetch_buffer(buf, sizeof(buf), stdin);
347                 if ((buf != NULL) && (buf[0] != '\0'))
348                         s = buf;
349         }
350
351         /* set the output string */
352         if (type == BASE_SEARCH)
353                 output_string = "  Search base is now ";
354         else if (type == BASE_GROUPS)
355                 output_string = "  Group base is now ";
356
357         if (!strcasecmp(s, "root")) {
358                 StrFreeDup(base, NULL);
359                 printbase("  Search base is ", *base);
360                 return;
361         }
362
363         /*
364          *  User wants to ascend one level in the LDAP tree.
365          *  Easy:  Just strip off the first element of the
366          *  current search base, unless it's the root, in
367          *  which case we just do nothing.
368          */
369         if (!strcasecmp(s, "..")) {
370                 if (*base == NULL) {
371                         printf("  You are already at the root\n");
372                         return;
373                 }
374                 cp = strchr(*base, '=');
375                 cp++;
376                 /*
377                  *  If there isn't a second "=" in the base, then this was
378                  *  a one element base, and so now it should be NULL.
379                  */
380                 if ((cp = strchr(cp, '=')) == NULL)
381                         StrFreeDup(base, NULL);
382                 else {
383                         /*
384                          *  Back up to the start of this
385                          *
386                          *      attr=value
387                          *
388                          *  sequence now that 'cp' is pointing to the '='.
389                          */
390                         while(!isspace(*cp))
391                                 cp--;
392                         cp++;
393                         /*
394                          *  Goofy, but need to do it this way since both *base
395                          *  and cp point into the same chunk of memory, and
396                          *  we want to free *base, but keep part of it around.
397                          */
398                         cp = strdup(cp);
399                         StrFreeDup(base, cp);
400                         Free(cp);
401                 }
402                 printbase(output_string, *base);
403                 return;
404         }
405
406         /* user wants to see what is directly below this level */
407         if (*s == '?') {
408                 /*
409                  *  Fetch the list of entries directly below this level.
410                  *  Once we have the list, we will print it for the user, one
411                  *  screenful at a time.  At the end of each screen, we ask
412                  *  the user if they want to see more.  They can also just
413                  *  type a number at that point too.
414                  */
415                 if (ldap_search_s(ld, *base, LDAP_SCOPE_ONELEVEL, "(|(objectClass=quipuNonLeafObject)(objectClass=externalNonLeafObject))", attrs, FALSE, &mp) != LDAP_SUCCESS) {
416                         if ((ld->ld_errno == LDAP_TIMELIMIT_EXCEEDED) ||
417                             (ld->ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
418                                 if (verbose) {
419                                         printf("  Your query was too general and a limit was exceeded.  The results listed\n");
420                                         printf("  are not complete.  You may want to try again with a more refined query.\n\n");
421                                 }
422                                 else
423                                         printf("  Time or size limit exceeded.  Partial results follow.\n\n");
424                         } else {
425                                 ldap_perror(ld, "ldap_search_s");
426                                 return;
427                         }
428                 }
429                 if ((matches = ldap_count_entries(ld, mp)) < 1) {
430                         printf("  There is nothing below this level.\n");
431                         (void) ldap_msgfree(mp);
432                         return;
433                 }
434                 num_picked = 0;
435                 printf(" There are %d choices:\n", matches);
436                 for (ep = ldap_first_entry(ld, mp); ep != NULL; ep = ldap_next_entry(ld, ep)) {
437                         /*
438                          *  Put the last component of the DN into 'lastDN'.
439                          *  If we are at the root level, convert any country
440                          *  codes to recognizable names for printing.
441                          */
442                         choices[i] = ldap_get_dn(ld, ep);
443                         rdns = ldap_explode_dn(choices[i], TRUE);
444                         printf(" %2d. %s\n", i, friendly_name(*rdns));
445                         (void) ldap_value_free(rdns);
446                         i++;
447                         if ((rest++ > (lpp - 3)) && (i < matches)) {
448                                 printf("More? ");
449                                 fflush(stdout);
450                                 fetch_buffer(resp, sizeof(resp), stdin);
451                                 if ((resp[0] == 'n') || (resp[0] == 'N'))
452                                         break;
453                                 else if (((num_picked = atoi(resp)) != 0) && (num_picked < i))
454                                         break;
455                                 rest = 1;
456                         }
457                 }
458                 for (;;) {
459                         if (num_picked != 0) {
460                                 j = num_picked;
461                                 num_picked = 0;
462                         }
463                         else {
464                                 printf(" Which number? ");
465                                 fflush(stdout);
466                                 fetch_buffer(resp, sizeof(resp), stdin);
467                                 j = atoi(resp);
468                         }
469                         if (j == 0) {
470                                 (void) ldap_msgfree(mp);
471                                 for (i = 0; i < matches; i++)
472                                         Free(choices[i]);
473                                 return;
474                         }
475                         if ((j < 1) || (j >= i))
476                                 printf(" Invalid number\n");
477                         else {
478                                 StrFreeDup(base, choices[j]);
479                                 printbase(output_string, *base);
480                                 (void) ldap_msgfree(mp);
481                                 for (i = 0; choices[i] != NULL; i++)
482                                         Free(choices[i]);
483                                 return;
484                         }
485                 }
486         }
487         /* set the search base back to the original default value */
488         else if (!strcasecmp(s, "default")) {
489                 if (type == BASE_SEARCH)
490                         StrFreeDup(base, UD_BASE);
491                 else if (type == BASE_GROUPS)
492                         StrFreeDup(base, UD_WHERE_GROUPS_ARE_CREATED);
493                 printbase(output_string, *base);
494         }
495         /* they typed in something -- see if it is legit */
496         else {
497                 /* user cannot do something like 'cb 33' */
498                 if (atoi(s) != 0) {
499                         printf("  \"%s\" is not a valid search base\n", s);
500                         printf("  Base unchanged.\n");
501                         printf("  Try using 'cb ?'\n");
502                         return;
503                 }
504                 /* was it a fully-specified DN? */
505                 if (vrfy(s)) {
506                         StrFreeDup(base, s);
507                         printbase(output_string, *base);
508                         return;
509                 }
510                 /* was it a RDN relative to the current base? */
511                 sprintf(tmp, "ou=%s, %s", s, *base);
512                 if (vrfy(tmp)) {
513                         StrFreeDup(base, tmp);
514                         printbase(output_string, *base);
515                         return;
516                 }
517                 printf("  \"%s\" is not a valid base\n  Base unchanged.\n", s);
518         }
519 }
520
521 void
522 initialize_client( void )
523 {
524         FILE *fp;                               /* for config file */
525         static char buffer[MED_BUF_SIZE];       /* for input */
526 #ifdef HAVE_GETPWUID
527         struct passwd *pw;                      /* for getting the home dir */
528 #endif
529         register char *cp;                      /* for fiddling with buffer */
530         char *config;                           /* config file to use */
531         static char bp[1024];                   /* for tty set-up */
532
533 #ifdef DEBUG
534         if (debug & D_TRACE)
535                 printf("->initialize_client()\n");
536 #endif
537         /*
538          *  A per-user config file has precedence over any system-wide
539          *  config file, if one exists.
540          */
541 #ifdef HAVE_GETPWUID
542         if ((pw = getpwuid((uid_t) geteuid())) == (struct passwd *) NULL)
543                 config = config_file;
544         else {
545                 if (pw->pw_dir == NULL)
546                         config = config_file;
547                 else {
548                         sprintf(buffer, "%s/%s", pw->pw_dir,
549                             UD_USER_CONFIG_FILE);
550                         if (access(buffer, R_OK) == 0)
551                                 config = buffer;
552                         else
553                                 config = config_file;
554                 }
555         }
556 #endif /* getpwduid() */
557 #ifdef DEBUG
558         if (debug & D_INITIALIZE)
559                 printf("Using config file %s\n", config);
560 #endif
561
562         /*
563          *  If there is a config file, read it.
564          *
565          *  Could have lines that look like this:
566          *
567          *      server <ip-address or domain-name>
568          *      base   <default search base>
569          *      groupbase <default place where groups are created>
570          *
571          */
572         if ((fp = fopen(config, "r")) != NULL) {
573                 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
574                         buffer[strlen(buffer) - 1] = '\0';
575                         if (!strncasecmp(buffer, "server", 6)) {
576                                 if (server != NULL)
577                                         continue;
578                                 cp = buffer + 6;
579                                 while (isspace(*cp))
580                                         cp++;
581                                 if ((*cp == '\0') || (*cp == '\n'))
582                                         continue;
583                                 server = strdup(cp);
584                         }
585                         else if (!strncasecmp(buffer, "base", 4)) {
586                                 cp = buffer + 4;
587                                 while (isspace(*cp))
588                                         cp++;
589                                 if ((*cp == '\0') || (*cp == '\n'))
590                                         continue;
591                                 search_base = strdup(cp);
592                         }
593                         else if (!strncasecmp(buffer, "groupbase", 9)) {
594                                 cp = buffer + 9;
595                                 while (isspace(*cp))
596                                         cp++;
597                                 if ((*cp == '\0') || (*cp == '\n'))
598                                         continue;
599                                 group_base = strdup(cp);
600                         }
601                         else
602                                 fprintf(stderr, "?? -> %s\n", buffer);
603                 }
604         }
605         if (group_base == NULL)
606                 group_base = strdup(UD_WHERE_GROUPS_ARE_CREATED);
607         if (search_base == NULL)
608                 search_base = strdup(UD_BASE);
609         if (server == NULL)
610                 server = strdup(LDAPHOST);
611
612         /*
613          *  Set up our LDAP connection.  The values of retry and timeout
614          *  are meaningless since we will immediately be doing a null bind
615          *  because we want to be sure to use TCP, not UDP.
616          */
617         if ((ld = ldap_open(server, ldap_port)) == NULL) {
618                 fprintf(stderr, "  The LDAP Directory is temporarily unavailable.  Please try again later.\n");
619                 exit(0);
620                 /* NOTREACHED */
621         }
622         if (ldap_bind_s(ld, (char *) default_bind_object, (char *) UD_BIND_CRED,
623             LDAP_AUTH_SIMPLE) != LDAP_SUCCESS) {
624                 fprintf(stderr, "  The LDAP Directory is temporarily unavailable.  Please try again later.\n");
625                 if (ld->ld_errno != LDAP_UNAVAILABLE)
626                         ldap_perror(ld, "  ldap_bind_s");
627                 exit(0);
628                 /* NOTREACHED */
629         }
630         ld->ld_deref = LDAP_DEREF_ALWAYS;
631         bind_status = UD_NOT_BOUND;
632         if ( default_bind_object != NULL ) {
633                 bound_dn = strdup(default_bind_object);
634         } else {
635                 bound_dn = NULL;
636         }
637
638         /* enabled local caching of ldap results, 15 minute lifetime */
639         ldap_enable_cache( ld, 60 * 15, 0 );            /* no memory limit */
640
641         /* initialize the search filters */
642         if ((lfdp = ldap_init_getfilter(filter_file)) == NULL) {
643                 fprintf(stderr, "  Problem with ldap_init_getfilter\n");
644                 fatal(filter_file);
645                 /*NOTREACHED*/
646         }
647
648         /* terminal initialization stuff goes here */
649         lpp = DEFAULT_TTY_HEIGHT;
650         col_size = DEFAULT_TTY_WIDTH;
651
652         (void) SIGNAL (SIGINT, attn);
653
654 #ifndef NO_TERMCAP
655         {
656         char *term;
657
658         if (((term = getenv("TERM")) == NULL) || (tgetent(bp, term) <= 0))
659                 return;
660         else {
661 #ifdef TIOCGWINSZ
662                 struct winsize win;             /* for tty set-up */
663                 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) >= 0) {
664                         if ((lpp = win.ws_row) == 0)
665                                 lpp = tgetnum("li");
666                         if ((col_size = win.ws_col) == 0)
667                                 col_size = tgetnum("co");
668                         if ((lpp <= 0) || tgetflag("hc"))
669                                 lpp = DEFAULT_TTY_HEIGHT;
670                         if ((col_size <= 0) || tgetflag("hc"))
671                                 col_size = DEFAULT_TTY_WIDTH;
672                         (void) SIGNAL (SIGWINCH, chwinsz);
673                 } else
674 #endif
675                 {
676                         lpp = tgetnum("li");
677                         col_size = tgetnum("co");
678                 }
679         }
680         }
681 #endif
682 }
683
684 RETSIGTYPE
685 attn( int sig )
686 {
687         fflush(stderr);
688         fflush(stdout);
689         printf("\n\n  INTERRUPTED!\n");
690
691         (void) SIGNAL (SIGINT, attn);
692
693         longjmp(env, 1);
694 }
695
696 #if !defined(NO_TERMCAP) && defined(TIOCGWINSZ)
697 RETSIGTYPE
698 chwinsz( int sig )
699 {
700         struct winsize win;
701
702         (void) SIGNAL (SIGWINCH, SIG_IGN);
703         if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
704                 if (win.ws_row != 0)
705                         lpp = win.ws_row;
706                 if (win.ws_col != 0)
707                         col_size = win.ws_col;
708         }
709
710         (void) SIGNAL (SIGWINCH, chwinsz);
711 }
712 #endif