util: Standardize console output in the m5 writefile command.
[gem5.git] / util / m5 / src / command / writefile.cc
index bef193296e9ba501ea73389a90e3c195585a3919..7771dfe0368f24cd6a345117a221cdedc18ffd17 100644 (file)
@@ -26,6 +26,8 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <errno.h>
+
 #include <cstring>
 #include <fstream>
 #include <iostream>
@@ -41,12 +43,13 @@ void
 write_file(const DispatchTable &dt, const std::string &filename,
            const std::string &host_filename)
 {
-    std::cerr << "opening " << filename << std::endl;
+    std::cout << "Opening \"" << filename << "\"." << std::endl;
     std::ifstream src(filename, std::ios_base::in | std::ios_base::binary);
 
     if (!src) {
-        std::cerr << "error opening " << filename << std::endl;
-        return;
+        std::cerr << "Error opening \"" << filename << "\": " <<
+            strerror(errno) << std::endl;
+        exit(2);
     }
 
     char buf[256 * 1024];
@@ -58,8 +61,11 @@ write_file(const DispatchTable &dt, const std::string &filename,
         src.seekg(offset);
         src.read(buf, sizeof(buf));
         int len = src.gcount();
-        if (!src && !src.eof())
-            break;
+        if (!src && !src.eof()) {
+            std::cerr << "Error reading \"" << filename << "\": " <<
+                strerror(errno) << std::endl;
+            exit(2);
+        }
         char *wbuf = buf;
         while (len) {
             int bytes = (*dt.m5_write_file)(
@@ -71,7 +77,7 @@ write_file(const DispatchTable &dt, const std::string &filename,
         if (src.eof())
             break;
     }
-    std::cerr << "Wrote " << offset << " bytes." << std::endl;
+    std::cout << "Wrote " << offset << " bytes." << std::endl;
 }
 
 bool