Changeset 9c1984f in mainline


Ignore:
Timestamp:
2011-07-08T04:03:40Z (13 years ago)
Author:
Petr Koupy <petr.koupy@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ddc63fd
Parents:
ec18957a
Message:

Bugfixes and tests for scanf.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/stdio/scanf.c

    rec18957a r9c1984f  
    206206                 * and flush the window. Regarding the scanf, this could happend only
    207207                 * when matching unbounded string (%s) or unbounded scanset (%[) not
    208                  * containing newline, and at the same time newline is the character
     208                 * containing newline, while at the same time newline is the character
    209209                 * that breaks the matching process. */
    210210                int rc = posix_fseek(
     
    349349        assert(modifier);
    350350
    351         /* Check whether the modifier was not already recognized. */
    352         if (*modifier != LMOD_NONE) {
    353                 /* Format string is invalid. Notify the caller. */
    354                 *modifier = LMOD_NONE;
    355                 return 1;
    356         }
    357 
    358351        switch (c) {
    359352        case 'h':
    360                 *modifier = _c == 'h' ? LMOD_hh : LMOD_h;
     353                /* Check whether the modifier was not already recognized. */
     354                if (*modifier == LMOD_NONE) {
     355                        *modifier = _c == 'h' ? LMOD_hh : LMOD_h;
     356                } else {
     357                        /* Format string is invalid. Notify the caller. */
     358                        *modifier = LMOD_NONE;
     359                }
    361360                return 1;
    362361        case 'l':
    363                 *modifier = _c == 'l' ? LMOD_ll : LMOD_l;
     362                if (*modifier == LMOD_NONE) {
     363                        *modifier = _c == 'l' ? LMOD_ll : LMOD_l;
     364                } else {
     365                        *modifier = LMOD_NONE;
     366                }
    364367                return 1;
    365368        case 'j':
    366                 *modifier = LMOD_j;
     369                *modifier = *modifier == LMOD_NONE ? LMOD_j : LMOD_NONE;
    367370                return 1;
    368371        case 'z':
    369                 *modifier = LMOD_z;
     372                *modifier = *modifier == LMOD_NONE ? LMOD_z : LMOD_NONE;
    370373                return 1;
    371374        case 't':
    372                 *modifier = LMOD_t;
     375                *modifier = *modifier == LMOD_NONE ? LMOD_t : LMOD_NONE;
    373376                return 1;
    374377        case 'L':
    375                 *modifier = LMOD_L;
     378                *modifier = *modifier == LMOD_NONE ? LMOD_L : LMOD_NONE;
    376379                return 1;
    377380        default:
     
    558561                                }
    559562                                char *fmt_new = NULL;
    560                                 width = posix_strtol(fmt - 1, &fmt_new, 10);
     563                                width = posix_strtol(fmt, &fmt_new, 10);
    561564                                if (width != 0) {
    562565                                        fmt = fmt_new;
     
    618621                                        in->undo(in);
    619622                                }
    620                                
     623
    621624                                const char *cur_borrowed = NULL;
    622625                                const char *cur_limited = NULL;
     
    668671                                }
    669672
    670                                 /* If nto supressed, assign the converted integer into
     673                                /* If not supressed, assign the converted integer into
    671674                                 * the next output argument. */
    672675                                if (!assign_supress) {
     
    12161219}
    12171220
     1221// FIXME: put the testcases somewhere else
     1222
     1223#if 0
     1224
     1225//#include <stdio.h>
     1226//#include <malloc.h>
     1227//#include <string.h>
     1228
     1229#define test_val(fmt, exp_val, act_val) \
     1230        if (exp_val == act_val) { \
     1231                printf("succ, expected "fmt", actual "fmt"\n", exp_val, act_val); \
     1232        } else { \
     1233                printf("fail, expected "fmt", actual "fmt"\n", exp_val, act_val); \
     1234                ++fail; \
     1235        }
     1236
     1237#define test_str(fmt, exp_str, act_str) \
     1238        if (posix_strcmp(exp_str, act_str) == 0) { \
     1239                printf("succ, expected "fmt", actual "fmt"\n", exp_str, act_str); \
     1240        } else { \
     1241                printf("fail, expected "fmt", actual "fmt"\n", exp_str, act_str); \
     1242                ++fail; \
     1243        }
     1244
     1245void __posix_scanf_test(void);
     1246void __posix_scanf_test(void)
     1247{
     1248        int fail = 0;
     1249
     1250        int ret;
     1251
     1252        unsigned char uhh;
     1253        signed char shh;
     1254        unsigned short uh;
     1255        short sh;
     1256        unsigned udef;
     1257        int sdef;
     1258        unsigned long ul;
     1259        long sl;
     1260        unsigned long long ull;
     1261        long long sll;
     1262        void *p;
     1263       
     1264        float f;
     1265        double d;
     1266        long double ld;
     1267
     1268        char str[20];
     1269        char seq[20];
     1270        char scanset[20];
     1271
     1272        char *pstr;
     1273        char *pseq;
     1274        char *pscanset;
     1275       
     1276        ret = posix_sscanf(
     1277            "\n j tt % \t -121314 98765 aqw 0765 0x77 0xABCDEF88 -99 884",
     1278            " j tt %%%3hhd%1hhu%3hd %3hu%u aqw%n %lo%llx %p %li %lld",
     1279            &shh, &uhh, &sh, &uh, &udef, &sdef, &ul, &ull, &p, &sl, &sll);
     1280        test_val("%d", -12, shh);
     1281        test_val("%u", 1, uhh);
     1282        test_val("%d", 314, sh);
     1283        test_val("%u", 987, uh);
     1284        test_val("%u", 65, udef);
     1285        test_val("%d", 28, sdef);
     1286        test_val("%lo", (unsigned long) 0765, ul);
     1287        test_val("%llx", (unsigned long long) 0x77, ull);
     1288        test_val("%p", (void *) 0xABCDEF88, p);
     1289        test_val("%ld", (long) -99, sl);
     1290        test_val("%lld", (long long) 884, sll);
     1291        test_val("%d", 10, ret);
     1292
     1293        ret = posix_sscanf(
     1294            "\n \t\t1.0 -0x555.AP10 1234.5678e12",
     1295            "%f %lf %Lf",
     1296            &f, &d, &ld);
     1297        test_val("%f", 1.0, f);
     1298        test_val("%lf", (double) -0x555.AP10, d);
     1299        test_val("%Lf", (long double) 1234.5678e12, ld);
     1300        test_val("%d", 3, ret);
     1301         
     1302        ret = posix_sscanf(
     1303            "\n\n\thello world    \n",
     1304            "%5s %ms",
     1305            str, &pstr);
     1306        test_str("%s", "hello", str);
     1307        test_str("%s", "world", pstr);
     1308        test_val("%d", 2, ret);
     1309        free(pstr);
     1310
     1311        ret = posix_sscanf(
     1312            "\n\n\thello world    \n",
     1313            " %5c %mc",
     1314            seq, &pseq);
     1315        seq[5] = '\0';
     1316        pseq[1] = '\0';
     1317        test_str("%s", "hello", seq);
     1318        test_str("%s", "w", pseq);
     1319        test_val("%d", 2, ret);
     1320        free(pseq);
     1321
     1322        ret = posix_sscanf(
     1323            "\n\n\th-e-l-l-o world-]    \n",
     1324            " %9[-eh-o] %m[^]-]",
     1325            scanset, &pscanset);
     1326        test_str("%s", "h-e-l-l-o", scanset);
     1327        test_str("%s", "world", pscanset);
     1328        test_val("%d", 2, ret);
     1329        free(pscanset);
     1330
     1331        printf("Failed: %d\n", fail);
     1332}
     1333
     1334#endif
     1335
    12181336/** @}
    12191337 */
Note: See TracChangeset for help on using the changeset viewer.