fix printf warning
[oweals/busybox.git] / libbb / my_getug.c
1 /* 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   *
24   * 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 
26   *                   (and a pointer to it is returned).
27   *                   if idname is NULL, id as string is written to the static allocated buffer
28   *                   and NULL is returned.
29   * 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.   
35   */
36   
37 #include <stdio.h>
38 #include <assert.h>
39 #include "libbb.h"
40
41
42 /* internal function for my_getpwuid and my_getgrgid */
43 char * 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 /*
59 Local Variables:
60 c-file-style: "linux"
61 c-basic-offset: 4
62 tab-width: 4
63 End:
64 */