aboutsummaryrefslogtreecommitdiff
path: root/utility.c
diff options
context:
space:
mode:
Diffstat (limited to 'utility.c')
-rw-r--r--utility.c151
1 files changed, 54 insertions, 97 deletions
diff --git a/utility.c b/utility.c
index e188ecdcd..eb24de2e6 100644
--- a/utility.c
+++ b/utility.c
@@ -53,12 +53,17 @@ volatile void usage(const char *usage)
53int 53int
54get_kernel_revision() 54get_kernel_revision()
55{ 55{
56 FILE *f; 56 FILE *file;
57 int major=0, minor=0, patch=0; 57 int major=0, minor=0, patch=0;
58 58 char* filename="/proc/sys/kernel/osrelease";
59 f = fopen("/proc/sys/kernel/osrelease","r"); 59
60 fscanf(f,"%d.%d.%d",&major,&minor,&patch); 60 file = fopen(filename,"r");
61 fclose(f); 61 if (file == NULL) {
62 perror(filename);
63 return( 0);
64 }
65 fscanf(file,"%d.%d.%d",&major,&minor,&patch);
66 fclose(file);
62 return major*65536 + minor*256 + patch; 67 return major*65536 + minor*256 + patch;
63} 68}
64 69
@@ -312,94 +317,6 @@ const char *timeString(time_t timeVal)
312 317
313 318
314/* 319/*
315 * Routine to see if a text string is matched by a wildcard pattern.
316 * Returns TRUE if the text is matched, or FALSE if it is not matched
317 * or if the pattern is invalid.
318 * * matches zero or more characters
319 * ? matches a single character
320 * [abc] matches 'a', 'b' or 'c'
321 * \c quotes character c
322 * Adapted from code written by Ingo Wilken.
323 */
324int match(const char *text, const char *pattern)
325{
326 const char *retryPat;
327 const char *retryText;
328 int ch;
329 int found;
330
331 retryPat = NULL;
332 retryText = NULL;
333
334 while (*text || *pattern) {
335 ch = *pattern++;
336
337 switch (ch) {
338 case '*':
339 retryPat = pattern;
340 retryText = text;
341 break;
342
343 case '[':
344 found = FALSE;
345
346 while ((ch = *pattern++) != ']') {
347 if (ch == '\\')
348 ch = *pattern++;
349
350 if (ch == '\0')
351 return FALSE;
352
353 if (*text == ch)
354 found = TRUE;
355 }
356
357 if (!found) {
358 pattern = retryPat;
359 text = ++retryText;
360 }
361
362 /* fall into next case */
363
364 case '?':
365 if (*text++ == '\0')
366 return FALSE;
367
368 break;
369
370 case '\\':
371 ch = *pattern++;
372
373 if (ch == '\0')
374 return FALSE;
375
376 /* fall into next case */
377
378 default:
379 if (*text == ch) {
380 if (*text)
381 text++;
382 break;
383 }
384
385 if (*text) {
386 pattern = retryPat;
387 text = ++retryText;
388 break;
389 }
390
391 return FALSE;
392 }
393
394 if (pattern == NULL)
395 return FALSE;
396 }
397
398 return TRUE;
399}
400
401
402/*
403 * Write all of the supplied buffer out to a file. 320 * Write all of the supplied buffer out to a file.
404 * This does multiple writes as necessary. 321 * This does multiple writes as necessary.
405 * Returns the amount written, or -1 on an error. 322 * Returns the amount written, or -1 on an error.
@@ -695,13 +612,17 @@ parse_mode( const char* s, mode_t* theMode)
695uid_t 612uid_t
696my_getid(const char *filename, char *name, uid_t id) 613my_getid(const char *filename, char *name, uid_t id)
697{ 614{
698 FILE *stream; 615 FILE *file;
699 char *rname, *start, *end, buf[128]; 616 char *rname, *start, *end, buf[128];
700 uid_t rid; 617 uid_t rid;
701 618
702 stream=fopen(filename,"r"); 619 file=fopen(filename,"r");
620 if (file == NULL) {
621 perror(filename);
622 return (-1);
623 }
703 624
704 while (fgets (buf, 128, stream) != NULL) { 625 while (fgets (buf, 128, file) != NULL) {
705 if (buf[0] == '#') 626 if (buf[0] == '#')
706 continue; 627 continue;
707 628
@@ -731,7 +652,7 @@ my_getid(const char *filename, char *name, uid_t id)
731 return( TRUE); 652 return( TRUE);
732 } 653 }
733 } 654 }
734 fclose(stream); 655 fclose(file);
735 return (-1); 656 return (-1);
736} 657}
737 658
@@ -763,4 +684,40 @@ my_getgrgid(char* group, gid_t gid)
763#endif 684#endif
764 685
765 686
687
688#if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND )
689/* This tries to find a needle in a haystack, but does so by
690 * only trying to match literal strings (look 'ma, no regexps!)
691 * This is short, sweet, and carries _very_ little baggage,
692 * unlike its beefier cousin a few lines down...
693 * -Erik Andersen
694 */
695extern int find_match(char *haystack, char *needle, int ignoreCase)
696{
697
698 if (ignoreCase == FALSE) {
699 haystack = strstr (haystack, needle);
700 if (haystack == NULL)
701 return FALSE;
702 return TRUE;
703 } else {
704 int i;
705 char needle1[BUF_SIZE];
706 char haystack1[BUF_SIZE];
707
708 strncpy( haystack1, haystack, sizeof(haystack1));
709 strncpy( needle1, needle, sizeof(needle1));
710 for( i=0; i<sizeof(haystack1) && haystack1[i]; i++)
711 haystack1[i]=tolower( haystack1[i]);
712 for( i=0; i<sizeof(needle1) && needle1[i]; i++)
713 needle1[i]=tolower( needle1[i]);
714 haystack = strstr (haystack1, needle1);
715 if (haystack == NULL)
716 return FALSE;
717 return TRUE;
718 }
719}
720#endif
721
766/* END CODE */ 722/* END CODE */
723