blob: 9b7292d133f18ff9892187561fc6d61fd1504707 [file] [log] [blame]
Glenn L McGrathf15dfc52004-09-15 03:04:08 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 2004 by Tito Ragusa <farmatito@tiscali.it>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 /*
23 *
Eric Andersen94d628c2004-10-08 08:07:40 +000024 * if bufsize is > 0 char *buffer can not be set to NULL.
25 * If idname is not NULL it is written on the static allocated buffer
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000026 * (and a pointer to it is returned).
Eric Andersen94d628c2004-10-08 08:07:40 +000027 * if idname is NULL, id as string is written to the static allocated buffer
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000028 * and NULL is returned.
Eric Andersen94d628c2004-10-08 08:07:40 +000029 * if bufsize is = 0 char *buffer can be set to NULL.
30 * If idname exists a pointer to it is returned,
31 * else NULL is returned.
32 * if bufsize is < 0 char *buffer can be set to NULL.
33 * If idname exists a pointer to it is returned,
34 * else an error message is printed and the program exits.
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000035 */
36
37#include <stdio.h>
38#include <assert.h>
39#include "libbb.h"
40
41
42/* internal function for my_getpwuid and my_getgrgid */
43char * my_getug(char *buffer, char *idname, long id, int bufsize, char prefix)
44{
45 if(bufsize > 0 ) {
46 assert(buffer!=NULL);
47 if(idname) {
48 return safe_strncpy(buffer, idname, bufsize);
49 }
50 snprintf(buffer, bufsize, "%ld", id);
51 } else if(bufsize < 0 && !idname) {
52 bb_error_msg_and_die("unknown %cid %ld", prefix, id);
53 }
54 return idname;
55}
56
57/* END CODE */
58/*
59Local Variables:
60c-file-style: "linux"
61c-basic-offset: 4
62tab-width: 4
63End:
64*/