"Fossies" - the Fresh Open Source Software Archive

Member "unarj-2.65/environ.c" (5 Jun 2002, 12943 Bytes) of package /linux/misc/old/unarj-2.65.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "environ.c" see the Fossies "Dox" file reference documentation.

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