1*a824f5a1SJean-Baptiste Boric /* $NetBSD: automatic.c,v 1.1.1.2 2009/02/02 20:44:05 joerg Exp $ */
2*a824f5a1SJean-Baptiste Boric
3*a824f5a1SJean-Baptiste Boric /*-
4*a824f5a1SJean-Baptiste Boric * Copyright (c) 2005 The NetBSD Foundation, Inc.
5*a824f5a1SJean-Baptiste Boric * All rights reserved.
6*a824f5a1SJean-Baptiste Boric *
7*a824f5a1SJean-Baptiste Boric * This code is derived from software contributed to The NetBSD Foundation
8*a824f5a1SJean-Baptiste Boric * by Dieter Baron and Thomas Klausner.
9*a824f5a1SJean-Baptiste Boric *
10*a824f5a1SJean-Baptiste Boric * Redistribution and use in source and binary forms, with or without
11*a824f5a1SJean-Baptiste Boric * modification, are permitted provided that the following conditions
12*a824f5a1SJean-Baptiste Boric * are met:
13*a824f5a1SJean-Baptiste Boric * 1. Redistributions of source code must retain the above copyright
14*a824f5a1SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer.
15*a824f5a1SJean-Baptiste Boric * 2. Redistributions in binary form must reproduce the above copyright
16*a824f5a1SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer in the
17*a824f5a1SJean-Baptiste Boric * documentation and/or other materials provided with the distribution.
18*a824f5a1SJean-Baptiste Boric * 3. Neither the name of The NetBSD Foundation nor the names of its
19*a824f5a1SJean-Baptiste Boric * contributors may be used to endorse or promote products derived
20*a824f5a1SJean-Baptiste Boric * from this software without specific prior written permission.
21*a824f5a1SJean-Baptiste Boric *
22*a824f5a1SJean-Baptiste Boric * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23*a824f5a1SJean-Baptiste Boric * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24*a824f5a1SJean-Baptiste Boric * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25*a824f5a1SJean-Baptiste Boric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26*a824f5a1SJean-Baptiste Boric * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27*a824f5a1SJean-Baptiste Boric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28*a824f5a1SJean-Baptiste Boric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29*a824f5a1SJean-Baptiste Boric * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30*a824f5a1SJean-Baptiste Boric * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31*a824f5a1SJean-Baptiste Boric * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32*a824f5a1SJean-Baptiste Boric * POSSIBILITY OF SUCH DAMAGE.
33*a824f5a1SJean-Baptiste Boric */
34*a824f5a1SJean-Baptiste Boric
35*a824f5a1SJean-Baptiste Boric #if HAVE_CONFIG_H
36*a824f5a1SJean-Baptiste Boric #include "config.h"
37*a824f5a1SJean-Baptiste Boric #endif
38*a824f5a1SJean-Baptiste Boric #include <nbcompat.h>
39*a824f5a1SJean-Baptiste Boric #if HAVE_SYS_CDEFS_H
40*a824f5a1SJean-Baptiste Boric #include <sys/cdefs.h>
41*a824f5a1SJean-Baptiste Boric #endif
42*a824f5a1SJean-Baptiste Boric __RCSID("$NetBSD: automatic.c,v 1.1.1.2 2009/02/02 20:44:05 joerg Exp $");
43*a824f5a1SJean-Baptiste Boric
44*a824f5a1SJean-Baptiste Boric #if HAVE_ASSERT_H
45*a824f5a1SJean-Baptiste Boric #include <assert.h>
46*a824f5a1SJean-Baptiste Boric #endif
47*a824f5a1SJean-Baptiste Boric #if HAVE_ERR_H
48*a824f5a1SJean-Baptiste Boric #include <err.h>
49*a824f5a1SJean-Baptiste Boric #endif
50*a824f5a1SJean-Baptiste Boric #if HAVE_ERRNO_H
51*a824f5a1SJean-Baptiste Boric #include <errno.h>
52*a824f5a1SJean-Baptiste Boric #endif
53*a824f5a1SJean-Baptiste Boric #if HAVE_FCNTL_H
54*a824f5a1SJean-Baptiste Boric #include <fcntl.h>
55*a824f5a1SJean-Baptiste Boric #endif
56*a824f5a1SJean-Baptiste Boric #if HAVE_STRING_H
57*a824f5a1SJean-Baptiste Boric #include <string.h>
58*a824f5a1SJean-Baptiste Boric #endif
59*a824f5a1SJean-Baptiste Boric #if HAVE_STDLIB_H
60*a824f5a1SJean-Baptiste Boric #include <stdlib.h>
61*a824f5a1SJean-Baptiste Boric #endif
62*a824f5a1SJean-Baptiste Boric #if HAVE_SYS_STAT_H
63*a824f5a1SJean-Baptiste Boric #include <sys/stat.h>
64*a824f5a1SJean-Baptiste Boric #endif
65*a824f5a1SJean-Baptiste Boric #include "lib.h"
66*a824f5a1SJean-Baptiste Boric
67*a824f5a1SJean-Baptiste Boric Boolean
is_automatic_installed(const char * pkg)68*a824f5a1SJean-Baptiste Boric is_automatic_installed(const char *pkg)
69*a824f5a1SJean-Baptiste Boric {
70*a824f5a1SJean-Baptiste Boric char *filename, *value;
71*a824f5a1SJean-Baptiste Boric Boolean ret;
72*a824f5a1SJean-Baptiste Boric
73*a824f5a1SJean-Baptiste Boric assert(pkg[0] != '/');
74*a824f5a1SJean-Baptiste Boric
75*a824f5a1SJean-Baptiste Boric filename = pkgdb_pkg_file(pkg, INSTALLED_INFO_FNAME);
76*a824f5a1SJean-Baptiste Boric
77*a824f5a1SJean-Baptiste Boric value = var_get(filename, AUTOMATIC_VARNAME);
78*a824f5a1SJean-Baptiste Boric
79*a824f5a1SJean-Baptiste Boric if (value && strcasecmp(value, "yes") == 0)
80*a824f5a1SJean-Baptiste Boric ret = TRUE;
81*a824f5a1SJean-Baptiste Boric else
82*a824f5a1SJean-Baptiste Boric ret = FALSE;
83*a824f5a1SJean-Baptiste Boric
84*a824f5a1SJean-Baptiste Boric free(value);
85*a824f5a1SJean-Baptiste Boric free(filename);
86*a824f5a1SJean-Baptiste Boric
87*a824f5a1SJean-Baptiste Boric return ret;
88*a824f5a1SJean-Baptiste Boric }
89*a824f5a1SJean-Baptiste Boric
90*a824f5a1SJean-Baptiste Boric int
mark_as_automatic_installed(const char * pkg,int value)91*a824f5a1SJean-Baptiste Boric mark_as_automatic_installed(const char *pkg, int value)
92*a824f5a1SJean-Baptiste Boric {
93*a824f5a1SJean-Baptiste Boric char *filename;
94*a824f5a1SJean-Baptiste Boric int retval;
95*a824f5a1SJean-Baptiste Boric
96*a824f5a1SJean-Baptiste Boric assert(pkg[0] != '/');
97*a824f5a1SJean-Baptiste Boric
98*a824f5a1SJean-Baptiste Boric filename = pkgdb_pkg_file(pkg, INSTALLED_INFO_FNAME);
99*a824f5a1SJean-Baptiste Boric
100*a824f5a1SJean-Baptiste Boric retval = var_set(filename, AUTOMATIC_VARNAME, value ? "yes" : NULL);
101*a824f5a1SJean-Baptiste Boric
102*a824f5a1SJean-Baptiste Boric free(filename);
103*a824f5a1SJean-Baptiste Boric
104*a824f5a1SJean-Baptiste Boric return retval;
105*a824f5a1SJean-Baptiste Boric }
106