As we know, gets() is unsafe, fgets() annoyingly retains the '\n', getline() is non-standard, and scanf() always stops at spaces. Many of us wind up rolling our own.
This one
1. always NUL-terminates
2. is efficient since there are only two tests in the main loop
3. discards everything until the end of the line if truncation occurs.
void
getstr(char *s, size_t siz)
{
int c;
char *max = s + siz - 1;
while ((c = getchar()) != '\n' && s < max)
*s++ = c;
*s = '\0';
while (c != '\n')
c = getchar();
}
>>7
Citation needed - lines are buffered by the terminal before being handed to the program (on <Enter>), so surely the terminal handles backspaces by itself.
>>6
'cause >>2's not a function, you retoid. And it doesn't handle the case where getchar returns EOF, you retoid. Back to retoid skool with you, you retoid.
Name:
Anonymous2013-03-14 16:15
kidz think stdin's guaranteed to be connected to KEYBOARDTERMINAL. hain't you rascals heard of PIPES? and REDIRECTS? and GRANDPA?
>>16
U DON'T. getchar RETURNZ EOF IF DA END-OF-FILE/ERROR INDICATOR IZ SET FOR stdin.
Name:
Anonymous2013-03-14 18:18
I WAZ ON THE STANDARD COMMITTEE BEFORE DEY THROUGH ME OUT FOR SHOUTING AT EVERYONE. TEN MINUTES ON THE STANDARD COMMITTEE N DEY THROW ME OUT LIKE DAT! CAN U BELIEVE IT?
Name:
Anonymous2013-03-14 18:25
HAVE YA SEEN DA CRAP DEY PUT IN DA NEWER VERSIONS OF THE STANDARD? I SHUDDA FORKED THE STANDARD BAK WEN I HAD A CHANCE.
Name:
Anonymous2013-03-14 18:37
AND Y DO U THINK DEANIS RICKY KIKKED DA BUKKET? DONT YA THINK IT'S STRANGE DAT HE DIED TWO DAYS THE STANDARD PASSED ITS FINAL DRAFT REVIEW? I KNOWN RICKY ALL MY LIFE, EVEN BAK FRUM WEN HE DESIGNED DA LANGUAGE, N I ALSO KNO WHY RICKY KIKKED DA BUKKET, IN FACT I WAS DER WHEN IT HAPPENED. WE WERE SITTIN AT HIS HOUSE N HE TURNED TO ME N SAID, "Lambda, as I watch these hyenas claw, tear, and butcher the language I designed with my bare hands, I begin to question why I should live any longer." N IT WAS AT DAT MOMENT DAT RICKY PULLED OUT A PISTOL N DID HIMSELF IN. I KNOW DA TRUTH COZ I WAS DER. DOSE FAT CATS AT ISO, DEYR DA KILLERS. DOSE SAME FUCKERS WHO THREW ME OFF DA COMMITTEE JUST CUZ I WAS SHOUTIN AT PEOPLE.
>>34
I don't think strtok works that way. It doesn't change the contents of buf at all but instead returns a char*. Also, make room for null terminator.
>>35 I don't think strtok works that way. It doesn't change the contents of buf at all but instead returns a char*.
IABT. strok replaces delimiters in the the target buf with nulls and returns a pointer to the start of each resulting token, one by one. Hence the supplied buf cannot be const and it's totally unsafe to share between threads.
Name:
Lambda Arthur Calculus2013-03-15 1:14
>>35
AINT RED DA STANDARD ON fgets >>34 if (fgets(buf, sizeof buf, stdin)) { buf[strcspn(buf, "\r\n")] = '\0'; ... }
>>35
Yaint thought bout wat happens wen fgets fills ur lil buffer with "\n". Aint gonna give ya an empty string, strtok. Not once, not twice, n certainly not thrice. >>34
Yaint thought bout wat a string is.
I was da guy who wrote strcspn n strtok. I kno wats wat. Though I didnt write fgets tho, dat was deanis. FUCKING POPPAVIC ATE HIS FUCKING CORPSE, THAT CANNIBAL.
Name:
432013-03-15 1:42
FUK I CONFUSED >>35 WITH >>34. EEEEEEEEEEEH EEEEEEEEEEEEEEEEEEH EEEEEEEEEEEEEEEEEEEEEH EEEEEEEEEEEEEEEEEEEEEEEEEEEEEH!!!!!!
Name:
Anonymous2013-03-15 1:44
cant believe dem ISO bastards threw me off da fuckin committee ...
Name:
Anonymous2013-03-15 1:49
I bet he's one of dem ISO-preaching hyenas, fuckin poppavic. wasn't enough 2 trash the language he loved, had to eat the poor guy's corpse too.
rsize_t -- wat da fuk is dat? aint seen stupid shit like dat back in C89.
Name:
Anonymous2013-03-15 2:46
woof it up sunday
Name:
Anonymous2013-03-15 3:47
My first reaction to rsize_t is oh boy! What's that! I can't wait to find out! Is it too late for me? Is this really the person I have become?
In the past I had to tell the compiler I needed some space for a structure. Terrible, because the compiler knows anything about the structure. Then I saw, this was because I created the struct dynamically.
C is a dynamic programming language, so the compiler cannot know the size of the structure in advance! Then I made a preprocessor, which transformed the structures in a const string and cast them to the structure I needed. I had created immutable data structures. So much fun I had. I could transform code in a static circuit! I never had segmentation faults, buffer overflows or other dynamic problems again.