Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 3295 → Rev 3296

/branches/shell/uspace/app/bdsh/cmds/modules/rm/rm.c
1,13 → 1,41
/* Automatically generated by mknewcmd on Mon Aug 11 00:14:56 PHT 2008
* This is machine generated output. The author of mknewcmd claims no
* copyright over the contents of this file. Where legally permitted, the
* contents herein are donated to the public domain.
/* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
* All rights reserved.
*
* You should apply any license and copyright that you wish to this file,
* replacing this header in its entirety. */
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the original program's authors nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include "config.h"
#include "errors.h"
#include "util.h"
#include "entry.h"
#include "rm.h"
#include "cmds.h"
14,12 → 42,53
 
static char *cmdname = "rm";
 
unsigned int rm_recursive(const char *path)
{
cli_error(CL_ENOTSUP, "I do not yet remove directories");
return 1;
}
 
unsigned int rm_single(const char *path)
{
if (unlink(path)) {
cli_error(CL_EFAIL, "rm: could not remove file %s", path);
return 1;
}
return 0;
}
 
unsigned int rm_scope(const char *path)
{
int fd;
DIR *dirp;
 
dirp = opendir(path);
if (dirp) {
closedir(dirp);
return RM_DIR;
}
 
fd = open(path, O_RDONLY);
if (fd > 0) {
close(fd);
return RM_FILE;
}
 
return RM_BOGUS;
}
 
/* Dispays help for rm in various levels */
void * help_cmd_rm(unsigned int level)
{
printf("This is the %s help for '%s'.\n",
level ? EXT_HELP : SHORT_HELP, cmdname);
return 0;
if (level == HELP_SHORT) {
printf("`%s' removes files and directories.\n", cmdname);
} else {
help_cmd_rm(HELP_SHORT);
printf(" %s <path> If <path> is a directory, all files and "
"directories\n within will be unconditionally removed.\n",
cmdname);
}
return CMD_VOID;
}
 
/* Main entry point for rm, accepts an array of arguments */
26,23 → 95,39
int * cmd_rm(char **argv)
{
unsigned int argc;
unsigned int i;
unsigned int i, scope, ret = 0;
char *buff;
 
/* Count the arguments */
for (argc = 0; argv[argc] != NULL; argc ++);
 
printf("%s %s\n", TEST_ANNOUNCE, cmdname);
printf("%d arguments passed to %s", argc - 1, cmdname);
 
if (argc < 2) {
printf("\n");
return 0;
cli_error(CL_EFAIL,
"%s - insufficient arguments. Try `help %s extended'",
cmdname, cmdname);
return CMD_FAILURE;
}
 
printf(":\n");
for (i = 1; i < argc; i++)
printf("[%d] -> %s\n", i, argv[i]);
for (i = 1; i < argc; i++) {
buff = cli_strdup(argv[i]);
scope = rm_scope(buff);
switch (scope) {
case RM_BOGUS:
cli_error(CL_ENOENT, "Can not unlink %s", buff);
break;
case RM_FILE:
ret += rm_single(buff);
break;
case RM_DIR:
ret += rm_recursive(buff);
break;
}
free(buff);
}
 
return 0;
if (ret)
return CMD_FAILURE;
else
return CMD_SUCCESS;
}