[openfirmware] r915 - clients/lib

svn at openfirmware.info svn at openfirmware.info
Tue Sep 2 23:18:02 CEST 2008


Author: wmb
Date: 2008-09-02 23:18:01 +0200 (Tue, 02 Sep 2008)
New Revision: 915

Modified:
   clients/lib/lib.c
   clients/lib/stdio.h
Log:
Client library stdio - pay attention to "read only" open mode, and
don't flush the buffer if it's not dirty.



Modified: clients/lib/lib.c
===================================================================
--- clients/lib/lib.c	2008-09-02 05:46:14 UTC (rev 914)
+++ clients/lib/lib.c	2008-09-02 21:18:01 UTC (rev 915)
@@ -111,6 +111,8 @@
   }
   
  good:
+  fp->dirty = 0;
+  fp->readonly = (strcmp(mode, "r") == 0) || (strcmp(mode, "rb") == 0);
   fp->bufc = 0;
   return(fp);
 }
@@ -121,26 +123,33 @@
   return(0);	/* Implement me */
 }
 
-void
+int
 fputc(char c, FILE *fp)
 {
+  if (fp->readonly)
+    return -1;  // EOF
+
   if (fp == stdout && c == '\n')
-    fputc('\r', fp);
+    (void) fputc('\r', fp);
 
   fp->buf[fp->bufc++] = c;
+  fp->dirty = 1;
 
   if ((fp->bufc == 127) || (fp == stdout && c == '\n')) {
     OFWrite(fp->id, fp->buf, fp->bufc);
     fp->bufc = 0;
+    fp->dirty = 0;
   }
+  return (int)c;
 }
 
 void
 fflush (FILE *fp)
 {
-  if (fp->bufc != 0) {
+  if (fp->dirty && fp->bufc != 0) {
     OFWrite(fp->id, fp->buf, fp->bufc);
     fp->bufc = 0;
+    fp->dirty = 0;
   }
 }
 

Modified: clients/lib/stdio.h
===================================================================
--- clients/lib/stdio.h	2008-09-02 05:46:14 UTC (rev 914)
+++ clients/lib/stdio.h	2008-09-02 21:18:01 UTC (rev 915)
@@ -3,6 +3,8 @@
 typedef struct _file {
   long id;
   int  bufc;
+  int  readonly;
+  int  dirty;
   char *inbufp;
   char buf[128];
 } FILE;




More information about the openfirmware mailing list