File: ttyio.c

package info (click to toggle)
unzip 5.40-1
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 4,120 kB
  • ctags: 5,900
  • sloc: ansic: 40,977; cpp: 3,778; makefile: 1,384; asm: 1,228; sh: 133
file content (625 lines) | stat: -rw-r--r-- 18,506 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*---------------------------------------------------------------------------

  ttyio.c

  This file contains routines for doing console input/output, including code
  for non-echoing input.  It is used by the encryption/decryption code but
  does not contain any restricted code itself.  This file is shared between
  Info-ZIP's Zip and UnZip.

  Contains:  echo()         (VMS only)
             Echon()        (Unix only)
             Echoff()       (Unix only)
             screenlines()  (Unix only)
             zgetch()       (Unix and non-Unix versions)
             getp()         ("PC," Unix/Atari/Be, VMS/VMCMS/MVS)

  ---------------------------------------------------------------------------*/

#define __TTYIO_C       /* identifies this source module */

#include "zip.h"
#include "crypt.h"

#if (CRYPT || (defined(UNZIP) && !defined(FUNZIP)))
/* Non-echo console/keyboard input is needed for (en/de)cryption's password
 * entry, and for UnZip(SFX)'s MORE and Pause features.
 * (The corresponding #endif is found at the end of this module.)
 */

#include "ttyio.h"

#ifndef PUTC
#  define PUTC putc
#endif

#ifdef ZIP
#  ifdef GLOBAL          /* used in Amiga system headers, maybe others too */
#    undef GLOBAL
#  endif
#  define GLOBAL(g) g
#else
#  define GLOBAL(g) G.g
#endif

#ifdef __BEOS__                /* why yes, we do */
#  define HAVE_TERMIOS_H
#endif

#ifdef _POSIX_VERSION
#  ifndef USE_POSIX_TERMIOS
#    define USE_POSIX_TERMIOS  /* use POSIX style termio (termios) */
#  endif
#  ifndef HAVE_TERMIOS_H
#    define HAVE_TERMIOS_H     /* POSIX termios.h */
#  endif
#endif /* _POSIX_VERSION */

#ifdef UNZIP            /* Zip handles this with the unix/configure script */
#  ifndef _POSIX_VERSION
#    if (defined(SYSV) || defined(CRAY)) &&  !defined(__MINT__)
#      ifndef USE_SYSV_TERMIO
#        define USE_SYSV_TERMIO
#      endif
#      ifdef COHERENT
#        ifndef HAVE_TERMIO_H
#          define HAVE_TERMIO_H
#        endif
#        ifdef HAVE_SYS_TERMIO_H
#          undef HAVE_SYS_TERMIO_H
#        endif
#      else /* !COHERENT */
#        ifdef HAVE_TERMIO_H
#          undef HAVE_TERMIO_H
#        endif
#        ifndef HAVE_SYS_TERMIO_H
#           define HAVE_SYS_TERMIO_H
#        endif
#      endif /* ?COHERENT */
#    endif /* (SYSV || CRAY) && !__MINT__ */
#  endif /* !_POSIX_VERSION */
#  if !(defined(BSD4_4) || defined(SYSV) || defined(__convexc__))
#    ifndef NO_FCNTL_H
#      define NO_FCNTL_H
#    endif
#  endif /* !(BSD4_4 || SYSV || __convexc__) */
#endif /* UNZIP */

#ifdef HAVE_TERMIOS_H
#  ifndef USE_POSIX_TERMIOS
#    define USE_POSIX_TERMIOS
#  endif
#endif

#if (defined(HAVE_TERMIO_H) || defined(HAVE_SYS_TERMIO_H))
#  ifndef USE_SYSV_TERMIO
#    define USE_SYSV_TERMIO
#  endif
#endif

#if (defined(UNZIP) && !defined(FUNZIP) && defined(UNIX) && defined(MORE))
#  include <sys/ioctl.h>
#  define GOT_IOCTL_H
   /* int ioctl OF((int, int, zvoid *));   GRR: may need for some systems */
#endif

#ifndef HAVE_WORKING_GETCH
   /* include system support for switching of console echo */
