home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / INTERNET / UPC2S1.ZIP / FOPEN.C < prev    next >
C/C++ Source or Header  |  1993-08-09  |  4KB  |  97 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    f o p e n . c                                                   */
  3. /*                                                                    */
  4. /*    Support routines for UUPC/extended                              */
  5. /*                                                                    */
  6. /*    Changes Copyright 1990, 1991 (c) Andrew H. Derbyshire           */
  7. /*                                                                    */
  8. /*    History:                                                        */
  9. /*       21Nov1991 Break out of lib.c                          ahd    */
  10. /*--------------------------------------------------------------------*/
  11.  
  12. #include <fcntl.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <errno.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <share.h>
  21. #include <io.h>
  22.  
  23. /*--------------------------------------------------------------------*/
  24. /*                    UUPC/extended include files                     */
  25. /*--------------------------------------------------------------------*/
  26.  
  27. #include "lib.h"
  28. #include "hlib.h"
  29. #include "ssleep.h"
  30.  
  31. #define SHARE_OPEN
  32.  
  33. /*--------------------------------------------------------------------*/
  34. /*    F O P E N                                                       */
  35. /*                                                                    */
  36. /*    Like fopen() but create imtermediate directories                */
  37. /*                                                                    */
  38. /*    This routine has dependency on the path separator characters    */
  39. /*    being '/', we should relove that somehow someday.               */
  40. /*--------------------------------------------------------------------*/
  41.  
  42. FILE *FSOPEN(const char *name, const char *mode)
  43. {
  44.  
  45.    char *last;
  46.    FILE *results;
  47.    char fname[FILENAME_MAX];
  48.  
  49.    int share = SH_DENYWR;
  50.    int retries = 0;
  51.  
  52.    strcpy( fname, name );
  53.    denormalize( fname );
  54.  
  55. /*--------------------------------------------------------------------*/
  56. /*                       Open file (first try)                        */
  57. /*--------------------------------------------------------------------*/
  58.  
  59.    results = _fsopen(fname, mode, share );
  60.  
  61. /*--------------------------------------------------------------------*/
  62. /*       Return if the file opened, or if in read mode (no            */
  63. /*       directories need to be built) and not in multi-tasking       */
  64. /*       mode (no retries).                                           */
  65. /*--------------------------------------------------------------------*/
  66.  
  67.    if (results != nil(FILE))
  68.       return results;
  69.  
  70.    if (*mode == 'r')
  71.    {
  72.       if ((!bflag[ F_MULTITASK ]) || access( fname, 0 ))
  73.          return results;
  74.    }
  75.    else if ((last = strrchr(name, '/')) != nil(char))
  76.    {                                // Make any needed directories
  77.       *last = '\0';
  78.       MKDIR(name);
  79.       *last = '/';
  80.    } /* else */
  81.  
  82. /*--------------------------------------------------------------------*/
  83. /*                         Now try open again                         */
  84. /*--------------------------------------------------------------------*/
  85.  
  86.    for ( ;; )
  87.    {
  88.       results = _fsopen(fname, mode, share);
  89.       if (( results != NULL ) || (!bflag[ F_MULTITASK ]) ||
  90.           (errno != EACCES)   || (retries++ > 10))
  91.          return results;
  92.       perror( fname );
  93.       ssleep( retries * 2);
  94.    }
  95.  
  96. } /*FOPEN*/
  97.