Portability improvements; SPARC64 fixes

diff --git a/ash/mystring.h b/ash/mystring.h
index 9620e7a..3e1baf2 100644
--- a/ash/mystring.h
+++ b/ash/mystring.h
@@ -37,33 +37,12 @@
  *	mystring.h,v 1.4 1993/08/01 18:58:39 mycroft Exp
  */
 
-#ifndef SYSV
-#define strchr mystrchr
-#endif
-
-#ifdef __STDC__
 void scopyn(const char *, char *, int);
 char *strchr(const char *, int);
 void mybcopy(const pointer, pointer, int);
 int prefix(const char *, const char *);
 int number(const char *);
 int is_number(const char *);
-int strcmp(const char *, const char *);	/* from C library */
-char *strcpy(char *, const char *);	/* from C library */
-int strlen(const char *);		/* from C library */
-char *strcat(char *, const char *);	/* from C library */
-#else
-void scopyn();
-char *strchr();
-void mybcopy();
-int prefix();
-int number();
-int is_number();
-int strcmp();
-char *strcpy();
-int strlen();
-char *strcat();
-#endif
 
 #define equal(s1, s2)	(strcmp(s1, s2) == 0)
 #define scopy(s1, s2)	((void)strcpy(s2, s1))
diff --git a/ash/parser.c b/ash/parser.c
index 3ee5514..fdd5638 100644
--- a/ash/parser.c
+++ b/ash/parser.c
@@ -95,25 +95,23 @@
 static const char types[] = "}-+?=";
 #endif
 
-#define STATIC static
-#define __P(X) X
-
-STATIC union node *list __P((int));
-STATIC union node *andor __P((void));
-STATIC union node *pipeline __P((void));
-STATIC union node *command __P((void));
-STATIC union node *simplecmd __P((union node **, union node *));
-STATIC void parsefname __P((void));
-STATIC void parseheredoc __P((void));
-STATIC int readtoken __P((void));
-STATIC int readtoken1 __P((int, char const *, char *, int));
-STATIC void attyline __P((void));
-STATIC int noexpand __P((char *));
-STATIC void synexpect __P((int));
-STATIC void synerror __P((char *));
+STATIC union node *list (int);
+STATIC union node *andor (void);
+STATIC union node *pipeline (void);
+STATIC union node *command (void);
+STATIC union node *simplecmd (union node **, union node *);
+STATIC void parsefname (void);
+STATIC void parseheredoc (void);
+STATIC int readtoken (void);
+STATIC int readtoken1 (int, char const *, char *, int);
+STATIC int peektoken(void);
+STATIC void attyline (void);
+STATIC int noexpand (char *);
+STATIC void synexpect (int);
+STATIC void synerror (char *);
 
 #if ATTY
-STATIC void putprompt __P((char *));
+STATIC void putprompt (char *);
 #else /* not ATTY */
 #define putprompt(s)	out2str(s)
 #endif
diff --git a/ash/syntax.h b/ash/syntax.h
index 9ab45d1..55d6b80 100644
--- a/ash/syntax.h
+++ b/ash/syntax.h
@@ -36,7 +36,7 @@
 #define DQSYNTAX (dqsyntax + SYNBASE)
 #define SQSYNTAX (sqsyntax + SYNBASE)
 
-#define is_digit(c)	((unsigned)((c) - '0') <= 9)
+#define is_digit(c)	((unsigned char)((c) - '0') <= 9)
 #define is_alpha(c)	((is_type+SYNBASE)[c] & (ISUPPER|ISLOWER))
 #define is_name(c)	((is_type+SYNBASE)[c] & (ISUPPER|ISLOWER|ISUNDER))
 #define is_in_name(c)	((is_type+SYNBASE)[c] & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))
diff --git a/include/arch/sparc64/klibc/archsys.h b/include/arch/sparc64/klibc/archsys.h
index 7a285eb..651e4f7 100644
--- a/include/arch/sparc64/klibc/archsys.h
+++ b/include/arch/sparc64/klibc/archsys.h
@@ -7,34 +7,151 @@
 #ifndef _KLIBC_ARCHSYS_H
 #define _KLIBC_ARCHSYS_H
 
-/* SPARC64 seems to lack _syscall6() in its headers */
+/* The Linux 2.5.31 SPARC64 syscall macros are just plain broken */
 
