diff options
| author | David M. Syzdek <syzdek@bindlebinaries.com> | 2017-07-05 18:59:13 -0800 |
|---|---|---|
| committer | David M. Syzdek <syzdek@bindlebinaries.com> | 2017-07-05 18:59:13 -0800 |
| commit | f82c9e9d824ee02b116c3eb390ff7666ac5d1a20 (patch) | |
| tree | 0dadd02129c5e5fc27581f38ebacebab6c6c8326 /src/widget-rmdir.c | |
| parent | 1b309abc4f5e33182db6a9fa5a145c4b45d8db64 (diff) | |
| download | securecoreutils-f82c9e9d824ee02b116c3eb390ff7666ac5d1a20.tar.gz securecoreutils-f82c9e9d824ee02b116c3eb390ff7666ac5d1a20.tar.bz2 securecoreutils-f82c9e9d824ee02b116c3eb390ff7666ac5d1a20.tar.xz securecoreutils-f82c9e9d824ee02b116c3eb390ff7666ac5d1a20.zip | |
Adding 'rmdir' widget
Diffstat (limited to 'src/widget-rmdir.c')
| -rw-r--r-- | src/widget-rmdir.c | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/src/widget-rmdir.c b/src/widget-rmdir.c new file mode 100644 index 0000000..073ffa7 --- /dev/null +++ b/src/widget-rmdir.c | |||
| @@ -0,0 +1,195 @@ | |||
| 1 | /* | ||
| 2 | * Secure Core Utilities | ||
| 3 | * Copyright (C) 2015 Bindle Binaries <syzdek@bindlebinaries.com>. | ||
| 4 | * | ||
| 5 | * @BINDLE_BINARIES_BSD_LICENSE_START@ | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions are | ||
| 9 | * met: | ||
| 10 | * | ||
| 11 | * * Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * * Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * * Neither the name of Bindle Binaries nor the | ||
| 17 | * names of its contributors may be used to endorse or promote products | ||
| 18 | * derived from this software without specific prior written permission. | ||
| 19 | * | ||
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 21 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 22 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BINDLE BINARIES BE LIABLE FOR | ||
| 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | * SUCH DAMAGE. | ||
| 31 | * | ||
| 32 | * @BINDLE_BINARIES_BSD_LICENSE_END@ | ||
| 33 | */ | ||
| 34 | #include "widget-rmdir.h" | ||
| 35 | |||
| 36 | /////////////// | ||
| 37 | // // | ||
| 38 | // Headers // | ||
| 39 | // // | ||
| 40 | /////////////// | ||
| 41 | #ifdef __SECURECOREUTILS_PMARK | ||
| 42 | #pragma mark - Headers | ||
| 43 | #endif | ||
| 44 | |||
| 45 | #include <assert.h> | ||
| 46 | #include <errno.h> | ||
| 47 | #include <stdio.h> | ||
| 48 | #include <string.h> | ||
| 49 | #include <strings.h> | ||
| 50 | #include <sys/types.h> | ||
| 51 | #include <sys/stat.h> | ||
| 52 | #include <unistd.h> | ||
| 53 | |||
| 54 | |||
| 55 | ///////////////// | ||
| 56 | // // | ||
| 57 | // Functions // | ||
| 58 | // // | ||
| 59 | ///////////////// | ||
| 60 | #ifdef __SECURECOREUTILS_PMARK | ||
| 61 | #pragma mark - Functions | ||
| 62 | #endif | ||
| 63 | |||
| 64 | int scu_widget_rmdir(scu_config * cnf) | ||
| 65 | { | ||
| 66 | int c; | ||
| 67 | int opt_index; | ||
| 68 | int rc; | ||
| 69 | struct stat sb; | ||
| 70 | |||
| 71 | // getopt options | ||
| 72 | static char short_opt[] = "+fhiqVv"; | ||
| 73 | static struct option long_opt[] = | ||
| 74 | { | ||
| 75 | {"help", no_argument, NULL, 'h' }, | ||
| 76 | {"quiet", no_argument, NULL, 'q' }, | ||
| 77 | {"silent", no_argument, NULL, 'q' }, | ||
| 78 | {"version", no_argument, NULL, 'V' }, | ||
| 79 | {"verbose", no_argument, NULL, 'v' }, | ||
| 80 | { NULL, 0, NULL, 0 } | ||
| 81 | }; | ||
| 82 | |||
| 83 | assert(cnf != NULL); | ||
| 84 | |||
| 85 | while((c = getopt_long(cnf->argc, cnf->argv, short_opt, long_opt, &opt_index)) != -1) | ||
| 86 | { | ||
| 87 | switch(c) | ||
| 88 | { | ||
| 89 | case -1: /* no more arguments */ | ||
| 90 | case 0: /* long options toggles */ | ||
| 91 | break; | ||
| 92 | |||
| 93 | case 'h': | ||
| 94 | scu_widget_rmdir_usage(); | ||
| 95 | return(0); | ||
| 96 | |||
| 97 | case 's': | ||
| 98 | cnf->quiet = 1; | ||
| 99 | if ((cnf->verbose)) | ||
| 100 | { | ||
| 101 | fprintf(stderr, "%s: %s: incompatible options\n", PROGRAM_NAME, cnf->widget->name); | ||
| 102 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 103 | return(1); | ||
| 104 | }; | ||
| 105 | break; | ||
| 106 | |||
| 107 | case 'V': | ||
| 108 | printf("%s widget\n", cnf->widget->name); | ||
| 109 | scu_version(); | ||
| 110 | return(0); | ||
| 111 | |||
| 112 | case 'v': | ||
| 113 | cnf->verbose++; | ||
| 114 | if ((cnf->quiet)) | ||
| 115 | { | ||
| 116 | fprintf(stderr, "%s: %s: incompatible options\n", PROGRAM_NAME, cnf->widget->name); | ||
| 117 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget->name); | ||
| 118 | return(1); | ||
| 119 | }; | ||
| 120 | break; | ||
| 121 | |||
| 122 | case '?': | ||
| 123 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget->name); | ||
| 124 | return(1); | ||
| 125 | |||
| 126 | default: | ||
| 127 | fprintf(stderr, "%s: %s: unrecognized option `--%c'\n", PROGRAM_NAME, cnf->widget->name, c); | ||
| 128 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget->name); | ||
| 129 | return(1); | ||
| 130 | }; | ||
| 131 | }; | ||
| 132 | |||
| 133 | if ((cnf->argc - optind) < 1) | ||
| 134 | { | ||
| 135 | fprintf(stderr, "%s: %s: missing required argument\n", PROGRAM_NAME, cnf->widget->name); | ||
| 136 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget->name); | ||
| 137 | return(1); | ||
| 138 | }; | ||
| 139 | if ((cnf->argc - optind) > 1) | ||
| 140 | { | ||
| 141 | fprintf(stderr, "%s: unrecognized argument `-- %s'\n", PROGRAM_NAME, cnf->argv[optind+1]); | ||
| 142 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget->name); | ||
| 143 | return(1); | ||
| 144 | }; | ||
| 145 | |||
| 146 | // checks file for restriction validations | ||
| 147 | if ((rc = stat(cnf->argv[optind], &sb)) == -1) | ||
| 148 | { | ||
| 149 | if (errno == ENOENT) | ||
| 150 | return(0); | ||
| 151 | fprintf(stderr, "%s: %s: %s\n", PROGRAM_NAME, cnf->widget->name, strerror(errno)); | ||
| 152 | return(1); | ||
| 153 | }; | ||
| 154 | if ((rc = scu_pathcheck(cnf->argv[optind], 1)) != 0) | ||
| 155 | { | ||
| 156 | fprintf(stderr, "%s: %s: %s\n", PROGRAM_NAME, cnf->widget->name, scu_strerror(rc)); | ||
| 157 | return(1); | ||
| 158 | }; | ||
| 159 | if (!(S_ISDIR(sb.st_mode))) | ||
| 160 | { | ||
| 161 | fprintf(stderr, "%s: %s: not a directory\n", PROGRAM_NAME, cnf->widget->name); | ||
| 162 | return(1); | ||
| 163 | }; | ||
| 164 | |||
| 165 | if ((cnf->verbose)) | ||
| 166 | printf("removing %s\n", cnf->argv[optind]); | ||
| 167 | |||
| 168 | if ((rc = rmdir(cnf->argv[optind])) == -1) | ||
| 169 | { | ||
| 170 | fprintf(stderr, "%s: %s: %s: %s\n", PROGRAM_NAME, cnf->widget->name, | ||
| 171 | cnf->argv[optind], strerror(errno)); | ||
| 172 | return(1); | ||
| 173 | }; | ||
| 174 | |||
| 175 | return(0); | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | void scu_widget_rmdir_usage(void) | ||
| 180 | { | ||
| 181 | printf("Usage: %s rmdir [OPTIONS] file\n", PROGRAM_NAME); | ||
| 182 | printf(" rmdir [OPTIONS] file\n"); | ||
| 183 | printf(" secrmdir [OPTIONS] file\n"); | ||
| 184 | printf("\n"); | ||
| 185 | scu_usage_options(); | ||
| 186 | printf(" -i prompt for confirmation\n"); | ||
| 187 | printf(" -f, --force ignore nonexistent files\n"); | ||
| 188 | printf("\n"); | ||
| 189 | scu_usage_restrictions(); | ||
| 190 | printf("\n"); | ||
| 191 | return; | ||
| 192 | } | ||
| 193 | |||
| 194 | |||
| 195 | /* end of source */ | ||
