示例#1
0
/* opens a rs232 window, returns handle to give to functions below. */
int rs232dev_open(int device)
{
    int i, fd;

    for (i = 0; i < RS232_NUM_DEVICES; i++) {
        if (!fds[i].inuse) {
            break;
        }
    }
    if (i >= RS232_NUM_DEVICES) {
        log_error(rs232dev_log, "No more devices available.");
        return -1;
    }

#ifdef DEBUG
    log_message(rs232dev_log, "rs232dev_open(device=%d).", device);
#endif

    if (rs232_devfile[device][0] == '|') {
#if defined(OPENSTEP_COMPILE) || defined(RHAPSODY_COMPILE) \
        || defined(NEXTSTEP_COMPILE)
        log_error(rs232dev_log, "Forking not supported on this platform.");
        return -1;
#else
        if (fork_coproc(&fds[i].fd_w, &fds[i].fd_r, rs232_devfile[device] + 1) < 0) {
            log_error(rs232dev_log, "Cannot fork process.");
            return -1;
        }
#endif
        fds[i].type = T_PROC;
        fds[i].inuse = 1;
        fds[i].file = rs232_devfile[device];
    } else {
#if !defined(OPENSTEP_COMPILE) && !defined(NEXTSTEP_COMPILE)
        fd = open(rs232_devfile[device], O_RDWR | O_NOCTTY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
        if (fd < 0) {
            log_error(rs232dev_log, "Cannot open file \"%s\": %s", rs232_devfile[device], strerror(errno));
            return -1;
        }
        fds[i].fd_r = fds[i].fd_w = fd;
        fds[i].file = rs232_devfile[device];

        if (isatty(fd)) {
            fds[i].type = T_TTY;
            set_tty(i, devbaud[device]);
        } else {
            fds[i].type = T_FILE;
        }
        fds[i].inuse = 1;
#endif
    }

    return i;
}
示例#2
0
/*
 * TODO: only do this on systems which support it.
 */
FILE *fopen_or_pipe(char *name)
{
    if (name[0] == '|') {
#if COPROC_SUPPORT
        int fd_rd, fd_wr;
        if (fork_coproc(&fd_wr, &fd_rd, name + 1) < 0) {
            /* error */
            return NULL;
        }
        close(fd_rd);   /* We only want to write to the process */
        return fdopen(fd_wr, MODE_WRITE);
#else
        log_error(LOG_DEFAULT, "Cannot fork process.");
        return NULL;
#endif
    } else {
        return fopen(name, MODE_APPEND);
    }
}