On Wed, Jul 20, 2011 at 6:57 PM, Mike Frysinger <[email protected]> wrote:
> On Wed, Jul 20, 2011 at 17:33, Jie Zhang wrote:
>> I thought about this. But I'm wondering if we really need those header
>> files. I don't know what system will reject "b" and if they are still
>> in use nowadays. So I think it will be safe to just always append "b"
>> for now until some complains. Actually I suspect it will happen.
>
> i'm fine with either
>
Here is a new patch. Compared to the previous one, I dropped 'B' from
FOPEN_R/W. I also changed conditionals for defining them, which I
think is better. It assumes three things:

1. All compilers for Windows should define _WIN32
2. No one will compile UrJTAG for DOS, which would also require "b"
3. On all other OSs "e" is defined as GLIBC or is ignored silently

I think all three assumptions are pretty safe. What do you think?

Jie
  * src/tap/detect.c (find_record): Use FOPEN_R or FOPEN_W for mode in fopen.
  * src/tap/cable/gpio.c (gpio_export): Likewise.
    (gpio_direction): Likewise.
  * src/tap/cable/ice100.c (ice_read_hex_file): Likewise.
  * src/bsdl/bsdl.c (urj_bsdl_read_file): Likewise.
  * src/bsdl/vhdl_flex.l (urj_vhdl_flex_switch_file): Likewise.
  * src/global/parse.c (urj_parse_file): Likewise.
  * src/cmd/cmd_bfin.c (cmd_bfin_run): Likewise.
  * src/cmd/cmd_flashmem.c (cmd_flashmem_run): Likewise.
  * src/cmd/cmd_pld.c (cmd_pld_run): Likewise.
  * src/cmd/cmd_writemem.c (cmd_writemem_run): Likewise.
  * src/cmd/cmd_svf.c (cmd_svf_run): Likewise.
  * src/cmd/cmd_readmem.c (cmd_readmem_run): Likewise.
  * src/apps/bsdl2jtag/bsdl2jtag.c (main): Likewise.
  * sysdep.h (FOPEN_RB, FOPEN_WB): Define for GLIBC >= 2.7.

Index: src/bsdl/bsdl.c
===================================================================
--- src/bsdl/bsdl.c	(revision 1972)
+++ src/bsdl/bsdl.c	(working copy)
@@ -112,7 +112,7 @@ urj_bsdl_read_file (urj_chain_t *chain,
         jtag_ctrl.part = NULL;
     }
 
-    BSDL_File = fopen (BSDL_File_Name, "rb");
+    BSDL_File = fopen (BSDL_File_Name, FOPEN_R);
 
     urj_bsdl_msg (proc_mode, _("Reading file '%s'\n"), BSDL_File_Name);
 
