示例#1
0
/* Save the current file on disk. Return 0 on success, 1 on error. */
int editorSave(void) {
    int len;
    char *buf = editorRowsToString(&len);
    int fd = open(E.filename,O_WRONLY|O_TRUNC,0644);
    if (fd == -1) goto writeerr;

    if (write(fd,buf,len) != len) goto writeerr;

    close(fd);
    free(buf);
    E.dirty = 0;
    editorSetStatusMessage("%d bytes written on disk", len);
    return 0;

writeerr:
    free(buf);
    if (fd != -1) close(fd);
    editorSetStatusMessage("Can't save! I/O error: %s",strerror(errno));
    return 1;
}
示例#2
0
文件: kilo.c 项目: Ruk33/kilo
/* Save the current file on disk. Return 0 on success, 1 on error. */
int editorSave(void) {
    int len;
    char *buf = editorRowsToString(&len);
    int fd = open(E.filename,O_RDWR|O_CREAT,0644);
    if (fd == -1) goto writeerr;

    /* Use truncate + a single write(2) call in order to make saving
     * a bit safer, under the limits of what we can do in a small editor. */
    if (ftruncate(fd,len) == -1) goto writeerr;
    if (write(fd,buf,len) != len) goto writeerr;

    close(fd);
    free(buf);
    E.dirty = 0;
    editorSetStatusMessage("%d bytes written on disk", len);
    return 0;

writeerr:
    free(buf);
    if (fd != -1) close(fd);
    editorSetStatusMessage("Can't save! I/O error: %s",strerror(errno));
    return 1;
}
int main(int argc, char **argv) {
  // takdit <ファイル名> という引数のみ実行
  if (argc != 2) {
    fprintf(stderr, "Usage: takdit <filename>\n");
    exit(1);
  }

  initEditor();                         // エディタの初期化
  editorSelectSyntaxHighlight(argv[1]); // シンタックスハイライトの適用
  editorOpen(argv[1]);                  // ファイルを開く
  enableRawMode(STDIN_FILENO);          // Rawモードの有効化
  editorSetStatusMessage(
      "HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find");
  while (1) {
    editorRefreshScreen();                // 変更の反映
    editorProcessKeypress(STDIN_FILENO);  // キー入力待ち
  }
  return 0;
}