source: mainline/uspace/app/bdsh/cmds/modules/touch/touch.c@ 948222e4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 948222e4 was 948222e4, checked in by Jiri Svoboda <jiri@…>, 7 years ago

A few more cases of for loops without iteration expression (which isn't really a for loop).

  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[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>
[8d2dd7f2]36#include <stdint.h>
[216d6fc]37#include <dirent.h>
[19f857a]38#include <str.h>
[971cc0cc]39#include <getopt.h>
40#include <errno.h>
[23a0368]41#include <vfs/vfs.h>
[216d6fc]42
43#include "config.h"
44#include "errors.h"
45#include "util.h"
46#include "entry.h"
47#include "touch.h"
48#include "cmds.h"
49
[a000878c]50static const char *cmdname = "touch";
[216d6fc]51
[971cc0cc]52static struct option const long_options[] = {
53 { "no-create", no_argument, 0, 'c' },
54 { 0, 0, 0, 0 }
55};
56
[216d6fc]57/* Dispays help for touch in various levels */
[809813d]58void help_cmd_touch(unsigned int level)
[216d6fc]59{
60 if (level == HELP_SHORT) {
[971cc0cc]61 printf("`%s' updates access times of files\n", cmdname);
[216d6fc]62 } else {
63 help_cmd_touch(HELP_SHORT);
[971cc0cc]64 printf("Usage: `%s' [-c|--no-create] <file>...\n\n"
65 "If the file does not exist it will be created empty,\n"
66 "unless -c (--no-create) is supplied.\n\n"
67 "Options:\n"
68 " -c, --no-create Do not create new files\n",
69 cmdname);
[216d6fc]70 }
[a35b458]71
[809813d]72 return;
[216d6fc]73}
74
75/* Main entry point for touch, accepts an array of arguments */
[809813d]76int cmd_touch(char **argv)
[216d6fc]77{
[971cc0cc]78 unsigned int argc = cli_count_args(argv);
79 unsigned int i = 0;
80 unsigned int ret = 0;
81 int c;
82 int longind;
83 bool no_create = false;
[39330200]84 vfs_stat_t file_stat;
[971cc0cc]85 int fd = -1;
[216d6fc]86 char *buff = NULL;
[a35b458]87
[216d6fc]88 DIR *dirp;
[a35b458]89
[948222e4]90 c = 0;
91 optreset = 1;
92 optind = 0;
93 longind = 0;
94
95 while (c != -1) {
[971cc0cc]96 c = getopt_long(argc, argv, "c", long_options, &longind);
97 switch (c) {
98 case 'c':
99 no_create = true;
100 break;
101 }
102 }
[a35b458]103
[971cc0cc]104 if (argc - optind < 1) {
105 printf("%s: Incorrect number of arguments. Try `help %s extended'\n",
106 cmdname, cmdname);
[216d6fc]107 return CMD_FAILURE;
108 }
[a35b458]109
[971cc0cc]110 for (i = optind; argv[i] != NULL; i++) {
[095003a8]111 buff = str_dup(argv[i]);
[971cc0cc]112 if (buff == NULL) {
113 cli_error(CL_ENOMEM, "Out of memory");
114 ret++;
115 continue;
116 }
[a35b458]117
[216d6fc]118 dirp = opendir(buff);
119 if (dirp) {
[971cc0cc]120 cli_error(CL_ENOTSUP, "`%s' is a directory", buff);
[216d6fc]121 closedir(dirp);
[971cc0cc]122 free(buff);
123 ret++;
[216d6fc]124 continue;
125 }
[a35b458]126
[971cc0cc]127 /* Check whether file exists if -c (--no-create) option is given */
[23a0368]128 if ((!no_create) ||
129 ((no_create) && (vfs_stat_path(buff, &file_stat) == EOK))) {
[b7fd2a0]130 errno_t rc = vfs_lookup(buff, WALK_REGULAR | WALK_MAY_CREATE, &fd);
[f77c1c9]131 if (rc != EOK) {
132 fd = -1;
133 }
[23a0368]134 }
[a35b458]135
[216d6fc]136 if (fd < 0) {
[971cc0cc]137 cli_error(CL_EFAIL, "Could not update or create `%s'", buff);
138 free(buff);
139 ret++;
[216d6fc]140 continue;
[971cc0cc]141 } else {
[9c4cf0d]142 vfs_put(fd);
[971cc0cc]143 fd = -1;
144 }
[a35b458]145
[216d6fc]146 free(buff);
147 }
[a35b458]148
[216d6fc]149 if (ret)
150 return CMD_FAILURE;
151 else
152 return CMD_SUCCESS;
153}
Note: See TracBrowser for help on using the repository browser.