home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / UNARJ.LZH / ENVIRON.C < prev    next >
C/C++ Source or Header  |  1991-05-13  |  9KB  |  503 lines

  1. /* ENVIRON.C, UNARJ, R JUNG, 05/08/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 archivers.
  6.  *
  7.  *   If you wish to distribute a modified version of this program, you
  8.  *   must include the source code.
  9.  *
  10.  *   If you modify this program, I would appreciate a copy of the new
  11.  *   source code.  I am holding the copyright on the source code, so
  12.  *   please do not delete my name from the program files or from the
  13.  *   documentation.
  14.  *
  15.  *   The vanilla section of this file was tested with UNIX in mind.
  16.  *
  17.  * Modification history:
  18.  * Date      Programmer  Description of modification.
  19.  * 04/09/91  R. Jung     Rewrote code.
  20.  * 04/23/91  M. Adler     Portabilized.
  21.  * 04/29/91  R. Jung     Added get_mode_str().
  22.  * 05/08/91  R. Jung     Combined set_ftime() and set_fmode().
  23.  *
  24.  */
  25.  
  26. #include "unarj.h"
  27.  
  28. #ifdef __TURBOC__
  29.  
  30. #define SUBS_DEFINED
  31.  
  32. #include <string.h>
  33. #include <dos.h>
  34. #include <io.h>
  35. #include <fcntl.h>
  36. #include <alloc.h>
  37.  
  38. FILE *
  39. file_open(name, mode)
  40. char *name;
  41. char *mode;
  42. {
  43.     return fopen(name, mode);
  44. }
  45.  
  46. int
  47. file_read(buf, size, nitems, stream)
  48. char *buf;
  49. int  size;
  50. int  nitems;
  51. FILE *stream;
  52. {
  53.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  54. }
  55.  
  56. int
  57. file_seek(stream, offset, mode)
  58. FILE *stream;
  59. long offset;
  60. int  mode;
  61. {
  62.     return fseek(stream, offset, mode);
  63. }
  64.  
  65. long
  66. file_tell(stream)
  67. FILE *stream;
  68. {
  69.     return ftell(stream);
  70. }
  71.  
  72. int
  73. file_write(buf, size, nitems, stream)
  74. char *buf;
  75. int  size;
  76. int  nitems;
  77. FILE *stream;
  78. {
  79.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  80. }
  81.  
  82. voidp *
  83. xmalloc(size)
  84. int size;
  85. {
  86.     return (voidp *)malloc((size_t) size);
  87. }
  88.  
  89. void
  90. case_path(name)
  91. char *name;
  92. {
  93.     strupper(name);
  94. }
  95.  
  96. int
  97. file_exists(name)
  98. char *name;
  99. {
  100.     return (access(name, 0) == 0);
  101. }
  102.  
  103. void
  104. get_mode_str(str, mode)
  105. char   *str;
  106. ushort mode;
  107. {
  108.     strcpy(str, "---W");
  109.     if (mode & FA_ARCH)
  110.     str[0] = 'A';
  111.     if (mode & FA_SYSTEM)
  112.     str[1] = 'S';
  113.     if (mode & FA_HIDDEN)
  114.     str[2] = 'H';
  115.     if (mode & FA_RDONLY)
  116.     str[3] = 'R';
  117. }
  118.  
  119. int
  120. set_ftime_mode(name, tstamp, attribute, host)
  121. char  *name;
  122. ulong tstamp;
  123. uint  attribute;
  124. uchar host;
  125. {
  126.     FILE *fd;
  127.     int code;
  128.  
  129.     if ((fd = fopen(name, "r+b")) == NULL)
  130.     return -1;
  131.     code = setftime(fileno(fd), (struct ftime *) &tstamp);
  132.     fclose(fd);
  133.     if (host == OS)
  134.     {
  135.     if (_chmod(name, 1, attribute) == -1)
  136.         return -1;
  137.     }
  138.     return code;
  139. }
  140.  
  141. #endif
  142.  
  143. #ifdef _QC
  144.  
  145. #define SUBS_DEFINED
  146.  
  147. #include <string.h>
  148. #include <dos.h>
  149. #include <io.h>
  150. #include <fcntl.h>
  151. #include <malloc.h>
  152.  
  153. FILE *
  154. file_open(name, mode)
  155. char *name;
  156. char *mode;
  157. {
  158.     return fopen(name, mode);
  159. }
  160.  
  161. int
  162. file_read(buf, size, nitems, stream)
  163. char *buf;
  164. int  size;
  165. int  nitems;
  166. FILE *stream;
  167. {
  168.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  169. }
  170.  
  171. int
  172. file_seek(stream, offset, mode)
  173. FILE *stream;
  174. long offset;
  175. int  mode;
  176. {
  177.     return fseek(stream, offset, mode);
  178. }
  179.  
  180. long
  181. file_tell(stream)
  182. FILE *stream;
  183. {
  184.     return ftell(stream);
  185. }
  186.  
  187. int
  188. file_write(buf, size, nitems, stream)
  189. char *buf;
  190. int  size;
  191. int  nitems;
  192. FILE *stream;
  193. {
  194.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  195. }
  196.  
  197. voidp *
  198. xmalloc(size)
  199. int size;
  200. {
  201.     return (voidp *)malloc((size_t) size);
  202. }
  203.  
  204. void
  205. case_path(name)
  206. char *name;
  207. {
  208.     strupper(name);
  209. }
  210.  
  211. int
  212. file_exists(name)
  213. char *name;
  214. {
  215.     return (access(name, 0) == 0);
  216. }
  217.  
  218. void
  219. get_mode_str(str, mode)
  220. char   *str;
  221. ushort mode;
  222. {
  223.     strcpy(str, "---W");
  224.     if (mode & FA_ARCH)
  225.     str[0] = 'A';
  226.     if (mode & FA_SYSTEM)
  227.     str[1] = 'S';
  228.     if (mode & FA_HIDDEN)
  229.     str[2] = 'H';
  230.     if (mode & FA_RDONLY)
  231.     str[3] = 'R';
  232. }
  233.  
  234. int
  235. set_ftime_mode(name, tstamp, attribute, host)
  236. char  *name;
  237. ulong tstamp;
  238. uint  attribute;
  239. uchar host;
  240. {
  241.     FILE *fd;
  242.     int code;
  243.     uint date_stamp, time_stamp;
  244.  
  245.     date_stamp = (uint)(tstamp >> 16);
  246.     time_stamp = (uint)(tstamp & 0xFFFF);
  247.     if ((fd = fopen(name, "r+b")) == NULL)
  248.     return -1;
  249.     code = _dos_setftime(fileno(fd), date_stamp, time_stamp);
  250.     fclose(fd);
  251.     if (host == OS)
  252.     {
  253.     if (_dos_setfileattr(name, attribute))
  254.         return -1;
  255.     }
  256.     return code;
  257. }
  258.  
  259. #endif
  260.  
  261. #ifdef _OS2
  262.  
  263. #define SUBS_DEFINED
  264.  
  265. #include <string.h>
  266. #define INCL_DOSFILEMGR
  267. #include <os2.h>
  268. #include <io.h>
  269. #include <fcntl.h>
  270.  
  271. FILE *
  272. file_open(name, mode)
  273. char *name;
  274. char *mode;
  275. {
  276.     return fopen(name, mode);
  277. }
  278.  
  279. int
  280. file_read(buf, size, nitems, stream)
  281. char *buf;
  282. int  size;
  283. int  nitems;
  284. FILE *stream;
  285. {
  286.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  287. }
  288.  
  289. int
  290. file_seek(stream, offset, mode)
  291. FILE *stream;
  292. long offset;
  293. int  mode;
  294. {
  295.     return fseek(stream, offset, mode);
  296. }
  297.  
  298. long
  299. file_tell(stream)
  300. FILE *stream;
  301. {
  302.     return ftell(stream);
  303. }
  304.  
  305. int
  306. file_write(buf, size, nitems, stream)
  307. char *buf;
  308. int  size;
  309. int  nitems;
  310. FILE *stream;
  311. {
  312.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  313. }
  314.  
  315. voidp *
  316. xmalloc(size)
  317. int size;
  318. {
  319.     return (voidp *)malloc((size_t) size);
  320. }
  321.  
  322. void
  323. case_path(name)
  324. char *name;
  325. {
  326.     strupper(name);
  327. }
  328.  
  329. int
  330. file_exists(name)
  331. char *name;
  332. {
  333.     return (access(name, 0) == 0);
  334. }
  335.  
  336. void
  337. get_mode_str(str, mode)
  338. char   *str;
  339. ushort mode;
  340. {
  341.     strcpy(str, "---W");
  342.     if (mode & FA_ARCH)
  343.         str[0] = 'A';
  344.     if (mode & FA_SYSTEM)
  345.         str[1] = 'S';
  346.     if (mode & FA_HIDDEN)
  347.         str[2] = 'H';
  348.     if (mode & FA_RDONLY)
  349.         str[3] = 'R';
  350. }
  351.  
  352. int
  353. set_ftime_mode(name, tstamp, attribute, host)
  354. char  *name;
  355. ulong tstamp;
  356. uint  attribute;
  357. uchar host;
  358. {
  359.     int code;
  360.     FDATE date_stamp;
  361.     FTIME time_stamp;
  362.     HFILE handle;
  363.     FILESTATUS info;
  364.     USHORT action;
  365.  
  366.     date_stamp.day = ts_day (tstamp);
  367.     date_stamp.month = ts_month (tstamp);
  368.     date_stamp.year = ts_year (tstamp) - 1980;
  369.     time_stamp.twosecs = ts_sec (tstamp) / 2;
  370.     time_stamp.minutes = ts_min (tstamp);
  371.     time_stamp.hours = ts_hour (tstamp);
  372.     if (DosOpen (name, &handle, &action, 0L, 0, FILE_OPEN,
  373.          OPEN_ACCESS_READWRITE|OPEN_SHARE_DENYREADWRITE, 0L) != 0)
  374.     return -1;
  375.     info.fdateCreation = date_stamp;
  376.     info.ftimeCreation = time_stamp;
  377.     info.fdateLastAccess = date_stamp;
  378.     info.ftimeLastAccess = time_stamp;
  379.     info.fdateLastWrite = date_stamp;
  380.     info.ftimeLastWrite = time_stamp;
  381.     info.cbFile = 0;
  382.     info.cbFileAlloc = 0;
  383.     info.attrFile = 0;
  384.     code = (int)DosSetFileInfo (handle, 1, (PBYTE)&info, sizeof (info));
  385.     (void)DosClose (handle);
  386.     if (host == OS)
  387.     {
  388.     if (DosSetFileMode (name, attribute, 0L))
  389.         return -1;
  390.     }
  391.     return code;
  392. }
  393.  
  394. #endif
  395.  
  396. #ifndef SUBS_DEFINED       /* vanilla version for other compilers */
  397.                /* specifically tested under SUN UNIX */
  398. #ifdef MODERN
  399. #  include <string.h>
  400. #else /* !MODERN */
  401.    extern char *strcpy();
  402.    extern voidp *malloc();
  403. #endif /* ?MODERN */
  404.  
  405. FILE *
  406. file_open(name, mode)
  407. char *name;
  408. char *mode;
  409. {
  410.     return fopen(name, mode);
  411. }
  412.  
  413. int
  414. file_read(buf, size, nitems, stream)
  415. char *buf;
  416. int  size;
  417. int  nitems;
  418. FILE *stream;
  419. {
  420.     return fread(buf, (int) size, (int) nitems, stream);
  421. }
  422.  
  423. int
  424. file_seek(stream, offset, mode)
  425. FILE *stream;
  426. long offset;
  427. int  mode;
  428. {
  429.     return fseek(stream, offset, mode);
  430. }
  431.  
  432. long
  433. file_tell(stream)
  434. FILE *stream;
  435. {
  436.     return ftell(stream);
  437. }
  438.  
  439. int
  440. file_write(buf, size, nitems, stream)
  441. char *buf;
  442. int  size;
  443. int  nitems;
  444. FILE *stream;
  445. {
  446.     return fwrite(buf, (int) size, (int) nitems, stream);
  447. }
  448.  
  449. voidp *
  450. xmalloc(size)
  451. int size;
  452. {
  453.     return (voidp *)malloc((uint) size);
  454. }
  455.  
  456. void
  457. case_path(name)
  458. char *name;
  459. {
  460. }
  461.  
  462. int
  463. file_exists(name)
  464. char *name;
  465. {
  466.     FILE *fd;
  467.  
  468.     if ((fd = fopen(name, "rb")) == NULL)
  469.     return 0;
  470.     fclose(fd);
  471.     return 1;
  472. }
  473.  
  474. void
  475. get_mode_str(str, mode)
  476. char   *str;
  477. ushort mode;
  478. {
  479.     strcpy(str, "---W");
  480.     if (mode & FA_ARCH)
  481.     str[0] = 'A';
  482.     if (mode & FA_SYSTEM)
  483.     str[1] = 'S';
  484.     if (mode & FA_HIDDEN)
  485.     str[2] = 'H';
  486.     if (mode & FA_RDONLY)
  487.     str[3] = 'R';
  488. }
  489.  
  490. int
  491. set_ftime_mode(name, tstamp, attribute, host)
  492. char  *name;
  493. ulong tstamp;
  494. uint  attribute;
  495. uchar host;
  496. {
  497.     return 0;
  498. }
  499.  
  500. #endif    /* end of vanilla section */
  501.  
  502. /* end ENVIRON.C */
  503.