gap.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /* gap.c, Ait, BSD 3-Clause, Kevin Bloom, 2023-2024,
  2. Derived from: Atto January 2017
  3. Derived from: Anthony's Editor January 93
  4. */
  5. #include <sys/stat.h>
  6. #include "header.h"
  7. #include "util.h"
  8. /* Enlarge gap by n chars, position of gap cannot change */
  9. int growgap(buffer_t *bp, point_t n)
  10. {
  11. char_t *new;
  12. point_t buflen, newlen, xgap, xegap;
  13. assert(bp->b_buf <= bp->b_gap);
  14. assert(bp->b_gap <= bp->b_egap);
  15. assert(bp->b_egap <= bp->b_ebuf);
  16. xgap = bp->b_gap - bp->b_buf;
  17. xegap = bp->b_egap - bp->b_buf;
  18. buflen = bp->b_ebuf - bp->b_buf;
  19. /* reduce number of reallocs by growing by a minimum amount */
  20. n = (n < MIN_GAP_EXPAND ? MIN_GAP_EXPAND : n);
  21. newlen = buflen + n * sizeof (char_t);
  22. if (buflen == 0) {
  23. if (newlen < 0 || MAX_SIZE_T < newlen)
  24. fatal("%s: Failed to allocate required memory.\n");
  25. new = (char_t*) malloc((size_t) newlen);
  26. if (new == NULL)
  27. fatal("%s: Failed to allocate required memory.\n"); /* Cannot edit a file without a buffer. */
  28. } else {
  29. if (newlen < 0 || MAX_SIZE_T < newlen) {
  30. msg("Failed to allocate required memory");
  31. return (FALSE);
  32. }
  33. new = (char_t*) realloc(bp->b_buf, (size_t) newlen);
  34. if (new == NULL) {
  35. msg("Failed to allocate required memory"); /* Report non-fatal error. */
  36. return (FALSE);
  37. }
  38. }
  39. /* Relocate pointers in new buffer and append the new
  40. * extension to the end of the gap.
  41. */
  42. bp->b_buf = new;
  43. bp->b_gap = bp->b_buf + xgap;
  44. bp->b_ebuf = bp->b_buf + buflen;
  45. bp->b_egap = bp->b_buf + newlen;
  46. while (xegap < buflen--)
  47. *--bp->b_egap = *--bp->b_ebuf;
  48. bp->b_ebuf = bp->b_buf + newlen;
  49. assert(bp->b_buf < bp->b_ebuf); /* Buffer must exist. */
  50. assert(bp->b_buf <= bp->b_gap);
  51. assert(bp->b_gap < bp->b_egap); /* Gap must grow only. */
  52. assert(bp->b_egap <= bp->b_ebuf);
  53. return (TRUE);
  54. }
  55. point_t movegap(buffer_t *bp, point_t offset)
  56. {
  57. char_t *p = ptr(bp, offset);
  58. while (p < bp->b_gap)
  59. *--bp->b_egap = *--bp->b_gap;
  60. while (bp->b_egap < p)
  61. *bp->b_gap++ = *bp->b_egap++;
  62. assert(bp->b_gap <= bp->b_egap);
  63. assert(bp->b_buf <= bp->b_gap);
  64. assert(bp->b_egap <= bp->b_ebuf);
  65. return (pos(bp, bp->b_egap));
  66. }
  67. /* Given a buffer offset, convert it to a pointer into the buffer */
  68. char_t * ptr(buffer_t *bp, register point_t offset)
  69. {
  70. if (offset < 0)
  71. return (bp->b_buf);
  72. return (bp->b_buf+offset + (bp->b_buf + offset < bp->b_gap ? 0 : bp->b_egap-bp->b_gap));
  73. }
  74. /* Given a pointer into the buffer, convert it to a buffer offset */
  75. point_t pos(buffer_t *bp, register char_t *cp)
  76. {
  77. assert(bp->b_buf <= cp && cp <= bp->b_ebuf);
  78. return (cp - bp->b_buf - (cp < bp->b_egap ? 0 : bp->b_egap - bp->b_gap));
  79. }
  80. int posix_file(char *fn)
  81. {
  82. if (fn[0] == '_')
  83. return (FALSE);
  84. for (; *fn != '\0'; ++fn) {
  85. if (!isalnum(*fn) && *fn != '.' && *fn != '_' && *fn != '-' && *fn != '/')
  86. return (FALSE);
  87. }
  88. return (TRUE);
  89. }
  90. void relocate_backup(char *fp)
  91. {
  92. char ltemp[PATH_MAX+2];
  93. strcpy(ltemp, fp);
  94. replace_all(ltemp, '/', '!');
  95. strcpy(fp, backup_dir);
  96. strcat(fp, ltemp);
  97. }
  98. /*
  99. This code is heavily based on OpenBSD's mg(1)
  100. See mg's `fileio.c` file.
  101. */
  102. int backup_file(char *fn)
  103. {
  104. int orig, backup;
  105. char bfn[PATH_MAX+2];
  106. char *tname;
  107. char buf[STRBUF_L];
  108. struct stat sb;
  109. ssize_t nread;
  110. struct timespec new_times[2];
  111. sprintf(bfn, "%s~", fn);
  112. if(backup_dir != NULL)
  113. relocate_backup(bfn);
  114. /* If we can't open the original, just don't make a back up */
  115. if (stat(fn, &sb) == -1) {
  116. msg("Can't stat %s : %s", fn, strerror(errno));
  117. return TRUE;
  118. }
  119. if((orig = open(fn, O_RDONLY)) == -1) {
  120. return TRUE;
  121. }
  122. if (asprintf(&tname, "%s.XXXXXXXXXX", bfn) == -1) {
  123. msg("Can't allocate temp file name : %s", strerror(errno));
  124. return FALSE;
  125. }
  126. backup = mkstemp(tname);
  127. if(backup == -1) {
  128. msg("Failed to open backup file: \"%s\"", bfn);
  129. return FALSE;
  130. }
  131. while ((nread = read(orig, buf, sizeof(buf))) > 0) {
  132. if (write(backup, buf, (size_t)nread) != nread) {
  133. nread = -1;
  134. break;
  135. }
  136. }
  137. (void) fchmod(backup, (sb.st_mode & 0777));
  138. new_times[0] = sb.st_atim;
  139. new_times[1] = sb.st_mtim;
  140. futimens(backup, new_times);
  141. close(orig);
  142. close(backup);
  143. if (nread == -1) {
  144. if (unlink(tname) == -1)
  145. msg("Can't unlink temp : %s", strerror(errno));
  146. } else {
  147. if (rename(tname, bfn) == -1) {
  148. msg("Can't rename temp : %s", strerror(errno));
  149. (void) unlink(tname);
  150. nread = -1;
  151. }
  152. }
  153. return (nread == -1 ? FALSE : TRUE);
  154. }
  155. int save(char *fn)
  156. {
  157. FILE *fp;
  158. point_t length;
  159. struct stat sb;
  160. // if (!posix_file(fn)) {
  161. // msg("Not a portable POSIX file name.");
  162. // return (FALSE);
  163. // }
  164. stat(fn, &sb);
  165. if (!backup_file(fn)) {
  166. // msg("Failed to backup file \"%s\".", fn);
  167. return (FALSE);
  168. }
  169. fp = fopen(fn, "w");
  170. if (fp == NULL) {
  171. msg("Failed to open file \"%s\".", fn);
  172. return (FALSE);
  173. }
  174. (void) movegap(curbp, (point_t) 0);
  175. length = (point_t) (curbp->b_ebuf - curbp->b_egap);
  176. if (fwrite(curbp->b_egap, sizeof (char), (size_t) length, fp) != length) {
  177. msg("Failed to write file \"%s\".", fn);
  178. return (FALSE);
  179. }
  180. if (fclose(fp) != 0) {
  181. msg("Failed to close file \"%s\".", fn);
  182. return (FALSE);
  183. }
  184. curbp->b_flags &= ~B_MODIFIED;
  185. if (stat(fn, &sb) < 0) {
  186. msg("Failed to find file \"%s\".", fn);
  187. return (FALSE);
  188. }
  189. if (MAX_SIZE_T < sb.st_size) {
  190. msg("File \"%s\" is too big to load.", fn);
  191. return (FALSE);
  192. }
  193. curbp->b_fmtime = sb.st_mtime;
  194. msg("File \"%s\" %ld bytes saved.", fn, pos(curbp, curbp->b_ebuf));
  195. return (TRUE);
  196. }
  197. int load_file(char *fn)
  198. {
  199. /* reset the gap, make it the whole buffer */
  200. curbp->b_gap = curbp->b_buf;
  201. curbp->b_egap = curbp->b_ebuf;
  202. top();
  203. return insert_file(fn, FALSE);
  204. }
  205. /* reads file into buffer at point */
  206. int insert_file(char *fn, int modflag)
  207. {
  208. FILE *fp;
  209. size_t len;
  210. struct stat sb;
  211. if (stat(fn, &sb) < 0) {
  212. msg("Failed to find file \"%s\".", fn);
  213. return (FALSE);
  214. }
  215. if (MAX_SIZE_T < sb.st_size) {
  216. msg("File \"%s\" is too big to load.", fn);
  217. return (FALSE);
  218. }
  219. if (curbp->b_egap - curbp->b_gap < sb.st_size * sizeof (char_t) && !growgap(curbp, sb.st_size))
  220. return (FALSE);
  221. if ((fp = fopen(fn, "r")) == NULL) {
  222. msg("Failed to open file \"%s\".", fn);
  223. return (FALSE);
  224. }
  225. curbp->b_point = movegap(curbp, curbp->b_point);
  226. // undoset();
  227. curbp->b_gap += len = fread(curbp->b_gap, sizeof (char), (size_t) sb.st_size, fp);
  228. if (fclose(fp) != 0) {
  229. msg("Failed to close file \"%s\".", fn);
  230. return (FALSE);
  231. }
  232. curbp->b_flags &= (modflag ? B_MODIFIED : ~B_MODIFIED);
  233. if(!modflag)
  234. curbp->b_fmtime = sb.st_mtime;
  235. msg("%ld bytes read.", len);
  236. return (TRUE);
  237. }
  238. /* Record a new undo */
  239. void undoset(int type, int shouldconcat)
  240. {
  241. int length = strlen((const char *)input);
  242. int ulen = 0;
  243. int npc = 0;
  244. char_t *p;
  245. undo_t *u = (undo_t *)malloc(sizeof(undo_t));
  246. assert(u != NULL);
  247. u->u_type = type;
  248. u->u_point = curbp->b_point;
  249. u->u_line = curbp->b_line;
  250. u->u_adjust = FALSE;
  251. u->u_data = NULL;
  252. switch(type) {
  253. case INSERT:
  254. if(curbp->b_undo != NULL && curbp->b_undo->u_type == INSERT && shouldconcat) {
  255. ulen = strlen((const char *)curbp->b_undo->u_data);
  256. curbp->b_undo->u_data = (char_t *)(realloc(curbp->b_undo->u_data, sizeof(char_t)*(length + ulen)+1));
  257. strncpy((char *)curbp->b_undo->u_data, (char *)curbp->b_undo->u_data, ulen);
  258. strncat((char *)curbp->b_undo->u_data, (char *)input, length);
  259. curbp->b_undo->u_data[(length + ulen)] = '\0';
  260. return;
  261. } else {
  262. u->u_data = (char_t *)(malloc(sizeof(char_t)*length+1));
  263. strncpy((char *)u->u_data, (char *)input, length);
  264. u->u_data[length] = '\0';
  265. }
  266. break;
  267. case DELETE:
  268. npc = utf8_size(*ptr(curbp,curbp->b_point));
  269. if(curbp->b_undo != NULL && curbp->b_undo->u_type == DELETE && shouldconcat) {
  270. ulen = strlen((const char *)curbp->b_undo->u_data);
  271. curbp->b_undo->u_data = (char_t *)(realloc(curbp->b_undo->u_data, sizeof(char_t)*(npc + ulen)+1));
  272. strncpy((char *)curbp->b_undo->u_data, (char *)curbp->b_undo->u_data, ulen);
  273. strncat((char *)curbp->b_undo->u_data, (const char *)ptr(curbp, curbp->b_point), npc);
  274. curbp->b_undo->u_data[npc+ulen] = '\0';
  275. return;
  276. } else {
  277. u->u_data = (char_t *)(malloc(sizeof(char_t)+npc));
  278. strncpy((char *)u->u_data, (const char *)ptr(curbp, curbp->b_point), npc);
  279. u->u_data[npc] = '\0';
  280. }
  281. break;
  282. case BACKSP:
  283. npc = prev_utf8_char_size();
  284. if(curbp->b_undo != NULL && curbp->b_undo->u_type == BACKSP && shouldconcat) {
  285. char_t *temp;
  286. curbp->b_undo->u_point -= npc;
  287. ulen = strlen((const char *)curbp->b_undo->u_data);
  288. temp = (char_t *)(malloc(sizeof(char_t)*ulen));
  289. strncpy((char *)temp, (char *)curbp->b_undo->u_data, ulen);
  290. /* if(curbp->b_undo->u_data != NULL) {
  291. free(curbp->b_undo->u_data);
  292. curbp->b_undo->u_data = NULL;
  293. } */
  294. curbp->b_undo->u_data = (char_t *)(malloc(sizeof(char_t)*(npc + ulen)+1));
  295. // memset(curbp->b_undo->u_data, 0, 1+ulen);
  296. strncpy((char *)curbp->b_undo->u_data, (const char *)ptr(curbp, curbp->b_undo->u_point), npc);
  297. strncat((char *)curbp->b_undo->u_data, (char *)temp, ulen);
  298. curbp->b_undo->u_data[(npc + ulen)] = '\0';
  299. free(temp);
  300. return;
  301. } else {
  302. u->u_point = curbp->b_point - npc;
  303. u->u_data = (char_t *)(malloc(sizeof(char_t)+npc));
  304. strncpy((char *)u->u_data, (const char *)ptr(curbp, u->u_point), npc);
  305. u->u_data[npc] = '\0';
  306. u->u_adjust = TRUE;
  307. }
  308. break;
  309. case CUT: {
  310. int bigger_mark = FALSE;
  311. if (curbp->b_point < curbp->b_mark) {
  312. (void) movegap(curbp, curbp->b_point);
  313. p = ptr(curbp, curbp->b_point);
  314. length = curbp->b_mark - curbp->b_point;
  315. bigger_mark = TRUE;
  316. u->u_adjust = TRUE;
  317. } else {
  318. (void) movegap(curbp, curbp->b_mark);
  319. p = ptr(curbp, curbp->b_mark);
  320. length = curbp->b_point - curbp->b_mark;
  321. u->u_point = curbp->b_mark;
  322. }
  323. u->u_data = (char_t*) malloc(sizeof(char_t)*length+1);
  324. (void) memcpy(u->u_data, p, length * sizeof (char_t));
  325. u->u_data[length] = '\0';
  326. /* Only adjust u_adjust if it's not a delete command, otherwise
  327. the line count will be off.
  328. */
  329. if(currentcommand != KBD_DELETE_CHAR)
  330. for(int i = 0; u->u_data[i] != '\0'; i++) {
  331. if(u->u_data[i] == '\n') {
  332. u->u_adjust = !bigger_mark;
  333. break;
  334. }
  335. }
  336. break;
  337. }
  338. case YANK:
  339. u->u_data = (char_t*) malloc(sizeof(char_t)*nscrap+1);
  340. (void) memcpy(u->u_data, scrap, nscrap * sizeof (char_t));
  341. u->u_data[nscrap] = '\0';
  342. break;
  343. case REPLACE:
  344. (void) movegap(curbp, curbp->b_point);
  345. p = ptr(curbp, curbp->b_point);
  346. length = curbp->b_mark - curbp->b_point;
  347. u->u_data = (char_t*) malloc(sizeof(char_t)*length+1);
  348. (void) memcpy(u->u_data, p, length * sizeof (char_t));
  349. u->u_data[length] = '\0';
  350. u->u_size = shouldconcat;
  351. break;
  352. case CLIPBOARD: {
  353. int ntemp = strlen(gtemp);
  354. u->u_data = (char_t*) malloc(sizeof(char_t)*ntemp+1);
  355. (void) memcpy(u->u_data, gtemp, ntemp * sizeof (char_t));
  356. u->u_data[ntemp] = '\0';
  357. break;
  358. }
  359. }
  360. u->u_next = curbp->b_undo;
  361. curbp->b_undo = u;
  362. curbp->b_redo = NULL;
  363. }
  364. /* Record a new redo if you've just undid or a undo if you've just redid. */
  365. void redo_or_undo_set(undo_t *up, int datalen, int isundo)
  366. {
  367. undo_t *rp = (undo_t *)malloc(sizeof(undo_t));
  368. char_t *p;
  369. int newlines = 0;
  370. assert(rp != NULL);
  371. rp->u_point = up->u_point;
  372. rp->u_line = up->u_line;
  373. if(up->u_adjust)
  374. for(int i = 0 ; up->u_data[i] != '\0'; i++) {
  375. if(up->u_data[i] == '\n')
  376. newlines++;
  377. }
  378. switch(up->u_type) {
  379. case INSERT:
  380. rp->u_type = DELETE;
  381. break;
  382. case BACKSP:
  383. rp->u_adjust = TRUE;
  384. rp->u_line -= newlines;
  385. case DELETE:
  386. rp->u_type = INSERT;
  387. break;
  388. case CUT:
  389. rp->u_type = YANK;
  390. rp->u_line -= newlines;
  391. break;
  392. case CLIPBOARD:
  393. case YANK:
  394. rp->u_type = CUT;
  395. rp->u_line += newlines;
  396. break;
  397. case REPLACE:
  398. rp->u_type = REPLACE;
  399. (void) movegap(curbp, up->u_point);
  400. p = ptr(curbp, up->u_point);
  401. rp->u_data = (char_t*) malloc(sizeof(char_t)*up->u_size+1);
  402. (void) memcpy(rp->u_data, p, up->u_size * sizeof (char_t));
  403. rp->u_data[up->u_size] = '\0';
  404. rp->u_size = datalen;
  405. break;
  406. }
  407. /* if(rp != NULL && rp->u_data != NULL && rp->u_type != REPLACE) {
  408. free(rp->u_data);
  409. rp->u_data = NULL;
  410. } */
  411. if(rp->u_type != REPLACE) {
  412. rp->u_data = (char_t *) calloc(datalen+1, sizeof(char_t));
  413. (void) memcpy(rp->u_data, up->u_data, datalen * sizeof (char_t));
  414. rp->u_data[datalen] = '\0';
  415. }
  416. /* if an undo was done, save to redo */
  417. if(isundo) {
  418. rp->u_next = curbp->b_redo;
  419. curbp->b_redo = rp;
  420. } else {
  421. rp->u_next = curbp->b_undo;
  422. curbp->b_undo = rp;
  423. }
  424. }
  425. /* Undo */
  426. void undo_or_redo(buffer_t *bp, undo_t *up, int isundo)
  427. {
  428. int n = 0;
  429. currentcommand = KBD_UNDO;
  430. if(up == NULL) {
  431. if(isundo)
  432. msg("Nothing to undo!");
  433. else
  434. msg("Nothing to redo!");
  435. return;
  436. }
  437. bp->b_point = up->u_point;
  438. bp->b_line = up->u_line;
  439. n = strlen((const char *)up->u_data);
  440. redo_or_undo_set(up, n, isundo);
  441. switch(up->u_type) {
  442. case INSERT:
  443. case YANK:
  444. case CLIPBOARD:
  445. (void) movegap(bp, bp->b_point);
  446. bp->b_egap += n;
  447. bp->b_point = pos(bp, bp->b_egap);
  448. break;
  449. case BACKSP:
  450. case DELETE:
  451. case CUT:
  452. bp->b_point = movegap(bp, bp->b_point);
  453. memcpy(bp->b_gap, up->u_data, n*sizeof(char_t));
  454. bp->b_gap += n;
  455. bp->b_point = pos(bp, bp->b_egap);
  456. break;
  457. case REPLACE:
  458. (void) movegap(bp, bp->b_point);
  459. bp->b_egap += up->u_size;
  460. bp->b_point = pos(bp, bp->b_egap);
  461. bp->b_point = movegap(bp, bp->b_point);
  462. memcpy(bp->b_gap, up->u_data, n*sizeof(char_t));
  463. bp->b_gap += n;
  464. bp->b_point = pos(bp, bp->b_egap);
  465. break;
  466. }
  467. if(up->u_adjust && isundo)
  468. bp->b_point = movegap(bp, up->u_point + n);
  469. else
  470. bp->b_point = movegap(bp, up->u_point);
  471. bp->b_flags |= B_MODIFIED;
  472. if(isundo)
  473. bp->b_undo = up->u_next;
  474. else
  475. bp->b_redo = up->u_next;
  476. if(curbp->b_point < curbp->b_page || curbp->b_point > curbp->b_epage)
  477. bp->b_reframe = 1;
  478. }
  479. void undo()
  480. {
  481. undo_or_redo(curbp, curbp->b_undo, TRUE);
  482. }
  483. /* Redo */
  484. void redo()
  485. {
  486. undo_or_redo(curbp, curbp->b_redo, FALSE);
  487. }
  488. /* find the point for start of line ln */
  489. point_t line_to_point(int ln)
  490. {
  491. point_t end_p = pos(curbp, curbp->b_ebuf);
  492. point_t p, start;
  493. for (p=0, start=0; p <= end_p; p++) {
  494. char_t *c = ptr(curbp, p);
  495. if(c == 0)
  496. break;
  497. if ( *c == '\n') {
  498. if (--ln == 0)
  499. return start;
  500. if (p + 1 <= end_p)
  501. start = p + 1;
  502. }
  503. if(!*c && ln == 1)
  504. return start;
  505. }
  506. return -1;
  507. }
  508. /* scan buffer and fill in curline and lastline */
  509. void get_line_stats(int *curline, int *lastline, buffer_t *bp)
  510. {
  511. point_t end_p = pos(bp, bp->b_ebuf);
  512. point_t p;
  513. int line;
  514. *curline = -1;
  515. for (p=0, line=0; p < end_p; p++) {
  516. line += (*(ptr(bp,p)) == '\n') ? 1 : 0;
  517. *lastline = line;
  518. if (*curline == -1 && p == bp->b_point) {
  519. *curline = (*(ptr(bp,p)) == '\n') ? line : line + 1;
  520. }
  521. }
  522. *lastline = *lastline + 1;
  523. if (bp->b_point == end_p)
  524. *curline = *lastline;
  525. }
  526. /* Return TRUE if file was modified after the current buffer's recored mtime. */
  527. int is_file_modified(char *fn)
  528. {
  529. struct stat sb;
  530. if(stat(fn, &sb) < 0) {
  531. return (FALSE);
  532. }
  533. if(sb.st_mtime != 0 && curbp->b_fmtime != sb.st_mtime) {
  534. return (TRUE);
  535. }
  536. return (FALSE);
  537. }
  538. /* Return TRUE to continue, FALSE to stop. Revert happens here. */
  539. int file_was_modified_prompt()
  540. {
  541. int current = 0, lastln = 0;
  542. const char *prompt = "This file has changed on disk; really edit (y/N/r) ?";
  543. char c = '\0';
  544. point_t p = curbp->b_point;
  545. struct stat sb;
  546. print_to_msgline(prompt);
  547. clrtoeol(prompt, MSGLINE);
  548. c = yesnomaybeso('n');
  549. if (c == 'n') {
  550. clrtoeol("", MSGLINE);
  551. return FALSE;
  552. } else if(c == 'r') {
  553. curbp->b_point = 0;
  554. load_file(curbp->b_fname);
  555. clrtoeol("", MSGLINE);
  556. curbp->b_point = p;
  557. get_line_stats(&current, &lastln, curbp);
  558. curbp->b_line = current;
  559. msg("Buffer reverted.");
  560. return FALSE;
  561. } else if(c == 'y') {
  562. clrtoeol("", MSGLINE);
  563. if (stat(curbp->b_fname, &sb) < 0) {
  564. msg("Failed to find file \"%s\".", curbp->b_fname);
  565. return (FALSE);
  566. }
  567. if (MAX_SIZE_T < sb.st_size) {
  568. msg("File \"%s\" is too big to load.", curbp->b_fname);
  569. return (FALSE);
  570. }
  571. curbp->b_fmtime = sb.st_mtime;
  572. return TRUE;
  573. }
  574. clrtoeol("", MSGLINE);
  575. return (FALSE);
  576. }