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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b19e892 was b19e892, checked in by Jakub Jermar <jakub@…>, 8 years ago

Merge open() with posix_open() and provide vfs_lookup_open() instead

vfs_lookup_open() is really just a convenience wrapper around
vfs_lookup() and vfs_open().

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 * Copyright (c) 2008 Tim Post
3 * All rights reserved.
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 *
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.
16 *
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.
27 */
28
29/*
30 * TODO: Options that people would expect, such as specifying the access time,
31 * etc.
32 */
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <unistd.h>
37#include <dirent.h>
38#include <sys/types.h>
39#include <str.h>
40#include <getopt.h>
41#include <errno.h>
42#include <vfs/vfs.h>
43
44#include "config.h"
45#include "errors.h"
46#include "util.h"
47#include "entry.h"
48#include "touch.h"
49#include "cmds.h"
50
51static const char *cmdname = "touch";
52
53static struct option const long_options[] = {
54 { "no-create", no_argument, 0, 'c' },
55 { 0, 0, 0, 0 }
56};
57
58/* Dispays help for touch in various levels */
59void help_cmd_touch(unsigned int level)
60{
61 if (level == HELP_SHORT) {
62 printf("`%s' updates access times of files\n", cmdname);
63 } else {
64 help_cmd_touch(HELP_SHORT);
65 printf("Usage: `%s' [-c|--no-create] <file>...\n\n"
66 "If the file does not exist it will be created empty,\n"
67 "unless -c (--no-create) is supplied.\n\n"
68 "Options:\n"
69 " -c, --no-create Do not create new files\n",
70 cmdname);
71 }
72
73 return;
74}
75
76/* Main entry point for touch, accepts an array of arguments */
77int cmd_touch(char **argv)
78{
79 unsigned int argc = cli_count_args(argv);
80 unsigned int i = 0;
81 unsigned int ret = 0;
82 int c;
83 int longind;
84 bool no_create = false;
85 struct stat file_stat;
86 int fd = -1;
87 char *buff = NULL;
88
89 DIR *dirp;
90
91 for (c = 0, optreset = 1, optind = 0, longind = 0; c != -1; ) {
92 c = getopt_long(argc, argv, "c", long_options, &longind);
93 switch (c) {
94 case 'c':
95 no_create = true;
96 break;
97 }
98 }
99
100 if (argc - optind < 1) {
101 printf("%s: Incorrect number of arguments. Try `help %s extended'\n",
102 cmdname, cmdname);
103 return CMD_FAILURE;
104 }
105
106 for (i = optind; argv[i] != NULL; i++) {
107 buff = str_dup(argv[i]);
108 if (buff == NULL) {
109 cli_error(CL_ENOMEM, "Out of memory");
110 ret++;
111 continue;
112 }
113
114 dirp = opendir(buff);
115 if (dirp) {
116 cli_error(CL_ENOTSUP, "`%s' is a directory", buff);
117 closedir(dirp);
118 free(buff);
119 ret++;
120 continue;
121 }
122
123 /* Check whether file exists if -c (--no-create) option is given */
124 if ((!no_create) ||
125 ((no_create) && (vfs_stat_path(buff, &file_stat) == EOK))) {
126 fd = vfs_lookup(buff, WALK_REGULAR | WALK_MAY_CREATE);
127 }
128
129 if (fd < 0) {
130 cli_error(CL_EFAIL, "Could not update or create `%s'", buff);
131 free(buff);
132 ret++;
133 continue;
134 } else {
135 close(fd);
136 fd = -1;
137 }
138
139 free(buff);
140 }
141
142 if (ret)
143 return CMD_FAILURE;
144 else
145 return CMD_SUCCESS;
146}
Note: See TracBrowser for help on using the repository browser.