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