157703Sbostic /*- 257703Sbostic * Copyright (c) 1992 The Regents of the University of California. 357703Sbostic * All rights reserved. 457703Sbostic * 557703Sbostic * This code is derived from software contributed to Berkeley by 657703Sbostic * Rodney Ruddock of the University of Guelph. 757703Sbostic * 857703Sbostic * %sccs.include.redist.c% 957703Sbostic */ 1057703Sbostic 1157703Sbostic #ifndef lint 12*58710Sbostic static char sccsid[] = "@(#)w.c 5.5 (Berkeley) 03/18/93"; 1357703Sbostic #endif /* not lint */ 1457703Sbostic 1557710Sbostic #include <sys/types.h> 1657710Sbostic 1757710Sbostic #include <regex.h> 1857710Sbostic #include <setjmp.h> 1957710Sbostic #include <stdio.h> 2057710Sbostic #include <stdlib.h> 2157710Sbostic #include <string.h> 2257710Sbostic 2358315Sbostic #ifdef DBI 2458315Sbostic #include <db.h> 2558315Sbostic #endif 2658315Sbostic 2757703Sbostic #include "ed.h" 2857710Sbostic #include "extern.h" 2957703Sbostic 3057703Sbostic /* 3157703Sbostic * Write the contents of the buffer out to the real file (spec'd or 3257703Sbostic * remembered). If 'w' then overwrite, if 'W' append to the file. 'W' 3357703Sbostic * is probably _the_ command that most editors don't have, and it's 3457703Sbostic * so(!) useful. The 'wq' works as 'w' but 'q' immediately follows. 3557703Sbostic * Shame on POSIX for not including 'W' and 'wq', they're not that 3657703Sbostic * hard to implement; yaaa! BSD for keeping it! :-) 3757703Sbostic */ 3857703Sbostic void 3957703Sbostic w(inputt, errnum) 4057710Sbostic FILE *inputt; 4157710Sbostic int *errnum; 4257703Sbostic { 4357710Sbostic FILE *fp; 4457710Sbostic int l_ttl = 0, l_q_flag = 0, l_sl; 4558315Sbostic char *filename_read=NULL, *temp; 4657703Sbostic 4758564Sralph if (Start_default && End_default) { 4858564Sralph Start = top; 4957710Sbostic End = bottom; 5057710Sbostic } else 5158564Sralph if (Start_default) 5258564Sralph Start = End; 5358564Sralph if (Start == NULL) { 5458315Sbostic strcpy(help_msg, "buffer empty"); 5557710Sbostic *errnum = -1; 5657710Sbostic return; 5757710Sbostic } 5858564Sralph Start_default = End_default = 0; 5957703Sbostic 6057710Sbostic l_sl = ss; 6157710Sbostic ss = getc(inputt); 6257703Sbostic 6357710Sbostic if (ss == 'q') /* "wq" and "Wq" command */ 6457710Sbostic l_q_flag = 1; 6557710Sbostic else 6657710Sbostic ungetc(ss, inputt); 6757703Sbostic 6857710Sbostic temp = filename(inputt, errnum); 6957710Sbostic if (*errnum == 1) 7057710Sbostic filename_read = temp; 7157710Sbostic else 7257710Sbostic if (*errnum == -2) { 7357710Sbostic while (((ss = getc(inputt)) != '\n') || (ss == EOF)); 7457710Sbostic filename_read = filename_current; 7557710Sbostic } else 7657710Sbostic if (*errnum < 0) 7757710Sbostic return; 7857710Sbostic *errnum = 0; 7957703Sbostic 8057710Sbostic if (filename_current == NULL) { 8157710Sbostic if (filename_read == NULL) { 8257710Sbostic strcpy(help_msg, "no filename given"); 8357710Sbostic *errnum = -1; 8457710Sbostic ungetc('\n', inputt); 8557710Sbostic return; 8657710Sbostic } else 8757710Sbostic filename_current = filename_read; 8857710Sbostic } 8958315Sbostic sigspecial++; 9057710Sbostic if (l_sl == 'W') 9157710Sbostic fp = fopen(filename_read, "a"); 9257710Sbostic else 9357710Sbostic fp = fopen(filename_read, "w"); 9457703Sbostic 9557710Sbostic if (fp == NULL) { 9657710Sbostic strcpy(help_msg, "cannot write to file"); 9757710Sbostic *errnum = -1; 9857710Sbostic ungetc('\n', inputt); 9957710Sbostic return; 10057710Sbostic } 10158315Sbostic sigspecial--; 10258315Sbostic if (sigint_flag && (!sigspecial)) 10357710Sbostic goto point; 10457703Sbostic 10557710Sbostic /* Write it out and get a report on the number of bytes written. */ 10658564Sralph l_ttl = edwrite(fp, Start, End); 107*58710Sbostic if (explain_flag > 0) /* For -s option. */ 10857710Sbostic printf("%d\n", l_ttl); 10957703Sbostic 11057710Sbostic point: fclose(fp); 11157710Sbostic if (filename_read != filename_current) 11257710Sbostic free(filename_read); 11357710Sbostic change_flag = 0L; 11457710Sbostic *errnum = 1; 11558315Sbostic if (l_q_flag) { /* For "wq" and "Wq". */ 11657710Sbostic ungetc('\n', inputt); 11757710Sbostic ss = (int) 'q'; 11857710Sbostic q(inputt, errnum); 11957710Sbostic } 12057703Sbostic } 12157703Sbostic 12257703Sbostic /* 12357703Sbostic * Actually writes out the contents of the buffer to the specified 12457703Sbostic * STDIO file pointer for the range of lines specified. 12557703Sbostic */ 12657703Sbostic int 12757703Sbostic edwrite(fp, begi, fini) 12857710Sbostic FILE *fp; 12957710Sbostic LINE *begi, *fini; 13057710Sbostic { 13157710Sbostic register int l_ttl = 0; 13257703Sbostic 13357710Sbostic for (;;) { 13457710Sbostic get_line(begi->handle, begi->len); 13558315Sbostic if (sigint_flag && (!sigspecial)) 13658315Sbostic break; 13757703Sbostic 13858315Sbostic sigspecial++; 13957710Sbostic /* Fwrite is about 20+% faster than fprintf -- no surprise. */ 14057710Sbostic fwrite(text, sizeof(char), begi->len, fp); 14157710Sbostic fputc('\n', fp); 14257710Sbostic l_ttl = l_ttl + (begi->len) + 1; 14357710Sbostic if (begi == fini) 14457710Sbostic break; 14557710Sbostic else 14657710Sbostic begi = begi->below; 14758315Sbostic sigspecial--; 14858315Sbostic if (sigint_flag && (!sigspecial)) 14958315Sbostic break; 15057710Sbostic } 15157710Sbostic return (l_ttl); 15257710Sbostic } 153