-#ifndef _syscall6
+#undef _syscall0
+#undef _syscall1
+#undef _syscall2
+#undef _syscall3
+#undef _syscall4
+#undef _syscall5
+#undef _syscall6
+
+#define _syscall0(type,name) \
+type name (void) \
+{ \
+  register unsigned long __g1 __asm__ ("g1") = __NR_##name; \
+  register unsigned long __ret __asm__("o0"); \
+  __asm__ __volatile__ ("t 0x6d\n\t" \
+      "bcs,a %%xcc, 1f\n\t" \
+      " st %0,%1\n\t" \
+      "1:" \
+      "movcs %%xcc,-1,%0\n" \
+      : "=&r" (__ret), "+m" (errno) \
+      : "r" (__g1) \
+      : "cc"); \
+  return (type) __ret; \
+}
+
+#define _syscall1(type,name,type1,arg1) \
+type name (type1 arg1) \
+{ \
+  register unsigned long __g1 __asm__ ("g1") = __NR_##name; \
+  register unsigned long __ret __asm__("o0"); \
+  type1 __o0 = (arg1); \
+  __asm__ __volatile__ ("t 0x6d\n\t" \
+      "bcs,a %%xcc, 1f\n\t" \
+      " st %0,%1\n\t" \
+      "1:" \
+      "movcs %%xcc,-1,%0\n" \
+      : "=&r" (__ret), "+m" (errno) \
+      : "0" (__o0), "r" (__g1) \
+      : "cc"); \
+  return (type) __ret; \
+}
+
+#define _syscall2(type,name,type1,arg1,type2,arg2) \
+type name (type1 arg1,type2 arg2) \
+{ \
+  register unsigned long __g1 __asm__ ("g1") = __NR_##name; \
+  register unsigned long __ret __asm__("o0"); \
+  type1 __o0 = (arg1); \
+  register type2 __o1 __asm__ ("o1") = (arg2); \
+  __asm__ __volatile__ ("t 0x6d\n\t" \
+      "bcs,a %%xcc, 1f\n\t" \
+      " st %0,%1\n\t" \
+      "1:" \
+      "movcs %%xcc,-1,%0\n" \
+      : "=&r" (__ret), "+m" (errno) \
+      : "0" (__o0), "r" (__o1), "r" (__g1) \
+      : "cc"); \
+  return (type) __ret; \
+}
+
+#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
+type name (type1 arg1,type2 arg2,type3 arg3) \
+{ \
+  register unsigned long __g1 __asm__ ("g1") = __NR_##name; \
+  register unsigned long __ret __asm__("o0"); \
+  type1 __o0 = (arg1); \
+  register type2 __o1 __asm__ ("o1") = (arg2); \
+  register type3 __o2 __asm__ ("o2") = (arg3); \
+  __asm__ __volatile__ ("t 0x6d\n\t" \
+      "bcs,a %%xcc, 1f\n\t" \
+      " st %0,%1\n\t" \
+      "1:" \
+      "movcs %%xcc,-1,%0\n" \
+      : "=&r" (__ret), "+m" (errno) \
+      : "0" (__o0), "r" (__o1), "r" (__o2), "r" (__g1) \
+      : "cc"); \
+  return (type) __ret; \
+}
+
+#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
+type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
+{ \
+  register unsigned long __g1 __asm__ ("g1") = __NR_##name; \
+  register unsigned long __ret __asm__("o0"); \
+  type1 __o0 = (arg1); \
+  register type2 __o1 __asm__ ("o1") = (arg2); \
+  register type3 __o2 __asm__ ("o2") = (arg3); \
+  register type4 __o3 __asm__ ("o3") = (arg4); \
+  __asm__ __volatile__ ("t 0x6d\n\t" \
+      "bcs,a %%xcc, 1f\n\t" \
+      " st %0,%1\n\t" \
+      "1:" \
+      "movcs %%xcc,-1,%0\n" \
+      : "=&r" (__ret), "+m" (errno) \
+      : "0" (__o0), "r" (__o1), "r" (__o2), "r" (__o3), "r" (__g1) \
+      : "cc"); \
+  return (type) __ret; \
+}
+
+#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,\
+  type5,arg5) \
+type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
+{ \
+  register unsigned long __g1 __asm__ ("g1") = __NR_##name; \
+  register unsigned long __ret __asm__("o0"); \
+  type1 __o0 = (arg1); \
+  register type2 __o1 __asm__ ("o1") = (arg2); \
+  register type3 __o2 __asm__ ("o2") = (arg3); \
+  register type4 __o3 __asm__ ("o3") = (arg4); \
+  register type5 __o4 __asm__ ("o4") = (arg5); \
+  __asm__ __volatile__ ("t 0x6d\n\t" \
+      "bcs,a %%xcc, 1f\n\t" \
+      " st %0,%1\n\t" \
+      "1:" \
+      "movcs %%xcc,-1,%0\n" \
+      : "=&r" (__ret), "+m" (errno) \
+      : "0" (__o0), "r" (__o1), "r" (__o2), "r" (__o3), "r" (__o4), \
+        "r" (__g1) \
+      : "cc"); \
+  return (type) __ret; \
+}
 
 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
   type5,arg5,type6,arg6) \
 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
 { \
-long __res; \
-register long __g1 __asm__ ("g1") = __NR_##name; \
-register long __o0 __asm__ ("o0") = (long)(arg1); \
-register long __o1 __asm__ ("o1") = (long)(arg2); \
-register long __o2 __asm__ ("o2") = (long)(arg3); \
-register long __o3 __asm__ ("o3") = (long)(arg4); \
-register long __o4 __asm__ ("o4") = (long)(arg5); \
-register long __o5 __asm__ ("o5") = (long)(arg6); \
-__asm__ __volatile__ ("t 0x6d\n\t" \
-      "sub %%g0, %%o0, %0\n\t" \
-      "movcc %%xcc, %%o0, %0\n\t" \
-      : "=r" (__res), "=&r" (__o0) \
-      : "1" (__o0), "r" (__o1), "r" (__o2), "r" (__o3), "r" (__o4), "r" (__o5), "r" (__g1) \
+  register unsigned long __g1 __asm__ ("g1") = __NR_##name; \
+  register unsigned long __ret __asm__("o0"); \
+  type1 __o0 = (arg1); \
+  register type2 __o1 __asm__ ("o1") = (arg2); \
+  register type3 __o2 __asm__ ("o2") = (arg3); \
+  register type4 __o3 __asm__ ("o3") = (arg4); \
+  register type5 __o4 __asm__ ("o4") = (arg5); \
+  register type6 __o5 __asm__ ("o5") = (arg6); \
+  __asm__ __volatile__ ("t 0x6d\n\t" \
+      "bcs,a %%xcc, 1f\n\t" \
+      " st %0,%1\n\t" \
+      "1:" \
+      "movcs %%xcc,-1,%0\n" \
+      : "=&r" (__ret), "+m" (errno) \
+      : "0" (__o0), "r" (__o1), "r" (__o2), "r" (__o3), "r" (__o4), \
+        "r" (__o5), "r" (__g1) \
       : "cc"); \
-if (__res>=0) \
-return (type) __res; \
-errno = -__res; \
-return (type)-1; \
+  return (type) __ret; \
 }
 
-#endif /* _syscall6 missing */
-
 #endif /* _KLIBC_ARCHSYS_H */
diff --git a/include/sys/stat.h b/include/sys/stat.h
index 4db6658..f7bb5aa 100644
--- a/include/sys/stat.h
+++ b/include/sys/stat.h
@@ -6,8 +6,8 @@
 #define _SYS_STAT_H
 
 #include <klibc/extern.h>
-#include <asm/stat.h>
 #include <sys/types.h>
+#include <asm/stat.h>
 #include <linux/stat.h>
 
 __extern int stat(const char *, struct stat *);
diff --git a/include/utime.h b/include/utime.h
index 1453696..2bc1d99 100644
--- a/include/utime.h
+++ b/include/utime.h
@@ -10,4 +10,5 @@
 
 __extern int utime(char *, struct utimbuf *);
 
-#endif _UTIME_H
+#endif /* _UTIME_H */
+
diff --git a/klibc/arch/sparc64/include/klibc/archsys.h b/klibc/arch/sparc64/include/klibc/archsys.h
index 7a285eb..651e4f7 100644
--- a/klibc/arch/sparc64/include/klibc/archsys.h
+++ b/klibc/arch/sparc64/include/klibc/archsys.h
@@ -7,34 +7,151 @@
 #ifndef _KLIBC_ARCHSYS_H
 #define _KLIBC_ARCHSYS_H
 
-/* SPARC64 seems to lack _syscall6() in its headers */
+/* The Linux 2.5.31 SPARC64 syscall macros are just plain broken */
 
-#ifndef _syscall6
+#undef _syscall0
+#undef _syscall1
+#undef _syscall2
+#undef _syscall3
+#undef _syscall4
+#undef _syscall5
+#undef _syscall6
+
+#define _syscall0(type,name) \
+type name (void) \
+{ \
+  register unsigned long __g1 __asm__ ("g1") = __NR_##name; \
+  register unsigned long __ret __asm__("o0"); \
+  __asm__ __volatile__ ("t 0x6d\n\t" \
+      "bcs,a %%xcc, 1f\n\t" \
+      " st %0,%1\n\t" \
+      "1:" \
+      "movcs %%xcc,-1,%0\n" \
+      : "=&r" (__ret), "+m" (errno) \
+      : "r" (__g1) \
+      : "cc"); \
+  return (type) __ret; \
+}
+
+#define _syscall1(type,name,type1,arg1) \
+type name (type1 arg1) \
+{ \
+  register unsigned long __g1 __asm__ ("g1") = __NR_##name; \
+  register unsigned long __ret __asm__("o0"); \
+  type1 __o0 = (arg1); \
+  __asm__ __volatile__ ("t 0x6d\n\t" \
+      "bcs,a %%xcc, 1f\n\t" \
+      " st %0,%1\n\t" \
+      "1:" \
+      "movcs %%xcc,-1,%0\n" \
+      : "=&r" (__ret), "+m" (errno) \
+      : "0" (__o0), "r" (__g1) \
+      : "cc"); \
+  return (type) __ret; \
+}
+
+#define _syscall2(type,name,type1,arg1,type2,arg2) \
+type name (type1 arg1,type2 arg2) \
+{ \
+  register unsigned long __g1 __asm__ ("g1") = __NR_##name; \
+  register unsigned long __ret __asm__("o0"); \
+  type1 __o0 = (arg1); \
+  register type2 __o1 __asm__ ("o1") = (arg2); \
+  __asm__ __volatile__ ("t 0x6d\n\t" \
+      "bcs,a %%xcc, 1f\n\t" \
+      " st %0,%1\n\t" \
+      "1:" \
+      "movcs %%xcc,-1,%0\n" \
+      : "=&r" (__ret), "+m" (errno) \
+      : "0" (__o0), "r" (__o1), "r" (__g1) \
+      : "cc"); \
+  return (type) __ret; \
+}
+
+#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
+type name (type1 arg1,type2 arg2,type3 arg3) \
+{ \
+  register unsigned long __g1 __asm__ ("g1") = __NR_##name; \
+  register unsigned long __ret __asm__("o0"); \
+  type1 __o0 = (arg1); \
+  register type2 __o1 __asm__ ("o1") = (arg2); \
+  register type3 __o2 __asm__ ("o2") = (arg3); \
+  __asm__ __volatile__ ("t 0x6d\n\t" \
+      "bcs,a %%xcc, 1f\n\t" \
+      " st %0,%1\n\t" \
+      "1:" \
+      "movcs %%xcc,-1,%0\n" \
+      : "=&r" (__ret), "+m" (errno) \
+      : "0" (__o0), "r" (__o1), "r" (__o2), "r" (__g1) \
+      : "cc"); \
+  return (type) __ret; \
+}
+
+#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
+type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
+{ \
+  register unsigned long __g1 __asm__ ("g1") = __NR_##name; \
+  register unsigned long __ret __asm__("o0"); \
+  type1 __o0 = (arg1); \
+  register type2 __o1 __asm__ ("o1") = (arg2); \
+  register type3 __o2 __asm__ ("o2") = (arg3); \
+  register type4 __o3 __asm__ ("o3") = (arg4); \
+  __asm__ __volatile__ ("t 0x6d\n\t" \
+      "bcs,a %%xcc, 1f\n\t" \
+      " st %0,%1\n\t" \
+      "1:" \
+      "movcs %%xcc,-1,%0\n" \
+      : "=&r" (__ret), "+m" (errno) \
+      : "0" (__o0), "r" (__o1), "r" (__o2), "r" (__o3), "r" (__g1) \
+      : "cc"); \
+  return (type) __ret; \
+}
+
+#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,\
+  type5,arg5) \
+type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
+{ \
+  register unsigned long __g1 __asm__ ("g1") = __NR_##name; \
+  register unsigned long __ret __asm__("o0"); \
+  type1 __o0 = (arg1); \
+  register type2 __o1 __asm__ ("o1") = (arg2); \
+  register type3 __o2 __asm__ ("o2") = (arg3); \
+  register type4 __o3 __asm__ ("o3") = (arg4); \
+  register type5 __o4 __asm__ ("o4") = (arg5); \
+  __asm__ __volatile__ ("t 0x6d\n\t" \
+      "bcs,a %%xcc, 1f\n\t" \
+      " st %0,%1\n\t" \
+      "1:" \
+      "movcs %%xcc,-1,%0\n" \
+      : "=&r" (__ret), "+m" (errno) \
+      : "0" (__o0), "r" (__o1), "r" (__o2), "r" (__o3), "r" (__o4), \
+        "r" (__g1) \
+      : "cc"); \
+  return (type) __ret; \
+}
 
 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
   type5,arg5,type6,arg6) \
 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
 { \
-long __res; \
-register long __g1 __asm__ ("g1") = __NR_##name; \
-register long __o0 __asm__ ("o0") = (long)(arg1); \
-register long __o1 __asm__ ("o1") = (long)(arg2); \
-register long __o2 __asm__ ("o2") = (long)(arg3); \
-register long __o3 __asm__ ("o3") = (long)(arg4); \
-register long __o4 __asm__ ("o4") = (long)(arg5); \
-register long __o5 __asm__ ("o5") = (long)(arg6); \
-__asm__ __volatile__ ("t 0x6d\n\t" \
-      "sub %%g0, %%o0, %0\n\t" \
-      "movcc %%xcc, %%o0, %0\n\t" \
-      : "=r" (__res), "=&r" (__o0) \
-      : "1" (__o0), "r" (__o1), "r" (__o2), "r" (__o3), "r" (__o4), "r" (__o5), "r" (__g1) \
+  register unsigned long __g1 __asm__ ("g1") = __NR_##name; \
+  register unsigned long __ret __asm__("o0"); \
+  type1 __o0 = (arg1); \
+  register type2 __o1 __asm__ ("o1") = (arg2); \
+  register type3 __o2 __asm__ ("o2") = (arg3); \
+  register type4 __o3 __asm__ ("o3") = (arg4); \
+  register type5 __o4 __asm__ ("o4") = (arg5); \
+  register type6 __o5 __asm__ ("o5") = (arg6); \
+  __asm__ __volatile__ ("t 0x6d\n\t" \
+      "bcs,a %%xcc, 1f\n\t" \
+      " st %0,%1\n\t" \
+      "1:" \
+      "movcs %%xcc,-1,%0\n" \
+      : "=&r" (__ret), "+m" (errno) \
+      : "0" (__o0), "r" (__o1), "r" (__o2), "r" (__o3), "r" (__o4), \
+        "r" (__o5), "r" (__g1) \
       : "cc"); \
-if (__res>=0) \
-return (type) __res; \
-errno = -__res; \
-return (type)-1; \
+  return (type) __ret; \
 }
 
-#endif /* _syscall6 missing */
-
 #endif /* _KLIBC_ARCHSYS_H */
diff --git a/klibc/include/sys/stat.h b/klibc/include/sys/stat.h
index 4db6658..f7bb5aa 100644
--- a/klibc/include/sys/stat.h
+++ b/klibc/include/sys/stat.h
@@ -6,8 +6,8 @@
 #define _SYS_STAT_H
 
 #include <klibc/extern.h>
-#include <asm/stat.h>
 #include <sys/types.h>
+#include <asm/stat.h>
 #include <linux/stat.h>
 
 __extern int stat(const char *, struct stat *);
diff --git a/klibc/include/utime.h b/klibc/include/utime.h
index 1453696..2bc1d99 100644
--- a/klibc/include/utime.h
+++ b/klibc/include/utime.h
@@ -10,4 +10,5 @@
 
 __extern int utime(char *, struct utimbuf *);
 
-#endif _UTIME_H
+#endif /* _UTIME_H */
+