Re: [hackers] [sbase][PATCH] cp: add -i flag

From: NRK <nrk_AT_disroot.org>
Date: Wed, 16 Apr 2025 12:06:25 +0000

+ if (cp_iflag && access(s2, F_OK) == 0) {
+ weprintf("overwrite '%s'? ", s2);
+ int c = getchar();
+ if (c != 'y' && c != 'Y')
+ return 0;
+
+ while (getchar() != '\n');
+ }

The whole line needs to be read, even when the first char is `y|Y`.
Also, getchar may return EOF before newline leading to an infinite loop.
Suggestion (untested):
        
        int ans = getchar();
        for (int c = ans; c != '\n' && c != EOF;)
                c = getchar();
        if (ans != 'Y' && ans != 'y')
                return 0;


- NRK
Received on Wed Apr 16 2025 - 14:06:25 CEST

This archive was generated by hypermail 2.3.0 : Wed Apr 16 2025 - 14:12:39 CEST