0

I am expanding the kilo text editor to include copying-and-pasting. I am getting a segfault, which I have narrowed down to be because of a strcpy. I am using a typedef to store information about the marking. Here's that typedef, as well as where it's used:

typedef struct editorCursor {
  char copied[80];
  int marking;
  int sx;
  int sy;
} cursor;
struct editorSyntax {
  char *filetype;
  char **filematch;
  char **keywords;
  char *singleline_comment_start;
  char *multiline_comment_start;
  char *multiline_comment_end;
  int flags;
};

My strcpy looks like this:

void editorCopy() {
  strcpy(E.cursor->copied,editorPrompt("What to copy %s", NULL)); 
}

Here's the function editorPrompt:

char *editorPrompt(char *prompt, void (*callback)(char *, int)) {
  size_t bufsize = 128;
  char *buf = malloc(bufsize);
  size_t buflen = 0;
  buf[0] = '\0';
  while (1) {
    editorSetStatusMessage(prompt, buf);
    editorRefreshScreen();
    int c = editorReadKey();
    if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE) {
      if (buflen != 0) buf[--buflen] = '\0';
    } else if (c == '\x1b') {
      editorSetStatusMessage("");
      if (callback) callback(buf, c);
      free(buf);
      return NULL;
    } else if (c == '\r') {
      if (buflen != 0) {
        editorSetStatusMessage("");
        if (callback) callback(buf, c);
        return buf;
      }
    } else if (!iscntrl(c) && c < 128) {
      if (buflen == bufsize - 1) {
        bufsize *= 2;
        buf = realloc(buf, bufsize);
      }
      buf[buflen++] = c;
      buf[buflen] = '\0';
    }
    if (callback) callback(buf, c);
  }
}

ARI FISHER
  • 343
  • 1
  • 13

0 Answers0