home *** CD-ROM | disk | FTP | other *** search
- /* getcursor.c
- *
- * Functions to deal with allocating and freeing cursors for Oracle
- */
- /* Copyright 1991 Kevin Stock.
- *
- * You may copy this under the terms of the GNU General Public License,
- * a copy of which should have accompanied your Perl kit.
- */
-
- #include "EXTERN.h"
- #include <stdio.h>
- #include <ctype.h>
- #include "orafns.h"
-
-
- /* head of the cursor list */
- struct cursor csr_list = { NULL, NULL, NULL, 0, NULL };
-
-
- /* ora_free_data(csr)
- *
- * Frees memory attached to csr->data
- */
-
- void ora_free_data(csr)
- struct cursor *csr;
- {
- int i;
-
- DEBUG(8, (fprintf(stderr, "ora_free_data(%#lx)\n", (long) csr)));
-
- if (csr->data == NULL)
- {
- DEBUG(8, (fputs("ora_free_data: returning\n", stderr)));
- return;
- }
-
- for (i = 0 ; i < csr->nfields ; i++)
- {
- if (csr->data[i] != NULL)
- {
- DEBUG(128, (fprintf(stderr, "freeing (%d) == %#lx\n",
- i, (long) csr->data[i])));
- free(csr->data[i]);
- }
- }
-
- DEBUG(128, (fprintf(stderr, "freeing %#lx\n", (long) csr->data)));
- free(csr->data);
- csr->data = NULL;
- csr->nfields = 0;
- DEBUG(8, (fputs("ora_free_data: returning\n", stderr)));
- }
-
-
- /* ora_getcursor()
- *
- * Allocates memory for a new cursor and returns its address.
- * Inserts the cursor at the front of the list.
- * Returns NULL if it can't get enough memory.
- */
-
- struct cursor *ora_getcursor()
- {
- struct cursor *tmp;
-
- DEBUG(8, (fputs("ora_getcursor()\n", stderr)));
-
- if ((tmp = (struct cursor *) malloc(sizeof(struct cursor))) == NULL)
- {
- DEBUG(128, (fputs("ora_getcursor: out of memory\n", stderr)));
- DEBUG(8, (fputs("ora_getcursor: returning NULL\n", stderr)));
- ora_errno = ORAP_NOMEM;
- return(NULL);
- }
- DEBUG(128, (fprintf(stderr,
- "ora_getcursor: got cursor at %#lx\n", (long) tmp)));
-
- if ((tmp->csr = (struct csrdef *) malloc(sizeof(struct csrdef))) == NULL)
- {
- free(tmp);
- DEBUG(128, (fputs("ora_getcursor: out of memory\n", stderr)));
- DEBUG(8, (fputs("ora_getcursor: returning NULL\n", stderr)));
- ora_errno = ORAP_NOMEM;
- return(NULL);
- }
- DEBUG(128, (fprintf(stderr,
- "ora_getcursor: got csr at %#lx\n", tmp->csr)));
-
- tmp->hda = NULL;
- tmp->data = NULL;
- tmp->nfields = 0;
- tmp->next = csr_list.next;
- csr_list.next = tmp;
-
- ora_errno = 0;
- DEBUG(8, (fprintf(stderr,"ora_getcursor: returning %#lx\n",(long)tmp)));
- return(tmp);
- }
-
-
- /* ora_getlda()
- *
- * Gets a new login data area.
- * Uses ora_getcursor and then allocates the host data area.
- */
-
- struct cursor *ora_getlda()
- {
- struct cursor *tmp;
-
- DEBUG(8, (fputs("ora_getlda()\n", stderr)));
-
- if ((tmp = ora_getcursor()) == NULL)
- {
- DEBUG(8, (fputs("ora_getlda: returning NULL\n", stderr)));
- return(NULL);
- }
-
- if ((tmp->hda = malloc(256)) == NULL)
- {
- DEBUG(128, (fputs("ora_getlda: out of memory\n", stderr)));
- ora_dropcursor(tmp);
- DEBUG(8, (fputs("ora_getlda: returning NULL\n", stderr)));
- ora_errno = ORAP_NOMEM;
- return(NULL);
- }
- DEBUG(128, (fprintf(stderr,
- "ora_getlda: got hda at %#lx\n", tmp->hda)));
-
- DEBUG(8, (fprintf(stderr, "ora_getlda: returning %#lx\n", tmp)));
- return(tmp);
- }
-
-
- /* ora_dropcursor(csr)
- *
- * Frees the space occupied by a given cursor, removing it from the list.
- */
-
- int ora_dropcursor(csr)
- struct cursor *csr;
- {
- struct cursor *tmp, *t;
-
- tmp = &csr_list;
-
- DEBUG(8, (fprintf(stderr, "ora_dropcursor(%#lx)\n", (long) csr)));
-
- while ((tmp->next != NULL) && (tmp->next != csr))
- {
- tmp = tmp->next;
- }
-
- if (tmp->next == NULL)
- {
- DEBUG(8, (fputs("ora_dropcursor: invalid\n", stderr)));
- ora_errno = ORAP_INVCSR;
- return(0);
- }
-
- t = tmp->next;
-
- if (t->hda != NULL)
- {
- DEBUG(128, (fprintf(stderr,
- "ora_dropcursor: freeing hda at %#lx\n", (long) t->hda)));
- free(t->hda);
- }
- if (t->data != NULL)
- {
- DEBUG(128, (fputs("ora_dropcursor: freeing data\n", stderr)));
- ora_free_data(t);
- }
-
- DEBUG(128, (fprintf(stderr,
- "ora_dropcursor: freeing csr at %#lx\n", (long) t->csr)));
- free(t->csr);
-
- t = t->next;
- DEBUG(128, (fprintf(stderr,
- "ora_dropcursor: freeing cursor at %#lx\n", (long) tmp->next)));
- free(tmp->next);
- tmp->next = t;
-
- DEBUG(8, (fputs("ora_dropcursor: returning\n", stderr)));
- return(1);
- }
-
-
- /* ora_droplda()
- *
- * This is just here for completeness' sake.
- * (I suppose we could check the value of hda in dropcursor and droplda
- * but I don't think it's worth it
- */
-
- int ora_droplda(lda)
- struct cursor *lda;
- {
- DEBUG(8, (fprintf(stderr,
- "ora_droplda(%#lx): calling ora_dropcursor\n", lda)));
- return(ora_dropcursor(lda));
- }
-
-
- /* ora_findcursor()
- *
- * Checks whether the specified csr is present in the list
- */
-
- int ora_findcursor(csr)
- struct cursor *csr;
- {
- struct cursor *tmp;
-
- tmp = &csr_list;
-
- DEBUG(8, (fprintf(stderr, "ora_findcursor(%#lx)\n", (long) csr)));
-
- while ((tmp->next != NULL) && (tmp->next != csr))
- {
- tmp = tmp->next;
- }
-
- if (tmp->next == NULL)
- {
- DEBUG(8, (fputs("ora_findcursor: not valid\n", stderr)));
- return(0);
- }
-
- DEBUG(8, (fputs("ora_findcursor: valid\n", stderr)));
- return(1);
- }
-
-
- /* check_lda()
- *
- * Checks whether the given address corresponds to a valid lda
- */
-
- int check_lda(lda)
- struct cursor *lda;
- {
- DEBUG(8, (fprintf(stderr, "check_lda(%#lx)\n", (long) lda)));
-
- if (ora_findcursor(lda) && (lda->hda != NULL) && (lda->data == NULL))
- {
- DEBUG(8, (fputs("check_lda: valid\n", stderr)));
- return (1);
- }
- else
- {
- DEBUG(8, (fputs("check_lda: invalid\n", stderr)));
- return (0);
- }
- };
-
-
- /* check_csr()
- *
- * Checks whether the given address corresponds to a valid csr
- */
-
- int check_csr(csr)
- struct cursor *csr;
- {
- DEBUG(8, (fprintf(stderr, "check_csr(%#lx)\n", (long) csr)));
-
- if (ora_findcursor(csr) && (csr->hda == NULL) && (csr->data != NULL))
- {
- DEBUG(8, (fputs("check_csr: valid\n", stderr)));
- return (1);
- }
- else
- {
- DEBUG(8, (fputs("check_csr: invalid\n", stderr)));
- return (0);
- }
- };
-