-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave.c
More file actions
30 lines (30 loc) · 782 Bytes
/
save.c
File metadata and controls
30 lines (30 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "save.h"
void saveToFile(){//TODO probably with the automatic cursor movement
if(E.file==NULL){
E.file=editorPrompt("Save as: %s(ESC to cancel)",NULL);
if(E.file==NULL){
editorSetStatusMessage("Save Aborted");
return;
}
editorSelectSyntaxHighlight();
}
int len;
char *text=editorRowsToString(&len);
int fd=open(E.file, O_RDWR|O_CREAT, 0644);
//O_RDWR: Read and write
//O_CREAT: creat if it doesn't exist //0644 defines permissions for the new file
if(fd!=-1){
if(ftruncate(fd,len)!=-1){
if(write(fd,text,len)==len){
close(fd);
free(text);
E.dirty=0;
editorSetStatusMessage("%d bytes written to disk",len);
return;
}
}
close(fd);
}
free(text);
// editorSetStatusMessage("Can't save! I/O error: %s",strerror(errno));
}