On Fri, Jan 01, 2016 at 02:19:16PM -0500, Michael Reed wrote:
> Hi,
> 
> I noticed that when doing `sed -i` on a file you don't have
> permission to read, the error message printed isn't very helpful:
> 
>   $ sed -i '/test/d' /var/log/Xorg.1.log.old
>   sed: /var/log/Xorg.1.log.old
> 
> The patch below seems to fix the issue for me:
> 
>   $ ./sed -i '/test/d' /var/log/Xorg.1.log.old
>   sed: /var/log/Xorg.1.log.old: Permission denied
> 
> Regards,
>   Michael
> 

I think you're right.  There's also an error message for unlink right
below the message you fixed that would also benefit from strerror.
I couldn't find any others.

How about:

Index: main.c
===================================================================
RCS file: /cvs/src/usr.bin/sed/main.c,v
retrieving revision 1.30
diff -u -p -r1.30 main.c
--- main.c      26 Oct 2015 22:22:56 -0000      1.30
+++ main.c      1 Jan 2016 19:46:05 -0000
@@ -374,10 +374,10 @@ mf_fgets(SPACE *sp, enum e_spflag spflag
                        if (len >= sizeof(tmpfname))
                                error(FATAL, "%s: name too long", fname);
                        if ((fd = mkstemp(tmpfname)) == -1)
-                               error(FATAL, "%s", fname);
+                               error(FATAL, "%s: %s", fname, strerror(errno));
                        if ((outfile = fdopen(fd, "w")) == NULL) {
                                unlink(tmpfname);
-                               error(FATAL, "%s", fname);
+                               error(FATAL, "%s: %s", fname, strerror(errno));
                        }
                        fchown(fileno(outfile), sb.st_uid, sb.st_gid);
                        fchmod(fileno(outfile), sb.st_mode & ALLPERMS);

Reply via email to