github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/lib9/dirfstat.c (about)

     1  /*
     2  Plan 9 from User Space src/lib9/dirfstat.c
     3  http://code.swtch.com/plan9port/src/tip/src/lib9/dirfstat.c
     4  
     5  Copyright 2001-2007 Russ Cox.  All Rights Reserved.
     6  
     7  Permission is hereby granted, free of charge, to any person obtaining a copy
     8  of this software and associated documentation files (the "Software"), to deal
     9  in the Software without restriction, including without limitation the rights
    10  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  copies of the Software, and to permit persons to whom the Software is
    12  furnished to do so, subject to the following conditions:
    13  
    14  The above copyright notice and this permission notice shall be included in
    15  all copies or substantial portions of the Software.
    16  
    17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  THE SOFTWARE.
    24  */
    25  #include <u.h>
    26  #define NOPLAN9DEFINES
    27  #include <libc.h>
    28  
    29  #include <sys/stat.h>
    30  
    31  extern int _p9dir(struct stat*, struct stat*, char*, Dir*, char**, char*);
    32  
    33  Dir*
    34  dirfstat(int fd)
    35  {
    36  	struct stat st;
    37  	int nstr;
    38  	Dir *d;
    39  	char *str, tmp[100];
    40  
    41  	if(fstat(fd, &st) < 0)
    42  		return nil;
    43  
    44  	snprint(tmp, sizeof tmp, "/dev/fd/%d", fd);
    45  	nstr = _p9dir(&st, &st, tmp, nil, nil, nil);
    46  	d = malloc(sizeof(Dir)+(size_t)nstr);
    47  	if(d == nil)
    48  		return nil;
    49  	memset(d, 0, sizeof(Dir)+(size_t)nstr);
    50  	str = (char*)&d[1];
    51  	_p9dir(&st, &st, tmp, d, &str, str+nstr);
    52  	return d;
    53  }
    54