[Eug-lug] LP64

Neil Parker nparker at LLX.COM
Mon Jan 10 00:30:40 PST 2005


Jacob Meuser wrote,
>the thing is, there's also a foo_open function, that returns a
>pointer to a struct.  the code was apparently written to make
>open and ibp_open interchangable.  since they have different
>return types, the author chose to use (void *).  the only use
>of data is "if(!data) { ... }".

In that case, the correct approach is probably to wrap open() with
something that returns a pointer.  For example,

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>

int *wrapped_open(cont char *name, int flags, ...)
{
	int *fdp = (int *)malloc(sizeof(int));
	mode_t mode;
	va_list v;

	if(fdp!=(int *)NULL) {
		if(flags&O_CREAT) {
			va_start(v, flags);
			mode = va_arg(v, mode_t);
			va_end(v);
			*fdp = open(name, flags, mode);
		} else
			*fdp = open(name, flags);
	}
	return fdp;
}

Then (unless I made a coding error), you can call wrapped_open() instead of
open(), and it will return a pointer the file descriptor instead of the
descriptor itself (unless you're out of memory, in which case it returns
NULL).  And gcc should be happy to cast the result of wrapped_open() to
void *.

Of course you'll have to free() the returned pointer after you close the
file.  You'll probably also have to rewrite some of the code that uses the
return value, since the file descriptor is now "*data" instead of
"(int)data".


I still don't understand why the original code did the cast, though.  The
only reason to make the returns from open() and ibp_open() interchangeable
is if you're planning to interchange them.  But it doesn't make sense to
pass your fake pointer from open() to a routine that expects ibp_open()'s
structure pointer, since the fake pointer doesn't actually point to the
right structure.

I suppose the routines that handle ibp_open()'s structure could be written
to call different code when they detect that the pointer has a nonsensical
value, but that strikes me as more of an ugly hack than a reasonable
design.

And are you SURE that "data" is only used in lines like "if(!data) ..."?  In
that case, why is the code bothering to open the file at all?  The usual
reason for opening a file is to do some kind of I/O on it...

               - Neil Parker


More information about the EUGLUG mailing list