[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: some more JOBS



On Fri, Aug 10, 2001 at 07:37:11PM +0530, Nikunj A. Dadhania wrote:
> +#elif defined(__GNU__)		
> +	case FSTYPE_UFS:
> +	        printf("UFS\n");
> +		break;
> +	case FSTYPE_NFS:
> +		printf("NFS\n");
> +		break;
> +	case FSTYPE_GFS:
> +		printf("GFS\n");
> +		break;
> +	case FSTYPE_LFS:
> +		printf("LFS\n");
> +		break;

No, no, no!

This #ifdef OS used in this way is hell to maintain as soon as more
than two operating system is to be supported.  In addition different
versions of the same OS would support different sets of filesystems!
A much better approach in cases like this, would be:

#ifdef FSTYPE_UFS
	case FSTYPE_UFS:
	        printf("UFS\n");
		break;
#endif
#ifdef FSTYPE_NFS
	case FSTYPE_NFS:
		printf("NFS\n");
		break;
#endif
#ifdef FSTYPE_GFS
	case FSTYPE_GFS:
		printf("GFS\n");
		break;
#endif
#ifdef FSTYPE_LFS
	case FSTYPE_LFS:
		printf("LFS\n");
		break;
#endif

This way, all filesytems known to tha system compile-time would be
included, regardless of the operating system.


And of course this could easily be automated, for example using a file

all_fstypes
===========
UFS
NFS
GFS
LFS
...

a script to convert to C:

gen_printf_all_fstypes
======================
#! /bin/sh
sed 's/.*/#ifdef FSTYPE_&\
	case FSTYPE_&:\
		printf("&\\n");\
		break;\
#endif/' <$1 >$2


and then in the Makefile:

printf_all_fstypes.h: all_fstypes gen_printf_all_fstypes
	./gen_printf_all_fstypes $< $@

and in the original source:

	switch (statfsbuf.f_type) {
		#include <printf_all_fstypes.h>
	
	}


(You may want another extension than .h)

#ifdef OS is bad  (and very un-GNUish) and should be avoided if possible.
#ifdef MACRO is always good.  (if it makes sense.)

	Steinar



Reply to: