chiark / gitweb /
[PATCH] volume-id build fix and update
[elogind.git] / klibc / klibc / include / stdio.h
index f57439f018a58f88b9f77cab7938d2242c39c69a..fba1e30746a9d33c0b153cf049b5e039209d608c 100644 (file)
 struct _IO_file;
 typedef struct _IO_file FILE;
 
-#define stdin  ((FILE *)0)
-#define stdout ((FILE *)1)
-#define stderr ((FILE *)2)
-
 #ifndef EOF
 # define EOF (-1)
 #endif
@@ -31,16 +27,25 @@ typedef struct _IO_file FILE;
 #define SEEK_CUR 1
 #define SEEK_END 2
 
+/*
+ * Convert between a FILE * and a file descriptor.  We don't actually
+ * have any in-memory data, so we just abuse the pointer itself to
+ * hold the data.  Note, however, that for file descriptors, -1 is
+ * error and 0 is a valid value; for FILE *, NULL (0) is error and
+ * non-NULL are valid.
+ */
 static __inline__ int fileno(FILE *__f)
 {
   /* This should really be intptr_t, but size_t should be the same size */
-  return (int)(size_t)__f;
+  return (int)(size_t)__f - 1;
 }
 
-static __inline__ FILE * __create_file(int __fd)
-{
-  return (FILE *)(size_t)__fd;
-}
+/* This is a macro so it can be used as initializer */
+#define __create_file(__fd) ((FILE *)(size_t)((__fd) + 1))
+
+#define stdin  __create_file(0)
+#define stdout __create_file(1)
+#define stderr __create_file(2)
 
 __extern FILE *fopen(const char *, const char *);