Clean (void) casts from usr.sbin
[dragonfly.git] / usr.sbin / pkg_install / delete / main.c
1 /*
2  *
3  * FreeBSD install - a package for the installation and maintainance
4  * of non-core utilities.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * Jordan K. Hubbard
16  * 18 July 1993
17  *
18  * This is the delete module.
19  *
20  * $FreeBSD: src/usr.sbin/pkg_install/delete/main.c,v 1.26 2004/06/29 18:54:47 eik Exp $
21  * $DragonFly: src/usr.sbin/pkg_install/delete/Attic/main.c,v 1.5 2004/12/18 22:48:04 swildner Exp $
22  */
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:rvxX";
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(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 'X':
91             MatchType = MATCH_EREGEX;
92             break;
93
94         case 'i':
95             Interactive = TRUE;
96             break;
97
98         case 'r':
99             Recursive = TRUE;
100             break;
101
102         case 'h':
103         case '?':
104         default:
105             usage();
106             break;
107         }
108
109     argc -= optind;
110     argv += optind;
111
112     /* Get all the remaining package names, if any */
113     while (*argv) {
114         /* Don't try to apply heuristics if arguments are regexs */
115         if (MatchType != MATCH_REGEX)
116             while ((pkgs_split = strrchr(*argv, (int)'/')) != NULL) {
117                 *pkgs_split++ = '\0';
118                 /*
119                  * If character after the '/' is alphanumeric, then we've found the
120                  * package name.  Otherwise we've come across a trailing '/' and
121                  * need to continue our quest.
122                  */
123                 if (isalpha(*pkgs_split) || ((MatchType == MATCH_GLOB) && \
124                     strpbrk(pkgs_split, "*?[]") != NULL)) {
125                     *argv = pkgs_split;
126                     break;
127                 }
128             }
129         *pkgs++ = *argv++;
130     }
131
132     /* If no packages, yelp */
133     if (pkgs == start && MatchType != MATCH_ALL)
134         warnx("missing package name(s)"), usage();
135     *pkgs = NULL;
136     tmp = LOG_DIR;
137     stat(tmp, &stat_s);
138     if (!Fake && getuid() && geteuid() != stat_s.st_uid) {
139         if (!Force)
140             errx(1, "you do not own %s, use -f to force", tmp);
141         else
142             warnx("you do not own %s (proceeding anyways)", tmp);
143     }
144     if ((error = pkg_perform(start)) != 0) {
145         if (Verbose)
146             warnx("%d package deletion(s) failed", error);
147         return error;
148     }
149     else
150         return 0;
151 }
152
153 static void
154 usage()
155 {
156     fprintf(stderr, "%s\n%s\n",
157         "usage: pkg_delete [-dDfGinrvxX] [-p prefix] pkg-name ...",
158         "       pkg_delete -a [flags]");
159     exit(1);
160 }