Here's a revised patch.

lib/gis/auto_mask.c (G__check_for_auto_masking): Handle `MASK_OVERRIDE';
allow mask to be in any mapset in the search path.
lib/gis/mask_info.c (G__mask_info): Likewise.

        I believe I've fixed all the `strncpy' issues that my previous
        patch has had.  Also, I've changed the interpretation of the
        empty string value for `MASK_OVERRIDE' to ``ignore the
        variable's value'', since I believe it matches the rest of the
        Unix world.  (One still can ask for ``no mask'' by specifying a
        non-existent raster via `MASK_OVERRIDE'.)

        Since there're some modules accessing `MASK', there should be a
        libgis function to obtain the name of the mask currently in use
        (besides, this would simplify the code below.)  Any suggestions?

diff --git a/lib/gis/auto_mask.c b/lib/gis/auto_mask.c
index 6828018..ada9adb 100644
--- a/lib/gis/auto_mask.c
+++ b/lib/gis/auto_mask.c
@@ -32,6 +32,7 @@
 
 int G__check_for_auto_masking (void)
 {
+    char name[GNAME_MAX] = "MASK", mapset[GMAPSET_MAX] = "";
     struct Cell_head cellhd;
 
     /* if mask is switched off (-2) return -2
@@ -42,14 +43,32 @@ int G__check_for_auto_masking (void)
 
     /* if(G__.mask_fd > 0) G_free (G__.mask_buf);*/
 
-    /* look for the existence of the MASK file */
-    G__.auto_mask = (G_find_cell ("MASK", G_mapset()) != 0);
+    /* check for MASK_OVERRIDE */
+    {
+       const char *override = getenv ("MASK_OVERRIDE");
+
+       /* an empty string value makes the variable to be ignored */
+       if (override && override[0] != '\0') {
+           strncpy (name, override, sizeof (name) - 1);
+           name[sizeof (name) - 1] = '\0';
+       }
+    }
+
+    /* look for the existence of the MASK file; get the mapset name */
+    {
+       char *mask_mapset = G_find_cell (name, mapset);
+
+       if ((G__.auto_mask = (mask_mapset != 0)) > 0) {
+           strncpy (mapset, mask_mapset, sizeof (mapset) - 1);
+           mapset[sizeof (mapset) - 1] = '\0';
+       }
+    }
 
     if (G__.auto_mask <= 0)
         return 0;
 
     /* check MASK projection/zone against current region */
-    if (G_get_cellhd ("MASK", G_mapset(), &cellhd) >= 0)
+    if (G_get_cellhd (name, mapset, &cellhd) >= 0)
     {
        if (cellhd.zone != G_zone() || cellhd.proj != G_projection())
        {
@@ -59,11 +78,12 @@ int G__check_for_auto_masking (void)
     }
 
     G_unopen_cell(G__.mask_fd );
-    G__.mask_fd = G__open_cell_old ("MASK", G_mapset());
+    G__.mask_fd = G__open_cell_old (name, mapset);
     if (G__.mask_fd < 0)
     {
         G__.auto_mask = 0;
-        G_warning (_("Unable to open automatic MASK file"));
+        G_warning (_("Unable to open automatic MASK (<[EMAIL PROTECTED]>) 
file"),
+                  name, mapset);
         return 0;
     }
 
diff --git a/lib/gis/mask_info.c b/lib/gis/mask_info.c
index b82e8ea..3687ab1 100644
--- a/lib/gis/mask_info.c
+++ b/lib/gis/mask_info.c
@@ -58,11 +58,34 @@ int G__mask_info (
 {
     char rname[GNAME_MAX], rmapset[GMAPSET_MAX];
 
-    strcpy (name, "MASK");
-    strcpy (mapset, G_mapset());
+    /* NB: the following is painfully close to G__check_for_auto_masking
+     *     (); consider a separate function here
+     */
 
-    if(!G_find_cell (name, mapset))
-       return -1;
+    /* check for MASK_OVERRIDE */
+    {
+       const char *override = getenv ("MASK_OVERRIDE");
+       if (override != 0) {
+           strncpy (name, override, GNAME_MAX - 1);
+           name[GNAME_MAX - 1] = '\0';
+       } else {
+           strcpy  (name, "MASK");
+       }
+    }
+
+    /* look for the existence of the MASK file; get the mapset name */
+    {
+       char *mask_mapset = G_find_cell (name, "");
+
+       if (mask_mapset == 0) {
+           /* NB: `mapset' won't be initialized */
+           /* . */
+           return -1;
+       }
+
+       strncpy (mapset, mask_mapset, GMAPSET_MAX - 1);
+       mapset[GMAPSET_MAX - 1] = '\0';
+    }
 
     if(G_is_reclass (name, mapset, rname, rmapset) > 0)
     {

_______________________________________________
grass-dev mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/grass-dev

Reply via email to