#  ifdef VMS
#    include <descrip.h>
#    include <iodef.h>
#    include <ttdef.h>
#    include <starlet.h>
#    include <ssdef.h>
#  else /* !VMS */
#    ifdef HAVE_TERMIOS_H
#      include <termios.h>
#      define sgttyb termios
#      define sg_flags c_lflag
#      define GTTY(f, s) tcgetattr(f, (zvoid *) s)
#      define STTY(f, s) tcsetattr(f, TCSAFLUSH, (zvoid *) s)
#    else /* !HAVE_TERMIOS_H */
#      ifdef USE_SYSV_TERMIO           /* Amdahl, Cray, all SysV? */
#        ifdef HAVE_TERMIO_H
#          include <termio.h>
#        endif
#        ifdef HAVE_SYS_TERMIO_H
#          include <sys/termio.h>
#        endif
#        ifdef NEED_PTEM
#          include <sys/stream.h>
#          include <sys/ptem.h>
#        endif
#        define sgttyb termio
#        define sg_flags c_lflag
#        define GTTY(f,s) ioctl(f,TCGETA,(zvoid *)s)
#        define STTY(f,s) ioctl(f,TCSETAW,(zvoid *)s)
#      else /* !USE_SYSV_TERMIO */
#        ifndef CMS_MVS
#          if (!defined(MINIX) && !defined(GOT_IOCTL_H))
#            include <sys/ioctl.h>
#          endif
#          include <sgtty.h>
#          define GTTY gtty
#          define STTY stty
#          ifdef UNZIP
             /*
              * XXX : Are these declarations needed at all ????
              */
             /*
              * GRR: let's find out...   Hmmm, appears not...
             int gtty OF((int, struct sgttyb *));
             int stty OF((int, struct sgttyb *));
              */
#          endif
#        endif /* !CMS_MVS */
#      endif /* ?USE_SYSV_TERMIO */
#    endif /* ?HAVE_TERMIOS_H */
#    ifndef NO_FCNTL_H
#      ifndef UNZIP
#        include <fcntl.h>
#      endif
#    else
       char *ttyname OF((int));
#    endif
#  endif /* ?VMS */
#endif /* !HAVE_WORKING_GETCH */



#ifndef HAVE_WORKING_GETCH
#ifdef VMS

/*
 * Turn keyboard echoing on or off (VMS).  Loosely based on VMSmunch.c
 * and hence on Joe Meadows' file.c code.
 */
int echo(opt)
    int opt;
{
    /*
     * For VMS v5.x:
     *   IO$_SENSEMODE/SETMODE info:  Programming, Vol. 7A, System Programming,
     *     I/O User's: Part I, sec. 8.4.1.1, 8.4.3, 8.4.5, 8.6
     *   sys$assign(), sys$qio() info:  Programming, Vol. 4B, System Services,
     *     System Services Reference Manual, pp. sys-23, sys-379
     *   fixed-length descriptor info:  Programming, Vol. 3, System Services,
     *     Intro to System Routines, sec. 2.9.2
     * Greg Roelofs, 15 Aug 91
     */

    /* SKM: make global? */
    static struct dsc$descriptor_s DevDesc =
        {11, DSC$K_DTYPE_T, DSC$K_CLASS_S, "SYS$COMMAND"};
     /* {dsc$w_length, dsc$b_dtype, dsc$b_class, dsc$a_pointer}; */
    static short           DevChan, iosb[4];
    static long            status;
    static unsigned long   oldmode[2], newmode[2];   /* each = 8 bytes */


    /* assign a channel to standard input */
    status = sys$assign(&DevDesc, &DevChan, 0, 0);
    if (!(status & 1))
        return status;

    /* use sys$qio and the IO$_SENSEMODE function to determine the current
     * tty status (for password reading, could use IO$_READVBLK function
     * instead, but echo on/off will be more general)
     */
    status = sys$qiow(0, DevChan, IO$_SENSEMODE, &iosb, 0, 0,
                     oldmode, 8, 0, 0, 0, 0);
    if (!(status & 1))
        return status;
    status = iosb[0];
    if (!(status & 1))
        return status;

    /* copy old mode into new-mode buffer, then modify to be either NOECHO or
     * ECHO (depending on function argument opt)
     */
    newmode[0] = oldmode[0];
    newmode[1] = oldmode[1];
    if (opt == 0)   /* off */
        newmode[1] |= TT$M_NOECHO;                      /* set NOECHO bit */
    else
        newmode[1] &= ~((unsigned long) TT$M_NOECHO);   /* clear NOECHO bit */

    /* use the IO$_SETMODE function to change the tty status */
    status = sys$qiow(0, DevChan, IO$_SETMODE, &iosb, 0, 0,
                     newmode, 8, 0, 0, 0, 0);
    if (!(status & 1))
        return status;
    status = iosb[0];
    if (!(status & 1))
        return status;

    /* deassign the sys$input channel by way of clean-up */
    status = sys$dassgn(DevChan);
    if (!(status & 1))
        return status;

    return SS$_NORMAL;   /* we be happy */

} /* end function echo() */


