home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1558 / savefile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.2 KB  |  109 lines

  1.  
  2. /*
  3.  * Copyright (C) 1990 by Jay Konigsberg. mail: jak@sactoh0
  4.  *
  5.  * Permission to use, copy, modify, and distribute this  software  and  its
  6.  * documentation is hereby  granted,  provided  that  the  above  copyright
  7.  * notice appear in all copies  and that  both  the  copyright  notice  and
  8.  * this permission notice appear in supporting documentation. This software
  9.  * is provided "as is" without express or implied  warranty.  However,  the
  10.  * author retains all Copyright priviliges and rights  to  renumeration  if
  11.  * this software is sold.
  12.  */
  13.  
  14. /*
  15.  * savefile - save the text buffer to a file.
  16.  */
  17.  
  18. #include "simped.h"
  19.  
  20. void savefile(editfile, newfile, fd, text, count)
  21. char    *editfile;
  22. int    *newfile;
  23. FILE    *fd;
  24. char    **text;
  25. int    count;
  26. {
  27. FILE    *fopen();
  28.  
  29. int    fclose(),
  30.     fprintf(),
  31.     printf(),
  32.     fputs(),
  33.     puts(),
  34.     unlink(),
  35.     cleanup();
  36.  
  37. char    *getline();
  38.  
  39. int    textinx,        /* index for the text buffer */
  40.     text_entered=0,        /* was something entered */
  41.     ch=0;            /* characters read in */
  42.  
  43. char    buffer[LINELEN+2];    /* buffer for filename entry */
  44.  
  45. puts("");
  46. if (! editfile)
  47.     {
  48.     for(;;)
  49.     {
  50.     fputs("\nPlease enter a filename: ", stdout);
  51.     if (getline(buffer, &text_entered, stdin, '\0', TRUE))
  52.         {
  53.         puts("Too many characters.");
  54.         return;
  55.         }
  56.     if( ! text_entered ) /* a return by itself was entered */
  57.         return;
  58.     buffer[strlen(buffer)-1]='\0';
  59.     editfile = buffer;
  60.     if ( (fd=fopen(editfile, "r")) == NULL)
  61.         {
  62.         break;
  63.         }
  64.     printf("%s: file exists! return to exit.\n", editfile);
  65.     }
  66.     fclose(fd);
  67.     if( (fd=fopen(editfile, "w")) == NULL )
  68.     {
  69.     fprintf(stderr,"Can't open %s, probably an illegal filename",editfile);
  70.     return;
  71.     }
  72.     }
  73.  
  74. if ( ! *newfile )
  75.     {
  76.     fclose(fd);
  77.     if( (fd=fopen(editfile, "w")) == NULL )
  78.     {
  79.     fprintf(stderr,"fopen for write failed.\n");
  80.     cleanup(2);
  81.     }
  82.     }
  83. else
  84.     {
  85.     rewind(fd);
  86.     }
  87. for (textinx=0; textinx < count; ++textinx)
  88.     {
  89.     ch += strlen(text[textinx]);
  90.     fputs(text[textinx], fd);
  91.     }
  92. if (ch > 0)
  93.     {
  94.     printf("\n%s: written, %d lines, %d characters\n", editfile, count, ch);
  95.     }
  96. else
  97.     {
  98.     if ( unlink(editfile) == -1 )
  99.     {
  100.     fprintf(stderr,"unlink failed: error=%d", errno);
  101.     }
  102.     else
  103.     {
  104.     puts("Empty file - no action");
  105.     }
  106.     }
  107. cleanup(0);
  108. }
  109.