Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[C] post your gets()

Name: Anonymous 2013-03-14 2:49

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();
}

Name: Anonymous 2013-03-14 3:09

OMG OPTIMIZED

while ((c = getchar() != '\n')
    if (s < max)
        *s++ = c;
*s = '\0'

Name: lambda arfer cack ya ass 2013-03-14 3:11

RYTE FRUM DA LAMBDA PAPERZ
int readline(char *s, size_t n)
{
    int c;

    n--;
    while ((c = getchar()) != EOF && c != '\n')
        if (n) {
            *s++ = c;
            n--;
        }
    *s = '\0';
    return c;
}

Name: THE LAMBDA PAPERS 2013-03-14 3:17

LIMITED EDITION:
int readline(char *s, size_t n)
{
    int c;

    for (const char *e = s + n - 1;
         (c = getchar()) != EOF && (*s = c) != '\n';
         s += s < e)
        /* THE PYRAMIDS OF THE OLD GEEZER */;
    *s = '\0';
    return c;
}

Name: The /prog/ commentator 2013-03-14 3:19

Well, that was a bit of a Frying Pan Combo from Lambda Arthur Calculus. Yes sir. Don't forget to pick up your copy of Dam Magazine on your way out.

Name: Anonymous 2013-03-14 11:31

Thread stopped at >>2. Why do you retards keep posting more verbose solutions?

Name: Anonymous 2013-03-14 12:10

>>6
None of the solutions in this thread will work if the user backspaces

Name: Anonymous 2013-03-14 12:19

>>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.

Name: Anonymous 2013-03-14 12:27

>>7
backspacing is for ``faggots'' anyway

Name: Anonymous 2013-03-14 12:33

>>1
fgets() annoyingly retains the '\n'
So just replace it with another '\0' after the call.

>>8
That didn't take long.

Name: Anonymous 2013-03-14 12:33

>>11
nehss dabs brew

Name: Anonymous 2013-03-14 12:40

>>10
lol yhbt

Name: Anonymous 2013-03-14 15:08

raw_input("")

Name: Anonymous 2013-03-14 16:11

>>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: Anonymous 2013-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?

Name: Anonymous 2013-03-14 16:15

>>14
the first argument doesn't even make sense

and how do you give getchar a fucking eof

Name: Anonymous 2013-03-14 16:19

>>16
Your answer is at >>15.

>>15
You don't need to write like a retard to make someone understand something.

Name: Anonymous 2013-03-14 16:53

why not just use String with InputStreamReader, you stupid noobs?

Name: Anonymous 2013-03-14 17:00

>>18
Because we don't use javashit or whatever fucking absurdly-high-level ENTERPRISE faggotry that is?

Name: Anonymous 2013-03-14 17:22

>>17
Actually he's writing like Lambda Arthur Calculus.

Name: Anonymous 2013-03-14 17:32

>>18
butthurt Cfag

Name: Anonymous 2013-03-14 17:39

>>18,21
Your /g/ is showing. Please go back there.

Name: Anonymous 2013-03-14 17:58

>>17,20
yain't red da standard

Name: Anonymous 2013-03-14 18:03

>>16
U DON'T. getchar RETURNZ EOF IF DA END-OF-FILE/ERROR INDICATOR IZ SET FOR stdin.

Name: Anonymous 2013-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: Anonymous 2013-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: Anonymous 2013-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.

THERE AIN'T NO STANDARD BUT C89.

Name: /prog/ news at 11: 2013-03-14 20:57

>>27
   
JEWS
DID dmr
   

Name: Anonymous 2013-03-14 20:58

DEM NOOBZ ON ##c ON irc.freenode.net R ALL BLOOD SUCKING HYENAS. WORSHIPPERS OF ISO, THOSE PUPPETS.

Name: Anonymous 2013-03-14 21:00

>>28
HE DID HIMSELF, I TELL YA. N IT WAS DA ISO DAT LED HIM 2 IT. FUCKERS AINT RED DA FUCKIN STANDARD.

Name: Anonymous 2013-03-14 21:13

>>28
I STOPPED LOVING JEWS AFTER READING YOUR POST

Name: Anonymous 2013-03-14 21:31

2^5 get

Name: Alexander Dubček 2013-03-14 21:32

/prog/ Spring.

Name: Anonymous 2013-03-15 0:00

Is this a fully correct and cross platform way to use fgets()?

fgets(buf, sizeof buf, stdin);
strtok(buf, "\r\n");

Name: Anonymous 2013-03-15 0:16

>>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.

Try:

fgets(buff, sizeof(buff)-1, stdin);
buff[strlen(buff)] = 0;

Name: Anonymous 2013-03-15 0:21

>>35
U AINT RED DA STAN DARD

Name: Anonymous 2013-03-15 0:24

>>36
Other than it needing to be strlen(buff)-1
What's wrong with it?

Name: Anonymous 2013-03-15 0:27

>>37
RED DA STTOK SEC TION

Name: Anonymous 2013-03-15 0:31

>>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 Calculus 2013-03-15 1:14

>>35
AINT RED DA STANDARD ON fgets
>>34
if (fgets(buf, sizeof buf, stdin)) { buf[strcspn(buf, "\r\n")] = '\0'; ... }

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List