Changeset 8df7a1c in mainline


Ignore:
Timestamp:
2008-11-29T12:29:00Z (16 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
890793b
Parents:
a3b5831
Message:

Several fixes for cp.

File:
1 edited

Legend:

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

    ra3b5831 r8df7a1c  
    3535#include <string.h>
    3636#include <fcntl.h>
     37#include <assert.h>
    3738#include "config.h"
    3839#include "util.h"
     
    7475        int fd1, fd2, bytes = 0;
    7576        off_t total = 0;
    76         int64_t copied = -1;
     77        int64_t copied = 0;
    7778        char *buff = NULL;
    7879
     
    8788        if (-1 == (fd2 = open(dest, O_CREAT))) {
    8889                printf("Unable to open destination file %s\n", dest);
     90                close(fd1);
    8991                return copied;
    9092        }
     
    9799        lseek(fd1, 0, SEEK_SET);
    98100
    99         if (NULL == (buff = (char *) malloc(blen + 1))) {
     101        if (NULL == (buff = (char *) malloc(blen))) {
    100102                printf("Unable to allocate enough memory to read %s\n",
    101                         src);
     103                    src);
     104                copied = -1;
    102105                goto out;
    103106        }
    104107
    105         do {
    106                 if (-1 == (bytes = read(fd1, buff, blen)))
    107                         break;
    108                 /* We read a terminating NULL */
    109                 if (0 == bytes) {
    110                         copied ++;
    111                         break;
    112                 }
     108        for (;;) {
     109                ssize_t res;
     110
     111                bytes = read(fd1, buff, blen);
     112                if (bytes <= 0)
     113                        break;
    113114                copied += bytes;
    114                 write(fd2, buff, blen);
    115         } while (bytes > 0);
    116 
    117         if (bytes == -1) {
    118                 printf("Error copying %s\n", src);
     115                res = bytes;
     116                do {
     117                        /*
     118                         * Theoretically, it may not be enough to call write()
     119                         * only once. Also the previous read() may have
     120                         * returned less data than requested.
     121                         */
     122                        bytes = write(fd2, buff, res);
     123                        if (bytes < 0)
     124                                goto err;
     125                        res -= bytes;
     126                } while (res > 0);
     127                assert(res == 0);
     128        }
     129
     130        if (bytes < 0) {
     131err:
     132                printf("Error copying %s, (%d)\n", src, bytes);
    119133                copied = bytes;
    120                 goto out;
    121134        }
    122135
     
    131144void help_cmd_cp(unsigned int level)
    132145{
     146        static char helpfmt[] =
     147            "Usage:  %s [options] <source> <dest>\n"
     148            "Options: (* indicates not yet implemented)\n"
     149            "  -h, --help       A short option summary\n"
     150            "  -v, --version    Print version information and exit\n"
     151            "* -V, --verbose    Be annoyingly noisy about what's being done\n"
     152            "* -f, --force      Do not complain when <dest> exists\n"
     153            "* -r, --recursive  Copy entire directories\n"
     154            "  -b, --buffer ## Set the read buffer size to ##\n"
     155            "Currently, %s is under development, some options may not work.\n";
    133156        if (level == HELP_SHORT) {
    134157                printf("`%s' copies files and directories\n", cmdname);
    135158        } else {
    136159                help_cmd_cp(HELP_SHORT);
    137                 printf(
    138                 "Usage:  %s [options] <source> <dest>\n"
    139                 "Options: (* indicates not yet implemented)\n"
    140                 "  -h, --help       A short option summary\n"
    141                 "  -v, --version    Print version information and exit\n"
    142                 "* -V, --verbose    Be annoyingly noisy about what's being done\n"
    143                 "* -f, --force      Do not complain when <dest> exists\n"
    144                 "* -r, --recursive  Copy entire directories\n"
    145                 "  -b, --buffer ## Set the read buffer size to ##\n"
    146                 "Currently, %s is under development, some options might not work.\n",
    147                 cmdname, cmdname);
     160                printf(helpfmt, cmdname, cmdname);
    148161        }
    149162
     
    178191                        if (-1 == (buffer = strtoint(optarg))) {
    179192                                printf("%s: Invalid buffer specification, "
    180                                         "(should be a number greater than zero)\n",
    181                                         cmdname);
     193                                    "(should be a number greater than zero)\n",
     194                                    cmdname);
    182195                                return CMD_FAILURE;
    183196                        }
     
    195208        if (argc != 2) {
    196209                printf("%s: invalid number of arguments. Try %s --help\n",
    197                         cmdname, cmdname);
     210                    cmdname, cmdname);
    198211                return CMD_FAILURE;
    199212        }
     
    204217                printf("%d bytes copied\n", ret);
    205218
    206         if (ret <= 0)
     219        if (ret >= 0)
    207220                return CMD_SUCCESS;
    208221        else
Note: See TracChangeset for help on using the changeset viewer.