rename.c

Go to the documentation of this file.
00001 /***********************************************************************
00002  *
00003  *  G_rename (element, oldname, newname)
00004  *     char *element          element in mapset containing name
00005  *     char *oldname          file name to be renamed
00006  *     char *newname          new name for file
00007  *
00008  *  Only files in current mapset can be renamed
00009  *
00010  *  Returns  -1  on fail
00011  *            0  if no file
00012  *            1  if successful
00013  *
00014  ***********************************************************************/
00015 
00016 #include <string.h>
00017 #include <unistd.h>
00018 #include <stdlib.h>
00019 #include "gis.h"
00020 
00021 
00038 int G_rename ( char *element,
00039     char *oldname, char *newname)
00040 {
00041     char mv[1024];
00042     char *path;
00043     char *mapset;
00044     char xname[512], xmapset[512];
00045 
00046 /* name in mapset legal only if mapset is current mapset */
00047     mapset = G_mapset();
00048     if (G__name_is_fully_qualified (oldname, xname, xmapset)
00049     && strcmp (mapset, xmapset))
00050             return -1;
00051     if (G__name_is_fully_qualified (newname, xname, xmapset)
00052     && strcmp (mapset, xmapset))
00053             return -1;
00054 
00055     strcpy (mv, "mv ");
00056     path = mv + strlen (mv);
00057 
00058 /* if file does not exist return 0 */
00059     if (access (G__file_name (path, element, oldname, mapset),0) != 0)
00060             return 0;
00061 
00062 /* now add new name to mv command */
00063     path = mv + strlen (mv);
00064     *path++ = ' ';
00065     G__file_name (path, element, newname, mapset);
00066 
00067 /* return result of the mv command */
00068     return system (mv) == 0 ? 1 : -1;
00069 }

Generated on Mon Jan 1 19:49:26 2007 for GRASS by  doxygen 1.5.1