File: remove.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (69 lines) | stat: -rw-r--r-- 1,648 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/***********************************************************************
 *
 *  G_remove (element, name)
 *     char *element          mapset element containing name
 *     char *name             file name to be removed
 *
 *  Only files in current mapset can be removed
 *
 *  Returns  -1  on fail
 *            0  if no file
 *            1  if successful
 *
 ***********************************************************************/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "gis.h"


/*!
 * \brief remove a database file
 *
 * The file or directory <b>name</b> under the database <b>element</b> directory
 * in the current mapset is removed.
 * Returns 1 if successful, 0 if <b>name</b> does not exist, and -1 if there
 * was an error.
 * <b>Note.</b> If <b>name</b> is a directory, everything within the
 * directory is removed as well.
 *
 *  \param element
 *  \param name
 *  \return int
 */

int G_remove ( char *element, char *name)
{
    char path[1040];
    char *mapset;
    char xname[512], xmapset[512];
    char cmd[1100];

    if (G_legal_filename(name) < 0)
	    return -1;

/* name in mapset legal only if mapset is current mapset */
    mapset = G_mapset();
    if (G__name_is_fully_qualified (name, xname, xmapset)
    && strcmp (mapset, xmapset))
	    return -1;

/* if file does not exist, return 0 */
    if (access (G__file_name (path, element, name, mapset),0) != 0)
	    return 0;

    if ( remove ( path ) == 0)
	    return 1;
    if (strchr(path, '\''))
	    return -1;

    sprintf(cmd, "rm -rf '%s'", path);

    if (G_system(cmd) == 0)
	    return 1;

    return -1;
}