2 * Copyright (C) 2008 Diego Hernan Borghetti.
24 E_File_Path
*e_file_get_paths(char *file
)
27 char *s
, *pwd
, *slast
;
28 int ndir_rem
, i
, count
, len
;
30 paths
= (E_File_Path
*)malloc(sizeof(E_File_Path
));
33 paths
->lock_file
= NULL
;
37 /* this is a absolute path, nothing to do here,
40 s
= strrchr(file
, '/');
41 paths
->path
= (char *)malloc(s
- file
+ 1);
42 strncpy(paths
->path
, file
, s
- file
);
43 paths
->path
[s
- file
]= '\0';
44 paths
->file
= strdup(file
);
46 else if (!strncmp(file
, "..", 2)) {
47 /* ok, the "hard" case, relative path. */
50 s
= strstr(file
, "..");
51 s
++; /* skip the first. */
61 /* get the current directory. */
64 /* now we need remove ndir_rem from the path. */
67 for (i
= len
-1; i
> 0; i
--) {
70 if (count
== ndir_rem
)
78 slast
+= 2; /* skip ./ */
80 paths
->path
= (char *)malloc(i
+1);
81 strncpy(paths
->path
, pwd
, i
);
83 paths
->file
= (char *)malloc(i
+ strlen(slast
) + 2);
84 strncpy(paths
->file
, pwd
, i
);
85 sprintf(paths
->file
+i
, "/%s", slast
);
89 paths
->path
= strdup(pwd
);
90 paths
->file
= (char *)malloc(strlen(paths
->path
) + strlen(file
) + 2);
91 sprintf(paths
->file
, "%s/%s", paths
->path
, file
);
95 paths
->lock_file
= (char *)malloc(strlen(paths
->file
)+6);
96 sprintf(paths
->lock_file
, "%s.eco~", paths
->file
);
102 int e_file_is_lock(E_File_Path
*paths
)
106 if (stat(paths
->lock_file
, &st
))
107 return(0); /* not locked. */
108 return(1); /* locked. */
111 void e_file_lock(E_File_Path
*paths
)
115 fp
= fopen(paths
->lock_file
, "w");
118 fprintf(fp
, "Eco lock file\n");
123 void e_file_lock_rem(E_File_Path
*paths
)
126 unlink(paths
->lock_file
);
131 E_Buffer
*e_file_read(char *file
, int *status
)
138 /* clean error status. */
141 bf
= e_buffer_new(file
);
145 if (stat(bf
->paths
->file
, &st
)) {
150 if (e_file_is_lock(bf
->paths
)) {
151 /* we already have this file open!! in other session!! */
157 if (!(S_ISREG(st
.st_mode
))) {
158 /* this is not a regular file. */
165 fp
= fopen(bf
->paths
->file
, "r");
167 /* the file exist, but we can't read it. */
177 e_buffer_newline(bf
);
179 e_buffer_insert(bf
, '\t');
181 e_buffer_insert(bf
, c
);
185 /* always go to the start with new files. */
186 e_buffer_goto_begin(bf
);
188 /* e_buffer_insert/newline mark the buffer with changes,
189 * but in this case we are reading the file, so unmark
192 BUFFER_UNSET(bf
, BUFFER_FLUSH
);
194 /* and lock the file. */
195 e_file_lock(bf
->paths
);
201 void e_file_write(E_Eco
*ec
, E_Buffer
*bf
)
207 if (!(bf
->flag
& BUFFER_FLUSH
))
210 fp
= fopen(bf
->paths
->file
, "w");
217 for (i
= 0; i
< ln
->used
; i
++)
218 fputc(ln
->text
[i
], fp
);
227 BUFFER_UNSET(bf
, BUFFER_FLUSH
);
229 e_status_set_msg(ec
, "(Wrote %d lines)", nlines
);