#else /* !VMS:  basically Unix */


/* For VM/CMS and MVS, non-echo terminal input is not (yet?) supported. */
#ifndef CMS_MVS

#ifdef ZIP                      /* moved to globals.h for UnZip */
   static int echofd=(-1);      /* file descriptor whose echo is off */
#endif

/*
 * Turn echo off for file descriptor f.  Assumes that f is a tty device.
 */
void Echoff(__G__ f)
    __GDEF
    int f;                    /* file descriptor for which to turn echo off */
{
    struct sgttyb sg;         /* tty device structure */

    GLOBAL(echofd) = f;
    GTTY(f, &sg);             /* get settings */
    sg.sg_flags &= ~ECHO;     /* turn echo off */
    STTY(f, &sg);
}

/*
 * Turn echo back on for file descriptor echofd.
 */
void Echon(__G)
    __GDEF
{
    struct sgttyb sg;         /* tty device structure */

    if (GLOBAL(echofd) != -1) {
        GTTY(GLOBAL(echofd), &sg);    /* get settings */
        sg.sg_flags |= ECHO;  /* turn echo on */
        STTY(GLOBAL(echofd), &sg);
        GLOBAL(echofd) = -1;
    }
}

#endif /* !CMS_MVS */
#endif /* ?VMS */


#if (defined(UNZIP) && !defined(FUNZIP))

#if (defined(UNIX) || defined(__BEOS__))
#ifdef MORE

/*
 * Get the number of lines on the output terminal.  SCO Unix apparently
 * defines TIOCGWINSZ but doesn't support it (!M_UNIX).
 *
 * GRR:  will need to know width of terminal someday, too, to account for
 *       line-wrapping.
 */

#if (defined(TIOCGWINSZ) && !defined(M_UNIX))

int screenlines()
{
    struct winsize wsz;
#ifdef DEBUG_WINSZ
    static int firsttime = TRUE;
#endif

    /* see termio(4) under, e.g., SunOS */
    if (ioctl(1, TIOCGWINSZ, &wsz) == 0) {
#ifdef DEBUG_WINSZ
        if (firsttime) {
            firsttime = FALSE;
            fprintf(stderr, "ttyio.c screenlines():  ws_row = %d\n",
              wsz.ws_row);
        }
#endif
        /* number of columns = ws_col */
        return (wsz.ws_row > 0)? wsz.ws_row : 24;   /* number of rows */

    } else {         /* this happens when piping to more(1), for example */
#ifdef DEBUG_WINSZ
        if (firsttime) {
            firsttime = FALSE;
            fprintf(stderr,
              "ttyio.c screenlines():  ioctl(TIOCGWINSZ) failed\n"));
        }
#endif
        return 24;   /* VT-100 assumed to be minimal hardware */
    }
}

#else /* !TIOCGWINSZ: service not available, fall back to semi-bogus method */

int screenlines()
{
    char *envptr, *getenv();
    int n;

    /* GRR:  this is overly simplistic, but don't have access to stty/gtty
     * system anymore
     */
    envptr = getenv("LINES");
    if (envptr == (char *)NULL || (n = atoi(envptr)) < 5)
        return 24;   /* VT-100 assumed to be minimal hardware */
    else
        return n;
}

#endif /* ?(TIOCGWINSZ && !M_UNIX) */
#endif /* MORE */


/*
 * Get a character from the given file descriptor without echo or newline.
 */
