Ignore:
Timestamp:
2012-04-08T17:53:15Z (12 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c5bff3c
Parents:
1b90e90 (diff), f3378ba (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/cmds/modules/touch/touch.c

    r1b90e90 r81716eb  
    2727 */
    2828
    29 /* TODO: Options that people would expect, such as not creating the file if
    30  * it doesn't exist, specifying the access time, etc */
     29/*
     30 * TODO: Options that people would expect, such as specifying the access time,
     31 * etc.
     32 */
    3133
    3234#include <stdio.h>
     
    3739#include <sys/types.h>
    3840#include <str.h>
     41#include <getopt.h>
     42#include <sys/stat.h>
     43#include <errno.h>
    3944
    4045#include "config.h"
     
    4752static const char *cmdname = "touch";
    4853
     54static struct option const long_options[] = {
     55        { "no-create", no_argument, 0, 'c' },
     56        { 0, 0, 0, 0 }
     57};
     58
    4959/* Dispays help for touch in various levels */
    5060void help_cmd_touch(unsigned int level)
    5161{
    5262        if (level == HELP_SHORT) {
    53                 printf("`%s' updates access times for files\n", cmdname);
     63                printf("`%s' updates access times of files\n", cmdname);
    5464        } else {
    5565                help_cmd_touch(HELP_SHORT);
    56                 printf("  `%s' <file>, if the file does not exist it will be "
    57                                 "created\n", cmdname);
     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);
    5872        }
    59 
     73       
    6074        return;
    6175}
     
    6478int cmd_touch(char **argv)
    6579{
    66         unsigned int argc, i = 0, ret = 0;
    67         int fd;
     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;
    6888        char *buff = NULL;
    69 
     89       
    7090        DIR *dirp;
    71 
    72         argc = cli_count_args(argv);
    73 
    74         if (argc == 1) {
    75                 printf("%s - incorrect number of arguments. Try `help %s extended'\n",
    76                         cmdname, cmdname);
     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);
    77104                return CMD_FAILURE;
    78105        }
    79 
    80         for (i = 1; i < argc; i ++) {
     106       
     107        for (i = optind; argv[i] != NULL; i++) {
    81108                buff = str_dup(argv[i]);
     109                if (buff == NULL) {
     110                        cli_error(CL_ENOMEM, "Out of memory");
     111                        ret++;
     112                        continue;
     113                }
     114               
    82115                dirp = opendir(buff);
    83116                if (dirp) {
    84                         cli_error(CL_ENOTSUP, "%s is a directory", buff);
     117                        cli_error(CL_ENOTSUP, "`%s' is a directory", buff);
    85118                        closedir(dirp);
    86                         ret ++;
     119                        free(buff);
     120                        ret++;
    87121                        continue;
    88122                }
    89 
    90                 fd = open(buff, O_RDWR | O_CREAT);
     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               
    91128                if (fd < 0) {
    92                         cli_error(CL_EFAIL, "Could not update / create %s ", buff);
    93                         ret ++;
     129                        cli_error(CL_EFAIL, "Could not update or create `%s'", buff);
     130                        free(buff);
     131                        ret++;
    94132                        continue;
    95                 } else
     133                } else {
    96134                        close(fd);
    97 
     135                        fd = -1;
     136                }
     137               
    98138                free(buff);
    99139        }
    100 
     140       
    101141        if (ret)
    102142                return CMD_FAILURE;
     
    104144                return CMD_SUCCESS;
    105145}
    106 
Note: See TracChangeset for help on using the changeset viewer.