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