home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- /* function prototypes */
- int GetValue(char *src, char *target);
- void CgiError(char *msg);
-
- int main(int argc, char** argv)
- {
- char* path_info;
- char* t;
- int x, y, row, col;
- int cellsize, numcols, cellnum;
-
- /* We expect the first and only argument to be the image coordinates
- * the user selected in the form "x,y".
- */
-
- if (argc != 2) {
- CgiError("Wrong number of arguments, client may not support ISMAP.");
- }
-
- /*
- * Check the selected region
- */
-
- if (!(t = strchr(argv[1], ',')))
- {
- CgiError("Your client doesn't support image mapping properly.");
- }
-
- /* Terminate the string at the comma and then
- * convert the strings into numbers.
- */
-
- *t++ = '\0';
- x = atoi(argv[1]);
- y = atoi(t);
-
- /* Some browsers actually return negative numbers for coordinates,
- * which is a bug in the browser software. Check for this case and
- * reject the input. The only valid range of both x and y values for
- * this example is from 0 to 399 since the image has a 400 x 400 dimension.
- */
-
- if (x < 0 || y < 0)
- {
- CgiError("Your browser has returned negative coordinates, which is an error. Try upgrading your browser to the latest version.");
- }
-
- /* Store the environment variable PATH_INFO in a local variable */
-
- path_info = getenv("PATH_INFO");
-
- /* Parse out the cellsize and numcols parameters from the path info
- * otherwise we cannot do anything.
- */
-
- if (path_info == NULL
- || (cellsize = GetValue(path_info, "/cellsize=")) < 1
- || (numcols = GetValue(path_info, "/numcols=")) < 1)
- {
- CgiError("Unable to determine image size from request");
- }
-
- /* The input is correct so output what the user clicked on */
-
- printf("Content-type: text/html\n\n");
-
- printf("<HTML>\n");
- printf("<HEAD><TITLE>CGI Script How-To: Test Script</TITLE></HEAD>\n");
- printf("<BODY>\n");
- printf("<H1>CGI Script How-to: Test Script</H1>\n");
-
- printf("You clicked on the region: <b>x=%d, y=%d</b><P>\n", x, y);
-
- /* Calculate the row and column numbers from the (x,y) coordinates
- * where column 1, row 1 is the top-left cell and column 4, row 4
- * is the bottom-right cell.
- */
-
- col = (x / cellsize) + 1;
- row = (y / cellsize) + 1;
-
- printf("This maps to row <b>#%d</b> and column <b>#%d</b><P>\n", row, col);
-
- /* Given the row and column numbers just computed and the number
- * of columns we can figure out the cell number.
- */
-
- cellnum = (row - 1) * numcols + col;
-
- printf("The number of this cell is <b>%d</b>\n", cellnum);
- printf("</BODY></HTML>\n");
- exit(0);
- }
-
-
- /* function CgiError
- *
- * Displays an error message to the client and aborts the program.
- *
- * where msg = the error message to display.
- */
-
- void CgiError(char *msg)
- {
- printf("Content-type: text/html\n\n");
- printf("<HTML>\n");
- printf("<HEAD><TITLE>Image Mapping Error</TITLE></HEAD>\n");
- printf("<BODY>\n");
- printf("<H1>Image Mapping Error</H1>\n");
- printf("This CGI program encountered an error:\n");
- printf("<P>%s\n", msg);
- printf("</BODY></HTML>\n");
- exit(1);
- }
-
-
- /* function GetValue
- *
- * Finds pattern within string and returns
- * the corresponding "value=xxx" numeric value.
- *
- * where src = string to check with extra path information
- * target = search pattern (e.g., "/cellsize=")
- *
- * Returns: value (non-negative number) if successful
- * -1 if target string not found or number is invalid
- */
-
- int GetValue(char *src, char *target)
- {
- int value;
- char *s = strstr(src, target);
-
- if (s != NULL && sscanf(s + strlen(target), "%d", &value) == 1)
- {
- return value;
- }
-
- /* Note that negative values are not valid in this context so we can just
- * return the value as is. Whether the value is negative or the value is
- * not a number is an indication of an error. Only a positive non-zero
- * value will be accepted as a successful value.
- */
-
- return -1; /* target not found */
- }
-
- /*
- * end of testgrid.c
- */
-