home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unarj32.lzh / environ.c < prev    next >
Text File  |  1994-03-08  |  15KB  |  781 lines

  1. /* ENVIRON.C, UNARJ, R JUNG, 09/01/91
  2.  * Implementation dependent routines
  3.  * Copyright (c) 1991 by Robert K Jung.  All rights reserved.
  4.  *
  5.  *   This code may be freely used in programs that are NOT ARJ archivers
  6.  *   (both compress and extract ARJ archives).
  7.  *
  8.  *   If you wish to distribute a modified version of this program, you
  9.  *   MUST indicate that it is a modified version both in the program and
  10.  *   source code.
  11.  *
  12.  *   If you modify this program, I would appreciate a copy of the new
  13.  *   source code.  I am holding the copyright on the source code, so
  14.  *   please do not delete my name from the program files or from the
  15.  *   documentation.
  16.  *
  17.  *   The UNIX file date-time stamping code is derived from ZOO by
  18.  *   Rahul Dhesi.
  19.  *
  20.  * Modification history:
  21.  * Date      Programmer  Description of modification.
  22.  * 04/09/91  R. Jung     Rewrote code.
  23.  * 04/23/91  M. Adler    Portabilized.
  24.  * 04/29/91  R. Jung     Added get_mode_str().
  25.  * 05/08/91  R. Jung     Combined set_ftime() and set_fmode().
  26.  * 06/03/91  R. Jung     Changed arguments in get_mode_str() and
  27.  *                       set_ftime_mode().
  28.  * 07/07/91  R. Jung     Added default_case_path() and UNIX section.
  29.  * 07/24/91  R. Jung     Fixed use of _chmod to handle directories.
  30.  * 08/27/91  R. Jung     Added date/time handling to Coherent.
  31.  * 09/01/91  R. Jung     Added #include <stdlib.h> to vanilla section.
  32.  *                       Added file date-time stamping to UNIX section.
  33.  *
  34.  */
  35.  
  36. #include "unarj.h"
  37.  
  38. #ifdef __TURBOC__
  39.  
  40. #define SUBS_DEFINED
  41.  
  42. #include <string.h>
  43. #include <dos.h>
  44. #include <io.h>
  45. #include <fcntl.h>
  46. #include <alloc.h>
  47.  
  48. FILE *
  49. file_open(name, mode)
  50. char *name;
  51. char *mode;
  52. {
  53.     return fopen(name, mode);
  54. }
  55.  
  56. int
  57. file_read(buf, size, nitems, stream)
  58. char *buf;
  59. int  size;
  60. int  nitems;
  61. FILE *stream;
  62. {
  63.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  64. }
  65.  
  66. int
  67. file_seek(stream, offset, mode)
  68. FILE *stream;
  69. long offset;
  70. int  mode;
  71. {
  72.     return fseek(stream, offset, mode);
  73. }
  74.  
  75. long
  76. file_tell(stream)
  77. FILE *stream;
  78. {
  79.     return ftell(stream);
  80. }
  81.  
  82. int
  83. file_write(buf, size, nitems, stream)
  84. char *buf;
  85. int  size;
  86. int  nitems;
  87. FILE *stream;
  88. {
  89.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  90. }
  91.  
  92. voidp *
  93. xmalloc(size)
  94. int size;
  95. {
  96.     return (voidp *)malloc((size_t) size);
  97. }
  98.  
  99. void
  100. case_path(name)
  101. char *name;
  102. {
  103.     strupper(name);
  104. }
  105.  
  106. void
  107. default_case_path(name)
  108. char *name;
  109. {
  110.     strupper(name);
  111. }
  112.  
  113. int
  114. file_exists(name)
  115. char *name;
  116. {
  117.     return (access(name, 0) == 0);
  118. }
  119.  
  120. void
  121. get_mode_str(str, mode)
  122. char *str;
  123. uint mode;
  124. {
  125.     strcpy(str, "---W");
  126.     if (mode & FA_ARCH)
  127.         str[0] = 'A';
  128.     if (mode & FA_SYSTEM)
  129.         str[1] = 'S';
  130.     if (mode & FA_HIDDEN)
  131.         str[2] = 'H';
  132.     if (mode & FA_RDONLY)
  133.         str[3] = 'R';
  134. }
  135.  
  136. int
  137. set_ftime_mode(name, tstamp, attribute, host)
  138. char  *name;
  139. ulong tstamp;
  140. uint  attribute;
  141. uint  host;
  142. {
  143.     FILE *fd;
  144.     int code;
  145.  
  146.     if ((fd = fopen(name, "r+b")) == NULL)
  147.         return -1;
  148.     code = setftime(fileno(fd), (struct ftime *) &tstamp);
  149.     fclose(fd);
  150.     if (host == OS)
  151.     {
  152.         attribute &= 0x27;
  153.         if (_chmod(name, 1, attribute) == -1)
  154.             return -1;
  155.     }
  156.     return code;
  157. }
  158.  
  159. #endif
  160.  
  161. #ifdef _QC
  162.  
  163. #define SUBS_DEFINED
  164.  
  165. #include <string.h>
  166. #include <dos.h>
  167. #include <io.h>
  168. #include <fcntl.h>
  169. #include <malloc.h>
  170.  
  171. FILE *
  172. file_open(name, mode)
  173. char *name;
  174. char *mode;
  175. {
  176.     return fopen(name, mode);
  177. }
  178.  
  179. int
  180. file_read(buf, size, nitems, stream)
  181. char *buf;
  182. int  size;
  183. int  nitems;
  184. FILE *stream;
  185. {
  186.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  187. }
  188.  
  189. int
  190. file_seek(stream, offset, mode)
  191. FILE *stream;
  192. long offset;
  193. int  mode;
  194. {
  195.     return fseek(stream, offset, mode);
  196. }
  197.  
  198. long
  199. file_tell(stream)
  200. FILE *stream;
  201. {
  202.     return ftell(stream);
  203. }
  204.  
  205. int
  206. file_write(buf, size, nitems, stream)
  207. char *buf;
  208. int  size;
  209. int  nitems;
  210. FILE *stream;
  211. {
  212.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  213. }
  214.  
  215. voidp *
  216. xmalloc(size)
  217. int size;
  218. {
  219.     return (voidp *)malloc((size_t) size);
  220. }
  221.  
  222. void
  223. case_path(name)
  224. char *name;
  225. {
  226.     strupper(name);
  227. }
  228.  
  229. void
  230. default_case_path(name)
  231. char *name;
  232. {
  233.     strupper(name);
  234. }
  235.  
  236. int
  237. file_exists(name)
  238. char *name;
  239. {
  240.     return (access(name, 0) == 0);
  241. }
  242.  
  243. void
  244. get_mode_str(str, mode)
  245. char *str;
  246. uint mode;
  247. {
  248.     strcpy(str, "---W");
  249.     if (mode & FA_ARCH)
  250.         str[0] = 'A';
  251.     if (mode & FA_SYSTEM)
  252.         str[1] = 'S';
  253.     if (mode & FA_HIDDEN)
  254.         str[2] = 'H';
  255.     if (mode & FA_RDONLY)
  256.         str[3] = 'R';
  257. }
  258.  
  259. int
  260. set_ftime_mode(name, tstamp, attribute, host)
  261. char  *name;
  262. ulong tstamp;
  263. uint  attribute;
  264. uint  host;
  265. {
  266.     FILE *fd;
  267.     int code;
  268.     uint date_stamp, time_stamp;
  269.  
  270.     date_stamp = (uint)(tstamp >> 16);
  271.     time_stamp = (uint)(tstamp & 0xFFFF);
  272.     if ((fd = fopen(name, "r+b")) == NULL)
  273.         return -1;
  274.     code = _dos_setftime(fileno(fd), date_stamp, time_stamp);
  275.     fclose(fd);
  276.     if (host == OS)
  277.     {
  278.         if (_dos_setfileattr(name, attribute))
  279.             return -1;
  280.     }
  281.     return code;
  282. }
  283.  
  284. #endif
  285.  
  286. #ifdef _OS2
  287.  
  288. #define SUBS_DEFINED
  289.  
  290. #include <string.h>
  291. #define INCL_DOSFILEMGR
  292. #include <os2.h>
  293. #include <io.h>
  294. #include <fcntl.h>
  295.  
  296. FILE *
  297. file_open(name, mode)
  298. char *name;
  299. char *mode;
  300. {
  301.     return fopen(name, mode);
  302. }
  303.  
  304. int
  305. file_read(buf, size, nitems, stream)
  306. char *buf;
  307. int  size;
  308. int  nitems;
  309. FILE *stream;
  310. {
  311.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  312. }
  313.  
  314. int
  315. file_seek(stream, offset, mode)
  316. FILE *stream;
  317. long offset;
  318. int  mode;
  319. {
  320.     return fseek(stream, offset, mode);
  321. }
  322.  
  323. long
  324. file_tell(stream)
  325. FILE *stream;
  326. {
  327.     return ftell(stream);
  328. }
  329.  
  330. int
  331. file_write(buf, size, nitems, stream)
  332. char *buf;
  333. int  size;
  334. int  nitems;
  335. FILE *stream;
  336. {
  337.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  338. }
  339.  
  340. voidp *
  341. xmalloc(size)
  342. int size;
  343. {
  344.     return (voidp *)malloc((size_t) size);
  345. }
  346.  
  347. void
  348. case_path(name)
  349. char *name;
  350. {
  351.     strupper(name);
  352. }
  353.  
  354. void
  355. default_case_path(name)
  356. char *name;
  357. {
  358.     strupper(name);
  359. }
  360.  
  361. int
  362. file_exists(name)
  363. char *name;
  364. {
  365. #ifdef __IBMC__
  366. #define NORMAL_FILES 0x0
  367.  HDIR          FindHandle;
  368.  FILEFINDBUF3  FindBuffer;
  369.  ULONG         FindCount;
  370.  ULONG         File_Attribute;
  371.  APIRET        rc;         /* Return code */
  372.  
  373.     FindHandle = HDIR_CREATE;
  374.     FindCount = 1;
  375.     File_Attribute = NORMAL_FILES | FILE_ARCHIVED | FILE_HIDDEN | FILE_READONLY;  /* All files, no dirs */
  376.  
  377.    rc = DosFindFirst(name,        /* File pattern */
  378.                      &FindHandle,           /* Directory search handle */
  379.                      File_Attribute,        /* Search attribute */
  380.                      (PVOID) &FindBuffer,   /* Result buffer */
  381.                      sizeof(FindBuffer),    /* Result buffer length */
  382.                      &FindCount,            /* # of entries to find */
  383.                      FIL_STANDARD);        /* Return level 1 file info */
  384.  
  385.  
  386.    if (rc != 0)
  387.    {
  388.       rc = DosFindClose(FindHandle);
  389.       return(FALSE);
  390.    }
  391.    rc = DosFindClose(FindHandle);
  392.    return(TRUE);
  393. #else
  394.     return (access(name, 0) == 0);
  395. #endif
  396. }
  397.  
  398. void
  399. get_mode_str(str, mode)
  400. char *str;
  401. uint mode;
  402. {
  403.     strcpy(str, "---W");
  404.     if (mode & FA_ARCH)
  405.         str[0] = 'A';
  406.     if (mode & FA_SYSTEM)
  407.         str[1] = 'S';
  408.     if (mode & FA_HIDDEN)
  409.         str[2] = 'H';
  410.     if (mode & FA_RDONLY)
  411.         str[3] = 'R';
  412. }
  413.  
  414. int
  415. set_ftime_mode(name, tstamp, attribute, host)
  416. char  *name;
  417. ulong tstamp;
  418. uint  attribute;
  419. uint  host;
  420. {
  421.     int code;
  422.     FDATE date_stamp;
  423.     FTIME time_stamp;
  424.     HFILE handle;
  425.     FILESTATUS info;
  426.     ULONG action;
  427.  
  428.     date_stamp.day = ts_day (tstamp);
  429.     date_stamp.month = ts_month (tstamp);
  430.     date_stamp.year = ts_year (tstamp) - 1980;
  431.     time_stamp.twosecs = ts_sec (tstamp) / 2;
  432.     time_stamp.minutes = ts_min (tstamp);
  433.     time_stamp.hours = ts_hour (tstamp);
  434.     if (DosOpen (name, &handle, &action, 0L, 0, FILE_OPEN,
  435.                  OPEN_ACCESS_READWRITE|OPEN_SHARE_DENYREADWRITE, 0L) != 0)
  436.         return -1;
  437.     info.fdateCreation = date_stamp;
  438.     info.ftimeCreation = time_stamp;
  439.     info.fdateLastAccess = date_stamp;
  440.     info.ftimeLastAccess = time_stamp;
  441.     info.fdateLastWrite = date_stamp;
  442.     info.ftimeLastWrite = time_stamp;
  443.     info.cbFile = 0;
  444.     info.cbFileAlloc = 0;
  445. #ifdef __IBMC__
  446.     if (host == OS) {
  447.        info.attrFile = (ULONG)attribute;
  448.     } else {
  449.        info.attrFile = 0;
  450.     } /* endif */
  451. #endif
  452.     info.attrFile = 0;
  453.     code = (int)DosSetFileInfo (handle, 1, (PBYTE)&info, sizeof (info));
  454.     (void)DosClose (handle);
  455. #ifndef __IBMC__
  456.     if (host == OS)
  457.     {
  458.         if (DosSetFileMode (name, attribute, 0L))
  459.             return -1;
  460.     }
  461. #endif
  462.     return code;
  463. }
  464.  
  465. #endif
  466.  
  467. #ifdef UNIX
  468.  
  469. #define SUBS_DEFINED
  470.  
  471. #include <time.h>
  472.  
  473. #ifndef time_t
  474. #define time_t long
  475. #endif
  476.  
  477. extern struct tm *localtime();
  478. extern time_t time();
  479. extern char   *strcpy();
  480. extern voidp  *malloc();
  481.  
  482. FILE *
  483. file_open(name, mode)
  484. char *name;
  485. char *mode;
  486. {
  487.     return fopen(name, mode);
  488. }
  489.  
  490. int
  491. file_read(buf, size, nitems, stream)
  492. char *buf;
  493. int  size;
  494. int  nitems;
  495. FILE *stream;
  496. {
  497.     return fread(buf, (int) size, (int) nitems, stream);
  498. }
  499.  
  500. int
  501. file_seek(stream, offset, mode)
  502. FILE *stream;
  503. long offset;
  504. int  mode;
  505. {
  506.     return fseek(stream, offset, mode);
  507. }
  508.  
  509. long
  510. file_tell(stream)
  511. FILE *stream;
  512. {
  513.     return ftell(stream);
  514. }
  515.  
  516. int
  517. file_write(buf, size, nitems, stream)
  518. char *buf;
  519. int  size;
  520. int  nitems;
  521. FILE *stream;
  522. {
  523.     return fwrite(buf, (int) size, (int) nitems, stream);
  524. }
  525.  
  526. voidp *
  527. xmalloc(size)
  528. int size;
  529. {
  530.     return (voidp *)malloc((uint) size);
  531. }
  532.  
  533. void
  534. case_path(name)
  535. char *name;
  536. {
  537.     (char *) name;
  538. }
  539.  
  540. void
  541. default_case_path(name)
  542. char *name;
  543. {
  544.     strlower(name);
  545. }
  546.  
  547. int
  548. file_exists(name)
  549. char *name;
  550. {
  551.     FILE *fd;
  552.  
  553.     if ((fd = fopen(name, "rb")) == NULL)
  554.         return 0;
  555.     fclose(fd);
  556.     return 1;
  557. }
  558.  
  559. void
  560. get_mode_str(str, mode)
  561. char *str;
  562. uint mode;
  563. {
  564.     strcpy(str, "---W");
  565.     if (mode & FA_ARCH)
  566.         str[0] = 'A';
  567.     if (mode & FA_SYSTEM)
  568.         str[1] = 'S';
  569.     if (mode & FA_HIDDEN)
  570.         str[2] = 'H';
  571.     if (mode & FA_RDONLY)
  572.         str[3] = 'R';
  573. }
  574.  
  575. long
  576. gettz()         /* returns the offset from GMT in seconds */
  577. {
  578. #define NOONOFFSET    43200L
  579. #define SEC_IN_DAY    (24L * 60L * 60L)
  580. #define INV_VALUE     (SEC_IN_DAY + 1L)
  581.     static long retval = INV_VALUE;
  582.     long now, noon;
  583.     struct tm *noontm;
  584.  
  585.     if (retval != INV_VALUE)
  586.         return retval;
  587.     now = (long) time((long *) 0);
  588.     /* Find local time for GMT noon today */
  589.     noon = now - now % SEC_IN_DAY + NOONOFFSET ;
  590.     noontm = localtime(&noon);
  591.     retval = NOONOFFSET - 60 * (60 * noontm->tm_hour - noontm->tm_min);
  592.     return retval;
  593. }
  594.  
  595. long
  596. mstonix(tstamp)
  597. ulong tstamp;
  598. {
  599.     uint date, time;
  600.     int year, month, day, hour, min, sec, daycount;
  601.     long longtime;
  602.     /* no. of days to beginning of month for each month */
  603.     static int dsboy[12] =
  604.         { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  605.  
  606.     date = (uint) ((tstamp >> 16) & 0xffff);
  607.     time = (uint) (tstamp & 0xffff);
  608.     if (date == 0 && time == 0)
  609.         return 0L;
  610.  
  611.     year  = ((date >> 9) & 0x7f) + 1980;
  612.     month = (date >> 5) & 0x0f;
  613.     day   = date & 0x1f;
  614.     hour  = (time >> 11) & 0x1f;
  615.     min   = (time >> 5) & 0x3f;
  616.     sec   = (time & 0x1f) * 2;
  617.  
  618.     daycount = 365 * (year - 1970) +   /* days due to whole years */
  619.                (year - 1969) / 4 +     /* days due to leap years */
  620.                dsboy[month-1] +        /* days since beginning of this year */
  621.                day-1;                  /* days since beginning of month */
  622.  
  623.     if (year % 4 == 0 &&
  624.         year % 400 != 0 && month >= 3)  /* if this is a leap year and month */
  625.         daycount++;                     /* is March or later, add a day */
  626.  
  627.     longtime = daycount * 24L * 60L * 60L +
  628.                hour * 60L * 60L + min * 60 + sec;
  629.     return longtime;
  630. }
  631.  
  632. int
  633. set_ftime_mode(name, tstamp, attribute, host)
  634. char  *name;
  635. ulong tstamp;
  636. uint  attribute;
  637. uint  host;
  638. {
  639.     time_t m_time;
  640.     struct utimbuf
  641.     {
  642.        time_t atime;             /* New access time */
  643.        time_t mtime;             /* New modification time */
  644.     } tb;
  645.  
  646.     (char *) name;
  647.     (uint) attribute;
  648.     (uint) host;
  649.  
  650.     m_time = mstonix(tstamp) + gettz();
  651.  
  652.     tb.mtime = m_time;                  /* Set modification time */
  653.     tb.atime = m_time;                  /* Set access time */
  654.  
  655.     /* set the time stamp on the file */
  656.     return utime(name, &tb);
  657. }
  658.  
  659. #endif  /* end of UNIX section */
  660.  
  661. #ifndef SUBS_DEFINED       /* vanilla version for other compilers */
  662.  
  663. #ifdef MODERN
  664. #include <string.h>
  665. #include <stdlib.h>
  666. #else /* !MODERN */
  667. extern char *strcpy();
  668. extern voidp *malloc();
  669. #endif /* ?MODERN */
  670.  
  671. FILE *
  672. file_open(name, mode)
  673. char *name;
  674. char *mode;
  675. {
  676.     return fopen(name, mode);
  677. }
  678.  
  679. int
  680. file_read(buf, size, nitems, stream)
  681. char *buf;
  682. int  size;
  683. int  nitems;
  684. FILE *stream;
  685. {
  686.     return fread(buf, (int) size, (int) nitems, stream);
  687. }
  688.  
  689. int
  690. file_seek(stream, offset, mode)
  691. FILE *stream;
  692. long offset;
  693. int  mode;
  694. {
  695.     return fseek(stream, offset, mode);
  696. }
  697.  
  698. long
  699. file_tell(stream)
  700. FILE *stream;
  701. {
  702.     return ftell(stream);
  703. }
  704.  
  705. int
  706. file_write(buf, size, nitems, stream)
  707. char *buf;
  708. int  size;
  709. int  nitems;
  710. FILE *stream;
  711. {
  712.     return fwrite(buf, (int) size, (int) nitems, stream);
  713. }
  714.  
  715. voidp *
  716. xmalloc(size)
  717. int size;
  718. {
  719.     return (voidp *)malloc((uint) size);
  720. }
  721.  
  722. void
  723. case_path(name)
  724. char *name;
  725. {
  726.     (char *) name;
  727. }
  728.  
  729. void
  730. default_case_path(name)
  731. char *name;
  732. {
  733.     (char *) name;
  734. }
  735.  
  736. int
  737. file_exists(name)
  738. char *name;
  739. {
  740.     FILE *fd;
  741.  
  742.     if ((fd = fopen(name, "rb")) == NULL)
  743.         return 0;
  744.     fclose(fd);
  745.     return 1;
  746. }
  747.  
  748. void
  749. get_mode_str(str, mode)
  750. char *str;
  751. uint mode;
  752. {
  753.     strcpy(str, "---W");
  754.     if (mode & FA_ARCH)
  755.         str[0] = 'A';
  756.     if (mode & FA_SYSTEM)
  757.         str[1] = 'S';
  758.     if (mode & FA_HIDDEN)
  759.         str[2] = 'H';
  760.     if (mode & FA_RDONLY)
  761.         str[3] = 'R';
  762. }
  763.  
  764. int
  765. set_ftime_mode(name, tstamp, attribute, host)
  766. char  *name;
  767. ulong tstamp;
  768. uint  attribute;
  769. uint  host;
  770. {
  771.     (char *) name;
  772.     (ulong) tstamp;
  773.     (uint) attribute;
  774.     (uint) host;
  775.     return 0;
  776. }
  777.  
  778. #endif  /* end of vanilla section */
  779.  
  780. /* end ENVIRON.C */
  781.