Path: blob/main/crypto/heimdal/appl/login/read_string.c
96309 views
/*1* Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "login_locl.h"3435RCSID("$Id$");3637static sig_atomic_t intr_flag;3839static void40intr(int sig)41{42intr_flag++;43}4445#ifndef NSIG46#define NSIG 4747#endif4849int50read_string(const char *prompt, char *buf, size_t len, int echo)51{52struct sigaction sigs[NSIG];53int oksigs[NSIG];54struct sigaction sa;55FILE *tty;56int ret = 0;57int of = 0;58int i;59int c;60char *p;6162struct termios t_new, t_old;6364memset(&oksigs, 0, sizeof(oksigs));6566memset(&sa, 0, sizeof(sa));67sa.sa_handler = intr;68sigemptyset(&sa.sa_mask);69sa.sa_flags = 0;70for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)71if (i != SIGALRM)72if (sigaction(i, &sa, &sigs[i]) == 0)73oksigs[i] = 1;7475if((tty = fopen("/dev/tty", "r")) == NULL)76tty = stdin;7778fprintf(stderr, "%s", prompt);79fflush(stderr);8081if(echo == 0){82tcgetattr(fileno(tty), &t_old);83memcpy(&t_new, &t_old, sizeof(t_new));84t_new.c_lflag &= ~ECHO;85tcsetattr(fileno(tty), TCSANOW, &t_new);86}87intr_flag = 0;88p = buf;89while(intr_flag == 0){90c = getc(tty);91if(c == EOF){92if(!ferror(tty))93ret = 1;94break;95}96if(c == '\n')97break;98if(of == 0)99*p++ = c;100of = (p == buf + len);101}102if(of)103p--;104*p = 0;105106if(echo == 0){107printf("\n");108tcsetattr(fileno(tty), TCSANOW, &t_old);109}110111if(tty != stdin)112fclose(tty);113114for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)115if (oksigs[i])116sigaction(i, &sigs[i], NULL);117118if(ret)119return -3;120if(intr_flag)121return -2;122if(of)123return -1;124return 0;125}126127128#if 0129int main()130{131char s[128];132int ret;133ret = read_string("foo: ", s, sizeof(s), 0);134printf("%d ->%s<-\n", ret, s);135}136#endif137138139