]> git.sur5r.net Git - openldap/blob - clients/ud/main.c
The world compiles and links....
[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 #include <pwd.h>
25
26 #include <ac/signal.h>
27 #include <ac/string.h>
28 #include <ac/termios.h>
29 #include <ac/time.h>
30 #include <ac/unistd.h>
31
32 #ifdef HAVE_SYS_FILE_H
33 #include <sys/file.h>
34 #endif
35
36 #include <lber.h>
37 #include <ldap.h>
38 #include <ldapconfig.h>
39 #include "ud.h"
40
41 #ifndef lint
42 char copyright[] =
43 "@(#) Copyright (c) 1991, 1992, 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
44 #endif
45
46 /*
47  *  Used with change_base() to indicate which base we are changing.
48  */
49 #define BASE_SEARCH     0
50 #define BASE_GROUPS     1
51
52 #define iscom(x)        (!strncasecmp(x, cmd, strlen(cmd)))
53
54 static char *server = NULL;
55 static char *config_file = UD_CONFIG_FILE;
56 static char *filter_file = FILTERFILE;
57 static int ldap_port = LDAP_PORT;
58 static int dereference = TRUE;
59
60 char *default_bind_object = UD_BINDDN;
61
62 char *bound_dn;                 /* bound user's Distinguished Name */
63 char *group_base;               /* place in LDAP tree where groups are */
64 char *search_base;              /* place in LDAP tree where searches start */
65
66 static jmp_buf env;             /* spot to jump to on an interrupt */
67
68 int lpp;                        /* lines per page */
69 int verbose;                    /* 1 if verbose mode on */
70 int col_size;                   /* characters across on the screen */
71 int bind_status;                /* user's bind status */
72
73 LDAP *ld;                       /* LDAP descriptor */
74 LDAPFiltDesc *lfdp;             /* LDAP filter descriptor */
75
76 #ifdef DEBUG
77 int debug;                      /* debug flag */
78 #endif
79
80 main(argc, argv)
81 int argc;
82 char *argv[];
83 {
84         extern char Version[];                  /* version number */
85         extern char *optarg;                    /* for parsing argv */
86         register int c;                         /* for parsing argv */
87         register char *cp;                      /* for parsing Version */
88         extern void initialize_attribute_strings();
89
90         verbose = 1;
91
92         /*  handle argument list */
93         while ((c = getopt(argc, argv, "c:d:Df:l:p:s:u:vV")) != -1) {
94                 switch (c) {
95                 case 'l' :
96 #ifdef LDAP_DEBUG
97                         ldap_debug = (int) strtol(optarg, (char **) NULL, 0);
98                         lber_debug = ldap_debug;
99 #endif
100                         break;
101                 case 'd' :
102 #ifdef DEBUG
103                         debug = (int) strtol(optarg, (char **) NULL, 0);
104 #endif
105                         break;
106                 case 's' :
107                         server = strdup(optarg);
108                         break;
109                 case 'c' :
110                         filter_file = strdup(optarg);
111                         break;
112                 case 'f' :
113                         config_file = optarg;
114                         break;
115                 case 'p' :
116                         ldap_port = atoi(optarg);
117                         break;
118                 case 'u' :
119                         default_bind_object = strdup(optarg);
120                         break;
121                 case 'v' :
122                         verbose = 1;    /* this is the default anyways... */
123                         break;
124                 case 'V' :
125                         verbose = 0;
126                         break;
127                 case 'D' :
128                         printf("\n\n  Debug flag values\n\n");
129                         printf("    1  function trace\n");
130                         printf("    2  find() information\n");
131                         printf("    4  group information\n");
132                         printf("    8  mod() information\n");
133                         printf("   16  parsing information\n");
134                         printf("   32  output information\n");
135                         printf("   64  authentication information\n");
136                         printf("  128  initialization information\n\n");
137                         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);
138                         exit(0);
139                 default:
140                         fprintf(stderr, "Usage: %s [-c filter-config-file] [-d debug-level] [-l ldap-debug-level] [-s server] [-p port] [-V]\n", argv[0]);
141                         exit(-1);
142                         /* NOTREACHED */
143                 }
144         }
145
146         /* just print the first line of Version[] */
147         cp = strchr(Version, '\t');
148         if (cp != NULL)
149                 *cp = '\0';
150         printf(Version);
151         fflush( stdout );
152
153         initialize_client();
154         initialize_attribute_strings();
155
156         /* now tackle the user's commands */
157         do_commands();
158         /* NOTREACHED */
159 }
160
161 do_commands()
162 {
163         LDAPMessage *mp;                        /* returned by find() */
164         register char *cp;                      /* misc char pointer */
165         register char *ap;                      /* misc char pointer */
166         static char buf[MED_BUF_SIZE];          /* for prompting */
167         static char cmd[MED_BUF_SIZE];          /* holds the command */
168         static char input[MED_BUF_SIZE];        /* buffer for input */
169         extern LDAPMessage *find();
170         extern void purge_group(), add_group(), remove_group(), x_group(),
171                 tidy_up(), list_groups(), list_memberships(), edit();
172         extern char *nextstr();
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 status()
281 {
282         void printbase();
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 change_base(type, base, s)
310 int type;
311 char **base, *s;
312 {
313         register char *cp;                      /* utility pointers */
314         char **rdns;                            /* for parsing */
315         char *output_string;                    /* for nice output */
316         int num_picked;                         /* # of selected base */
317         int j;                                  /* used with num_picked */
318         int i = 1;                              /* index into choices array */
319         int matches;                            /* # of matches found */
320         int rest = 1;                           /* # left to display */
321         char tmp[MED_BUF_SIZE];                 /* temporary buffer */
322         static char *choices[MED_BUF_SIZE];     /* bases from which to choose */
323         static char resp[SMALL_BUF_SIZE];       /* for prompting user */
324         static char buf[MED_BUF_SIZE];
325         void printbase();
326         static char *attrs[] = { "objectClass", NULL };
327         LDAPMessage *mp;                        /* results from a search */
328         LDAPMessage *ep;                        /* for going thru bases */
329         extern char * friendly_name();
330         extern void StrFreeDup();
331         extern void Free();
332
333 #ifdef DEBUG
334         if (debug & D_TRACE)
335                 printf("->change_base(%s, %s)\n", s, s);
336 #endif
337         /*
338          *  If s is NULL we need to prompt the user for an argument.
339          */
340         while (s == NULL) {
341                 if (verbose) {
342                         printf("  You need to specify how the base is to be changed.  Valid choices are:\n");
343                         printf("     ?       - list the choices immediately below this level\n");
344                         printf("     ..      - move up one level in the Directory tree\n");
345                         printf("     root    - move to the root of the Directory tree\n");
346                         printf("     default - move to the default level built into this program\n");
347                         printf("     <entry> - move to the entry specified\n");
348                 }
349                 printf("  Change base to? ");
350                 fflush(stdout);
351                 fetch_buffer(buf, sizeof(buf), stdin);
352                 if ((buf != NULL) && (buf[0] != '\0'))
353                         s = buf;
354         }
355
356         /* set the output string */
357         if (type == BASE_SEARCH)
358                 output_string = "  Search base is now ";
359         else if (type == BASE_GROUPS)
360                 output_string = "  Group base is now ";
361
362         if (!strcasecmp(s, "root")) {
363                 StrFreeDup(base, NULL);
364                 printbase("  Search base is ", *base);
365                 return;
366         }
367
368         /*
369          *  User wants to ascend one level in the LDAP tree.
370          *  Easy:  Just strip off the first element of the
371          *  current search base, unless it's the root, in
372          *  which case we just do nothing.
373          */
374         if (!strcasecmp(s, "..")) {
375                 if (*base == NULL) {
376                         printf("  You are already at the root\n");
377                         return;
378                 }
379                 cp = strchr(*base, '=');
380                 cp++;
381                 /*
382                  *  If there isn't a second "=" in the base, then this was
383                  *  a one element base, and so now it should be NULL.
384                  */
385                 if ((cp = strchr(cp, '=')) == NULL)
386                         StrFreeDup(base, NULL);
387                 else {
388                         /*
389                          *  Back up to the start of this
390                          *
391                          *      attr=value
392                          *
393                          *  sequence now that 'cp' is pointing to the '='.
394                          */
395                         while(!isspace(*cp))
396                                 cp--;
397                         cp++;
398                         /*
399                          *  Goofy, but need to do it this way since both *base
400                          *  and cp point into the same chunk of memory, and
401                          *  we want to free *base, but keep part of it around.
402                          */
403                         cp = strdup(cp);
404                         StrFreeDup(base, cp);
405                         Free(cp);
406                 }
407                 printbase(output_string, *base);
408                 return;
409         }
410
411         /* user wants to see what is directly below this level */
412         if (*s == '?') {
413                 /*
414                  *  Fetch the list of entries directly below this level.
415                  *  Once we have the list, we will print it for the user, one
416                  *  screenful at a time.  At the end of each screen, we ask
417                  *  the user if they want to see more.  They can also just
418                  *  type a number at that point too.
419                  */
420                 if (ldap_search_s(ld, *base, LDAP_SCOPE_ONELEVEL, "(|(objectClass=quipuNonLeafObject)(objectClass=externalNonLeafObject))", attrs, FALSE, &mp) != LDAP_SUCCESS) {
421                         if ((ld->ld_errno == LDAP_TIMELIMIT_EXCEEDED) ||
422                             (ld->ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
423                                 if (verbose) {
424                                         printf("  Your query was too general and a limit was exceeded.  The results listed\n");
425                                         printf("  are not complete.  You may want to try again with a more refined query.\n\n");
426                                 }
427                                 else
428                                         printf("  Time or size limit exceeded.  Partial results follow.\n\n");
429                         } else {
430                                 ldap_perror(ld, "ldap_search_s");
431                                 return;
432                         }
433                 }
434                 if ((matches = ldap_count_entries(ld, mp)) < 1) {
435                         printf("  There is nothing below this level.\n");
436                         (void) ldap_msgfree(mp);
437                         return;
438                 }
439                 num_picked = 0;
440                 printf(" There are %d choices:\n", matches);
441                 for (ep = ldap_first_entry(ld, mp); ep != NULL; ep = ldap_next_entry(ld, ep)) {
442                         /*
443                          *  Put the last component of the DN into 'lastDN'.
444                          *  If we are at the root level, convert any country
445                          *  codes to recognizable names for printing.
446                          */
447                         choices[i] = ldap_get_dn(ld, ep);
448                         rdns = ldap_explode_dn(choices[i], TRUE);
449                         printf(" %2d. %s\n", i, friendly_name(*rdns));
450                         (void) ldap_value_free(rdns);
451                         i++;
452                         if ((rest++ > (lpp - 3)) && (i < matches)) {
453                                 printf("More? ");
454                                 fflush(stdout);
455                                 fetch_buffer(resp, sizeof(resp), stdin);
456                                 if ((resp[0] == 'n') || (resp[0] == 'N'))
457                                         break;
458                                 else if (((num_picked = atoi(resp)) != 0) && (num_picked < i))
459                                         break;
460                                 rest = 1;
461                         }
462                 }
463                 for (;;) {
464                         if (num_picked != 0) {
465                                 j = num_picked;
466                                 num_picked = 0;
467                         }
468                         else {
469                                 printf(" Which number? ");
470                                 fflush(stdout);
471                                 fetch_buffer(resp, sizeof(resp), stdin);
472                                 j = atoi(resp);
473                         }
474                         if (j == 0) {
475                                 (void) ldap_msgfree(mp);
476                                 for (i = 0; i < matches; i++)
477                                         Free(choices[i]);
478                                 return;
479                         }
480                         if ((j < 1) || (j >= i))
481                                 printf(" Invalid number\n");
482                         else {
483                                 StrFreeDup(base, choices[j]);
484                                 printbase(output_string, *base);
485                                 (void) ldap_msgfree(mp);
486                                 for (i = 0; choices[i] != NULL; i++)
487                                         Free(choices[i]);
488                                 return;
489                         }
490                 }
491         }
492         /* set the search base back to the original default value */
493         else if (!strcasecmp(s, "default")) {
494                 if (type == BASE_SEARCH)
495                         StrFreeDup(base, UD_BASE);
496                 else if (type == BASE_GROUPS)
497                         StrFreeDup(base, UD_WHERE_GROUPS_ARE_CREATED);
498                 printbase(output_string, *base);
499         }
500         /* they typed in something -- see if it is legit */
501         else {
502                 /* user cannot do something like 'cb 33' */
503                 if (atoi(s) != 0) {
504                         printf("  \"%s\" is not a valid search base\n", s);
505                         printf("  Base unchanged.\n");
506                         printf("  Try using 'cb ?'\n");
507                         return;
508                 }
509                 /* was it a fully-specified DN? */
510                 if (vrfy(s)) {
511                         StrFreeDup(base, s);
512                         printbase(output_string, *base);
513                         return;
514                 }
515                 /* was it a RDN relative to the current base? */
516                 sprintf(tmp, "ou=%s, %s", s, *base);
517                 if (vrfy(tmp)) {
518                         StrFreeDup(base, tmp);
519                         printbase(output_string, *base);
520                         return;
521                 }
522                 printf("  \"%s\" is not a valid base\n  Base unchanged.\n", s);
523         }
524 }
525
526 initialize_client()
527 {
528         FILE *fp;                               /* for config file */
529         static char buffer[MED_BUF_SIZE];       /* for input */
530         struct passwd *pw;                      /* for getting the home dir */
531         register char *cp;                      /* for fiddling with buffer */
532         char *term;                             /* for tty set-up */
533         char *config;                           /* config file to use */
534         static char bp[1024];                   /* for tty set-up */
535         extern RETSIGTYPE attn();                       /* ^C signal handler */
536         extern char *getenv();
537         extern void Free();
538
539 #ifdef DEBUG
540         if (debug & D_TRACE)
541                 printf("->initialize_client()\n");
542 #endif
543         /*
544          *  A per-user config file has precedence over any system-wide
545          *  config file, if one exists.
546          */
547         if ((pw = getpwuid((uid_t) geteuid())) == (struct passwd *) NULL)
548                 config = config_file;
549         else {
550                 if (pw->pw_dir == NULL)
551                         config = config_file;
552                 else {
553                         sprintf(buffer, "%s/%s", pw->pw_dir,
554                             UD_USER_CONFIG_FILE);
555                         if (access(buffer, R_OK) == 0)
556                                 config = buffer;
557                         else
558                                 config = config_file;
559                 }
560         }
561 #ifdef DEBUG
562         if (debug & D_INITIALIZE)
563                 printf("Using config file %s\n", config);
564 #endif
565
566         /*
567          *  If there is a config file, read it.
568          *
569          *  Could have lines that look like this:
570          *
571          *      server <ip-address or domain-name>
572          *      base   <default search base>
573          *      groupbase <default place where groups are created>
574          *
575          */
576         if ((fp = fopen(config, "r")) != NULL) {
577                 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
578                         buffer[strlen(buffer) - 1] = '\0';
579                         if (!strncasecmp(buffer, "server", 6)) {
580                                 if (server != NULL)
581                                         continue;
582                                 cp = buffer + 6;
583                                 while (isspace(*cp))
584                                         cp++;
585                                 if ((*cp == '\0') || (*cp == '\n'))
586                                         continue;
587                                 server = strdup(cp);
588                         }
589                         else if (!strncasecmp(buffer, "base", 4)) {
590                                 cp = buffer + 4;
591                                 while (isspace(*cp))
592                                         cp++;
593                                 if ((*cp == '\0') || (*cp == '\n'))
594                                         continue;
595                                 search_base = strdup(cp);
596                         }
597                         else if (!strncasecmp(buffer, "groupbase", 9)) {
598                                 cp = buffer + 9;
599                                 while (isspace(*cp))
600                                         cp++;
601                                 if ((*cp == '\0') || (*cp == '\n'))
602                                         continue;
603                                 group_base = strdup(cp);
604                         }
605                         else
606                                 fprintf(stderr, "?? -> %s\n", buffer);
607                 }
608         }
609         if (group_base == NULL)
610                 group_base = strdup(UD_WHERE_GROUPS_ARE_CREATED);
611         if (search_base == NULL)
612                 search_base = strdup(UD_BASE);
613         if (server == NULL)
614                 server = strdup(LDAPHOST);
615
616         /*
617          *  Set up our LDAP connection.  The values of retry and timeout
618          *  are meaningless since we will immediately be doing a null bind
619          *  because we want to be sure to use TCP, not UDP.
620          */
621         if ((ld = ldap_open(server, ldap_port)) == NULL) {
622                 fprintf(stderr, "  The LDAP Directory is temporarily unavailable.  Please try again later.\n");
623                 exit(0);
624                 /* NOTREACHED */
625         }
626         if (ldap_bind_s(ld, (char *) default_bind_object, (char *) UD_BIND_CRED,
627             LDAP_AUTH_SIMPLE) != LDAP_SUCCESS) {
628                 fprintf(stderr, "  The LDAP Directory is temporarily unavailable.  Please try again later.\n");
629                 if (ld->ld_errno != LDAP_UNAVAILABLE)
630                         ldap_perror(ld, "  ldap_bind_s");
631                 exit(0);
632                 /* NOTREACHED */
633         }
634         ld->ld_deref = LDAP_DEREF_ALWAYS;
635         bind_status = UD_NOT_BOUND;
636         if ( default_bind_object != NULL ) {
637                 bound_dn = strdup(default_bind_object);
638         } else {
639                 bound_dn = NULL;
640         }
641
642         /* enabled local caching of ldap results, 15 minute lifetime */
643 #ifdef DOS
644         ldap_enable_cache( ld, 60 * 15, 100 * 1024 );   /* 100k max memory */
645 #else /* DOS */
646         ldap_enable_cache( ld, 60 * 15, 0 );            /* no memory limit */
647 #endif /* DOS */
648
649         /* initialize the search filters */
650         if ((lfdp = ldap_init_getfilter(filter_file)) == NULL) {
651                 fprintf(stderr, "  Problem with ldap_init_getfilter\n");
652                 fatal(filter_file);
653                 /*NOTREACHED*/
654         }
655
656         /* terminal initialization stuff goes here */
657         lpp = DEFAULT_TTY_HEIGHT;
658         col_size = DEFAULT_TTY_WIDTH;
659
660         (void) SIGNAL (SIGINT, attn);
661
662 #ifndef NO_TERMCAP
663         {
664         struct winsize win;                     /* for tty set-up */
665         extern RETSIGTYPE chwinsz();            /* WINSZ signal handler */
666
667         if (((term = getenv("TERM")) == NULL) || (tgetent(bp, term) <= 0))
668                 return;
669         else {
670                 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) {
671                         lpp = tgetnum("li");
672                         col_size = tgetnum("co");
673                 }
674                 else {
675                         if ((lpp = win.ws_row) == 0)
676                                 lpp = tgetnum("li");
677                         if ((col_size = win.ws_col) == 0)
678                                 col_size = tgetnum("co");
679                         if ((lpp <= 0) || tgetflag("hc"))
680                                 lpp = DEFAULT_TTY_HEIGHT;
681                         if ((col_size <= 0) || tgetflag("hc"))
682                                 col_size = DEFAULT_TTY_WIDTH;
683                 }
684         }
685         (void) SIGNAL (SIGWINCH, chwinsz);
686
687         }
688 #endif
689 }
690
691 RETSIGTYPE attn()
692 {
693         fflush(stderr);
694         fflush(stdout);
695         printf("\n\n  INTERRUPTED!\n");
696
697         (void) SIGNAL (SIGINT, attn);
698
699         longjmp(env, 1);
700 }
701
702 #ifndef NO_TERMCAP
703 RETSIGTYPE chwinsz() 
704 {
705         struct winsize win;
706
707         (void) SIGNAL (SIGWINCH, SIG_IGN);
708         if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
709                 if (win.ws_row != 0)
710                         lpp = win.ws_row;
711                 if (win.ws_col != 0)
712                         col_size = win.ws_col;
713         }
714
715         (void) SIGNAL (SIGWINCH, chwinsz);
716 }
717 #endif