Esempio n. 1
0
/*=export_func  optionFileCheck
 * private:
 *
 * what:  Decipher a boolean value
 * arg:   + tOptions*     + pOpts    + program options descriptor  +
 * arg:   + tOptDesc*     + pOptDesc + the descriptor for this arg +
 * arg:   + teOptFileType + ftype    + File handling type          +
 * arg:   + tuFileMode    + mode     + file open mode (if needed)  +
 *
 * doc:
 *   Make sure the named file conforms with the file type mode.
 *   The mode specifies if the file must exist, must not exist or may
 *   (or may not) exist.  The mode may also specify opening the
 *   file: don't, open just the descriptor (fd), or open as a stream
 *   (FILE* pointer).
=*/
void
optionFileCheck(tOptions * pOpts, tOptDesc * pOD,
                teOptFileType ftype, tuFileMode mode)
{
    if (pOpts <= OPTPROC_EMIT_LIMIT) {
        if (pOpts != OPTPROC_EMIT_USAGE)
            return;

        switch (ftype & FTYPE_MODE_EXIST_MASK) {
        case FTYPE_MODE_MUST_NOT_EXIST:
            fputs(zFileCannotExist + tab_skip_ct, option_usage_fp);
            break;

        case FTYPE_MODE_MUST_EXIST:
            fputs(zFileMustExist + tab_skip_ct, option_usage_fp);
            break;
        }
        return;
    }

    if ((pOD->fOptState & OPTST_RESET) != 0) {
        if (pOD->optCookie != NULL)
            AGFREE(pOD->optCookie);
        return;
    }

    check_existence(ftype, pOpts, pOD);

    switch (ftype & FTYPE_MODE_OPEN_MASK) {
    default:
    case FTYPE_MODE_NO_OPEN:  break;
    case FTYPE_MODE_OPEN_FD:  open_file_fd( pOpts, pOD, mode); break;
    case FTYPE_MODE_FOPEN_FP: fopen_file_fp(pOpts, pOD, mode); break;
    }
}
Esempio n. 2
0
int main(int argc, char **argv) {
    int serfd;

    if (argc != 4) {
        fprintf(stderr,
                "Usage: %s <device> <rate> <file> - act as a serial forwarder on <port>\n"
                "(listens to serial port <device> at baud rate <rate>)\n",
                argv[0]);
        exit(2);
    }

    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
        fprintf(stderr, "Warning: failed to ignore SIGPIPE.\n");
    if (signal(SIGINT, sigproc) == SIG_ERR)
        fprintf(stderr, "Warning: failed to set the SIGINT interruption.\n");
    srcId = -1;

    open_serial(argv[1], platform_baud_rate(argv[2]));

    serfd = serial_source_fd(src);

    open_file_fd(argv[3]);

    dup2(filefd, 1);

    for (;;) {
        fd_set rfds;
        int maxfd = -1;
        struct timeval zero;
        int serial_empty;
        int ret;

        zero.tv_sec = zero.tv_usec = 0;

        FD_ZERO(&rfds);
        fd_wait(&rfds, &maxfd, serfd);

        serial_empty = serial_source_empty(src);
        check_serial();

        if (ret >= 0) {
            if (FD_ISSET(serfd, &rfds))
                check_serial();
        }
    }
}