Ejemplo n.º 1
0
static int remove_temporary_streams(endpoint_t *endpoint) {
    endpoint_stream_t *current = endpoint->streams;
    endpoint_stream_t *temporary = NULL;
    endpoint_stream_t *prev = NULL;

    int ctr = -1;

    if (current == NULL) {
        ERR("endpoint has no streams.\n");

        return ctr;
    }

    do {
        // switch temporary and old streams
        current->temporary = !current->temporary;

        if (current->temporary == 1) {
            temporary = current;

            /*
             * when we are about to remove head of streams
             * we need to make sure that we link streams back to endpoint
             */
            if (endpoint->streams == current) {
                lock_endpoint(endpoint);
                endpoint->streams = current->next;
                unlock_endpoint(endpoint);
            }

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

            if (prev != NULL) {
                lock_stream(prev);
                prev->next = current;
                unlock_stream(prev);
            }

            // free temporary stream
            destroy_stream(temporary);
            ctr++;
        }

        if (current != NULL) {
            prev = current;
            current = current->next;
        }

    } while (current != NULL);

    return ctr;
}
Ejemplo n.º 2
0
int
fgetc(FILE *stream)
{
	unsigned char ch;

	lock_stream(stream);
	if (stream->unget_pos) {
        // printf("unget pos: %d\n", stream->unget_pos);
		ch = stream->unget_stack[--stream->unget_pos];
        // printf("unget pos: %d, returning '%c'\n", stream->unget_pos, ch);
        // L4_KDB_Enter("foo");
		unlock_stream(stream);
		return (int) ch;
	}

    if (stream == stdin) {
        /* This is where we should do input buffering */
        if (stream->read_fn(&ch, stream->current_pos, 1, stream->handle) == 1) {
            /* Success */
            stream->current_pos++;
            unlock_stream(stream);
            /* Translate CR -> LF for our programs to behave */
            if (ch == '\r') ch = '\n';
            return (int) ch;
        } else {
            stream->eof = 1;
            unlock_stream(stream);
            return EOF;
        }
    } else {
        /* Do the input buffering up to 1024 characters */
        int cnt = __UNGET_SIZE - stream->unget_pos;
        char tmp[__UNGET_SIZE];
        cnt = stream->read_fn(tmp, stream->current_pos, cnt, stream->handle);
        if (cnt > 0) {
            int i;
            // copy in reverse to the stack
            // printf("[fgetc: read %d chars in cache, first %c, last %c]\n", cnt, tmp[0], tmp[cnt - 1]);
            for (i = 0; i < cnt; i++) {
                stream->unget_stack[i] = tmp[cnt - i - 1];
            }
            stream->unget_pos = cnt;
            stream->current_pos += cnt;
            // printf("unget pos: %d, current pos: %d\n", stream->unget_pos, stream->current_pos);
            unlock_stream(stream);
            return fgetc(stream);
        } else {
            stream->eof = 1;
            unlock_stream(stream);
            return EOF;
        }
    }
}
Ejemplo n.º 3
0
char *
fgets(char *s, int n, FILE *stream)
{
	int i;
	int c = EOF;
	
	lock_stream(stream);
	for (i = 0; i < n-1; i++) {
		c = fgetc(stream);
		if (c == EOF) {
			break;
		}
		s[i] = (char) c;
		if (c == '\n') {
			break;
		}
	}
	unlock_stream(stream);

	s[i+1] = '\0';
	if (c == EOF && i == 0) {
		return NULL;
	} else {
		return s;
	}
}
Ejemplo n.º 4
0
int setvbuf(FILE *stream, char *buf, int mode, size_t size)
{
    int retval = 0;

    lock_stream(stream);

    // XXX: HACK: if we have an existing buffer, flush it and throw it away -AB
    // needed as long as we pre-allocate a static buffer in libbarrelfish init
    if(stream->buffer != NULL) {
        fflush(stream);
        if (stream->buf_allocated) {
            free(stream->buffer);
            stream->buf_allocated = 0;
        }
        stream->buffer = NULL;
    }

    if(stream->buffer == NULL) {
        stream->buffer = buf;
        stream->buf_size = size;
        stream->buffering_mode = mode;
    } else {
        // Can only be called when buffer is not allocated yet
        retval = -1;
    }

    unlock_stream(stream);
    return retval;
}
Ejemplo n.º 5
0
int
setvbuf(FILE * stream, char * buf, int mode, size_t size)
{
    /* Ensure mode is sane */
    if (mode != _IONBF && mode != _IOLBF && mode != _IOFBF) {
        return 1;
    }

    lock_stream(stream);

    /* Set mode */
    stream->buffering_mode = mode;

    /* Allocate a buffer of the given size if buf is NULL and
       mode requires a buffer */
    if (buf == NULL && mode != _IONBF) {
        buf = malloc(sizeof(char) * size);
    }

    /* Set buffer */
    stream->buffer = buf;
    stream->buffer_end = buf;
    stream->buffer_size = size;

    unlock_stream(stream);

    return 0;
}
Ejemplo n.º 6
0
size_t
fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
    if(size == 0 || nmemb == 0) {
        return 0;
    }

    lock_stream(stream);

    if (stream->unget_pos) {
        USER_PANIC("handling unget not implemented");
    }

    size_t actual_size = size * nmemb; // Actual amount requested to be read
    unsigned char *p = ptr;

    size_t ret = stream->read_fn(p, stream->current_pos, actual_size,
                                 stream->handle);
    stream->current_pos += ret;
    if (ret != actual_size) {
        stream->eof = 1;
    }

    unlock_stream(stream);
    return ret / size;
}
Ejemplo n.º 7
0
void
clearerr(FILE *stream)
{
	lock_stream(stream);
	stream->error = 0;
	stream->eof = 0;
	unlock_stream(stream);
}
Ejemplo n.º 8
0
Archivo: feof.c Proyecto: ahixon/papaya
int
feof(FILE *f)
{
	int res;
	lock_stream(f);
	res = f->eof;
	unlock_stream(f);
	return res;
}
Ejemplo n.º 9
0
long int
ftell(FILE *stream)
{
	int res;
	lock_stream(stream);
	res = stream->current_pos;
	unlock_stream(stream);
	return res;
}
Ejemplo n.º 10
0
int
ferror(FILE *stream)
{
	int res;
	lock_stream(stream);
	res = stream->error;
	unlock_stream(stream);
	return res;
}
Ejemplo n.º 11
0
Archivo: puts.c Proyecto: BruceYi/okl4
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;
}
Ejemplo n.º 12
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;
}
Ejemplo n.º 13
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;
}
Ejemplo n.º 14
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;
}
Ejemplo n.º 15
0
Archivo: fread.c Proyecto: hro424/arcos
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;
}
Ejemplo n.º 16
0
Archivo: fseek.c Proyecto: BruceYi/okl4
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;
}
Ejemplo n.º 17
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;
    }

}
Ejemplo n.º 18
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;
}