⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ttyio.c

📁 zip压缩
💻 C
📖 第 1 页 / 共 2 页
字号:
    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 screensize():  ws_row = %d\n",              wsz.ws_row);            fprintf(stderr, "ttyio.c screensize():  ws_col = %d\n",              wsz.ws_col);        }#endif        /* number of rows */        if (tt_rows != NULL)            *tt_rows = (int)((wsz.ws_row > 0) ? wsz.ws_row : 24);        /* number of columns */        if (tt_cols != NULL)            *tt_cols = (int)((wsz.ws_col > 0) ? wsz.ws_col : 80);        return 0;    /* signal success */    } else {         /* this happens when piping to more(1), for example */#ifdef DEBUG_WINSZ        if (firsttime) {            firsttime = FALSE;            fprintf(stderr,              "ttyio.c screensize():  ioctl(TIOCGWINSZ) failed\n"));        }#endif        /* VT-100 assumed to be minimal hardware */        if (tt_rows != NULL)            *tt_rows = 24;        if (tt_cols != NULL)            *tt_cols = 80;        return 1;       /* signal failure */    }}#else /* !TIOCGWINSZ: service not available, fall back to semi-bogus method */int screensize(tt_rows, tt_cols)    int *tt_rows;    int *tt_cols;{    char *envptr, *getenv();    int n;    int errstat = 0;    /* GRR:  this is overly simplistic, but don't have access to stty/gtty     * system anymore     */    if (tt_rows != NULL) {        envptr = getenv("LINES");        if (envptr == (char *)NULL || (n = atoi(envptr)) < 5) {            /* VT-100 assumed to be minimal hardware */            *tt_rows = 24;            errstat = 1;    /* signal failure */        } else {            *tt_rows = n;        }    }    if (tt_cols != NULL) {        envptr = getenv("COLUMNS");        if (envptr == (char *)NULL || (n = atoi(envptr)) < 5) {            *tt_cols = 80;            errstat = 1;    /* signal failure */        } else {            *tt_cols = n;        }    }    return errstat;}#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)(uch)c;}#else /* !UNIX && !__BEOS__ */#ifndef VMS     /* VMS supplies its own variant of getch() */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 /* !VMS */#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#endifchar *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) */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -