diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index 3324b0f7dfa2d58c69e3a2d14e16e55067e26be7..256e04116327617338e3febce93487fbf87ede8c 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,3 +1,21 @@
+2004-11-20  Eric Botcazou  <ebotcazou@libertysurf.fr>
+
+	PR target/16135
+	* acinclude.m4 (LIBGFOR_TARGET_ILP32): New check.
+	* configure.ac: Include LIBGFOR_TARGET_ILP32.
+	* configure: Regenerate.
+	* config.h.in: Likewise.
+	* libgfortran.h: Provide default definitions for C99 types
+	on ILP32 targets that don't have them.
+
+	PR target/17999
+	* configure.ac: Check for snprintf.
+	* configure: Regenerate.
+	* config.h.in: Likewise.
+	* intrinsics/date_and_time.c (date_and_time): Do not
+	use snprinf if it is not available.
+	* io/write.c (output_float): Likewise.
+
 2004-11-20  Steven G. Kargl  <kargls@comcast.net>
 
 	* Makefile.am: Add intrinsics/{umask.c,unlink.c,exit.c}
diff --git a/libgfortran/acinclude.m4 b/libgfortran/acinclude.m4
index ebee89df8563381277d35ba3a4034621d1ba12e4..87dc72283f98a3f7338a5ed3aed36e54d9168d10 100644
--- a/libgfortran/acinclude.m4
+++ b/libgfortran/acinclude.m4
@@ -83,3 +83,22 @@ AC_DEFUN([AC_LIBTOOL_DLOPEN])
 AC_DEFUN([AC_PROG_LD])
 ])
 
+dnl Check whether the target is ILP32.
+AC_DEFUN([LIBGFOR_TARGET_ILP32], [
+  AC_CACHE_CHECK([whether the target is ILP32], target_ilp32, [
+  save_CFLAGS="$CFLAGS"
+  CFLAGS="-O2"
+  AC_TRY_LINK(,[
+if (sizeof(int) == 4 && sizeof(long) == 4 && sizeof(void *) == 4)
+  ;
+else
+  undefined_function ();
+               ],
+               target_ilp32=yes,
+               target_ilp32=no)
+  CFLAGS="$save_CFLAGS"])
+  if test $target_ilp32 = yes; then
+    AC_DEFINE(TARGET_ILP32, 1,
+      [Define to 1 if the target is ILP32.])
+  fi
+  ])
diff --git a/libgfortran/config.h.in b/libgfortran/config.h.in
index d31c21b84c944de8de3b96c6d2a6674e0b49714e..db4da7aaed6abba7c2ff47dc2457879fc922af79 100644
--- a/libgfortran/config.h.in
+++ b/libgfortran/config.h.in
@@ -135,6 +135,9 @@
 /* libm includes sinhf */
 #undef HAVE_SINHF
 
+/* Define to 1 if you have the `snprintf' function. */
+#undef HAVE_SNPRINTF
+
 /* libm includes sqrtf */
 #undef HAVE_SQRTF
 
@@ -234,6 +237,9 @@
 /* Define to 1 if you have the ANSI C header files. */
 #undef STDC_HEADERS
 
