00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <unistd.h>
00037 #include <fcntl.h>
00038 #include <dirent.h>
00039 #include <sys/types.h>
00040 #include <str.h>
00041 
00042 #include "config.h"
00043 #include "errors.h"
00044 #include "util.h"
00045 #include "entry.h"
00046 #include "touch.h"
00047 #include "cmds.h"
00048 
00049 static const char *cmdname = "touch";
00050 
00051 
00052 void help_cmd_touch(unsigned int level)
00053 {
00054         if (level == HELP_SHORT) {
00055                 printf("`%s' updates access times for files\n", cmdname);
00056         } else {
00057                 help_cmd_touch(HELP_SHORT);
00058                 printf("  `%s' <file>, if the file does not exist it will be "
00059                                 "created\n", cmdname);
00060         }
00061 
00062         return;
00063 }
00064 
00065 
00066 int cmd_touch(char **argv)
00067 {
00068         unsigned int argc, i = 0, ret = 0;
00069         int fd;
00070         char *buff = NULL;
00071 
00072         DIR *dirp;
00073 
00074         argc = cli_count_args(argv);
00075 
00076         if (argc == 1) {
00077                 printf("%s - incorrect number of arguments. Try `help %s extended'\n",
00078                         cmdname, cmdname);
00079                 return CMD_FAILURE;
00080         }
00081 
00082         for (i = 1; i < argc; i ++) {
00083                 buff = str_dup(argv[i]);
00084                 dirp = opendir(buff);
00085                 if (dirp) {
00086                         cli_error(CL_ENOTSUP, "%s is a directory", buff);
00087                         closedir(dirp);
00088                         ret ++;
00089                         continue;
00090                 }
00091 
00092                 fd = open(buff, O_RDWR | O_CREAT);
00093                 if (fd < 0) {
00094                         cli_error(CL_EFAIL, "Could not update / create %s ", buff);
00095                         ret ++;
00096                         continue;
00097                 } else
00098                         close(fd);
00099 
00100                 free(buff);
00101         }
00102 
00103         if (ret)
00104                 return CMD_FAILURE;
00105         else
00106                 return CMD_SUCCESS;
00107 }
00108