diff mbox

[U-Boot,PATCHv3,5/8] mkenvimage: Read/Write from/to stdin/out by default or if the filename is "-"

Message ID 1325789099-9260-5-git-send-email-david.wagner@free-electrons.com
State Superseded
Headers show

Commit Message

David Wagner Jan. 5, 2012, 6:44 p.m. UTC
Signed-off-by: David Wagner <david.wagner@free-electrons.com>
---
 tools/mkenvimage.c |   26 +++++++++++++-------------
 1 files changed, 13 insertions(+), 13 deletions(-)

Comments

Mike Frysinger Jan. 8, 2012, 6:50 a.m. UTC | #1
On Thursday 05 January 2012 13:44:56 David Wagner wrote:
> +		bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
> +					     S_IWGRP);

this should prob be open()
-mike
David Wagner Jan. 8, 2012, 12:02 p.m. UTC | #2
Le 08/01/2012 07:50, Mike Frysinger a écrit :
> On Thursday 05 January 2012 13:44:56 David Wagner wrote:
>> +		bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
>> +					     S_IWGRP);
>
> this should prob be open()
> -mike

What is wrong with creat() ?
from 'man 2 creat': creat() is equivalent to open() with flags equal to 
O_CREAT|O_WRONLY|O_TRUNC.
We want to create the file if it doesn't exist and truncate it if it does.

The only issue I can think of is that no additional flag can be passed 
to creat(); fcntl can alter some of them.  But will we need any ?

Regards,
David.
Mike Frysinger Jan. 8, 2012, 7:29 p.m. UTC | #3
On Sunday 08 January 2012 07:02:40 David Wagner wrote:
> Le 08/01/2012 07:50, Mike Frysinger a écrit :
> > On Thursday 05 January 2012 13:44:56 David Wagner wrote:
> >> +		bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
> >> +					     S_IWGRP);
> > 
> > this should prob be open()
> 
> What is wrong with creat() ?

nothings wrong with it per-say ... just unusual ;).  if you really want to 
keep it, then it's fine.
-mike
diff mbox

Patch

diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c
index eb9a8f2..6db2b21 100644
--- a/tools/mkenvimage.c
+++ b/tools/mkenvimage.c
@@ -173,15 +173,9 @@  int main(int argc, char **argv)
 	memset(envptr, padbyte, envsize);
 
 	/* Open the input file ... */
-	if (optind >= argc) {
-		fprintf(stderr, "Please specify an input filename\n");
-		return EXIT_FAILURE;
-	}
-
-	txt_filename = argv[optind];
-	if (strcmp(txt_filename, "-") == 0) {
+	if (optind >= argc || strcmp(argv[optind], "-") == 0) {
 		int readbytes = 0;
-		int readlen = sizeof(*envptr) * 2048;
+		int readlen = sizeof(*envptr) * 4096;
 		txt_fd = STDIN_FILENO;
 
 		do {
@@ -199,6 +193,7 @@  int main(int argc, char **argv)
 		} while (readbytes == readlen);
 
 	} else {
+		txt_filename = argv[optind];
 		txt_fd = open(txt_filename, O_RDONLY);
 		if (txt_fd == -1) {
 			fprintf(stderr, "Can't open \"%s\": %s\n",
@@ -288,11 +283,16 @@  int main(int argc, char **argv)
 
 	memcpy(dataptr, &targetendian_crc, sizeof(uint32_t));
 
-	bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
-	if (bin_fd == -1) {
-		fprintf(stderr, "Can't open output file \"%s\": %s\n",
-				bin_filename, strerror(errno));
-		return EXIT_FAILURE;
+	if (!bin_filename || strcmp(bin_filename, "-") == 0) {
+		bin_fd = STDOUT_FILENO;
+	} else {
+		bin_fd = creat(bin_filename, S_IRUSR | S_IWUSR | S_IRGRP |
+					     S_IWGRP);
+		if (bin_fd == -1) {
+			fprintf(stderr, "Can't open output file \"%s\": %s\n",
+					bin_filename, strerror(errno));
+			return EXIT_FAILURE;
+		}
 	}
 
 	if (write(bin_fd, dataptr, sizeof(*dataptr) * datasize) !=