2c5baf659211e8b2c155d81e16cd49e551bdd975
[platform/upstream/glibc.git] / login / utmp_db.c
1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@cygnus.com>
4    and Paul Janzen <pcj@primenet.com>, 1996.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <assert.h>
22 #include <db.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <utmp.h>
28 #include <sys/stat.h>
29
30 #include "utmp-private.h"
31
32
33 /* This is the default name.  */
34 static const char default_file_name[] = _PATH_UTMP_DB;
35
36 /* Current file name.  */
37 static const char *file_name = (const char *) default_file_name;
38
39 /* Descriptor for database.  */
40 static DB *db_fd;
41 static char last_date[16];
42
43
44 /* Our local functions.  */
45 static int setutent_db (int reset);
46 static void endutent_db (void);
47 static int utmpname_db (const char *name);
48
49
50 /* The jump table for the local functions.  */
51 struct utfuncs __libc_utmp_db_functions =
52 {
53   setutent_db,
54   NULL,
55   NULL,
56   NULL,
57   NULL,
58   endutent_db,
59   utmpname_db
60 };
61
62
63 static int
64 setutent_db (int reset)
65 {
66   return 0;
67 }
68
69
70 static void
71 endutent_db (void)
72 {
73 }
74
75
76 static int
77 utmpname_db (const char *name)
78 {
79   if (strcmp (name, file_name) != 0)
80     {
81       if (strcmp (name, default_file_name) == 0)
82         {
83           if (file_name != default_file_name)
84             free ((char *) file_name);
85
86           file_name = default_file_name;
87         }
88       else
89         {
90           char *new_name = __strdup (name);
91           if (new_name == NULL)
92             /* Out of memory.  */
93             return -1;
94
95           if (file_name != default_file_name)
96             free ((char *) file_name);
97
98           file_name = new_name;
99         }
100     }
101   return 0;
102 }