Thread: make won't filter-out

  1. #16
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    839
    I found my workaround, I edited the loslib.c file to use this instead:
    Code:
    typedef unsigned int lua_uint;
    size_t u2a( char *str, size_t size, lua_uint val, lua_uint base, lua_uint small ) {
    static const char bstr[] = "0123456789ABCDEF";
    static const char sstr[] = "0123456789abcdef";
    static const size_t min = (sizeof(int) * CHAR_BIT);
    	const char *s = small ? sstr : bstr;
    	size_t i = 0;
    	if ( !str || !size || base < 2 || base > strlen(s) ) return 0;
    	--size;
    	if ( size < min ) {
    		memset( str, 0, size );
    		return 0;
    	}
    	while ( val > 0u ) {
    		str[i++] = s[val % base];
    		val /= base;
    	}
    	str[i] = 0;
    	return i;
    }
    /* Avoids unistd.h, can expect sys/stat.h on all supported systems */
    #include <sys/stat.h>
    size_t tmpname( char *str, size_t size ) {
    	size_t i;
    	struct stat m = {0};
    	if ( u2a( str, --size, 0, 2, 0 ) == 0 ) return 0;
    	str[0] = '_';
    	++str;
    	do {
    		i = u2a( str, size, rand(), 16, 1 );
    	} while ( stat(str,&m) == 1 );
    	return i;
    }
    
    static int os_tmpname (lua_State *L) {
      char buff[LUA_TMPNAMBUFSIZE];
      if ( tmpname(buff, LUA_TMPNAMBUFSIZE) == 0 )
        return luaL_error(L, "unable to generate a unique filename");
      lua_pushstring(L, buff);
      return 1;
    }
    I'm going to send this to the lua mailing list and hopefully it will be incorporated in their sources (with a few edits to conform to their own library stuff)

  2. #17
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    839
    submitted

  3. #18
    Registered User
    Join Date
    Dec 2017
    Posts
    836
    I don't understand your tmpname function. Is it supposed to be "better" than the standard tmpnam? How? tmpnam also ensures that the filename it creates doesn't exist. The problem is that that may not be true by the time you actually create the file. It's unlikely not to be true, but that's the supposed "danger" of tmpnam.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  4. #19
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    839
    Yes but this doesn't set the compiler off when compiling, there is also the factor of it being significantly more thread safe since one doesn't know whether to free the pointer returned by tmpnam, my one you gotta provide the string buffer and size of it (to prevent eternal loop), while it cannot avoid the caveat of the file being created between returning an OK and caller attempting to create the file, it at least reduces other risks and stops the compiler complaining of a dangerous function, if you can think of a way to avoid that one caveat (without opening the file) then be my guest

  5. #20
    Registered User
    Join Date
    Dec 2017
    Posts
    836
    Whatever. I'll leave you to it.

    BTW, on Linux at least, stat's return value is never 1:
    Upon successful completion, 0 shall be returned. Otherwise, -1 shall be returned and errno set to indicate the error.
    There are, of course, many reasons it might fail besides the file not existing.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  6. #21
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    839
    Ah I must've mixed up the reference I was reading then, I searched for alternatives to access since when I tried it the compiler threw hissy fit about implicit function, I ended up choosing stat because I knew that was in one way or another available on windows too and was very likely available on any linux variant (albiet not always with all the defined variables)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DNS Filter ( Redirect)
    By jhsedona in forum C Programming
    Replies: 1
    Last Post: 04-08-2012, 04:20 PM
  2. Fir filter
    By kiros88 in forum C Programming
    Replies: 3
    Last Post: 06-18-2010, 03:02 PM
  3. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 09:51 PM
  4. How to make a GrayScale filter?
    By Raigne in forum Game Programming
    Replies: 11
    Last Post: 02-25-2008, 05:52 AM
  5. Filter Sort
    By Showster in forum C Programming
    Replies: 9
    Last Post: 11-30-2001, 11:42 AM

Tags for this Thread