diff options
author | Eric Andersen <andersen@codepoet.org> | 1999-10-22 08:30:20 +0400 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 1999-10-22 08:30:20 +0400 |
commit | aa0765e11bdeba5c5abf745369a8430c8311d60c (patch) | |
tree | 3593c1a2ff03bfa79982fa12b55c9489f969e057 /utility.c | |
parent | c49960189a04b73e033016bd0f43fbb950f800e1 (diff) |
Added regexp support, fixed Changelog.
Diffstat (limited to 'utility.c')
-rw-r--r-- | utility.c | 151 |
1 files changed, 54 insertions, 97 deletions
@@ -53,12 +53,17 @@ volatile void usage(const char *usage) int get_kernel_revision() { - FILE *f; + FILE *file; int major=0, minor=0, patch=0; - - f = fopen("/proc/sys/kernel/osrelease","r"); - fscanf(f,"%d.%d.%d",&major,&minor,&patch); - fclose(f); + char* filename="/proc/sys/kernel/osrelease"; + + file = fopen(filename,"r"); + if (file == NULL) { + perror(filename); + return( 0); + } + fscanf(file,"%d.%d.%d",&major,&minor,&patch); + fclose(file); return major*65536 + minor*256 + patch; } @@ -312,94 +317,6 @@ const char *timeString(time_t timeVal) /* - * Routine to see if a text string is matched by a wildcard pattern. - * Returns TRUE if the text is matched, or FALSE if it is not matched - * or if the pattern is invalid. - * * matches zero or more characters - * ? matches a single character - * [abc] matches 'a', 'b' or 'c' - * \c quotes character c - * Adapted from code written by Ingo Wilken. - */ -int match(const char *text, const char *pattern) -{ - const char *retryPat; - const char *retryText; - int ch; - int found; - - retryPat = NULL; - retryText = NULL; - - while (*text || *pattern) { - ch = *pattern++; - - switch (ch) { - case '*': - retryPat = pattern; - retryText = text; - break; - - case '[': - found = FALSE; - - while ((ch = *pattern++) != ']') { - if (ch == '\\') - ch = *pattern++; - - if (ch == '\0') - return FALSE; - - if (*text == ch) - found = TRUE; - } - - if (!found) { - pattern = retryPat; - text = ++retryText; - } - - /* fall into next case */ - - case '?': - if (*text++ == '\0') - return FALSE; - - break; - - case '\\': - ch = *pattern++; - - if (ch == '\0') - return FALSE; - - /* fall into next case */ - - default: - if (*text == ch) { - if (*text) - text++; - break; - } - - if (*text) { - pattern = retryPat; - text = ++retryText; - break; - } - - return FALSE; - } - - if (pattern == NULL) - return FALSE; - } - - return TRUE; -} - - -/* * Write all of the supplied buffer out to a file. * This does multiple writes as necessary. * Returns the amount written, or -1 on an error. @@ -695,13 +612,17 @@ parse_mode( const char* s, mode_t* theMode) uid_t my_getid(const char *filename, char *name, uid_t id) { - FILE *stream; + FILE *file; char *rname, *start, *end, buf[128]; uid_t rid; - stream=fopen(filename,"r"); + file=fopen(filename,"r"); + if (file == NULL) { + perror(filename); + return (-1); + } - while (fgets (buf, 128, stream) != NULL) { + while (fgets (buf, 128, file) != NULL) { if (buf[0] == '#') continue; @@ -731,7 +652,7 @@ my_getid(const char *filename, char *name, uid_t id) return( TRUE); } } - fclose(stream); + fclose(file); return (-1); } @@ -763,4 +684,40 @@ my_getgrgid(char* group, gid_t gid) #endif + +#if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND ) +/* This tries to find a needle in a haystack, but does so by + * only trying to match literal strings (look 'ma, no regexps!) + * This is short, sweet, and carries _very_ little baggage, + * unlike its beefier cousin a few lines down... + * -Erik Andersen + */ +extern int find_match(char *haystack, char *needle, int ignoreCase) +{ + + if (ignoreCase == FALSE) { + haystack = strstr (haystack, needle); + if (haystack == NULL) + return FALSE; + return TRUE; + } else { + int i; + char needle1[BUF_SIZE]; + char haystack1[BUF_SIZE]; + + strncpy( haystack1, haystack, sizeof(haystack1)); + strncpy( needle1, needle, sizeof(needle1)); + for( i=0; i<sizeof(haystack1) && haystack1[i]; i++) + haystack1[i]=tolower( haystack1[i]); + for( i=0; i<sizeof(needle1) && needle1[i]; i++) + needle1[i]=tolower( needle1[i]); + haystack = strstr (haystack1, needle1); + if (haystack == NULL) + return FALSE; + return TRUE; + } +} +#endif + /* END CODE */ + |