[36ab7c7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Tim Post
|
---|
[216d6fc] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
[36ab7c7] | 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
[216d6fc] | 8 | *
|
---|
[36ab7c7] | 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
[216d6fc] | 16 | *
|
---|
[36ab7c7] | 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
[216d6fc] | 27 | */
|
---|
| 28 |
|
---|
[971cc0cc] | 29 | /*
|
---|
| 30 | * TODO: Options that people would expect, such as specifying the access time,
|
---|
| 31 | * etc.
|
---|
| 32 | */
|
---|
[216d6fc] | 33 |
|
---|
| 34 | #include <stdio.h>
|
---|
| 35 | #include <stdlib.h>
|
---|
| 36 | #include <unistd.h>
|
---|
| 37 | #include <fcntl.h>
|
---|
| 38 | #include <dirent.h>
|
---|
| 39 | #include <sys/types.h>
|
---|
[19f857a] | 40 | #include <str.h>
|
---|
[971cc0cc] | 41 | #include <getopt.h>
|
---|
| 42 | #include <sys/stat.h>
|
---|
| 43 | #include <errno.h>
|
---|
[216d6fc] | 44 |
|
---|
| 45 | #include "config.h"
|
---|
| 46 | #include "errors.h"
|
---|
| 47 | #include "util.h"
|
---|
| 48 | #include "entry.h"
|
---|
| 49 | #include "touch.h"
|
---|
| 50 | #include "cmds.h"
|
---|
| 51 |
|
---|
[a000878c] | 52 | static const char *cmdname = "touch";
|
---|
[216d6fc] | 53 |
|
---|
[971cc0cc] | 54 | static struct option const long_options[] = {
|
---|
| 55 | { "no-create", no_argument, 0, 'c' },
|
---|
| 56 | { 0, 0, 0, 0 }
|
---|
| 57 | };
|
---|
| 58 |
|
---|
[216d6fc] | 59 | /* Dispays help for touch in various levels */
|
---|
[809813d] | 60 | void help_cmd_touch(unsigned int level)
|
---|
[216d6fc] | 61 | {
|
---|
| 62 | if (level == HELP_SHORT) {
|
---|
[971cc0cc] | 63 | printf("`%s' updates access times of files\n", cmdname);
|
---|
[216d6fc] | 64 | } else {
|
---|
| 65 | help_cmd_touch(HELP_SHORT);
|
---|
[971cc0cc] | 66 | printf("Usage: `%s' [-c|--no-create] <file>...\n\n"
|
---|
| 67 | "If the file does not exist it will be created empty,\n"
|
---|
| 68 | "unless -c (--no-create) is supplied.\n\n"
|
---|
| 69 | "Options:\n"
|
---|
| 70 | " -c, --no-create Do not create new files\n",
|
---|
| 71 | cmdname);
|
---|
[216d6fc] | 72 | }
|
---|
[971cc0cc] | 73 |
|
---|
[809813d] | 74 | return;
|
---|
[216d6fc] | 75 | }
|
---|
| 76 |
|
---|
| 77 | /* Main entry point for touch, accepts an array of arguments */
|
---|
[809813d] | 78 | int cmd_touch(char **argv)
|
---|
[216d6fc] | 79 | {
|
---|
[971cc0cc] | 80 | unsigned int argc = cli_count_args(argv);
|
---|
| 81 | unsigned int i = 0;
|
---|
| 82 | unsigned int ret = 0;
|
---|
| 83 | int c;
|
---|
| 84 | int longind;
|
---|
| 85 | bool no_create = false;
|
---|
| 86 | struct stat file_stat;
|
---|
| 87 | int fd = -1;
|
---|
[216d6fc] | 88 | char *buff = NULL;
|
---|
[971cc0cc] | 89 |
|
---|
[216d6fc] | 90 | DIR *dirp;
|
---|
[971cc0cc] | 91 |
|
---|
| 92 | for (c = 0, optind = 0, longind = 0; c != -1; ) {
|
---|
| 93 | c = getopt_long(argc, argv, "c", long_options, &longind);
|
---|
| 94 | switch (c) {
|
---|
| 95 | case 'c':
|
---|
| 96 | no_create = true;
|
---|
| 97 | break;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | if (argc - optind < 1) {
|
---|
| 102 | printf("%s: Incorrect number of arguments. Try `help %s extended'\n",
|
---|
| 103 | cmdname, cmdname);
|
---|
[216d6fc] | 104 | return CMD_FAILURE;
|
---|
| 105 | }
|
---|
[971cc0cc] | 106 |
|
---|
| 107 | for (i = optind; argv[i] != NULL; i++) {
|
---|
[095003a8] | 108 | buff = str_dup(argv[i]);
|
---|
[971cc0cc] | 109 | if (buff == NULL) {
|
---|
| 110 | cli_error(CL_ENOMEM, "Out of memory");
|
---|
| 111 | ret++;
|
---|
| 112 | continue;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[216d6fc] | 115 | dirp = opendir(buff);
|
---|
| 116 | if (dirp) {
|
---|
[971cc0cc] | 117 | cli_error(CL_ENOTSUP, "`%s' is a directory", buff);
|
---|
[216d6fc] | 118 | closedir(dirp);
|
---|
[971cc0cc] | 119 | free(buff);
|
---|
| 120 | ret++;
|
---|
[216d6fc] | 121 | continue;
|
---|
| 122 | }
|
---|
[971cc0cc] | 123 |
|
---|
| 124 | /* Check whether file exists if -c (--no-create) option is given */
|
---|
| 125 | if ((!no_create) || ((no_create) && (stat(buff, &file_stat) == EOK)))
|
---|
| 126 | fd = open(buff, O_RDWR | O_CREAT);
|
---|
| 127 |
|
---|
[216d6fc] | 128 | if (fd < 0) {
|
---|
[971cc0cc] | 129 | cli_error(CL_EFAIL, "Could not update or create `%s'", buff);
|
---|
| 130 | free(buff);
|
---|
| 131 | ret++;
|
---|
[216d6fc] | 132 | continue;
|
---|
[971cc0cc] | 133 | } else {
|
---|
[216d6fc] | 134 | close(fd);
|
---|
[971cc0cc] | 135 | fd = -1;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[216d6fc] | 138 | free(buff);
|
---|
| 139 | }
|
---|
[971cc0cc] | 140 |
|
---|
[216d6fc] | 141 | if (ret)
|
---|
| 142 | return CMD_FAILURE;
|
---|
| 143 | else
|
---|
| 144 | return CMD_SUCCESS;
|
---|
| 145 | }
|
---|