home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / disk / archive / nspark_1 / nspark-1.7.5 / unix.c < prev    next >
C/C++ Source or Header  |  1994-12-13  |  3KB  |  126 lines

  1. /*
  2.  * Operating System specific function (UNIX)
  3.  *
  4.  * $Header: unix.c 1.6 94/12/13 $
  5.  * $Log:    unix.c,v $
  6.  * Revision 1.6  94/12/13  11:36:31  arb
  7.  * Changed use of timelocal() to SunOS 4 only... hope that's right!
  8.  *
  9.  * Revision 1.5  92/12/07  17:18:49  duplain
  10.  * reformatted source.
  11.  * 
  12.  * Revision 1.4  92/10/19  09:35:10  duplain
  13.  * Changed use of timelocal() to SunOS only... hope that's right.
  14.  * 
  15.  * Revision 1.3  92/10/06  12:12:51  duplain
  16.  * Date/time conversion now done by mktime() or timelocal().
  17.  * 
  18.  * Revision 1.2  92/10/01  11:23:10  duplain
  19.  * Added filesize().
  20.  * 
  21.  * Revision 1.1  92/09/29  18:02:28  duplain
  22.  * Initial revision
  23.  * 
  24.  */
  25.  
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <time.h>
  29. #if !defined(BSD42) && !defined(SYSV2)
  30. #include <utime.h>
  31. #endif /* not 4.2BSD and not SysVr2 */
  32. #include <stdio.h>
  33. #include "spark.h"
  34. #include "date.h"
  35.  
  36. static char rcsid[] = "$Header: unix.c 1.6 94/12/13 $";
  37.  
  38. /*
  39.  * return the length of a file
  40.  */
  41. Word
  42. filesize(pathname)
  43.     char *pathname;
  44. {
  45.     struct stat statb;
  46.  
  47.     if (stat(pathname, &statb) < 0)
  48.     return (0);
  49.     else
  50.     return ((Word)statb.st_size);
  51. }
  52.  
  53. /*
  54.  * test for the existance of a file or directory
  55.  */
  56. Ftype
  57. exist(pathname)
  58.     char *pathname;
  59. {
  60.     struct stat statb;
  61.  
  62.     if (stat(pathname, &statb) < 0)
  63.     return (NOEXIST);    /* assumes error was because file
  64.                        doesn't exist... could be wrong! */
  65.     if (statb.st_mode & S_IFDIR)
  66.     return (ISDIR);
  67.     else
  68.     return (ISFILE);    /* might not be a regular file... */
  69. }
  70.     
  71. /*
  72.  * make a directory
  73.  */
  74. int
  75. makedir(pathname)
  76.     char *pathname;
  77. {
  78.     return (mkdir(pathname, 0777));
  79. }
  80.  
  81. /*
  82.  * stamp a file with date and time
  83.  */
  84. int
  85. filestamp(header, filename)
  86.     Header *header;
  87.     char *filename;
  88. {
  89. #if defined(BSD42) || defined(SYSV2)
  90.     return (0);    /* not by supported 4.2BSD or SysV2 */
  91. #else /* not 4.2BSD or SysV */
  92.     Date *date;
  93.     struct tm tm;
  94.     struct utimbuf utimbuf;
  95.     time_t filetime;
  96.  
  97.     if ((header->load & (Word)0xfff00000) != (Word)0xfff00000)
  98.     return (0);    /* not a timestamp */
  99.  
  100. #ifdef BSD
  101.     bzero((char *)&tm, sizeof(tm));
  102. #else /* not BSD */
  103.     memset((char *)&tm, '\0', sizeof(tm));
  104. #endif /* BSD */
  105.  
  106.     if (!(date = makedate(header)))
  107.     return (-1);
  108.  
  109.     tm.tm_sec = date->second;
  110.     tm.tm_min = date->minute;
  111.     tm.tm_hour = date->hour;
  112.     tm.tm_mday = date->day;
  113.     tm.tm_mon = date->month - 1;
  114.     tm.tm_year = date->year;
  115. #if defined(sun) && defined(BSD) /* SunOS 4 */
  116.     filetime = timelocal(&tm);
  117. #else /* not SunOS */
  118.     filetime = mktime(&tm);
  119. #endif /* SunOS */
  120.  
  121.     utimbuf.actime = filetime;
  122.     utimbuf.modtime = filetime;
  123.     return (utime(filename, &utimbuf));
  124. #endif /* not 4.2BSD and not SysV2 */
  125. }
  126.