Mercurial > hg > toybox
view lib/portability.c @ 1572:da1bf31ed322 draft
Tweak the "ignoring return value" fortify workaround for readlinkat.
We zero the buffer and if the link read fails that's left alone, so
it's ok for the symlink not to be there. Unfortunately, typecasting the
return value to (void) doesn't shut up gcc, and having an if(); with the
semicolon on the same line doesn't shut up llvm. (The semicolon on a new
line would, but C does not have significant whitespace and I'm not going
to humor llvm if it plans to start.)
So far, empty curly brackets consistently get the warning to shut up.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 24 Nov 2014 17:23:23 -0600 |
parents | da72fa267b7b |
children | ce22ad7a26c1 |
line wrap: on
line source
/* portability.c - code to workaround the deficiencies of various platforms. * * Copyright 2012 Rob Landley <rob@landley.net> * Copyright 2012 Georgi Chorbadzhiyski <gf@unixsol.org> */ #include "toys.h" #if defined(__APPLE__) ssize_t getdelim(char **linep, size_t *np, int delim, FILE *stream) { int ch; size_t new_len; ssize_t i = 0; char *line, *new_line; // Invalid input if (!linep || !np) { errno = EINVAL; return -1; } if (*linep == NULL || *np == 0) { *np = 1024; *linep = calloc(1, *np); if (*linep == NULL) return -1; } line = *linep; while ((ch = getc(stream)) != EOF) { if (i > *np) { // Need more space new_len = *np + 1024; new_line = realloc(*linep, new_len); if (!new_line) return -1; *np = new_len; *linep = new_line; } line[i] = ch; if (ch == delim) break; i += 1; } if (i > *np) { // Need more space new_len = i + 2; new_line = realloc(*linep, new_len); if (!new_line) return -1; *np = new_len; *linep = new_line; } line[i + 1] = '\0'; return i > 0 ? i : -1; } ssize_t getline(char **linep, size_t *np, FILE *stream) { return getdelim(linep, np, '\n', stream); } extern char **environ; int clearenv(void) { *environ = NULL; return 0; } #endif