Sigrid Solveig Haflínudóttirsys_mkdir: zero-init stat struct7d065022 months agoHistory
210 lines · 3.0 KB · C
RawDownload
#include <errno.h>#include <fcntl.h>#include <time.h>#include <locale.h>#include <sys/stat.h>#include "inc/priv.h"#include "inc/time.h" u64intsec_realtime(void){	struct timeval now; 	if(gettimeofday(&now, nil) != 0)		return 0;	return (u64int)now.tv_sec*1000000000ULL + (u64int)now.tv_usec*1000ULL;} u64intnanosec_monotonic(void){	static u64int xstart = ~0ULL;	u64int x; 	if(xstart == ~0ULL){		xstart = uclock();		return 0;	}	x = uclock() - xstart; 	u64int q = x / UCLOCKS_PER_SEC;	u64int r = x % UCLOCKS_PER_SEC; 	return q*Nsec + r*Nsec/UCLOCKS_PER_SEC;} voidtimestr(u64int ns, char *buf, int sz){	time_t tme = ns/Nsec;	struct tm tm; 	*buf = 0;	if(localtime_r(&tme, &tm) != nil)		strftime(buf, sz, "%a %b %e %H:%M:%S %Y", &tm);} u64intparsetime(const char *s){	USED(s);	return 0;} voidsleep_ms(int ms){	if(ms != 0){		struct timeval timeout;		timeout.tv_sec = ms/1000;		timeout.tv_usec = (ms % 1000) * 1000;		select(0, nil, nil, nil, &timeout);	}} void __attribute__((noinline))sys_setcurrpc(int dummy){	USED(dummy);	sys_setcallpc(dummy);} voidsys_seed(void *p, int sz){	u64int r = sec_realtime();	u8int *o = p; 	for(; sz > 0; r >>= 8, sz--)		*o++ = r & 0xff;} intsys_read(int fd, u8int *buf, usize n, usize *nread){	ssize r; 	while(1){		r = read(fd, buf, n);		if(r > -1){			*nread = (usize)r;			break;		}		return r;	}	return 0;} intsys_write(int fd, const void *buf, usize n, usize *nwritten){	ssize r = write(fd, buf, n);	if(r < 0)		return -1;	*nwritten = (usize)r;	return 0;} intsys_open(const char *fname, ios_flags flags, usize mode){	int of =		(flags & IOS_WRITE)		? ((flags & IOS_READ) ? O_RDWR : O_WRONLY)		: O_RDONLY;	if(flags & IOS_TRUNC)		of |= O_TRUNC;	if(flags & IOS_CREATE)		of |= O_CREAT;	if(flags & IOS_APPEND)		of |= O_APPEND;	if(flags & IOS_EXCL)		of |= O_EXCL;	return open(fname, of, mode);} voidsys_fd_setup(int fd){	USED(fd);} intsys_rename(const char *old, const char *new){	return rename(old, new);} soffsetsys_seek(int fd, soffset offset, int whence){	return lseek(fd, offset, whence);} intsys_close(int fd){	return close(fd);} char *sys_getcwd(void){	char tmp[4096];	if(getcwd(tmp, sizeof(tmp)) != nil)		return MEM_STRDUP(tmp);	return nil;} intsys_chdir(int fd, const char *dir){	USED(fd);	return chdir(dir);} intsys_mkdir(const char *path, usize mode){	if(mkdir(path, mode) == 0)		return 0;	char *p = MEM_STRDUP(path);	if(p == nil)		return -1;	int r = -1;	for(char *s = p+1; (s = strchr(s, '\\')) != nil;){		struct stat sb = {0};		*s = 0;		if(stat(p, &sb) == 0){			if((sb.st_mode & S_IFMT) != S_IFDIR)				goto done;		}else if(mkdir(p, mode) != 0 && errno != EEXIST)			goto done;		*s++ = '\\';		while(*s == '\\')			s++;	}	if((r = mkdir(path, mode)) != 0 && errno == EEXIST)		r = 0;done:	MEM_FREE(p);	return r;} voidsys_init(void){} static const u8int boot[] = {#include "sl.boot.h"}; intmain(int argc, char **argv){	setlocale(LC_NUMERIC, "C");	setlocale(LC_CTYPE, "");	slmain(boot, sizeof(boot), argc, argv);}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t