Modify strtofflags so that it returns a malloced string instead of a

pointer to a static buffer.
This commit is contained in:
Josef Karthauser 2000-06-17 11:09:24 +00:00
parent 7684b08b56
commit 8c7bdc130e
2 changed files with 30 additions and 4 deletions

View File

@ -38,7 +38,7 @@
.Sh NAME
.Nm fflagstostr ,
.Nm strtofflags
.Nd modify file flag bits
.Nd convert between file flag bits and their string names
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
@ -54,6 +54,19 @@ function returns a comma separated string of the file flags represented by
.Fa flags .
If no flags are set a zero length string is returned.
.Pp
If memory cannot be allocated for the return value,
.Fn fflagstostr
returns
.Dv NULL .
.Pp
The value returned from
.Fn fflagstostr
is obtained from
.Fn malloc
and should be returned to the system with
.Fn free
when the program is done with it.
.Pp
The
.Fn strtofflags
function takes a string of file flags, as described in
@ -66,13 +79,21 @@ On success
returns 0, otherwise it returns non-zero and
.Fa stringp
is left pointing to the offending token.
.Sh ERRORS
The
.Fn fflagstostr
function
may fail and set errno for any of the errors specified for the library
routine
.Xr malloc 3 .
.Sh SEE ALSO
.Xr chflags 1 ,
.Xr chflags 2 ,
.Xr malloc 3
.Sh HISTORY
The
.Fn strtofflags
and
.Fn fflagstostr
and
.Fn strtofflags
functions first appeared in
.Fx 4.0 .

View File

@ -44,6 +44,7 @@ static const char rcsid[] =
#include <sys/stat.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
static struct {
@ -71,6 +72,7 @@ static struct {
{ "nouunlnk", UF_NOUNLINK, 0 },
{ "nouunlink", UF_NOUNLINK, 0 }
};
#define longestflaglen 12
#define nmappings (sizeof(mapping) / sizeof(mapping[0]))
/*
@ -82,11 +84,14 @@ char *
fflagstostr(flags)
u_long flags;
{
static char string[128];
char *string;
char *sp, *dp;
u_long setflags;
int i;
if ((string = (char *)malloc(nmappings * (longestflaglen + 1))) == NULL)
return (NULL);
setflags = flags;
dp = string;
for (i = 0; i < nmappings; i++) {