Пример #1
0
int
puts(const char *s)
{

/* If in POSIX environment, this is a cancellation point */
#ifdef __USE_POSIX
    pthread_testcancel();
#endif

    lock_stream(stdout);
    while (*s != '\0')
        (void)fputc(*s++, stdout);
    (void)fputc('\n', stdout);
    unlock_stream(stdout);
    return 0;
}
Пример #2
0
static void fini_stream(MyStream *v, bool abort)
{
  if (v->as) {
    if (abort)
      Pa_AbortStream(v->as);
    else
      Pa_StopStream(v->as);
    pthread_cleanup_push(unlock_stream, (void*)v);
    lock_stream(v);
    Pa_CloseStream(v->as);
    v->as = NULL;
    /* wake up threads waiting to read or write the stream */
    if (has_input(v)) pthread_cond_broadcast(&v->in_cond);
    if (has_output(v)) pthread_cond_broadcast(&v->out_cond);
    pthread_cleanup_pop(1);
  }
}
Пример #3
0
size_t
fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
    size_t elems, sz;
    const unsigned char *p = ptr;

    lock_stream(stream);
    for (elems = 0; elems < nmemb; elems++) {
        for (sz = 0; sz < size; sz++, p++) {
            if (fputc(*p, stream) == EOF) {
                goto out;
            }
        }
    }
out:
    unlock_stream(stream);
    return elems;
}
Пример #4
0
int
fclose(FILE *stream)
{
    int r;

    fflush(stream);

    lock_stream(stream);
    if(stream->buf_allocated && stream->buffer != NULL) {
        free(stream->buffer);
    }
    r = stream->close_fn(stream->handle);
    unlock_stream(stream);

    /* FIXME: aren't we leaking the FILE struct here? -AB */

    return r;
}
Пример #5
0
int
fclose(FILE *stream)
{

/* If in POSIX environment, this is a cancellation point */
#ifdef __USE_POSIX
    pthread_testcancel();
#endif

    int r;
    fflush(stream);
    lock_stream(stream);
    r = stream->close_fn(stream->handle);
    unlock_stream(stream);
#ifdef THREAD_SAFE
    okl4_mutex_free(&stream->mutex);
#endif
    return r;
}
Пример #6
0
size_t
fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
    size_t elems, sz;
    unsigned char *p = ptr;

    lock_stream(stream);
    for (elems = 0; elems < nmemb; elems++) {
        for (sz = 0; sz < size; sz++, p++) {
            int ch;

            if ((ch = fgetc(stream)) == EOF) {
                goto out;
            }
            *p = (unsigned char)ch;
        }
    }
out:
    unlock_stream(stream);
    return elems;
}
Пример #7
0
int
fseek(FILE *stream, long int offset, int whence)
{

/* If in POSIX environment, this is a cancellation point */
#ifdef __USE_POSIX
    pthread_testcancel();
#endif

    int res = 0;

    lock_stream(stream);
    switch (whence) {
    case SEEK_SET:
        stream->current_pos = offset;
#ifdef __USE_POSIX
        lseek((int)(stream->handle), offset, SEEK_SET);
#endif
        break;
    case SEEK_CUR:
        stream->current_pos += offset;
#ifdef __USE_POSIX
        lseek((int)(stream->handle), offset, SEEK_CUR);
#endif
        break;
    case SEEK_END:
        stream->current_pos = stream->eof_fn(stream->handle) + offset;
#ifdef __USE_POSIX
        lseek((int)(stream->handle), offset, SEEK_END);
#endif
        break;
    default:
        res = -1;
    }
    unlock_stream(stream);
    return res;
}
Пример #8
0
static int add_temporary_streams(endpoint_t *endpoint, char *port) {
    endpoint_stream_t *current;
    current = endpoint->streams;

    endpoint_stream_t *temporary = NULL;
    endpoint_stream_t *next = NULL;

    while (current != NULL) {
        next = current->next;

        temporary = (endpoint_stream_t *) shm_malloc(sizeof(endpoint_stream_t));

        if (temporary == NULL) {
            ERR("cannot allocate shm memory\n");
            return -1;
        }

        temporary->media = NULL;
        temporary->port = NULL;
        temporary->rtcp_port = NULL;
        temporary->next = NULL;

        temporary->lock = lock_alloc();

        if (temporary->lock == NULL) {
            ERR("cannot allocate the lock for stream\n");
            destroy_stream(temporary);
            return -1;
        }

        if (lock_init(temporary->lock) == NULL) {
            ERR("lock initialization failed\n");
            destroy_stream(temporary);
            return -1;
        }

        temporary->temporary = 1;

        if (shm_copy_string(current->media, (int) strlen(current->media), &(temporary->media)) == -1) {
            ERR("cannot copy string.\n");
            destroy_stream(temporary);

            return -1;
        }

        if (shm_copy_string(port, (int) strlen(port), &(temporary->port)) == -1) {
            ERR("cannot copy string.\n");
            destroy_stream(temporary);

            return -1;
        }

        if (shm_copy_string(port, (int) strlen(port), &(temporary->rtcp_port)) == -1) {
            ERR("cannot copy string.\n");
            destroy_stream(temporary);

            return -1;
        }

        lock_stream(current);
        current->next = temporary;
        unlock_stream(current);

        lock_stream(temporary);
        temporary->next = next;
        unlock_stream(temporary);

        current = next;
        temporary = NULL;
    }

    return 0;
}
Пример #9
0
/*
 * parse scanf format string 
 */
