gets() has been removed from C11 and on FreeBSD 13 libc no longer provides
it by default. Use fgets() instead.
Details
Diff Detail
- Repository
- rT test-suite
- Build Status
Buildable 118519 Build 171037: arc lint + arc unit
Event Timeline
MultiSource/Benchmarks/DOE-ProxyApps-C/Pathfinder/main.c | ||
---|---|---|
316 | gets always removes the newline, but fgets will include it (except in the case where the input reaches EOF before one is found) so the final character needs to be removed if it's \n. |
MultiSource/Benchmarks/DOE-ProxyApps-C/Pathfinder/main.c | ||
---|---|---|
316 | Can you introduce mygets that implements gets_s internally? |
MultiSource/Benchmarks/DOE-ProxyApps-C/Pathfinder/main.c | ||
---|---|---|
316 | I could do, but that seems like overkill for the one use of gets(). |
MultiSource/Benchmarks/DOE-ProxyApps-C/Pathfinder/main.c | ||
---|---|---|
316 | static char *mygets(char *str, size_t n) { char *ret; size_t last; if ((ret = fgets(str, n, stdin)) != NULL) { last = strlen(str) - 1; if (str[last] == '\n' || str[last] == '\r' ) str[last] = '\0'; } return ret; } And then use mygets(stringBuffer, sizeof(stringBuffer)); It looks nicer to me. |
MultiSource/Benchmarks/DOE-ProxyApps-C/Pathfinder/main.c | ||
---|---|---|
316 | That will not work with zero-length strings (it will access str[-1]), Adding stringBuffer[strcspn(stringBuffer, "\r\n")] = '\0'; seems much simpler to me. |
MultiSource/Benchmarks/DOE-ProxyApps-C/Pathfinder/main.c | ||
---|---|---|
316 | Also the \r check is misguided. In text mode, line endings get normalised to just \n. In binary mode they will stay as-is, but on Windows that means you have \r\n (and \r on classic Mac OS 9 and below I guess) so the if would need to be a while and last decremented. But stdin should be in text mode by default. |
MultiSource/Benchmarks/DOE-ProxyApps-C/Pathfinder/main.c | ||
---|---|---|
316 | strcspn() returns the length up to the first occurrence of \r or \n, so it should work? |
MultiSource/Benchmarks/DOE-ProxyApps-C/Pathfinder/main.c | ||
---|---|---|
316 | Yeah, that avoids any such complications. Technically the \r can just be dropped from it I believe but it does no harm (well unless people feed in \r on systems with Unix line endings I guess...). |
MultiSource/Benchmarks/DOE-ProxyApps-C/Pathfinder/main.c | ||
---|---|---|
316 |
How so? fgets returns NULL for 0-length strings. I find this switch more elegant with gets_s()-like code, but this is a matter of taste. |
MultiSource/Benchmarks/DOE-ProxyApps-C/Pathfinder/main.c | ||
---|---|---|
316 | I'd prefer gets_s, but we can't assume that the platform provides it. using fgets+ strcspn is only one additional line and supported everywhere so it's IMO the simpliest solution. If there were more uses of gets(), then a wrapper might make sense. |
MultiSource/Benchmarks/DOE-ProxyApps-C/Pathfinder/main.c | ||
---|---|---|
316 |
As I proposed, including it inline as mygets or similar, as almost no platforms delivers it.
In my personal taste open-coding this looks awful, but this is up to you. If the LOC would matter in my proposal, mygets could be compressed to 2 lines too.
This is fine. |
MultiSource/Benchmarks/DOE-ProxyApps-C/Pathfinder/main.c | ||
---|---|---|
290 |
|