Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / pkg_install / delete / main.c
1 /*
2  * FreeBSD install - a package for the installation and maintainance
3  * of non-core utilities.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * Jordan K. Hubbard
15  * 18 July 1993
16  *
17  * This is the delete module.
18  *
19  */
20
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD: src/usr.sbin/pkg_install/delete/main.c,v 1.17.2.8 2002/08/20 06:35:07 obrien Exp $");
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <err.h>
27 #include "lib.h"
28 #include "delete.h"
29
30 static char Options[] = "adDfGhinp:rvx";
31
32 char    *Prefix         = NULL;
33 Boolean CleanDirs       = FALSE;
34 Boolean Interactive     = FALSE;
35 Boolean NoDeInstall     = FALSE;
36 Boolean Recursive       = FALSE;
37 match_t MatchType       = MATCH_GLOB;
38
39 static void usage __P((void));
40
41 int
42 main(int argc, char **argv)
43 {
44     int ch, error;
45     char **pkgs, **start;
46     char *pkgs_split;
47     const char *tmp;
48     struct stat stat_s;
49
50     pkgs = start = argv;
51     while ((ch = getopt(argc, argv, Options)) != -1)
52         switch(ch) {
53         case 'v':
54             Verbose = TRUE;
55             break;
56
57         case 'f':
58             Force = TRUE;
59             break;
60
61         case 'p':
62             Prefix = optarg;
63             break;
64
65         case 'D':
66             NoDeInstall = TRUE;
67             break;
68
69         case 'd':
70             CleanDirs = TRUE;
71             break;
72
73         case 'n':
74             Fake = TRUE;
75             Verbose = TRUE;
76             break;
77
78         case 'a':
79             MatchType = MATCH_ALL;
80             break;
81
82         case 'G':
83             MatchType = MATCH_EXACT;
84             break;
85
86         case 'x':
87             MatchType = MATCH_REGEX;
88             break;
89
90         case 'i':
91             Interactive = TRUE;
92             break;
93
94         case 'r':
95             Recursive = TRUE;
96             break;
97
98         case 'h':
99         case '?':
100         default:
101             usage();
102             break;
103         }
104
105     argc -= optind;
106     argv += optind;
107
108     /* Get all the remaining package names, if any */
109     while (*argv) {
110         /* Don't try to apply heuristics if arguments are regexs */
111         if (MatchType != MATCH_REGEX)
112             while ((pkgs_split = strrchr(*argv, (int)'/')) != NULL) {
113                 *pkgs_split++ = '\0';
114                 /*
115                  * If character after the '/' is alphanumeric, then we've found the
116                  * package name.  Otherwise we've come across a trailing '/' and
117                  * need to continue our quest.
118                  */
119                 if (isalpha(*pkgs_split) || ((MatchType == MATCH_GLOB) && \
120                     strpbrk(pkgs_split, "*?[]") != NULL)) {
121                     *argv = pkgs_split;
122                     break;
123                 }
124             }
125         *pkgs++ = *argv++;
126     }
127
128     /* If no packages, yelp */
129     if (pkgs == start && MatchType != MATCH_ALL)
130         warnx("missing package name(s)"), usage();
131     *pkgs = NULL;
132     tmp = LOG_DIR;
133     (void) stat(tmp, &stat_s);
134     if (!Fake && getuid() && geteuid() != stat_s.st_uid) {
135         if (!Force)
136             errx(1, "you do not own %s, use -f to force", tmp);
137         else
138             warnx("you do not own %s (proceeding anyways)", tmp);
139     }
140     if ((error = pkg_perform(start)) != 0) {
141         if (Verbose)
142             warnx("%d package deletion(s) failed", error);
143         return error;
144     }
145     else
146         return 0;
147 }
148
149 static void
150 usage()
151 {
152     fprintf(stderr, "%s\n%s\n",
153         "usage: pkg_delete [-dDfGinrvx] [-p prefix] pkg-name ...",
154         "       pkg_delete -a [flags]");
155     exit(1);
156 }