"Fossies" - the Fresh Open Source Software Archive

Member "fe-1.9/misc.c" (1 Feb 2011, 3951 Bytes) of archive /linux/misc/fe-1.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 /* #includes *//*{{{C}}}*//*{{{*/
    2 #ifdef DMALLOC
    3 #include "dmalloc.h"
    4 #endif
    5 
    6 #include <sys/types.h>
    7 #include <assert.h>
    8 #include <errno.h>
    9 #include <fcntl.h>
   10 #include <stdio.h>
   11 #include <stdlib.h>
   12 #include <string.h>
   13 #include <pwd.h>
   14 #include <unistd.h>
   15 
   16 #include "misc.h"
   17 /*}}}*/
   18 
   19 /* mymemmem    -- strstr for binary data */ /*{{{*/
   20 /* Notes *//*{{{*/
   21 /*
   22 
   23 You probably heard about so called "efficient" search algorithms, which
   24 are not of the order O(n*n).  They are a typical example of a theorey
   25 which makes wrong assumptions for practical use, e.g. that you can
   26 neglect any constant factors.  Those fancy algorithms are slow for
   27 small, medium and large n, and n is usually not huge.  No startup costs
   28 and only operations which can be translated directly to native machine
   29 code on most machines make brute force work well here, due to using lots
   30 of it.
   31 
   32 */
   33 /*}}}*/
   34 /*@null@*/ char *mymemmem(const char *hay, size_t haysize, const char *needle, size_t needlesize)
   35 {
   36   while (haysize!=0)
   37   {
   38     while (haysize!=0 && *needle!=*hay) { ++hay; --haysize; }
   39     if (haysize==0) return (char*)0;
   40     else
   41     {
   42       const char *oldhay,*oldneedle;
   43       size_t oldhaysize,oldneedlesize;
   44     
   45       oldhay=hay;
   46       oldneedle=needle;
   47       oldhaysize=haysize;
   48       oldneedlesize=needlesize;
   49       while (*hay++==*needle++ && --needlesize!=0 && --haysize!=0);
   50       if (needlesize==0) return (char*)oldhay;
   51       else if (haysize==0) return (char*)0;
   52       else
   53       {
   54         hay=oldhay+1;
   55         haysize=oldhaysize-1;
   56         needle=oldneedle;
   57         needlesize=oldneedlesize;
   58         continue;
   59       }
   60     }
   61   }
   62   return (char*)0;
   63 }
   64 /*}}}*/
   65 /* myrmemmem   -- rstrstr for binary data */ /*{{{*/
   66 /*@null@*/ char *myrmemmem(const char *hay, size_t haysize, const char *needle, size_t needlesize)
   67 {
   68   const char *rhay,*rneedle;
   69 
   70   rhay=hay+haysize-1;
   71   rneedle=needle+needlesize-1;
   72   while (haysize!=0)
   73   {
   74     while (haysize!=0 && *rneedle!=*rhay) { --rhay; --haysize; }
   75     if (haysize==0) return (char*)0;
   76     else
   77     {
   78       const char *oldrhay,*oldrneedle;
   79       size_t oldhaysize,oldneedlesize;
   80     
   81       oldrhay=rhay;
   82       oldrneedle=rneedle;
   83       oldhaysize=haysize;
   84       oldneedlesize=needlesize;
   85       while (*rhay--==*rneedle-- && --needlesize!=0 && --haysize!=0);
   86       if (needlesize==0) return (char*)(rhay+1);
   87       else if (haysize==0) return (char*)0;
   88       else
   89       {
   90         rhay=oldrhay-1;
   91         haysize=oldhaysize-1;
   92         rneedle=oldrneedle;
   93         needlesize=oldneedlesize;
   94         continue;
   95       }
   96     }
   97   }
   98   return (char*)0;
   99 }
  100 /*}}}*/
  101 /* mystrmalloc -- return malloced copy of string */ /*{{{*/
  102 /*@null@*/ char *mystrmalloc(const char *str)
  103 {
  104   char *m;
  105 
  106   if ((m=malloc(strlen(str)+1))==(char*)0) return (char*)0;
  107   strcpy(m,str);
  108   return m;
  109 }
  110 /*}}}*/
  111 /* myrealloc   -- ANSI conforming realloc() */ /*{{{*/
  112 #ifdef OLD_REALLOC
  113 #undef realloc
  114 void *myrealloc(void *p, size_t n)
  115 {
  116   return (p==(void*)0 ? malloc(n) : realloc(p,n));
  117 }
  118 #endif
  119 /*}}}*/
  120 /* getshell    -- get name of shell */ /*{{{*/
  121 /*@null@*/ const char *getshell(void)
  122 {
  123   static const char *shell=(const char*)0;
  124 
  125   if (shell==(const char*)0)
  126   {
  127     if ((shell=getenv("SHELL"))==(const char*)0)
  128     {
  129       struct passwd *pwd;
  130 
  131       if ((pwd=getpwuid(getuid()))==(struct passwd*)0)
  132       {
  133         shell="/bin/sh";
  134       }
  135       else
  136       {
  137         shell=mystrmalloc(pwd->pw_shell);
  138       }
  139     }
  140   }
  141   return shell;
  142 }
  143 /*}}}*/
  144 /* mytmpnam    -- tmpnam without race condition */ /*{{{*/
  145 /*@null@*/ char *mytmpnam(char *buf)
  146 {
  147   const char *tmpdir;  
  148   pid_t i;
  149   int fd=-1;
  150 
  151   if ((tmpdir=getenv("TMPDIR"))==(char*)0) tmpdir="/tmp";
  152   if ((strlen(tmpdir)+1+8+1)>(size_t)L_tmpnam) return (char*)0;
  153   i=getpid();
  154   while (i<0xffffffff && snprintf(buf,(size_t)L_tmpnam,"%s/%08x",tmpdir,(unsigned int)i)!=0,(fd=open(buf,O_RDWR|O_CREAT|O_EXCL,0666))==-1 && errno==EEXIST) ++i;
  155   if (fd==-1) return (char*)0;
  156   (void)close(fd);
  157   return buf;
  158 }
  159 /*}}}*/