Subversion Repositories HelenOS-historic

Rev

Rev 1288 | Rev 1605 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1288 Rev 1604
Line 48... Line 48...
48
 * @param data structure with destination string, counter of used space and total string size.
48
 * @param data structure with destination string, counter of used space and total string size.
49
 * @return number of characters to print (not characters really printed!)
49
 * @return number of characters to print (not characters really printed!)
50
 */
50
 */
51
int vsnprintf_write(const char *str, size_t count, struct vsnprintf_data *data)
51
int vsnprintf_write(const char *str, size_t count, struct vsnprintf_data *data)
52
{
52
{
53
    size_t i,j;
53
    size_t i;
54
    i = data->size - data->len;
54
    i = data->size - data->len;
55
    j = count;
-
 
56
 
55
 
57
    if (i > 0) {
-
 
58
        if (i <= j) {
-
 
59
            if (i == 1) {
56
    if ((count == 0) || (i == 0)) {
60
                /* We have only one free byte left in buffer => write there trailing zero */
-
 
61
                data->string[data->size - 1] = 0;
-
 
62
                data->len = data->size;
-
 
63
            } else {
-
 
64
                /* We have not enought space for whole string with the trailing zero => print only a part of string */
-
 
65
                memcpy((void *)(data->string + data->len), (void *)str, i - 1);
-
 
66
                data->string[data->size - 1] = 0;
-
 
67
                data->len = data->size;
-
 
68
            }
-
 
69
        } else {
57
        return 0;
70
            /* Buffer is big enought to print whole string */
-
 
71
            memcpy((void *)(data->string + data->len), (void *)str, j);
-
 
72
            data->len += j;
-
 
73
            /* Put trailing zero at end, but not count it into data->len so it could be rewritten next time */
-
 
74
            data->string[data->len] = 0;
-
 
75
        }
-
 
76
    }
58
    }
77
   
59
   
-
 
60
    if (i == 1) {
-
 
61
        /* We have only one free byte left in buffer => write there trailing zero */
-
 
62
        data->string[data->size - 1] = 0;
-
 
63
        data->len = data->size;
-
 
64
        return 1;
-
 
65
    }
-
 
66
   
-
 
67
    if (i <= count) {
-
 
68
        /* We have not enought space for whole string with the trailing zero => print only a part of string */
-
 
69
            memcpy((void *)(data->string + data->len), (void *)str, i - 1);
-
 
70
            data->string[data->size - 1] = 0;
-
 
71
            data->len = data->size;
-
 
72
            return i;
-
 
73
    }
-
 
74
   
-
 
75
    /* Buffer is big enought to print whole string */
-
 
76
    memcpy((void *)(data->string + data->len), (void *)str, count);
-
 
77
    data->len += count;
-
 
78
    /* Put trailing zero at end, but not count it into data->len so it could be rewritten next time */
-
 
79
    data->string[data->len] = 0;
-
 
80
 
78
    return count;  
81
    return count;  
79
}
82
}
80
 
83
 
81
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
84
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
82
{
85
{