Changeset c111da2 in mainline for uspace/lib/c


Ignore:
Timestamp:
2025-10-09T15:44:52Z (2 months ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master
Children:
cfd04c4
Parents:
1a96db9
Message:

Create non-zero size file in Navigator, new newfile utility.

Location:
uspace/lib/c
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/capa.c

    r1a96db9 rc111da2  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2025 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    233233}
    234234
     235errno_t capa_format_buf(capa_spec_t *capa, char *buf, size_t bufsize)
     236{
     237        errno_t rc;
     238        const char *sunit;
     239        uint64_t ipart;
     240        uint64_t fpart;
     241        uint64_t div;
     242
     243        sunit = NULL;
     244
     245        assert(capa->cunit < CU_LIMIT);
     246
     247        rc = ipow10_u64(capa->dp, &div);
     248        if (rc != EOK)
     249                return rc;
     250
     251        ipart = capa->m / div;
     252        fpart = capa->m % div;
     253
     254        sunit = cu_str[capa->cunit];
     255        if (capa->dp > 0) {
     256                snprintf(buf, bufsize, "%" PRIu64 ".%0*" PRIu64 " %s", ipart,
     257                    (int)capa->dp, fpart, sunit);
     258        } else {
     259                snprintf(buf, bufsize, "%" PRIu64 " %s", ipart, sunit);
     260        }
     261
     262        return EOK;
     263}
     264
     265errno_t capa_blocks_format(uint64_t nblocks, size_t block_size,
     266    char **rptr)
     267{
     268        capa_spec_t capa;
     269
     270        capa_from_blocks(nblocks, block_size, &capa);
     271        capa_simplify(&capa);
     272        return capa_format(&capa, rptr);
     273}
     274
     275void capa_blocks_format_buf(uint64_t nblocks, size_t block_size,
     276    char *buf, size_t bsize)
     277{
     278        capa_spec_t capa;
     279        errno_t rc;
     280
     281        capa_from_blocks(nblocks, block_size, &capa);
     282        capa_simplify(&capa);
     283
     284        /* Should not get range error because of nblocks * block_size limits */
     285        rc = capa_format_buf(&capa, buf, bsize);
     286        assert(rc == EOK);
     287        (void)rc;
     288}
     289
    235290static errno_t capa_digit_val(char c, int *val)
    236291{
  • uspace/lib/c/include/capa.h

    r1a96db9 rc111da2  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2025 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    8787} capa_spec_t;
    8888
     89/** Size of buffer large enough for capa_blocks_format_buf */
     90#define CAPA_BLOCKS_BUFSIZE 16
     91
    8992extern errno_t capa_format(capa_spec_t *, char **);
     93extern errno_t capa_format_buf(capa_spec_t *, char *, size_t);
     94extern errno_t capa_blocks_format(uint64_t, size_t, char **);
     95extern void capa_blocks_format_buf(uint64_t, size_t, char *, size_t);
    9096extern errno_t capa_parse(const char *, capa_spec_t *);
    9197extern void capa_simplify(capa_spec_t *);
  • uspace/lib/c/test/capa.c

    r1a96db9 rc111da2  
    11/*
     2 * Copyright (c) 2025 Jiri Svoboda
    23 * Copyright (c) 2019 Matthieu Riolo
    34 * All rights reserved.
     
    285286}
    286287
     288PCUT_TEST(capa_blocks_format)
     289{
     290        errno_t rc;
     291        char *str;
     292
     293        rc = capa_blocks_format(42, 1, &str);
     294        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     295        PCUT_ASSERT_STR_EQUALS("42 B", str);
     296        free(str);
     297}
     298
     299PCUT_TEST(capa_blocks_format_buf)
     300{
     301        char str[CAPA_BLOCKS_BUFSIZE];
     302
     303        capa_blocks_format_buf(42, 1, str, sizeof(str));
     304        PCUT_ASSERT_STR_EQUALS("42 B", str);
     305}
     306
    287307PCUT_EXPORT(capa);
Note: See TracChangeset for help on using the changeset viewer.