# include "basic.h"

int substr_func (int argc, char **argv) {

  int N1, N2, len;
  char *c, *string;

  if ((argc != 4) && (argc != 5)) {
    gprint (GP_ERR, "USAGE: substr (string) N1 N2 [var]\n");
    return (FALSE);
  }

  N1 = atof (argv[2]);
  N2 = atof (argv[3]);

  len = strlen (argv[1]);
  if ((N1 >= len) || (N1 + N2 > len)) {
    c = (char *) NULL;
  } else {
    c = strncreate (&argv[1][N1], N2);
  }

  if (c == (char *) NULL) {
    string = strcreate ("");
  } else {
    string = strcreate (c);
  }

  if (argc == 5) {
    set_str_variable (argv[4], string);
  } else {
    gprint (GP_ERR, "%s\n", string);
  }
  free (string);
  return (TRUE);

}
