Changeset 6eb2e96 in mainline


Ignore:
Timestamp:
2009-04-10T07:53:54Z (15 years ago)
Author:
Jiri Svoboda <jirik.svoboda@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
3cc6a52
Parents:
f4b1535
Message:

str_cpy() and str_ncpy() in userspace. Nuke strcpy() and strncpy().

Location:
uspace
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/cmds/modules/ls/ls.c

    rf4b1535 r6eb2e96  
    183183                getcwd(buff, PATH_MAX);
    184184        else
    185                 str_ncpy(buff, argv[1], PATH_MAX);
     185                str_cpy(buff, PATH_MAX, argv[1]);
    186186
    187187        scope = ls_scope(buff);
  • uspace/app/tetris/scores.c

    rf4b1535 r6eb2e96  
    118118static void copyhiscore(int dest, int src)
    119119{
    120         strcpy(scores[dest].hs_name, scores[src].hs_name);
     120        str_cpy(scores[dest].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
     121            scores[src].hs_name);
    121122        scores[dest].hs_score = scores[src].hs_score;   
    122123        scores[dest].hs_level = scores[src].hs_level;
     
    132133        moveto(10 , 10);
    133134        puts("Insert your name: ");
    134         str_ncpy(scores[NUMSPOTS - 1].hs_name, "Player", MAXLOGNAME);
     135        str_cpy(scores[NUMSPOTS - 1].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
     136            "Player");
    135137        i = 6; off = 6;
    136138
     
    196198        int i;
    197199        for(i = 0; i < NUMSPOTS; i++) {
    198                 str_ncpy(scores[i].hs_name, "HelenOS Team", MAXLOGNAME);
     200                str_cpy(scores[i].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, "HelenOS Team");
    199201                scores[i].hs_score = (NUMSPOTS - i) * 200;     
    200202                scores[i].hs_level = (i + 1 > MAXLEVEL?MAXLEVEL:i + 1);
  • uspace/app/tetris/tetris.c

    rf4b1535 r6eb2e96  
    312312                }
    313313                if (keys[i] == ' ')
    314                         str_ncpy(key_write[i], "<space>", sizeof key_write[i]);
     314                        str_cpy(key_write[i], sizeof key_write[i], "<space>");
    315315                else {
    316316                        key_write[i][0] = keys[i];
  • uspace/lib/libc/generic/loader.c

    rf4b1535 r6eb2e96  
    181181
    182182        while (*ap != NULL) {
    183                 str_ncpy(dp, *ap, buffer_size - (dp - arg_buf));
     183                str_cpy(dp, buffer_size - (dp - arg_buf), *ap);
    184184                dp += str_size(*ap) + 1;
    185185
  • uspace/lib/libc/generic/string.c

    rf4b1535 r6eb2e96  
    463463}
    464464
    465 /** Copy NULL-terminated string.
    466  *
    467  * Copy source string @a src to destination buffer @a dst.
    468  * No more than @a size bytes are written. NULL-terminator is always
    469  * written after the last succesfully copied character (i.e. if the
    470  * destination buffer is has at least 1 byte, it will be always
    471  * NULL-terminated).
    472  *
    473  * @param src   Source string.
     465/** Copy string.
     466 *
     467 * Copy source string @a src to destination buffer @a dest.
     468 * No more than @a size bytes are written. If the size of the output buffer
     469 * is at least one byte, the output string will always be well-formed, i.e.
     470 * null-terminated and containing only complete characters.
     471 *
    474472 * @param dst   Destination buffer.
    475473 * @param count Size of the destination buffer.
    476  *
    477  */
    478 void str_ncpy(char *dst, const char *src, size_t size)
    479 {
    480         /* No space for the NULL-terminator in the buffer */
     474 * @param src   Source string.
     475 */
     476void str_cpy(char *dest, size_t size, const char *src)
     477{
     478        wchar_t ch;
     479        size_t src_off;
     480        size_t dest_off;
     481
     482        /* No space for the NULL-terminator in the buffer. */
    481483        if (size == 0)
    482484                return;
    483485       
     486        src_off = 0;
     487        dest_off = 0;
     488
     489        while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
     490                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
     491                        break;
     492        }
     493
     494        dest[dest_off] = '\0';
     495}
     496
     497/** Copy size-limited substring.
     498 *
     499 * Copy source string @a src to destination buffer @a dest.
     500 * No more than @a size bytes are written. If the size of the output buffer
     501 * is at least one byte, the output string will always be well-formed, i.e.
     502 * null-terminated and containing only complete characters.
     503 *
     504 * No more than @a n bytes are read from the input string, so it does not
     505 * have to be null-terminated.
     506 *
     507 * @param dst   Destination buffer.
     508 * @param count Size of the destination buffer.
     509 * @param src   Source string.
     510 */
     511void str_ncpy(char *dest, size_t size, const char *src, size_t n)
     512{
    484513        wchar_t ch;
    485         size_t str_off = 0;
    486         size_t dst_off = 0;
    487        
    488         while ((ch = str_decode(src, &str_off, STR_NO_LIMIT)) != 0) {
    489                 if (chr_encode(ch, dst, &dst_off, size) != EOK)
     514        size_t src_off;
     515        size_t dest_off;
     516
     517        /* No space for the null terminator in the buffer. */
     518        if (size == 0)
     519                return;
     520       
     521        src_off = 0;
     522        dest_off = 0;
     523
     524        while ((ch = str_decode(src, &src_off, n)) != 0) {
     525                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
    490526                        break;
    491527        }
    492        
    493         if (dst_off >= size)
    494                 dst[size - 1] = 0;
    495         else
    496                 dst[dst_off] = 0;
     528
     529        dest[dest_off] = '\0';
    497530}
    498531
     
    799832}
    800833
    801 char *strcpy(char *dest, const char *src)
    802 {
    803         char *orig = dest;
    804        
    805         while ((*(dest++) = *(src++)))
    806                 ;
    807         return orig;
    808 }
    809 
    810834char *strcat(char *dest, const char *src)
    811835{
  • uspace/lib/libc/generic/vfs/vfs.c

    rf4b1535 r6eb2e96  
    7777                        return NULL;
    7878                }
    79                 str_ncpy(ncwd_path_nc, cwd_path, cwd_size + 1 + size + 1);
     79                str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);
    8080                ncwd_path_nc[cwd_size] = '/';
    8181                ncwd_path_nc[cwd_size + 1] = '\0';
     
    535535                return NULL;
    536536        }
    537         str_ncpy(buf, cwd_path, size);
     537        str_cpy(buf, size, cwd_path);
    538538        futex_up(&cwd_futex);
    539539        return buf;
  • uspace/lib/libc/include/string.h

    rf4b1535 r6eb2e96  
    7070extern int str_lcmp(const char *s1, const char *s2, count_t max_len);
    7171
    72 extern void str_ncpy(char *dst, const char *src, size_t size);
     72extern void str_cpy(char *dest, size_t size, const char *src);
     73extern void str_ncpy(char *dest, size_t size, const char *src, size_t n);
     74
    7375extern void wstr_nstr(char *dst, const wchar_t *src, size_t size);
    7476
     
    8890extern int stricmp(const char *, const char *);
    8991
    90 extern char *strcpy(char *, const char *);
    9192extern char *strcat(char *, const char *);
    9293
  • uspace/srv/fs/fat/fat_ops.c

    rf4b1535 r6eb2e96  
    490490            str_cmp(d->name, FAT_NAME_DOT) == 0) {
    491491                memset(d, 0, sizeof(fat_dentry_t));
    492                 strcpy(d->name, FAT_NAME_DOT);
    493                 strcpy(d->ext, FAT_EXT_PAD);
     492                str_cpy(d->name, 8, FAT_NAME_DOT);
     493                str_cpy(d->ext, 3, FAT_EXT_PAD);
    494494                d->attr = FAT_ATTR_SUBDIR;
    495495                d->firstc = host2uint16_t_le(childp->firstc);
     
    500500            str_cmp(d->name, FAT_NAME_DOT_DOT) == 0) {
    501501                memset(d, 0, sizeof(fat_dentry_t));
    502                 strcpy(d->name, FAT_NAME_DOT_DOT);
    503                 strcpy(d->ext, FAT_EXT_PAD);
     502                str_cpy(d->name, 8, FAT_NAME_DOT_DOT);
     503                str_cpy(d->ext, 3, FAT_EXT_PAD);
    504504                d->attr = FAT_ATTR_SUBDIR;
    505505                d->firstc = (parentp->firstc == FAT_CLST_ROOT) ?
  • uspace/srv/fs/tmpfs/tmpfs_ops.c

    rf4b1535 r6eb2e96  
    326326                return ENOMEM;
    327327        }
    328         strcpy(namep->name, nm);
     328        str_cpy(namep->name, size + 1, nm);
    329329        namep->parent = parentp;
    330330       
Note: See TracChangeset for help on using the changeset viewer.