home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / text / hyper / hsc_source.lha / hsc / source / ugly / ufile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-06  |  4.6 KB  |  207 lines

  1. /*
  2.  * ugly/ufile.c
  3.  *
  4.  * misc. filename functions
  5.  *
  6.  * Copyright (C) 1996  Thomas Aglassinger
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * updated:  6-Jan-1997
  23.  * created: 14-Oct-1996
  24.  *
  25.  *-------------------------------------------------------------------
  26.  *
  27.  */
  28.  
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <errno.h>
  33.  
  34. #include "utypes.h"
  35. #include "umemory.h"
  36. #include "ustring.h"
  37. #include "expstr.h"
  38.  
  39. #define NOEXTERN_UGLY_UFILE_H
  40. #include "ufile.h"
  41.  
  42. /*
  43.  * fexists
  44.  *
  45.  * check if file exists ( = could have been opened for input)
  46.  *
  47.  * result: TRUE, if file exists, else FALSE
  48.  *
  49.  */
  50. BOOL fexists(STRPTR filename)
  51. {
  52.     FILE *file = fopen(filename, "r");
  53.     if (file)
  54.     {
  55.         fclose(file);
  56.     }
  57.     return ((BOOL) (file != NULL));
  58. }
  59.  
  60. /*
  61.  * getfsize
  62.  *
  63.  * get size of file
  64.  *
  65.  * params: filename...path and name of file to examine
  66.  * result: size of file; if file does not exist, result
  67.  *         is 0, and ``errno'' is set
  68.  */
  69. LONG getfsize(CONSTRPTR filename)
  70. {
  71.     FILE *file = fopen(filename, "rb");
  72.     LONG filesize = 0;
  73.     if (file)
  74.     {
  75.         /* retrieve size */
  76.         fseek(file, 0L, SEEK_END);
  77.         filesize = ftell(file);
  78.         fclose(file);
  79.     }
  80.     return (filesize);
  81. }
  82.  
  83. /*
  84.  * fcopy
  85.  *
  86.  * copy file (binary)
  87.  *
  88.  * params: oldname,newname...filename of input/output files
  89.  * result: value of type ``fcopy_t'', see "ugly/ufile.h" for details
  90.  *
  91.  * NOTE: you can use ``errno'' to obtain details about the error
  92.  */
  93. fcopy_t fcopy(CONSTRPTR oldname, CONSTRPTR newname)
  94. {
  95. #define BUFSIZE_FCOPY (16*1024)
  96. #define SETERR(code) {err = (code); nowErrno = errno; }
  97.     fcopy_t err = FC_OK;        /* function result */
  98.     FILE *inpf = fopen(oldname, "rb");  /* input file */
  99.     FILE *outf = fopen(newname, "wb");  /* output file */
  100.     char *buf = (char *) umalloc(sizeof(char) * BUFSIZE_FCOPY);         /* copy buffer */
  101.     int nowErrno = 0;
  102.  
  103.     /* check for errors */
  104.     if (!buf)
  105.     {
  106.         SETERR(FC_ERR_NoMemory);
  107.     }
  108.     else if (!inpf)
  109.     {
  110.         SETERR(FC_ERR_OpenInput);
  111.     }
  112.     else if (!outf)
  113.     {
  114.         SETERR(FC_ERR_OpenOutput);
  115.     }
  116.     else
  117.     {
  118.         /* copy data */
  119.         BOOL quit = FALSE;
  120.         do
  121.         {
  122.             size_t byteRead = fread(buf, sizeof(char), BUFSIZE_FCOPY, inpf);
  123.  
  124.             if (ferror(inpf))
  125.             {
  126.                 SETERR(FC_ERR_Read);
  127.             }
  128.             else if (byteRead == 0)
  129.             {
  130.                 quit = TRUE;
  131.             }
  132.             else
  133.             {
  134.                 fwrite(buf, sizeof(char), byteRead, outf);
  135.                 if (ferror(outf))
  136.                 {
  137.                     SETERR(FC_ERR_Write);
  138.                 }
  139.             }
  140.         }
  141.         while (!quit && (err == FC_OK));
  142.     }
  143.  
  144.     /* cleanup */
  145.     if (inpf)
  146.         fclose(outf);
  147.     if (inpf)
  148.         fclose(inpf);
  149.     if (buf)
  150.         ufree(buf);
  151.  
  152.     /* set errno to value where error occured first */
  153.     if (nowErrno)
  154.         errno = nowErrno;
  155.  
  156.     return err;
  157. }
  158.  
  159. /*
  160.  * fmove
  161.  *
  162.  * rename or move file; act's same as rename(), but is guaranteed
  163.  * to work accross devices
  164.  *
  165.  * params: oldname...source filename
  166.  *         newname...destination filename
  167.  * result: same as fcopy()
  168.  */
  169. fcopy_t fmove(CONSTRPTR oldname, CONSTRPTR newname)
  170. {
  171.     fcopy_t ferr = FC_OK;
  172.  
  173.     if (rename(oldname, newname))
  174.     {
  175.         int nowErrno = 0;
  176.  
  177.         /* rename failed; perform a copy-and-delete */
  178.         ferr = fcopy(oldname, newname);
  179.         nowErrno = errno;       /* remember errno */
  180.  
  181.         switch (ferr)
  182.         {
  183.         case FC_OK:
  184.             remove(oldname);    /* remove old file */
  185.             break;
  186.  
  187.         case FC_ERR_Write:
  188.             remove(newname);    /* remove (incomplete) new file */
  189.             errno = nowErrno;
  190.             break;
  191.  
  192.         case FC_ERR_NoMemory:
  193.         case FC_ERR_OpenInput:
  194.         case FC_ERR_OpenOutput:
  195.         case FC_ERR_Read:
  196.             /* do nufin */
  197.             break;
  198.  
  199.         default:
  200.             panic("unhandled return value");
  201.             break;
  202.         }
  203.     }
  204.  
  205.     return ferr;
  206. }
  207.