eddy_em: (Default)
eddy_em ([personal profile] eddy_em) wrote2022-11-08 08:57 pm

А с чего это линукс заменяет "\r" на "\n"?

Заметил эту странность, еще когда настраивал концы строки в своем tty_term. А тут, балуясь с приводом нашего третьего телескопчика, обратил внимание, что у меня как-то странно ответы выглядят. Продампил: вах, да там же вместо "\r\n" постоянно выдается "\n\n"! Интересно, для бинарных данных такая замена тоже будет работать?

Читаю функцией read_tty:
int read_tty(TTY_descr *d){
    if(!d || d->comfd < 0) return 0;
    size_t L = 0;
    ssize_t l;
    size_t length = d->bufsz;
    char *ptr = d->buf;
    fd_set rfds;
    struct timeval tv;
    int retval;
    do{
        l = 0;
        FD_ZERO(&rfds);
        FD_SET(d->comfd, &rfds);
        //memcpy(&tv, &tvdefault, sizeof(struct timeval));
        tv = tvdefault;
        retval = select(d->comfd + 1, &rfds, NULL, NULL, &tv);
        if(!retval) break;
        if(retval < 0){
            if(errno == EINTR) continue;
            return -1;
        }
        if(FD_ISSET(d->comfd, &rfds)){
            l = read(d->comfd, ptr, length);
            if(l < 1) return -1; // disconnected
            ptr += l; L += l;
            length -= l;
        }
    }while(l && length);
    d->buflen = L;
    d->buf[L] = 0;
    return (size_t)L;
}

Никаких шуток с заменой здесь не делаю. Настраиваю при помощи tty_init:
static int tty_init(TTY_descr *descr){
    // |O_NONBLOCK ?
    if ((descr->comfd = open(descr->portname, O_RDWR|O_NOCTTY)) < 0){
        WARN(_("Can't use port %s"), descr->portname);
        return globErr ? globErr : 1;
    }
    if(tcgetattr(descr->comfd, &descr->oldtty) < 0){ // Get settings
        WARN(_("Can't get old TTY settings"));
        return globErr ? globErr : 1;
    }
    descr->tty = descr->oldtty;
    descr->tty.c_lflag     = 0; // ~(ICANON | ECHO | ECHOE | ISIG)
    descr->tty.c_oflag     = 0;
    descr->tty.c_cflag     = descr->baudrate|CS8|CREAD|CLOCAL; // 9.6k, 8N1, RW, ignore line ctrl
    descr->tty.c_cc[VMIN]  = 0;  // non-canonical mode
    descr->tty.c_cc[VTIME] = 5;
    if(tcsetattr(descr->comfd, TCSANOW, &descr->tty) < 0){
        WARN(_("Can't apply new TTY settings"));
        return globErr ? globErr : 1;
    }
    // make exclusive open
    if(descr->exclusive){
    if(ioctl(descr->comfd, TIOCEXCL)){
        WARN(_("Can't do exclusive open"));
    }}
    return 0;
}


Шайтан, однако!

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org