"Fossies" - the Fresh Open Source Software Archive

Member "authforce-0.9.9/src/misc.c" (13 May 2007, 2432 Bytes) of package /linux/www/old/authforce-0.9.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "misc.c" see the Fossies "Dox" file reference documentation.

    1 /* $Id: misc.c,v 1.3 2001/02/13 22:54:53 kapheine Exp $ */
    2 
    3 #include <config.h>
    4 #include <stdio.h>
    5 #include <stdlib.h>
    6 #include <malloc.h>
    7 #include <string.h>
    8 #include <ctype.h>
    9 #ifdef MEMWATCH
   10 #include "memwatch.h"
   11 #endif /* MEMWATCH */
   12 #include "extern.h"
   13 
   14 /* malloc wrapper with error checking */
   15 void *malloc_wrapper(size_t size) {
   16     void *ptr;
   17 
   18     ptr = malloc(size);
   19 
   20     if (!ptr) {
   21         fprintf(stderr, "malloc_wrapper: couldn't allocate memory\n");
   22         exit(EXIT_FAILURE);
   23     }
   24 
   25     return (ptr);
   26 }
   27 
   28 /* strdup wrapper with error checking */
   29 char *strdup_wrapper(const char *s) {
   30     char *ptr;
   31 
   32     ptr = strdup(s);
   33 
   34     if (!ptr) {
   35         fprintf(stderr, "strdup_wrapper: couldn't allocate memory\n");
   36         exit(EXIT_FAILURE);
   37     }
   38 
   39     return (ptr);
   40 }
   41 
   42 /*
   43  * seperates str using token, putting the first part in str and returning
   44  * the second part. (does a function like this already exist?)
   45  * why the hell wont it ever return null blah!
   46  */
   47 char *extract(char *str, char token) {
   48 
   49     while (*str != token) {
   50         if (*str == '\0')
   51             return(NULL);
   52         ++str;
   53     }
   54     *str = '\0';
   55     str++;
   56     return(str);
   57 }
   58 
   59 /* calculate new average, average being new entry, element being the new
   60  * number and num being the total elements in average
   61  */
   62 
   63 float stats(float average, float element, int num) {
   64     printf("avg: %f, el: %f, num: %i\n", average, element, num);
   65     return (((average*num)+element)/num+1);
   66 }
   67 
   68 /* code released as public domain by Bob Stout */
   69 char *strrev(char *str)
   70 {
   71       char *p1, *p2;
   72 
   73       if (! str || ! *str)
   74             return str;
   75       for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
   76       {
   77             *p1 ^= *p2;
   78             *p2 ^= *p1;
   79             *p1 ^= *p2;
   80       }
   81       return str;
   82 }
   83 
   84 /* +++Date last modified: 05-Jul-1997 */
   85 /*
   86 **  Originally published as part of the MicroFirm Function Library
   87 **
   88 **  Copyright 1986, S.E. Margison
   89 **  Copyright 1989, Robert B.Stout
   90 **
   91 **  The user is granted a free limited license to use this source file
   92 **  to create royalty-free programs, subject to the terms of the
   93 **  license restrictions specified in the LICENSE.MFL file.
   94 **
   95 **  remove all whitespace from a string
   96 */
   97 /* slightly modified */
   98 
   99 void remove_crud(char *str)
  100 {
  101       char *obuf, *nbuf;
  102 
  103       if (str)
  104       {
  105             for (obuf = str, nbuf = str; *obuf; ++obuf)
  106             {
  107                   if (*obuf != ' ' && *obuf != '\t')
  108                         *nbuf++ = *obuf;
  109 
  110             }
  111             *nbuf = 0;
  112       }
  113 }