int
__scanf(const char *input, FILE *stream, bool stream_or_memory,
                                       const char *fmt, va_list ap)
{
    unsigned int assign_count = 0;
    va_list ap_copy, ap_tmp;
    int width, base = 0;
    char *p, *s;
    long long num_res = 0;
    unsigned long long num_ures = 0;
    bool num_unsigned = false;
    bool suppress = false;
    bool use_width = false;
    bool using_nth = false;
    /* length modifiers */
    bool lm_h, lm_hh, lm_l, lm_ll, lm_j, lm_z, lm_t, lm_L;
    int arg_nth = 0;
    int i = 0;
    bool list_not;
    char *list_start, *list_end;
    unsigned char *user_us;
    unsigned long *user_ul;
    unsigned long long *user_ull;
    unsigned short *user_ush;
    unsigned int *user_ui;
    uintmax_t *user_uj;
    char *user_s = NULL;
    long *user_l;
    long long *user_ll;
    short *user_sh;
    int *user_i;
    intmax_t *user_j;
    size_t *user_z;
    ptrdiff_t *user_t;
    void **user_pp;
#ifndef CONFIG_WITHOUT_FLOATING
#ifndef CONFIG_WITHOUT_LONG_DOUBLE
    long double *user_ld;
#endif /* CONFIG_WITHOUT_LONG_DOUBLE */
    double *user_d;
    float *user_f;
#endif /* CONFIG_WITHOUT_FLOATING */
    struct st_mem_stream mem_stream;
    mem_stream.stream_or_memory = stream_or_memory;
    mem_stream.mem = (char**)&input;
    mem_stream.stream = stream;
    mem_stream.last_stream_char = '\0';
    mem_stream.read_bytes = 0;

    __va_copy(ap_copy, ap);

    if (stream != NULL)
        lock_stream(stream);

    for (p = (char*)fmt; *p != '\0'; p++) {
        use_width = false;
        width = 1;
        num_unsigned = false;
        suppress = false;
        num_res = 0;
        num_ures = 0;
        lm_h = false;
        lm_hh = false;
        lm_l = false;
        lm_ll = false;
        lm_j = false;
        lm_z = false;
        lm_t = false;
        lm_L = false;
        if (*p != '%') {
char_comp:
            s = __get_next_char(&mem_stream);
            if (__is_eof(s)) {
                goto eof_failure;
            }
            if (isspace(*p)) {
                while (isspace(*s)) {
                    __inc_next_char(&mem_stream);
                    s = __get_next_char(&mem_stream);
                    if (__is_eof(s)) {
                        goto eof_failure;
                    }
                }
            } else {
                if (*p == *s) {
                    __inc_next_char(&mem_stream);
                } else {
                    goto matching_failure;
                }
            }
            continue;
        }
        /* *p == '%' */
again_inc:
        p++;
again:
        switch(*p) {
        case '%':
            goto char_comp;
            break;
        case '*':
            suppress = true;
            goto again_inc;
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            i = *p - '0';
            for (p++; p && (unsigned int)(*p - '0') < 10; p++) {
                i = i * 10 + (*p - '0');
            }
            if (*p == '$') {
                using_nth = true;
                if (i == 0) {
                    goto matching_failure;
                } else {
                    arg_nth = i;
                }
                p++;
            } else {
                width = i;
                if (width != 0) {
                    use_width = true;
                }
            }
            goto again;
        /* Length modifiers */
        case 'h':
            if (lm_h) {
                lm_hh = true;
                lm_h = false;
            } else {
                lm_h = true;
            }
            goto again_inc;
        case 'l':
            if (lm_l) {
                lm_ll = true;
                lm_l = false;
            } else {
                lm_l = true;
            }
            goto again_inc;
        case 'j':
            lm_j = true;
            goto again_inc;
        case 'z':
            lm_z = true;
            goto again_inc;
        case 't':
            lm_t = true;
            goto again_inc;
#ifndef CONFIG_WITHOUT_LONG_DOUBLE
        case 'L':
            lm_L = true;
            goto again_inc;
#endif /* CONFIG_WITHOUT_LONG_DOUBLE */
        /* Numbers */
        case 'd':
            base = 10;
            goto number;
        case 'i':
            base = 0;
            goto number;
        case 'o':
            base = 8;
            num_unsigned = true;
            goto number;
        case 'u':
            base = 10;
            num_unsigned = true;
            goto number;
        case 'x':
        case 'X':
            base = 16;
            num_unsigned = true;
number:
            eat_spaces(&mem_stream);
            if (!use_width) {
                width = 0;
            }
    
            errno = 0; /* Check for strto* errors */
            if (suppress) {
                (void)__strtoll(NULL, base, width, get_next_char, inc_next_char,
                                                    (void*)&mem_stream);
            } else {
                if (num_unsigned) {
                    uintmax_t num_tmp;

                    errno = 0;
                    num_tmp = __strtoull(NULL, base, width, get_next_char,
                                               inc_next_char, (void*)&mem_stream);

                    if(errno == EINVAL) {
                        goto matching_failure;
                    }

                    assign_count++;

                    if (lm_ll) {
                        GET_ARG(user_ull, unsigned long long*);
                        *user_ull = (unsigned long long)num_tmp;
                    } else if (lm_l) {
                        GET_ARG(user_ul, unsigned long*);
                        *user_ul = (unsigned long)num_tmp;
                    } else if (lm_hh) {
                        GET_ARG(user_us, unsigned char*);
                        *user_us = (unsigned char)num_tmp;
                    } else if (lm_h) {
                        GET_ARG(user_ush, unsigned short*);
                        *user_ush = (unsigned short)num_tmp;
                    } else if (lm_j) {
                        GET_ARG(user_uj, uintmax_t*);
                        *user_uj = (uintmax_t)num_tmp;
                    } else if (lm_z) {
                        GET_ARG(user_z, size_t*);
                        *user_z = (size_t)num_tmp;
                    } else if (lm_t) {
                        GET_ARG(user_t, ptrdiff_t*);
                        *user_t = (unsigned long)num_tmp;
                    } else {
                        GET_ARG(user_ui, unsigned int*);
                        *user_ui = (unsigned int)num_tmp;
                    }
                } else {
                    intmax_t num_tmp;

                    errno = 0;
                    num_tmp = __strtoll(NULL, base, width, get_next_char,
                                               inc_next_char, (void*)&mem_stream);

                    if(errno == EINVAL) {
                        goto matching_failure;
                    }

                    assign_count++;

                    if (lm_ll) {
                        GET_ARG(user_ll, long long*);
                        *user_ll = (long long)num_tmp;
                    } else if (lm_l) {
                        GET_ARG(user_l, long*);
                        *user_l = (long int)num_tmp;
                    } else if (lm_hh) {
                        GET_ARG(user_s, char*);
                        *user_s = (char)num_tmp;
                    } else if (lm_h) {
                        GET_ARG(user_sh, short*);
                        *user_sh = (short)num_tmp;
                    } else if (lm_j) {
                        GET_ARG(user_j, intmax_t*);
                        *user_j = (intmax_t)num_tmp;
                    } else if (lm_z) {
                        GET_ARG(user_z, size_t*);
                        *user_z = (signed long)num_tmp;
                    } else if (lm_t) {
                        GET_ARG(user_t, ptrdiff_t*);
                        *user_t = (ptrdiff_t)num_tmp;
                    } else {
                        GET_ARG(user_i, int*);
                        *user_i = (int)num_tmp;
                    }
                }
            }
            /*
            if (errno == EINVAL) {
                goto matching_failure;
            }
            */
            break;
#ifndef CONFIG_WITHOUT_FLOATING
        case 'A':
        case 'a':
        case 'E':
        case 'e':
        case 'F':
        case 'f':
        case 'G':
        case 'g':
            eat_spaces(&mem_stream);
            if (!use_width) {
                width = 0;
            }

            s = __get_next_char(&mem_stream);
            if (__is_eof(s)) {
                goto eof_failure;
            }
            if (*s == 'i' || *s == 'I') {
                if (!eat_infinity(&mem_stream)) {
                    s = __get_next_char(&mem_stream);
                    if (*s == '\0') {
                        goto input_failure;
                    } else {
                        goto matching_failure;
                    }
                }
                /*
                if (!suppress) {
                    assign_count++;
                    if (lm_l) {
                        GET_ARG(user_d, double*);
                        *user_d = INFINITY;
#ifndef CONFIG_WITHOUT_LONG_DOUBLE
                    } else if (lm_L) {
                        GET_ARG(user_ld, long double*);
                        *user_ld = INFINITY;
#endif
                    } else {
                        GET_ARG(user_f, float*);
                        *user_f = INFINITY;
                    }
                }
                */
            } else if (*s == 'n' || *s == 'N') {
                if (!eat_nan(&mem_stream)) {
                    goto matching_failure;
                }
                /*
                if (!suppress) {
                    assign_count++;
                    if (lm_l) {
                        GET_ARG(user_d, double*);
                        *user_d = NAN;
#ifndef CONFIG_WITHOUT_LONG_DOUBLE
                    } else if (lm_L) {
                        GET_ARG(user_ld, long double*);
                        *user_ld = NAN;
#endif
                    } else {
                        GET_ARG(user_f, float*);
                        *user_f = NAN;
                    }
                }
                */
            } else {
                if (suppress) {
                    (void)__strtold(NULL, width, get_next_char, inc_next_char,
                                                    (void*)&mem_stream);
                } else {
                    assign_count++;
                    if (lm_l) {
                        GET_ARG(user_d, double*);
                        *user_d = (double)__strtold(NULL, width, get_next_char,
                                        inc_next_char, (void*)&mem_stream);
#ifndef CONFIG_WITHOUT_LONG_DOUBLE
                    } else if (lm_L) {
                        GET_ARG(user_ld, long double*);
                        *user_ld = (long double)__strtold(NULL, width,
                                                          get_next_char,
                                                          inc_next_char,
                                                          (void*)&mem_stream);
#endif /* CONFIG_WITHOUT_LONG_DOUBLE */
                    } else {
                        GET_ARG(user_f, float*);
                        *user_f = (float)__strtold(NULL, width, get_next_char,
                                        inc_next_char, (void*)&mem_stream);
                    }
                }
            }
            break;
#endif /* CONFIG_WITHOUT_FLOATING */
        case 'S':
            lm_l = true;
        case 's':
            eat_spaces(&mem_stream);
            if (!suppress) {
                GET_ARG(user_s, char*);
                i = 0;
            }
            s = __get_next_char(&mem_stream);
            if (__is_eof(s)) {
                goto eof_failure;
            }
            while (s != NULL && *s != '\0' && !isspace(*s)) {
                if (use_width) {
                    if (width > 0) {
                        width--;
                    } else {
                        break;
                    }
                }
                if (!suppress) {
                    user_s[i] = *s;
                    i++;
                }
                __inc_next_char(&mem_stream);
                s = __get_next_char(&mem_stream);
            }
            if (!suppress) {
                assign_count++;
                user_s[i] = '\0';
            }
            break;
        case '[':
            list_not = false;
            p++;
            if (*p == '^') {
                list_not = true;
                p++;
            }
            if (*p == '\0') {
                goto matching_failure;
            }
            list_start = p;
            p++;
            while (*p != ']') {
                if (*p == '\0') {
                    goto matching_failure;
                }
                p++;
            }
            list_end = p;

            if (!suppress) {
                GET_ARG(user_s, char*);
                i = 0;
            }
            s = __get_next_char(&mem_stream);
            if (__is_eof(s)) {
                goto eof_failure;
            }
            while (s != NULL && *s != '\0' &&
                        check_list(*s, list_start, list_end, list_not)) {
                if (use_width) {
                    if (width > 0) {
                        width--;
                    } else {
                        break;
                    }
                }
                if (!suppress) {
                    user_s[i] = *s;
                    i++;
                }
                __inc_next_char(&mem_stream);
                s = __get_next_char(&mem_stream);
            }
            if (!suppress) {
                assign_count++;
                user_s[i] = '\0';
            }
            break;
        case 'C':
            lm_l = true;
        case 'c':
            use_width = true;
            if (!suppress) {
                GET_ARG(user_s, char*);
                i = 0;
            }
            s = __get_next_char(&mem_stream);
            if (__is_eof(s)) {
                goto eof_failure;
            }
            for (; width > 0 && s != NULL && *s != '\0'; width--) {
                if (!suppress) {
                    user_s[i] = *s;
                    i++;
                }
                __inc_next_char(&mem_stream);
                s = __get_next_char(&mem_stream);
            }
            if (__is_eof(s) && width > 0) {
                goto eof_failure;
            }
            if (!suppress) {
                assign_count++;
                user_s[i] = '\0';
            }
            break;
        case 'p':
            eat_spaces(&mem_stream);
            if (!use_width) {
                width = 0;
            } else if (width < 2) {
                goto matching_failure;
            }
            s = __get_next_char(&mem_stream);
            if (__is_eof(s)) {
                goto eof_failure;
            }
            if (*s != '0') {
                goto matching_failure;
            }
            __inc_next_char(&mem_stream);
            s = __get_next_char(&mem_stream);
            if (__is_eof(s)) {
                goto eof_failure;
            }
            if (*s != 'x' && *s != 'X') {
                goto matching_failure;
            }
            __inc_next_char(&mem_stream);
            width -= 2;
            if (suppress) {
                (void)__strtoll(NULL, 16, width, get_next_char, inc_next_char,
                                                    (void*)&mem_stream);
            } else {
                assign_count++;
                GET_ARG(user_pp, void**);
                *user_pp = (void*)(long)__strtoll(NULL, 16, width, get_next_char,
                                    inc_next_char, (void*)&mem_stream);
            }
            break;
        case 'n':
            if (lm_ll) {
                GET_ARG(user_ll, long long*);
                *user_ll = (long long)mem_stream.read_bytes;
            } else if (lm_l) {
                GET_ARG(user_l, long*);
                *user_l = (long)mem_stream.read_bytes;
            } else if (lm_hh) {
                GET_ARG(user_s, char*);
                *user_s = (char)mem_stream.read_bytes;
            } else if (lm_h) {
                GET_ARG(user_sh, short*);
                *user_sh = (short)mem_stream.read_bytes;
            } else if (lm_j) {
                GET_ARG(user_j, intmax_t*);
                *user_j = (intmax_t)mem_stream.read_bytes;
            } else if (lm_z) {
                GET_ARG(user_i, int*);
                *user_i = (int)mem_stream.read_bytes;
            } else if (lm_t) {
                GET_ARG(user_t, ptrdiff_t*);
                *user_t = (ptrdiff_t)mem_stream.read_bytes;
            } else {
                GET_ARG(user_i, int*);
                *user_i = (int)mem_stream.read_bytes;
            }
            break;
        default:
            if (*p == '\0') {
                break;
            }
        }
    }

matching_failure:
    if (stream != NULL) {
        unlock_stream(stream);
    }

    return assign_count;
    /* XXX: va_end */

input_failure:
    if (stream != NULL) {
        unlock_stream(stream);
    }
    return EOF;

eof_failure:
    if (assign_count > 0) {
        goto matching_failure;
    } else {
        goto input_failure;
    }

}