home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / linux / backup / star-1.3.1.tar.gz / star-1.3.1.tar / star-1.3.1 / star / star_unix.c < prev    next >
C/C++ Source or Header  |  2001-04-30  |  9KB  |  392 lines

  1. /* @(#)star_unix.c    1.28 01/04/30 Copyright 1985, 1995 J. Schilling */
  2. #ifndef lint
  3. static    char sccsid[] =
  4.     "@(#)star_unix.c    1.28 01/04/30 Copyright 1985, 1995 J. Schilling";
  5. #endif
  6. /*
  7.  *    Stat / mode / owner routines for unix like
  8.  *    operating systems
  9.  *
  10.  *    Copyright (c) 1985, 1995 J. Schilling
  11.  */
  12. /*
  13.  * This program is free software; you can redistribute it and/or modify
  14.  * it under the terms of the GNU General Public License as published by
  15.  * the Free Software Foundation; either version 2, or (at your option)
  16.  * any later version.
  17.  *
  18.  * This program is distributed in the hope that it will be useful,
  19.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  * GNU General Public License for more details.
  22.  *
  23.  * You should have received a copy of the GNU General Public License
  24.  * along with this program; see the file COPYING.  If not, write to
  25.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  */
  27.  
  28. #include <mconfig.h>
  29. #include <stdio.h>
  30. #include <sys/types.h>
  31. #include <errno.h>
  32. #include "star.h"
  33. #include "table.h"
  34. #include <standard.h>
  35. #include <unixstd.h>
  36. #include <dirdefs.h>
  37. #include <statdefs.h>
  38. #include <device.h>
  39. #include <schily.h>
  40. #include "dirtime.h"
  41. #include "xutimes.h"
  42. #include "starsubs.h"
  43.  
  44. #ifndef    HAVE_LSTAT
  45. #define    lstat    stat
  46. #endif
  47. #ifndef    HAVE_LCHOWN
  48. #define    lchown    chown
  49. #endif
  50.  
  51. #define    ROOT_UID    0
  52.  
  53. extern    int    uid;
  54. extern    Ulong    curfs;
  55. extern    BOOL    nomtime;
  56. extern    BOOL    nochown;
  57. extern    BOOL    dirmode;
  58. extern    BOOL    follow;
  59.  
  60. EXPORT    BOOL    getinfo        __PR((char* name, FINFO * info));
  61. EXPORT    void    checkarch    __PR((FILE *f));
  62. EXPORT    void    setmodes    __PR((FINFO * info));
  63. LOCAL    int    sutimes        __PR((char* name, FINFO *info));
  64. EXPORT    int    snulltimes    __PR((char* name, FINFO *info));
  65. EXPORT    int    sxsymlink    __PR((FINFO *info));
  66. EXPORT    int    rs_acctime    __PR((int fd, FINFO * info));
  67.  
  68. EXPORT BOOL
  69. getinfo(name, info)
  70.     char    *name;
  71.     register FINFO    *info;
  72. {
  73.     struct stat    stbuf;
  74.  
  75.     if (follow?stat(name, &stbuf):lstat(name, &stbuf) < 0)
  76.         return (FALSE);
  77.     info->f_name = name;
  78.     info->f_uname = info->f_gname = NULL;
  79.     info->f_umaxlen = info->f_gmaxlen = 0L;
  80.     info->f_dev    = (Ulong) stbuf.st_dev;
  81.     if (curfs == -1L)
  82.         curfs = info->f_dev;
  83.     info->f_ino    = stbuf.st_ino;
  84.     info->f_nlink    = stbuf.st_nlink;
  85.     info->f_mode    = stbuf.st_mode & 07777;
  86.     info->f_uid    = stbuf.st_uid;
  87.     info->f_gid    = stbuf.st_gid;
  88.     info->f_size    = 0L;
  89.     info->f_rsize    = 0L;
  90.     info->f_flags    = 0L;
  91.     info->f_type    = stbuf.st_mode & ~07777;
  92.  
  93.     if (sizeof(stbuf.st_rdev) == sizeof(short)) {
  94.         info->f_rdev = (Ushort) stbuf.st_rdev;
  95.     } else if ((sizeof(int) != sizeof(long)) &&
  96.             (sizeof(stbuf.st_rdev) == sizeof(int))) {
  97.         info->f_rdev = (Uint) stbuf.st_rdev;
  98.     } else {
  99.         info->f_rdev = (Ulong) stbuf.st_rdev;
  100.     }
  101.     info->f_rdevmaj    = major(info->f_rdev);
  102.     info->f_rdevmin    = minor(info->f_rdev);
  103.     info->f_atime    = stbuf.st_atime;
  104.     info->f_mtime    = stbuf.st_mtime;
  105.     info->f_ctime    = stbuf.st_ctime;
  106. #ifdef    HAVE_ST_SPARE1        /* if struct stat contains st_spare1 (usecs) */
  107.     info->f_ansec    = stbuf.st_spare1*1000;
  108.     info->f_mnsec    = stbuf.st_spare2*1000;
  109.     info->f_cnsec    = stbuf.st_spare3*1000;
  110. #else
  111.  
  112. #ifdef    HAVE_ST_NSEC        /* if struct stat contains st_atim.st_nsec (nanosecs */ 
  113.     info->f_ansec    = stbuf.st_atim.tv_nsec;
  114.     info->f_mnsec    = stbuf.st_mtim.tv_nsec;
  115.     info->f_cnsec    = stbuf.st_ctim.tv_nsec;
  116. #else
  117.     info->f_ansec = info->f_mnsec = info->f_cnsec = 0L;
  118. #endif    /* HAVE_ST_NSEC */
  119. #endif    /* HAVE_ST_SPARE1 */
  120.  
  121.     switch ((int)(stbuf.st_mode & S_IFMT)) {
  122.  
  123.     case S_IFREG:
  124.             info->f_filetype = F_FILE;
  125.             info->f_rsize = info->f_size = stbuf.st_size;
  126.             break;
  127.     case S_IFDIR:
  128.             info->f_filetype = F_DIR;
  129.             break;
  130. #ifdef    S_IFLNK
  131.     case S_IFLNK:
  132.             info->f_filetype = F_SLINK;
  133.             break;
  134. #endif
  135. #ifdef    S_IFIFO
  136.     case S_IFIFO:
  137. #endif
  138.     default:
  139.             info->f_filetype = F_SPEC;
  140.     }
  141.     info->f_xftype = IFTOXT(info->f_type);
  142.  
  143. #ifdef    HAVE_ST_BLOCKS
  144. #if    defined(hpux) || defined(__hpux)
  145.     if (info->f_size > (stbuf.st_blocks * 1024 + 1024))
  146. #else
  147.     if (info->f_size > (stbuf.st_blocks * 512 + 512))
  148. #endif
  149.         info->f_flags |= F_SPARSE;
  150. #endif
  151.  
  152.     return (TRUE);
  153. }
  154.  
  155. EXPORT void
  156. checkarch(f)
  157.     FILE    *f;
  158. {
  159.     struct stat    stbuf;
  160.     extern    long    tape_dev;
  161.     extern    long    tape_ino;
  162.  
  163.     if (fstat(fdown(f), &stbuf) < 0)
  164.         return;
  165.     
  166.     if (S_ISREG(stbuf.st_mode)) {
  167.         tape_dev = stbuf.st_dev;
  168.         tape_ino = stbuf.st_ino;
  169.     } else if (((stbuf.st_mode & S_IFMT) == 0) ||
  170.             S_ISFIFO(stbuf.st_mode) ||
  171.             S_ISSOCK(stbuf.st_mode)) {
  172.         /*
  173.          * This is a pipe or similar on different UNIX implementations.
  174.          */
  175.         tape_dev = tape_ino = -1;
  176.     }
  177. }
  178.  
  179. EXPORT void
  180. setmodes(info)
  181.     register FINFO    *info;
  182. {
  183.     if (!nomtime && !is_symlink(info)) {
  184.         /*
  185.          * With Cygwin32,
  186.          * DOS will not allow us to set file times on read-only files.
  187.          * We set the time before we change the access modes to
  188.          * overcode this problem.
  189.          */
  190.         if (!is_dir(info))
  191.             sutimes(info->f_name, info);
  192.     }
  193.     if ((!is_dir(info) || dirmode) && !is_symlink(info))
  194.         if (chmod(info->f_name, (int)info->f_mode) < 0) {
  195.             ;
  196.         }
  197.     if (!nochown && uid == ROOT_UID) {
  198.         /*
  199.          * Star will not allow non root users to give away files.
  200.          */
  201.         lchown(info->f_name, (int)info->f_uid, (int)info->f_gid);
  202.  
  203.         if ((!is_dir(info) || dirmode) && !is_symlink(info) &&
  204.                 (info->f_mode & 07000) != 0) {
  205.             /*
  206.              * On a few systems, seeting the owner of a file
  207.              * destroys the suid and sgid bits.
  208.              * We repeat the chmod() in this case.
  209.              */
  210.             if (chmod(info->f_name, (int)info->f_mode) < 0) {
  211.                 ;
  212.             }
  213.         }
  214.     }
  215.     if (!nomtime && !is_symlink(info)) {
  216.         if (is_dir(info))
  217.             sdirtimes(info->f_name, info);
  218.         else
  219.             sutimes(info->f_name, info);
  220.     }
  221. }
  222.  
  223. EXPORT    int    xutimes        __PR((char* name, struct timeval *tp));
  224.  
  225. LOCAL int
  226. sutimes(name, info)
  227.     char    *name;
  228.     FINFO    *info;
  229. {
  230.     struct    timeval tp[3];
  231.  
  232.     tp[0].tv_sec = info->f_atime;
  233.     tp[0].tv_usec = info->f_ansec/1000;
  234.  
  235.     tp[1].tv_sec = info->f_mtime;
  236.     tp[1].tv_usec = info->f_mnsec/1000;
  237. #ifdef    SET_CTIME
  238.     tp[2].tv_sec = info->f_ctime;
  239.     tp[2].tv_usec = info->f_cnsec/1000;
  240. #else
  241.     tp[2].tv_sec = 0;
  242.     tp[2].tv_usec = 0;
  243. #endif
  244.     return (xutimes(name, tp));
  245. }
  246.  
  247. EXPORT int
  248. snulltimes(name, info)
  249.     char    *name;
  250.     FINFO    *info;
  251. {
  252.     struct    timeval tp[3];
  253.  
  254.     fillbytes((char *)tp, sizeof(tp), '\0');
  255.     return (xutimes(name, tp));
  256. }
  257.  
  258. EXPORT int
  259. xutimes(name, tp)
  260.     char    *name;
  261.     struct    timeval tp[3];
  262. {
  263.     struct  timeval curtime;
  264.     struct  timeval pasttime;
  265.     extern int Ctime;
  266.     int    ret;
  267.     int    errsav;
  268.  
  269. #ifdef    SET_CTIME
  270.     if (Ctime) {
  271.         gettimeofday(&curtime, 0);
  272.         settimeofday(&tp[2], 0);
  273.     }
  274. #endif
  275.     ret = utimes(name, tp);
  276.     errsav = errno;
  277.  
  278. #ifdef    SET_CTIME
  279.     if (Ctime) {
  280.         gettimeofday(&pasttime, 0);
  281.         /* XXX Hack: f_ctime.tv_usec ist immer 0! */
  282.         curtime.tv_usec += pasttime.tv_usec;
  283.         if (curtime.tv_usec > 1000000) {
  284.             curtime.tv_sec += 1;
  285.             curtime.tv_usec -= 1000000;
  286.         }
  287.         settimeofday(&curtime, 0);
  288. /*        printf("pasttime.usec: %d\n", pasttime.tv_usec);*/
  289.     }
  290. #endif
  291.     errno = errsav;
  292.     return (ret);
  293. }
  294.  
  295. EXPORT int
  296. sxsymlink(info)
  297.     FINFO    *info;
  298. {
  299.     struct    timeval tp[3];
  300.     struct  timeval curtime;
  301.     struct  timeval pasttime;
  302.     char    *linkname;
  303.     char    *name;
  304.     extern int Ctime;
  305.     int    ret;
  306.     int    errsav;
  307.  
  308.     tp[0].tv_sec = info->f_atime;
  309.     tp[0].tv_usec = info->f_ansec/1000;
  310.  
  311.     tp[1].tv_sec = info->f_mtime;
  312.     tp[1].tv_usec = info->f_mnsec/1000;
  313. #ifdef    SET_CTIME
  314.     tp[2].tv_sec = info->f_ctime;
  315.     tp[2].tv_usec = info->f_cnsec/1000;
  316. #endif
  317.     linkname = info->f_lname;
  318.     name = info->f_name;
  319.  
  320. #ifdef    SET_CTIME
  321.     if (Ctime) {
  322.         gettimeofday(&curtime, 0);
  323.         settimeofday(&tp[2], 0);
  324.     }
  325. #endif
  326.     ret = symlink(linkname, name);
  327.     errsav = errno;
  328.  
  329. #ifdef    SET_CTIME
  330.     if (Ctime) {
  331.         gettimeofday(&pasttime, 0);
  332.         /* XXX Hack: f_ctime.tv_usec ist immer 0! */
  333.         curtime.tv_usec += pasttime.tv_usec;
  334.         if (curtime.tv_usec > 1000000) {
  335.             curtime.tv_sec += 1;
  336.             curtime.tv_usec -= 1000000;
  337.         }
  338.         settimeofday(&curtime, 0);
  339. /*        printf("pasttime.usec: %d\n", pasttime.tv_usec);*/
  340.     }
  341. #endif
  342.     errno = errsav;
  343.     return (ret);
  344. }
  345.  
  346. #ifdef    HAVE_SYS_FILIO_H
  347. #include <sys/filio.h>
  348. #endif
  349.  
  350. EXPORT int
  351. rs_acctime(fd, info)
  352.          int    fd;
  353.     register FINFO    *info;
  354. {
  355. #ifdef    _FIOSATIME
  356.     struct timeval    atv;
  357. #endif
  358.  
  359.     if (is_symlink(info))
  360.         return (0);
  361.  
  362. #ifdef    _FIOSATIME
  363.     /*
  364.      * On Solaris 2.x root may reset accesstime without changing ctime.
  365.      */
  366.     if (uid == ROOT_UID) {
  367.         atv.tv_sec = info->f_atime;
  368.         atv.tv_usec = info->f_ansec/1000;
  369.         return (ioctl(fd, _FIOSATIME, &atv));
  370.     }
  371. #endif
  372.     return (sutimes(info->f_name, info));
  373. }
  374.  
  375. #ifndef    HAVE_UTIMES
  376. #include <utimdefs.h>
  377.  
  378. EXPORT int
  379. utimes(name, tp)
  380.     char        *name;
  381.     struct timeval    *tp;
  382. {
  383.     struct utimbuf    ut;
  384.  
  385.     ut.actime = tp->tv_sec;
  386.     tp++;
  387.     ut.modtime = tp->tv_sec;
  388.     
  389.     return (utime(name, &ut));
  390. }
  391. #endif
  392.