+/* Define to 1 if the target is ILP32. */
+#undef TARGET_ILP32
+
 /* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
 #undef TIME_WITH_SYS_TIME
 
diff --git a/libgfortran/configure b/libgfortran/configure
index e486b4e9ec8d1934894b3d8c9e025e9e1ac45b98..0e738dfde89173461276323ea6b403ed15ae391a 100755
--- a/libgfortran/configure
+++ b/libgfortran/configure
@@ -6822,7 +6822,8 @@ fi
 
 
 
-for ac_func in getrusage times mkstemp strtof
+
+for ac_func in getrusage times mkstemp strtof snprintf
 do
 as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
 echo "$as_me:$LINENO: checking for $ac_func" >&5
@@ -10716,6 +10717,86 @@ _ACEOF
     fi
   fi
 
+# Attempt to assert that the target is of common type in case we don't
+# have C99 integer types at all.
+
+  echo "$as_me:$LINENO: checking whether the target is ILP32" >&5
+echo $ECHO_N "checking whether the target is ILP32... $ECHO_C" >&6
+if test "${target_ilp32+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+
+  save_CFLAGS="$CFLAGS"
+  CFLAGS="-O2"
+  if test x$gcc_no_link = xyes; then
+  { { echo "$as_me:$LINENO: error: Link tests are not allowed after GCC_NO_EXECUTABLES." >&5
+echo "$as_me: error: Link tests are not allowed after GCC_NO_EXECUTABLES." >&2;}
+   { (exit 1); exit 1; }; }
+fi
+cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+
+int
+main ()
+{
+
+if (sizeof(int) == 4 && sizeof(long) == 4 && sizeof(void *) == 4)
+  ;
+else
+  undefined_function ();
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext conftest$ac_exeext
+if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5
+  (eval $ac_link) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+	 { ac_try='test -z "$ac_c_werror_flag"
+			 || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+	 { ac_try='test -s conftest$ac_exeext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  target_ilp32=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+target_ilp32=no
+fi
+rm -f conftest.err conftest.$ac_objext \
+      conftest$ac_exeext conftest.$ac_ext
+  CFLAGS="$save_CFLAGS"
+fi
+echo "$as_me:$LINENO: result: $target_ilp32" >&5
+echo "${ECHO_T}$target_ilp32" >&6
+  if test $target_ilp32 = yes; then
+
+cat >>confdefs.h <<\_ACEOF
+#define TARGET_ILP32 1
+_ACEOF
+
+  fi
+
+
 cat >confcache <<\_ACEOF
 # This file is a shell script that caches the results of configure
 # tests run on this system so they can be shared between configure
diff --git a/libgfortran/configure.ac b/libgfortran/configure.ac
index 3adcfd61c3d5756950567283d9c5edf834f7805d..f91b2604bc8aa3d72649d041927b786380d88443 100644
--- a/libgfortran/configure.ac
+++ b/libgfortran/configure.ac
@@ -159,7 +159,7 @@ AC_CHECK_HEADER([complex.h],[AC_DEFINE([HAVE_COMPLEX_H], [1], [complex.h exists]
 AC_CHECK_LIB([m],[csin],[need_math="no"],[need_math="yes"])
 
 # Check for library functions.
-AC_CHECK_FUNCS(getrusage times mkstemp strtof)
+AC_CHECK_FUNCS(getrusage times mkstemp strtof snprintf)
 
 # Check libc for getgid, getpid, getuid
 AC_CHECK_LIB([c],[getgid],[AC_DEFINE([HAVE_GETGID],[1],[libc includes getgid])])
@@ -226,6 +226,10 @@ AC_SUBST([MATH_OBJ],["$extra_math_obj"])
 # to call gettimeofday if we have it.
 LIBGFOR_GETTIMEOFDAY
 
+# Attempt to assert that the target is of common type in case we don't
+# have C99 integer types at all.
+LIBGFOR_TARGET_ILP32
+
 AC_CACHE_SAVE
 
 if test ${multilib} = yes; then
diff --git a/libgfortran/intrinsics/date_and_time.c b/libgfortran/intrinsics/date_and_time.c
index 63d18f5cb2d57c2b9079f4893012593d4a38c3dd..e0b01234556cea68f95363c928f88bd6ff24f9e6 100644
--- a/libgfortran/intrinsics/date_and_time.c
+++ b/libgfortran/intrinsics/date_and_time.c
@@ -182,20 +182,35 @@ date_and_time (char *__date,
 
   if (__date)
     {
+#if HAVE_SNPRINTF
       snprintf (date, DATE_LEN + 1, "%04d%02d%02d",
 		values[0], values[1], values[2]);
+#else
+      sprintf (date, "%04d%02d%02d",
+	       values[0], values[1], values[2]);
+#endif
     }
 
   if (__time)
     {
+#if HAVE_SNPRINTF
       snprintf (timec, TIME_LEN + 1, "%02d%02d%02d.%03d",
 		values[4], values[5], values[6], values[7]);
+#else
+      sprintf (timec, "%02d%02d%02d.%03d",
+	       values[4], values[5], values[6], values[7]);
+#endif
     }
 
   if (__zone)
     {
+#if HAVE_SNPRINTF
       snprintf (zone, ZONE_LEN + 1, "%+03d%02d",
 		values[3] / 60, abs (values[3] % 60));
+#else
+      sprintf (zone, "%+03d%02d",
+	       values[3] / 60, abs (values[3] % 60));
+#endif
     }
 #else /* if defined HAVE_NO_DATE_TIME  */
   /* We really have *nothing* to return, so return blanks and HUGE(0).  */
diff --git a/libgfortran/io/write.c b/libgfortran/io/write.c
index f98ec1f1f36196203da9a6bbf89dd18db4b69537..a121a8f57aad1b7662520e208ecd2c6674c6e0ad 100644
--- a/libgfortran/io/write.c
+++ b/libgfortran/io/write.c
@@ -615,7 +615,11 @@ output_float (fnode *f, double value, int len)
 	  *(out++) = expchar;
 	  edigits--;
 	}
+#if HAVE_SNPRINTF
       snprintf (buffer, 32, "%+0*d", edigits, e);
+#else
+      sprintf (buffer, "%+0*d", edigits, e);
+#endif
       memcpy (out, buffer, edigits);
     }
 }
diff --git a/libgfortran/libgfortran.h b/libgfortran/libgfortran.h
index bbe6824770675e34619b74bed6c7bcaba729a887..e73c00a480cb6fb50d9aa8c406ab0dafa9b5380c 100644
--- a/libgfortran/libgfortran.h
+++ b/libgfortran/libgfortran.h
@@ -47,6 +47,17 @@ Boston, MA 02111-1307, USA.  */
 #include <inttypes.h>
 #endif
 
+#if !defined(HAVE_STDINT_H) && !defined(HAVE_INTTYPES_H) && defined(TARGET_ILP32)
+typedef char int8_t;
+typedef short int16_t;
+typedef int int32_t;
+typedef long long int64_t;
+typedef unsigned char uint8_t;
+typedef unsigned short uint16_t;
+typedef unsigned int uint32_t;
+typedef unsigned long long uint64_t;
+#endif
+
 #if HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif