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