1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <dirent.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <unistd.h>
7 #include <sys/stat.h>
8 #include <aos/cli.h>
9 #include <stdbool.h>
10 #include <path_helper.h>
11 
touch_do(const char * file)12 static int touch_do(const char *file)
13 {
14     int fd;
15 
16     fd = open(file, O_WRONLY | O_CREAT);
17     if (fd < 0) {
18         aos_cli_printf("open %s failed - %s\n", file, strerror(errno));
19         return -1;
20     }
21 
22     close(fd);
23     return 0;
24 }
25 
touch_main(int argc,char ** argv)26 static int touch_main(int argc, char **argv)
27 {
28     int index;
29     int ret = 0;
30     char abspath[256];
31 
32     for (index = 1; index < argc; index++) {
33         char *file = argv[index];
34 
35         memset(abspath, 0 , sizeof(abspath));
36         file = get_realpath(file, abspath, sizeof(abspath));
37         if (!file) {
38             aos_cli_printf("Failed to get real path!\r\n");
39             return -1;
40         }
41 
42         if (access(file, F_OK))
43             ret |= touch_do(file);
44     }
45     return ret;
46 }
47 ALIOS_CLI_CMD_REGISTER(touch_main, touch, create empty file);
48