Changeset a92cf94f in mainline for uspace/lib/posix


Ignore:
Timestamp:
2011-08-18T18:45:42Z (15 years ago)
Author:
Petr Koupy <petr.koupy@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/fix-logger-deadlock, topic/msim-upgrade, topic/simplify-dev-export
Children:
1f44b056
Parents:
ee24574 (diff), f55b12b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge libposix changes.

Location:
uspace/lib/posix
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/pwd.c

    ree24574 ra92cf94f  
    5151
    5252/**
     53 * Retrieve next broken-down entry from the user database.
     54 *
    5355 * Since HelenOS doesn't have user accounts, this always returns
    5456 * the same made-up entry.
    5557 *
    56  * @return
     58 * @return Next user database entry or NULL if not possible. Since HelenOS
     59 *     doesn't have user accounts, this always returns the same made-up entry.
    5760 */
    5861struct posix_passwd *posix_getpwent(void)
     
    6770
    6871/**
    69  * "Rewind the user list".
     72 * Rewind the user list.
    7073 */
    7174void posix_setpwent(void)
     
    8689 *
    8790 * @param name Name of the entry.
    88  * @return
     91 * @return Either found entry or NULL if no such entry exists.
    8992 */
    9093struct posix_passwd *posix_getpwnam(const char *name)
     
    103106 *
    104107 * @param name Name of the entry.
    105  * @param pwd
    106  * @param buffer
    107  * @param bufsize
    108  * @param result
    109  * @return
     108 * @param pwd Original structure.
     109 * @param buffer Buffer for the strings referenced from the result structure.
     110 * @param bufsize Length of the buffer.
     111 * @param result Where to store updated structure.
     112 * @return Zero on success (either found or not found, but without an error),
     113 *     non-zero error number if error occured.
    110114 */
    111115int posix_getpwnam_r(const char *name, struct posix_passwd *pwd,
     
    129133 *
    130134 * @param uid UID of the entry.
    131  * @return
     135 * @return Either found entry or NULL if no such entry exists.
    132136 */
    133137struct posix_passwd *posix_getpwuid(posix_uid_t uid)
     
    144148 *
    145149 * @param uid UID of the entry.
    146  * @param pwd
    147  * @param buffer
    148  * @param bufsize
    149  * @param result
    150  * @return
     150 * @param pwd Original structure.
     151 * @param buffer Buffer for the strings referenced from the result structure.
     152 * @param bufsize Length of the buffer.
     153 * @param result Where to store updated structure.
     154 * @return Zero on success (either found or not found, but without an error),
     155 *     non-zero error number if error occured.
    151156 */
    152157int posix_getpwuid_r(posix_uid_t uid, struct posix_passwd *pwd,
  • uspace/lib/posix/string.c

    ree24574 ra92cf94f  
    4848
    4949/**
    50  * Decides whether s2 is a prefix of s1.
    51  *
    52  * @param s1 String in which to look for a prefix.
    53  * @param s2 Prefix string to look for.
    54  * @return True if s2 is a prefix of s1, false otherwise.
    55  */
    56 static bool begins_with(const char *s1, const char *s2)
    57 {
    58         while (*s1 == *s2 && *s2 != '\0') {
    59                 s1++;
    60                 s2++;
    61         }
    62        
    63         /* true if the end was reached */
    64         return *s2 == '\0';
    65 }
    66 
    67 /**
    6850 * The same as strpbrk, except it returns pointer to the nul terminator
    6951 * if no occurence is found.
     
    471453
    472454/**
    473  * Find a substring.
     455 * Find a substring. Uses Knuth-Morris-Pratt algorithm.
    474456 *
    475457 * @param s1 String in which to look for a substring.
     
    478460 *     not found.
    479461 */
    480 char *posix_strstr(const char *s1, const char *s2)
    481 {
    482         assert(s1 != NULL);
    483         assert(s2 != NULL);
    484 
    485         /* special case - needle is an empty string */
    486         if (*s2 == '\0') {
    487                 return (char *) s1;
    488         }
    489 
    490         // TODO: use faster algorithm
    491         /* check for prefix from every position - quadratic complexity */
    492         while (*s1 != '\0') {
    493                 if (begins_with(s1, s2)) {
    494                         return (char *) s1;
    495                 }
     462char *posix_strstr(const char *haystack, const char *needle)
     463{
     464        assert(haystack != NULL);
     465        assert(needle != NULL);
     466       
     467        /* Special case - needle is an empty string. */
     468        if (needle[0] == '\0') {
     469                return (char *) haystack;
     470        }
     471       
     472        /* Preprocess needle. */
     473        size_t nlen = posix_strlen(needle);
     474        size_t prefix_table[nlen + 1];
     475       
     476        {
     477                size_t i = 0;
     478                ssize_t j = -1;
    496479               
    497                 s1++;
     480                prefix_table[i] = j;
     481               
     482                while (i < nlen) {
     483                        while (j >= 0 && needle[i] != needle[j]) {
     484                                j = prefix_table[j];
     485                        }
     486                        i++; j++;
     487                        prefix_table[i] = j;
     488                }
     489        }
     490       
     491        /* Search needle using the precomputed table. */
     492        size_t npos = 0;
     493       
     494        for (size_t hpos = 0; haystack[hpos] != '\0'; ++hpos) {
     495                while (npos != 0 && haystack[hpos] != needle[npos]) {
     496                        npos = prefix_table[npos];
     497                }
     498               
     499                if (haystack[hpos] == needle[npos]) {
     500                        npos++;
     501                       
     502                        if (npos == nlen) {
     503                                return (char *) (haystack + hpos - nlen + 1);
     504                        }
     505                }
    498506        }
    499507       
  • uspace/lib/posix/string.h

    ree24574 ra92cf94f  
    8686extern size_t posix_strcspn(const char *s1, const char *s2);
    8787extern size_t posix_strspn(const char *s1, const char *s2);
    88 extern char *posix_strstr(const char *s1, const char *s2);
     88extern char *posix_strstr(const char *haystack, const char *needle);
    8989
    9090/* Collation Functions */
Note: See TracChangeset for help on using the changeset viewer.