GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
rename.c
Go to the documentation of this file.
1 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <grass/gis.h>
22 
23 
35 int G_rename_file(const char *oldname, const char *newname)
36 {
37 
38 #ifdef __MINGW32__
39  remove(newname);
40 #endif
41 
42  return rename(oldname, newname);
43 }
44 
62 int G_rename(const char *element, const char *oldname, const char *newname)
63 {
64  const char *mapset;
65  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
66  char from[512], to[512];
67 
68  /* name in mapset legal only if mapset is current mapset */
69  mapset = G_mapset();
70  if (G__name_is_fully_qualified(oldname, xname, xmapset)
71  && strcmp(mapset, xmapset))
72  return -1;
73  if (G__name_is_fully_qualified(newname, xname, xmapset)
74  && strcmp(mapset, xmapset))
75  return -1;
76 
77  /* if file does not exist return 0 */
78  if (access(G__file_name(from, element, oldname, mapset), 0) != 0)
79  return 0;
80 
81  G__file_name(to, element, newname, mapset);
82 
83  /* return result of rename */
84  return G_rename_file(from, to) == 0 ? 1 : -1;
85 }