home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip531.zip / amiga / stat.c < prev    next >
C/C++ Source or Header  |  1996-11-24  |  8KB  |  286 lines

  1. /* Here we have a handmade stat() function because Aztec's c.lib stat() */
  2. /* does not support an st_mode field, which we need... also a chmod().  */
  3.  
  4. /* This stat() is by Paul Wells, modified by Paul Kienitz. */
  5. /* for use with Aztec C >= 5.0 and Lattice C <= 4.01  */
  6.  
  7. /* POLICY DECISION: We will not attempt to remove global variables from */
  8. /* this source file for Aztec C.  These routines are essentially just   */
  9. /* augmentations of Aztec's c.lib, which is itself not reentrant.  If   */
  10. /* we want to produce a fully reentrant UnZip, we will have to use a    */
  11. /* suitable startup module, such as purify.a for Aztec by Paul Kienitz. */
  12.  
  13. #ifndef __amiga_stat_c
  14. #define __amiga_stat_c
  15.  
  16. #ifdef AZTEC_C
  17. #  include <exec/types.h>
  18. #  include <exec/memory.h>
  19. #  include <libraries/dos.h>
  20. #  include <libraries/dosextens.h>
  21. #  include <clib/exec_protos.h>
  22. #  include <clib/dos_protos.h>
  23. #  include <pragmas/exec_lib.h>
  24. #  include <pragmas/dos_lib.h>
  25. #  include "amiga/z-stat.h"             /* fake version of stat.h */
  26. #  include <string.h>
  27. #else /* __SASC */
  28. /* Uncomment define of USE_REPLACEMENTS to use the directory functions below */
  29. /* #define USE_REPLACEMENTS */
  30. #  include <sys/stat.h>
  31. #  ifndef USE_REPLACEMENTS
  32. #     include <sys/dir.h>               /* SAS/C dir function prototypes */
  33. #     include <dos.h>
  34. #  else
  35. #     include <exec/types.h>
  36. #     include <exec/memory.h>
  37. #     include <sys/types.h>
  38. #     include <proto/exec.h>
  39. #     include <proto/dos.h>
  40. #     include <string.h>
  41. #  endif
  42. #endif
  43.  
  44. #ifndef SUCCESS
  45. #  define SUCCESS (-1)
  46. #  define FAILURE (0)
  47. #endif
  48.  
  49.  
  50. void close_leftover_open_dirs(void);    /* prototype */
  51.  
  52. static DIR *dir_cleanup_list = NULL;    /* for resource tracking */
  53.  
  54. /* CALL THIS WHEN HANDLING CTRL-C OR OTHER UNEXPECTED EXIT! */
  55. void close_leftover_open_dirs(void)
  56. {
  57.     while (dir_cleanup_list)
  58.         closedir(dir_cleanup_list);
  59. }
  60.  
  61. #if defined(AZTEC_C) || defined(USE_REPLACEMENTS)
  62.  
  63. unsigned short disk_not_mounted;
  64.  
  65. extern int stat(char *file,struct stat *buf);
  66.  
  67. stat(file,buf)
  68. char *file;
  69. struct stat *buf;
  70. {
  71.  
  72.         struct FileInfoBlock *inf;
  73.         BPTR lock;
  74.         long ftime;
  75.         void tzset(void);
  76.  
  77.         if( (lock = Lock(file,SHARED_LOCK))==0 )
  78.                 /* file not found */
  79.                 return(-1);
  80.  
  81.         if( !(inf = (struct FileInfoBlock *)AllocMem(
  82.                 (long)sizeof(struct FileInfoBlock),MEMF_PUBLIC|MEMF_CLEAR)) )
  83.         {
  84.                 UnLock(lock);
  85.                 return(-1);
  86.         }
  87.  
  88.         if( Examine(lock,inf)==FAILURE )
  89.         {
  90.                 FreeMem((char *)inf,(long)sizeof(*inf));
  91.                 UnLock(lock);
  92.                 return(-1);
  93.         }
  94.  
  95.         /* fill in buf */
  96.         buf->st_dev         =
  97.         buf->st_nlink       =
  98.         buf->st_uid         =
  99.         buf->st_gid         =
  100.         buf->st_rdev        = 0;
  101.         buf->st_ino         = inf->fib_DiskKey;
  102.         buf->st_blocks      = inf->fib_NumBlocks;
  103.         buf->st_size        = inf->fib_Size;
  104.  
  105.         /* now the date.  AmigaDOS has weird datestamps---
  106.          *      ds_Days is the number of days since 1-1-1978;
  107.          *      however, as Unix wants date since 1-1-1970...
  108.          */
  109.  
  110.         ftime =
  111.                 (inf->fib_Date.ds_Days * 86400 )                +
  112.                 (inf->fib_Date.ds_Minute * 60 )                 +
  113.                 (inf->fib_Date.ds_Tick / TICKS_PER_SECOND )     +
  114.                 (86400 * 8 * 365 )                              +
  115.                 (86400 * 2 );  /* two leap years */
  116.  
  117.         tzset();
  118.         ftime += timezone;
  119.  
  120.         buf->st_ctime =
  121.         buf->st_atime =
  122.         buf->st_mtime = ftime;
  123.  
  124.         buf->st_mode = (inf->fib_DirEntryType < 0 ? S_IFREG : S_IFDIR);
  125.  
  126.         /* lastly, throw in the protection bits */
  127.         buf->st_mode |= ((inf->fib_Protection ^ 0xF) & 0xFF);
  128.  
  129.         FreeMem((char *)inf, (long)sizeof(*inf));
  130.         UnLock((BPTR)lock);
  131.  
  132.         return(0);
  133.  
  134. }
  135.  
  136. /* opendir(), readdir(), closedir() and rmdir() by Paul Kienitz. */
  137.  
  138. DIR *opendir(char *path)
  139. {
  140.     DIR *dd = AllocMem(sizeof(DIR), MEMF_PUBLIC);
  141.     if (!dd) return NULL;
  142.     if (!(dd->d_parentlock = Lock(path, MODE_OLDFILE))) {
  143.         disk_not_mounted = IoErr() == ERROR_DEVICE_NOT_MOUNTED;
  144.         FreeMem(dd, sizeof(DIR));
  145.         return NULL;
  146.     } else
  147.         disk_not_mounted = 0;
  148.     if (!Examine(dd->d_parentlock, &dd->d_fib) || dd->d_fib.fib_EntryType < 0) {
  149.         UnLock(dd->d_parentlock);
  150.         FreeMem(dd, sizeof(DIR));
  151.         return NULL;
  152.     }
  153.     dd->d_cleanuplink = dir_cleanup_list;       /* track them resources */
  154.     if (dir_cleanup_list)
  155.         dir_cleanup_list->d_cleanupparent = &dd->d_cleanuplink;
  156.     dd->d_cleanupparent = &dir_cleanup_list;
  157.     dir_cleanup_list = dd;
  158.     return dd;
  159. }
  160.  
  161. void closedir(DIR *dd)
  162. {
  163.     if (dd) {
  164.         if (dd->d_cleanuplink)
  165.             dd->d_cleanuplink->d_cleanupparent = dd->d_cleanupparent;
  166.         *(dd->d_cleanupparent) = dd->d_cleanuplink;
  167.         if (dd->d_parentlock)
  168.             UnLock(dd->d_parentlock);
  169.         FreeMem(dd, sizeof(DIR));
  170.     }
  171. }
  172.  
  173. struct dirent *readdir(DIR *dd)
  174. {
  175.     return (ExNext(dd->d_parentlock, &dd->d_fib) ? (struct dirent *)dd : NULL);
  176. }
  177.  
  178.  
  179. int rmdir(char *path)
  180. {
  181.     return (DeleteFile(path) ? 0 : IoErr());
  182. }
  183.  
  184.  
  185. int chmod(char *filename, int bits)     /* bits are as for st_mode */
  186. {
  187.     long protmask = (bits & 0xFF) ^ 0xF;
  188.     return !SetProtection(filename, protmask);
  189. }
  190.  
  191. #endif /* AZTEC_C || USE_REPLACEMENTS */
  192.  
  193. #ifdef AZTEC_C
  194.  
  195. /* This here removes unnecessary bulk from the executable with Aztec: */
  196. void _wb_parse(void)  { }
  197.  
  198. /* fake a unix function that does not apply to amigados: */
  199. int umask(void)  { return 0; }
  200.  
  201.  
  202. #  include <signal.h>
  203.  
  204. /* C library signal() messes up debugging yet adds no actual usefulness */
  205. typedef void (*__signal_return_type)(int);
  206. __signal_return_type signal()  { return SIG_ERR; }
  207.  
  208.  
  209. /* The following replaces Aztec's argv-parsing function for compatibility with
  210. Unix-like syntax used on other platforms.  It also fixes the problem the
  211. standard _cli_parse() has of accepting only lower-ascii characters. */
  212.  
  213. int _argc, _arg_len;
  214. char **_argv, *_arg_lin;
  215.  
  216. void _cli_parse(struct Process *pp, long alen, register UBYTE *aptr)
  217. {
  218.     register UBYTE *cp;
  219.     register struct CommandLineInterface *cli;
  220.     register short c;
  221.     register short starred = 0;
  222. #  ifdef PRESTART_HOOK
  223.     void Prestart_Hook(void);
  224. #  endif
  225.  
  226.     cli = (struct CommandLineInterface *) (pp->pr_CLI << 2);
  227.     cp = (UBYTE *) (cli->cli_CommandName << 2);
  228.     _arg_len = cp[0] + alen + 2;
  229.     if (!(_arg_lin = AllocMem((long) _arg_len, 0L)))
  230.         return;
  231.     c = cp[0];
  232.     strncpy(_arg_lin, cp + 1, c);
  233.     _arg_lin[c] = 0;
  234.     for (cp = _arg_lin + c + 1; alen && (*aptr < '\n' || *aptr > '\r'); alen--)
  235.         *cp++ = *aptr++;
  236.     *cp = 0;
  237.     aptr = cp = _arg_lin + c + 1;
  238.     for (_argc = 1; ; _argc++) {
  239.         while (*cp == ' ' || *cp == '\t')
  240.             cp++;
  241.         if (!*cp)
  242.             break;
  243.         if (*cp == '"') {
  244.             cp++;
  245.             while (c = *cp++) {
  246.                 if (c == '"' && !starred) {
  247.                     *aptr++ = 0;
  248.                     starred = 0;
  249.                     break;
  250.                 } else if (c == '\\' && !starred)
  251.                     starred = 1;
  252.                 else {
  253.                     *aptr++ = c;
  254.                     starred = 0;
  255.                 }
  256.             }
  257.         } else {
  258.             while ((c = *cp++) && c != ' ' && c != '\t')
  259.                 *aptr++ = c;
  260.             *aptr++ = 0;
  261.         }
  262.         if (c == 0)
  263.             --cp;
  264.     }
  265.     *aptr = 0;
  266.     if (!(_argv = AllocMem((_argc + 1) * sizeof(*_argv), 0L))) {
  267.         _argc = 0;
  268.         return;
  269.     }
  270.     for (c = 0, cp = _arg_lin; c < _argc; c++) {
  271.         _argv[c] = cp;
  272.         cp += strlen(cp) + 1;
  273.     }
  274.     _argv[c] = NULL;
  275. #  ifdef PRESTART_HOOK
  276.     Prestart_Hook();
  277. #  endif
  278. }
  279.  
  280. #endif /* AZTEC_C */
  281. /* remove local define */
  282. #ifdef USE_REPLACEMENTS
  283. #   undef USE_REPLACEMENTS
  284. #endif
  285. #endif /* __amiga_stat_c */
  286.