[libgfortran,patch] MinGW wrapper for getlogin, gethostname and getpid functions

Danny Smith dannysmith@clear.net.nz
Sun Sep 25 07:55:00 GMT 2005


----- Original Message ----- 
From: "FX Coudert" 
Sent: Sunday, 25 September 2005 00:46
> Index: intrinsics/getlog.c
> ===================================================================
> RCS file: /cvs/gcc/gcc/libgfortran/intrinsics/getlog.c,v
> retrieving revision 1.3
> diff -u -3 -p -r1.3 getlog.c
> --- intrinsics/getlog.c 17 Aug 2005 02:48:53 -0000 1.3
> +++ intrinsics/getlog.c 24 Sep 2005 12:42:44 -0000
> @@ -38,6 +38,24 @@ Boston, MA 02110-1301, USA.  */
>  #include <unistd.h>
>  #endif
>  
> +#ifdef __MINGW32__
> +#define HAVE_GETLOGIN
> +#include <windows.h>
> +
> +char *getlogin (void);
> +
> +char *
> +getlogin (void)
> +{
> +  char *p;
> +  DWORD dum = 100;
> +
> +  p = gfc_alloca (dum);
> +  GetUserName (p, &dum);
> +  return p;
> +}
> +#endif
> +
>  

Returning memory allocated by  __builtin_alloca is not a good idea.
This would be safer:

/* Windows32 version */
#if defined _WIN32 && !defined  HAVE_GETLOGIN
#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#include <lmcons.h>  /* for UNLEN */ 

static char *
w32_getlogin (void)
{
  static char name [UNLEN + 1];
  DWORD namelen = sizeof (name);
   
  GetUserName (name, &namelen);
  return (name[0] == 0 ?  NULL : name);
}

#undef getlogin
#define getlogin w32_getlogin
#define  HAVE_GETLOGIN 1

#endif







More information about the Gcc-patches mailing list