Index: src/bsdl/vhdl_flex.l
===================================================================
--- src/bsdl/vhdl_flex.l	(revision 1972)
+++ src/bsdl/vhdl_flex.l	(working copy)
@@ -123,6 +123,11 @@ LEGAL NOTICES:
 
 %{
 /* Begin lex input specifications */
+#include <sysdep.h>
+/* Undef TRUE and FALSE to avoid redefinition error
+   when compiling for Windows host.  */
+#undef TRUE
+#undef FALSE
 
 #include <stdlib.h>
 #include <ctype.h>
@@ -506,7 +511,7 @@ urj_vhdl_flex_switch_file (yyscan_t scan
     extra = yyget_extra (scanner);
 
     /* file in current directory has precedence */
-    f = fopen (filename, "rb");
+    f = fopen (filename, FOPEN_R);
     if (!f)
     {
         const char *db_path = urj_get_data_dir ();
@@ -523,7 +528,7 @@ urj_vhdl_flex_switch_file (yyscan_t scan
             strcat (db_file, "bsdl");
             strcat (db_file, "/");
             strcat (db_file, filename);
-            f = fopen (db_file, "rb");
+            f = fopen (db_file, FOPEN_R);
 
             if (!f)
                 urj_bsdl_ftl_set (extra->proc_mode, URJ_ERROR_IO,
Index: src/tap/detect.c
===================================================================
--- src/tap/detect.c	(revision 1972)
+++ src/tap/detect.c	(working copy)
@@ -59,7 +59,7 @@ find_record (char *filename, urj_tap_reg
     urj_tap_register_t *tr;
     int r = 0;
 
-    file = fopen (filename, "rb");
+    file = fopen (filename, FOPEN_R);
     if (!file)
     {
         urj_log (URJ_LOG_LEVEL_ERROR, _("Unable to open file '%s'\n"), filename);
Index: src/tap/cable/gpio.c
===================================================================
--- src/tap/cable/gpio.c	(revision 1972)
+++ src/tap/cable/gpio.c	(working copy)
@@ -69,7 +69,7 @@ static int gpio_export (unsigned int gpi
     else
         fname = GPIO_UNEXPORT_PATH;
 
-    fp = fopen (fname, "w");
+    fp = fopen (fname, FOPEN_W);
     if (!fp)
     {
         urj_warning (_("%s: cannot open to (un)export GPIO %u\n"), fname, gpio);
@@ -92,7 +92,7 @@ static int gpio_direction (unsigned int
         "%sgpio%u/direction", GPIO_PATH, gpio);
     fname[sizeof (fname) - 1] = '\0';
 
-    fp = fopen (fname, "w");
+    fp = fopen (fname, FOPEN_W);
     if (!fp)
     {
         urj_warning (_("%s: cannot open to set direction\n"), fname);
Index: src/tap/cable/ice100.c
===================================================================
--- src/tap/cable/ice100.c	(revision 1972)
+++ src/tap/cable/ice100.c	(working copy)
@@ -429,7 +429,7 @@ ice_read_hex_file (const char *filename,
     struct flash_block *last_flash_block = NULL, *q;
     int base_address = 0;
 
-    hex_file = fopen (filename, "rbe");
+    hex_file = fopen (filename, FOPEN_R);
     if (!hex_file)
     {
         urj_error_IO_set (_("Unable to open file `%s'"), filename);
Index: src/global/parse.c
===================================================================
--- src/global/parse.c	(revision 1972)
+++ src/global/parse.c	(working copy)
@@ -225,7 +225,7 @@ urj_parse_file (urj_chain_t *chain, cons
     FILE *f;
     int go;
 
-    f = fopen (filename, "rb");
+    f = fopen (filename, FOPEN_R);
     if (!f)
     {
         urj_error_IO_set ("Cannot open file '%s' to parse", filename);
Index: src/apps/bsdl2jtag/bsdl2jtag.c
===================================================================
--- src/apps/bsdl2jtag/bsdl2jtag.c	(revision 1972)
+++ src/apps/bsdl2jtag/bsdl2jtag.c	(working copy)
@@ -22,8 +22,9 @@
  *
  */
 
-#include <stdio.h>
+#include <sysdep.h>
 
+#include <stdio.h>
 #include <urjtag/chain.h>
 #include <urjtag/log.h>
 
@@ -79,7 +80,7 @@ main (int argc, char *const argv[])
         return 1;
     }
 
-    jtag_file = fopen (argv[2], "wb");
+    jtag_file = fopen (argv[2], FOPEN_W);
     if (jtag_file == NULL)
     {
         printf ("Error: Can't open '%s' in write mode.\n", argv[2]);
Index: src/cmd/cmd_bfin.c
===================================================================
--- src/cmd/cmd_bfin.c	(revision 1972)
+++ src/cmd/cmd_bfin.c	(working copy)
@@ -350,7 +350,7 @@ cmd_bfin_run (urj_chain_t *chain, char *
                     }
 
                     /* Read the binary blob from the toolchain */
-                    fp = fopen (tmpfile, "rb");
+                    fp = fopen (tmpfile, FOPEN_R);
                     if (fp == NULL)
                         goto execute_cleanup;
 
Index: src/cmd/cmd_flashmem.c
===================================================================
--- src/cmd/cmd_flashmem.c	(revision 1972)
+++ src/cmd/cmd_flashmem.c	(working copy)
@@ -70,7 +70,7 @@ cmd_flashmem_run (urj_chain_t *chain, ch
     else
         noverify = 0;
 
-    f = fopen (params[2], "rb");
+    f = fopen (params[2], FOPEN_R);
     if (!f)
     {
         urj_error_IO_set (_("Unable to open file `%s'"), params[2]);
Index: src/cmd/cmd_pld.c
===================================================================
--- src/cmd/cmd_pld.c	(revision 1972)
+++ src/cmd/cmd_pld.c	(working copy)
@@ -66,7 +66,7 @@ cmd_pld_run (urj_chain_t *chain, char *p
             return URJ_STATUS_FAIL;
         }
 
-        if ((pld_file = fopen (params[2], "rb")) != NULL)
+        if ((pld_file = fopen (params[2], FOPEN_R)) != NULL)
         {
             result = urj_pld_configure (chain, pld_file);
             fclose (pld_file);
Index: src/cmd/cmd_writemem.c
===================================================================
--- src/cmd/cmd_writemem.c	(revision 1972)
+++ src/cmd/cmd_writemem.c	(working copy)
@@ -60,7 +60,7 @@ cmd_writemem_run (urj_chain_t *chain, ch
         || urj_cmd_get_number (params[2], &len) != URJ_STATUS_OK)
         return URJ_STATUS_FAIL;
 
-    f = fopen (params[3], "rb");
+    f = fopen (params[3], FOPEN_R);
     if (!f)
     {
         urj_error_IO_set (_("Unable to open file `%s'"), params[3]);
Index: src/cmd/cmd_svf.c
===================================================================
--- src/cmd/cmd_svf.c	(revision 1972)
+++ src/cmd/cmd_svf.c	(working copy)
@@ -78,7 +78,7 @@ cmd_svf_run (urj_chain_t *chain, char *p
     if (print_progress)
         urj_log_state.level = URJ_LOG_LEVEL_DETAIL;
 
-    if ((SVF_FILE = fopen (params[1], "rb")) != NULL)
+    if ((SVF_FILE = fopen (params[1], FOPEN_R)) != NULL)
     {
         result = urj_svf_run (chain, SVF_FILE, stop, ref_freq);
 
Index: src/cmd/cmd_readmem.c
===================================================================
--- src/cmd/cmd_readmem.c	(revision 1972)
+++ src/cmd/cmd_readmem.c	(working copy)
@@ -62,7 +62,7 @@ cmd_readmem_run (urj_chain_t *chain, cha
         || urj_cmd_get_number (params[2], &len) != URJ_STATUS_OK)
         return URJ_STATUS_FAIL;
 
-    f = fopen (params[3], "wb");
+    f = fopen (params[3], FOPEN_W);
     if (!f)
     {
         urj_error_IO_set (_("Unable to create file `%s'"), params[3]);
Index: sysdep.h
===================================================================
--- sysdep.h	(revision 1972)
+++ sysdep.h	(working copy)
@@ -89,4 +89,16 @@ extern ssize_t getline(char **line, size
 
 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
 
+/* "b" is needed for Windows host.  "e" is a GLIBC extension, but Windows
+   does not like it.  Most C libraries on UNIX-like systems hopefully
+   implement the same extension or just ignore it without causing any trouble.
+   So always appending "e" for them should be safe.  */
+#if defined(_WIN32)
+#define FOPEN_R  "rb"
+#define FOPEN_W  "wb"
+#else
+#define FOPEN_R  "re"
+#define FOPEN_W  "we"
+#endif
+
 #endif /* SYSDEP_H */
------------------------------------------------------------------------------
5 Ways to Improve & Secure Unified Communications
Unified Communications promises greater efficiencies for business. UC can 
improve internal communications as well as offer faster, more efficient ways
to interact with customers and streamline customer service. Learn more!
http://www.accelacomm.com/jaw/sfnl/114/51426253/
_______________________________________________
UrJTAG-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/urjtag-development

Reply via email to