scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

posix.c (999B)


      1 static char sccsid[] = "@(#) ./ar/posix/driver.c";
      2 
      3 #include <sys/types.h>
      4 #include <sys/time.h>
      5 #include <sys/stat.h>
      6 #include <string.h>
      7 #include <unistd.h>
      8 #include <utime.h>
      9 
     10 #include <limits.h>
     11 
     12 #include "sys.h"
     13 
     14 const char invalidchars[] = " ";
     15 
     16 time_t
     17 totime(long long t)
     18 {
     19 	return t;
     20 }
     21 
     22 char *
     23 canonical(char *path)
     24 {
     25 	char *name = strrchr(path, '/');
     26 	return (name && name[1]) ? name+1 : path;
     27 }
     28 
     29 int
     30 getstat(char *fname, struct fprop *prop)
     31 {
     32 	struct stat st;
     33 
     34 	if (stat(fname, &st) < 0)
     35 		return -1;
     36 	if (st.st_size > LONG_MAX)
     37 		return -1;
     38 	prop->uid = st.st_uid;
     39 	prop->gid = st.st_gid;
     40 	prop->mode = st.st_mode;
     41 	prop->time = st.st_mtime;
     42 	prop->size = st.st_size;
     43 
     44 	return 0;
     45 }
     46 
     47 int
     48 setstat(char *fname, struct fprop *prop)
     49 {
     50 	struct utimbuf ut = {prop->time, prop->time};
     51 
     52 	if (chown(fname, prop->uid, prop->gid) < 0) {
     53 		if (chown(fname, getuid(), getgid()) < 0)
     54 			return -1;
     55 	}
     56 	if (chmod(fname, prop->mode) < 0)
     57 		return -1;
     58 	if (utime(fname, &ut) < 0)
     59 		return -1;
     60 	return 0;
     61 }