File: _PDCLIB_init_file_t.c

package info (click to toggle)
libconvert-binary-c-perl 0.86-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,264 kB
  • sloc: ansic: 47,836; perl: 4,980; yacc: 2,143; makefile: 61
file content (79 lines) | stat: -rw-r--r-- 1,541 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* _PDCLIB_init_file_t( _PDCLIB_file_t * )

   This file is part of the Public Domain C Library (PDCLib).
   Permission is granted to use, modify, and / or redistribute at will.
*/

#ifndef REGTEST

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <threads.h>

struct _PDCLIB_file_t * _PDCLIB_init_file_t( struct _PDCLIB_file_t * stream )
{
    struct _PDCLIB_file_t * rc = stream;

    if ( rc == NULL )
    {
        if ( ( rc = (struct _PDCLIB_file_t *)malloc( sizeof( struct _PDCLIB_file_t ) ) ) == NULL )
        {
            /* No memory */
            return NULL;
        }
    }

    if ( ( rc->buffer = (char *)malloc( BUFSIZ ) ) == NULL )
    {
        /* No memory */
        free( rc );
        return NULL;
    }

    rc->bufsize = BUFSIZ;
    rc->bufidx = 0;
    rc->bufend = 0;
    rc->pos.offset = 0;
    rc->pos.status = 0;
    rc->ungetidx = 0;
    rc->status = _PDCLIB_FREEBUFFER;

#ifndef __STDC_NO_THREADS

    if ( stream == NULL )
    {
        /* If called by freopen() (stream not NULL), mutex is already
           initialized.
        */
        if ( mtx_init( &rc->mtx, mtx_plain | mtx_recursive ) != thrd_success )
        {
            /* could not initialize stream mutex */
            free( rc->buffer );
            free( rc );
            return NULL;
        }
    }

#endif

    /* TODO: Setting mbstate */

    return rc;
}

#endif

#ifdef TEST

#include "_PDCLIB_test.h"

int main( void )
{
#ifndef REGTEST
    TESTCASE( NO_TESTDRIVER );
#endif
    return TEST_RESULTS;
}

#endif