int zgetch(__G__ f)
    __GDEF
    int f;                      /* file descriptor from which to read */
{
#if (defined(USE_SYSV_TERMIO) || defined(USE_POSIX_TERMIOS))
    char oldmin, oldtim;
#endif
    char c;
    struct sgttyb sg;           /* tty device structure */

    GTTY(f, &sg);               /* get settings */
#if (defined(USE_SYSV_TERMIO) || defined(USE_POSIX_TERMIOS))
    oldmin = sg.c_cc[VMIN];     /* save old values */
    oldtim = sg.c_cc[VTIME];
    sg.c_cc[VMIN] = 1;          /* need only one char to return read() */
    sg.c_cc[VTIME] = 0;         /* no timeout */
    sg.sg_flags &= ~ICANON;     /* canonical mode off */
#else
    sg.sg_flags |= CBREAK;      /* cbreak mode on */
#endif
    sg.sg_flags &= ~ECHO;       /* turn echo off, too */
    STTY(f, &sg);               /* set cbreak mode */
    GLOBAL(echofd) = f;         /* in case ^C hit (not perfect: still CBREAK) */

    read(f, &c, 1);             /* read our character */

#if (defined(USE_SYSV_TERMIO) || defined(USE_POSIX_TERMIOS))
    sg.c_cc[VMIN] = oldmin;     /* restore old values */
    sg.c_cc[VTIME] = oldtim;
    sg.sg_flags |= ICANON;      /* canonical mode on */
#else
    sg.sg_flags &= ~CBREAK;     /* cbreak mode off */
#endif
    sg.sg_flags |= ECHO;        /* turn echo on */
    STTY(f, &sg);               /* restore canonical mode */
    GLOBAL(echofd) = -1;

    return (int)c;
}


#else /* !UNIX && !__BEOS__ */


int zgetch(__G__ f)
    __GDEF
    int f;    /* file descriptor from which to read (must be open already) */
{
    char c, c2;

/*---------------------------------------------------------------------------
    Get a character from the given file descriptor without echo; can't fake
    CBREAK mode (i.e., newline required), but can get rid of all chars up to
    and including newline.
  ---------------------------------------------------------------------------*/

    echoff(f);
    read(f, &c, 1);
    if (c != '\n')
        do {
            read(f, &c2, 1);   /* throw away all other chars up thru newline */
        } while (c2 != '\n');
    echon();
    return (int)c;
}

#endif /* ?(UNIX || __BEOS__) */

#endif /* UNZIP && !FUNZIP */
#endif /* !HAVE_WORKING_GETCH */


#if CRYPT                       /* getp() is only used with full encryption */

/*
 * Simple compile-time check for source compatibility between
 * zcrypt and ttyio:
 */
#if (!defined(CR_MAJORVER) || (CR_MAJORVER < 2) || (CR_MINORVER < 7))
   error:  This Info-ZIP tool requires zcrypt 2.7 or later.
#endif

/*
 * Get a password of length n-1 or less into *p using the prompt *m.
 * The entered password is not echoed.
 */

#ifdef HAVE_WORKING_GETCH
/*
 * For the AMIGA, getch() is defined as Agetch(), which is in
 * amiga/filedate.c; SAS/C 6.x provides a getch(), but since Agetch()
 * uses the infrastructure that is already in place in filedate.c, it is
 * smaller.  With this function, echoff() and echon() are not needed.
 *
 * For the MAC, a non-echo macgetch() function is defined in the MacOS
 * specific sources which uses the event handling mechanism of the
 * desktop window manager to get a character from the keyboard.
 *
 * For the other systems in this section, a non-echo getch() function
 * is either contained the C runtime library (conio package), or getch()
 * is defined as an alias for a similar system specific RTL function.
 */

#ifndef WINDLL   /* WINDLL does not support a console interface */
#ifndef QDOS     /* QDOS supplies a variant of this function */

/* This is the getp() function for all systems (with TTY type user interface)
 * that supply a working `non-echo' getch() function for "raw" console input.
 */
