Skip to content

Commit ffd34ba

Browse files
committed
More stdlib.c improvements
- Add setrlimit(), ttyname_r() - Fix setvbuf()
1 parent 0b62ef8 commit ffd34ba

1 file changed

Lines changed: 35 additions & 28 deletions

File tree

examples/stdlib.c

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,11 @@ static int getrlimit(int resource, struct rlimit *rlim)
13791379
return (int)syscall(SYS_getrlimit, resource, rlim);
13801380
}
13811381

1382+
static int setrlimit(int resource, const struct rlimit *rlim)
1383+
{
1384+
return (int)syscall(SYS_setrlimit, resource, rlim);
1385+
}
1386+
13821387
static int getrusage(int who, struct rusage *usage)
13831388
{
13841389
return (int)syscall(SYS_getrusage, who, usage);
@@ -3357,42 +3362,32 @@ static int fileno(FILE *stream)
33573362

33583363
static int setvbuf(FILE *stream, char *buf, int mode, size_t size)
33593364
{
3360-
void *oldbuf = NULL;
3361-
stdio_lock(stream, -1);
3362-
fflush_unlocked(stream);
33633365
switch (mode)
33643366
{
3365-
case _IOFBF:
3366-
if (buf == NULL && stream->buf == NULL)
3367-
goto invalid;
3368-
stream->flags &= ~STDIO_FLAG_NO_BUF;
3369-
stream->eol = EOF;
3370-
break;
3371-
case _IOLBF:
3372-
if (buf == NULL && stream->buf == NULL)
3373-
goto invalid;
3374-
stream->flags &= ~STDIO_FLAG_NO_BUF;
3375-
stream->eol = '\n';
3376-
break;
33773367
case _IONBF:
3378-
if (buf != NULL || size > 0)
3368+
if (buf != NULL)
33793369
goto invalid;
3380-
stream->flags |= STDIO_FLAG_NO_BUF;
3381-
stream->eol = EOF;
33823370
break;
3383-
default:
3384-
invalid:
3385-
stdio_unlock(stream);
3371+
case _IOFBF: case _IOLBF:
3372+
break;
3373+
default: invalid:
33863374
errno = EINVAL;
33873375
return -1;
33883376
}
3389-
if ((buf != NULL && size > 0) || mode == _IONBF)
3390-
{
3391-
if (stream->buf != NULL && (stream->flags & STDIO_FLAG_OWN_BUF) != 0)
3392-
oldbuf = stream->buf;
3393-
stream->buf = buf;
3394-
stream->bufsiz = size;
3395-
}
3377+
3378+
void *oldbuf = NULL;
3379+
stdio_lock(stream, -1);
3380+
fflush_unlocked(stream);
3381+
3382+
if (stream->buf != NULL && (stream->flags & STDIO_FLAG_OWN_BUF))
3383+
oldbuf = stream->buf;
3384+
stream->flags &=
3385+
~(STDIO_FLAG_INITED | STDIO_FLAG_OWN_BUF | STDIO_FLAG_NO_BUF);
3386+
stream->buf = buf;
3387+
stream->bufsiz = (buf == NULL? BUFSIZ: size);
3388+
stream->eol = (mode == _IOLBF? '\n': EOF);
3389+
stream->flags |= (mode == _IONBF? STDIO_FLAG_NO_BUF: 0x0);
3390+
33963391
stdio_unlock(stream);
33973392
free(oldbuf);
33983393
return 0;
@@ -5545,6 +5540,18 @@ static int isatty(int fd)
55455540
return 1;
55465541
}
55475542

5543+
static int ttyname_r(int fd, char *buf, size_t buflen)
5544+
{
5545+
char path[32];
5546+
ssize_t r = snprintf(path, sizeof(path)-1, "/proc/self/fd/%d", fd);
5547+
if (r < 0 || r >= sizeof(path)-1)
5548+
return errno;
5549+
r = readlink(path, buf, buflen-1);
5550+
if (r < 0)
5551+
return (errno == ENAMETOOLONG? ERANGE: errno);
5552+
return 0;
5553+
}
5554+
55485555
static unsigned sleep(unsigned sec)
55495556
{
55505557
struct timespec ts = {sec, 0}, tr;

0 commit comments

Comments
 (0)