char *getp(__G__ m, p, n)
    __GDEF
    ZCONST char *m;             /* prompt for password */
    char *p;                    /* return value: line input */
    int n;                      /* bytes available in p[] */
{
    char c;                     /* one-byte buffer for read() to use */
    int i;                      /* number of characters input */
    char *w;                    /* warning on retry */

    /* get password */
    w = "";
    do {
        fputs(w, stderr);       /* warning if back again */
        fputs(m, stderr);       /* display prompt and flush */
        fflush(stderr);
        i = 0;
        do {                    /* read line, keeping first n characters */
            if ((c = (char)getch()) == '\r')
                c = '\n';       /* until user hits CR */
            if (c == 8 || c == 127) {
                if (i > 0) i--; /* the `backspace' and `del' keys works */
            }
            else if (i < n)
                p[i++] = c;     /* truncate past n */
        } while (c != '\n');
        PUTC('\n', stderr);  fflush(stderr);
        w = "(line too long--try again)\n";
    } while (p[i-1] != '\n');
    p[i-1] = 0;                 /* terminate at newline */

    return p;                   /* return pointer to password */

} /* end function getp() */

#endif /* !QDOS */
#endif /* !WINDLL */


#else /* !HAVE_WORKING_GETCH */


#if (defined(UNIX) || defined(__MINT__) || defined(__BEOS__))

#ifndef _PATH_TTY
#  ifdef __MINT__
#    define _PATH_TTY ttyname(2)
#  else
#    define _PATH_TTY "/dev/tty"
#  endif
#endif

char *getp(__G__ m, p, n)
    __GDEF
    ZCONST char *m;             /* prompt for password */
    char *p;                    /* return value: line input */
    int n;                      /* bytes available in p[] */
{
    char c;                     /* one-byte buffer for read() to use */
    int i;                      /* number of characters input */
    char *w;                    /* warning on retry */
    int f;                      /* file descriptor for tty device */

#ifdef PASSWD_FROM_STDIN
    /* Read from stdin. This is unsafe if the password is stored on disk. */
    f = 0;
#else
    /* turn off echo on tty */

    if ((f = open(_PATH_TTY, 0)) == -1)
        return NULL;
#endif
    /* get password */
    w = "";
    do {
        fputs(w, stderr);       /* warning if back again */
        fputs(m, stderr);       /* prompt */
        fflush(stderr);
        i = 0;
        echoff(f);
        do {                    /* read line, keeping n */
            read(f, &c, 1);
            if (i < n)
                p[i++] = c;
        } while (c != '\n');
        echon();
        PUTC('\n', stderr);  fflush(stderr);
        w = "(line too long--try again)\n";
    } while (p[i-1] != '\n');
    p[i-1] = 0;                 /* terminate at newline */

#ifndef PASSWD_FROM_STDIN
    close(f);
#endif

    return p;                   /* return pointer to password */

} /* end function getp() */

#endif /* UNIX || __MINT__ || __BEOS__ */



#if (defined(VMS) || defined(CMS_MVS))

char *getp(__G__ m, p, n)
    __GDEF
    ZCONST char *m;             /* prompt for password */
    char *p;                    /* return value: line input */
    int n;                      /* bytes available in p[] */
{
    char c;                     /* one-byte buffer for read() to use */
    int i;                      /* number of characters input */
    char *w;                    /* warning on retry */
    FILE *f;                    /* file structure for SYS$COMMAND device */

#ifdef PASSWD_FROM_STDIN
    f = stdin;
#else
    if ((f = fopen(ctermid(NULL), "r")) == NULL)
        return NULL;
#endif

    /* get password */
    fflush(stdout);
    w = "";
    do {
        if (*w)                 /* bug: VMS apparently adds \n to NULL fputs */
            fputs(w, stderr);   /* warning if back again */
        fputs(m, stderr);       /* prompt */
        fflush(stderr);
        i = 0;
        echoff(f);
        do {                    /* read line, keeping n */
            if ((c = (char)getc(f)) == '\r')
                c = '\n';
            if (i < n)
                p[i++] = c;
        } while (c != '\n');
        echon();
        PUTC('\n', stderr);  fflush(stderr);
        w = "(line too long--try again)\n";
    } while (p[i-1] != '\n');
    p[i-1] = 0;                 /* terminate at newline */
#ifndef PASSWD_FROM_STDIN
    fclose(f);
#endif

    return p;                   /* return pointer to password */

} /* end function getp() */

#endif /* VMS || CMS_MVS */
#endif /* ?HAVE_WORKING_GETCH */
#endif /* CRYPT */
#endif /* CRYPT || (UNZIP && !FUNZIP) */