Net-LibNFS

 view release on metacpan or  search on metacpan

libnfs/utils/nfs-stat.c  view on Meta::CPAN

		return NULL;
	}

	if (flags == O_RDONLY) {
		if (nfs_open(file_context->nfs, file_context->url->file, flags,
				&file_context->nfsfh) != 0) {
 			fprintf(stderr, "Failed to open file %s: %s\n",
				       file_context->url->file,
				       nfs_get_error(file_context->nfs));
			free_file_context(file_context);
			return NULL;
		}
	} else {
		if (nfs_creat(file_context->nfs, file_context->url->file, 0660,
				&file_context->nfsfh) != 0) {
 			fprintf(stderr, "Failed to creat file %s: %s\n",
				       file_context->url->file,
				       nfs_get_error(file_context->nfs));
			free_file_context(file_context);
			return NULL;
		}
	}
	return file_context;
}

char *get_file_type(int mode)
{
        switch (mode & S_IFMT) {
#ifndef WIN32
	case S_IFLNK: return "symbolic link";
#endif
	case S_IFDIR: return "directory";
	case S_IFCHR: return "character device";
	case S_IFBLK: return "block device";
	default: return "regular file";
	}
}

char uidbuf[16];
char gidbuf[16];

#ifdef WIN32
char *uid_to_name(int uid)
{
	sprintf(uidbuf, "%d", uid);
	return uidbuf;
}
char *gid_to_name(int gid)
{
	sprintf(gidbuf, "%d", gid);
	return gidbuf;
}
#else
#include <sys/types.h>
#include <grp.h>
#include <pwd.h>
char *uid_to_name(int uid)
{
	struct passwd *pw;

	pw = getpwuid(uid);
	if (pw) {
		return pw->pw_name;
	} else {
		sprintf(uidbuf, "%d", uid);
		return uidbuf;
	}
}	
char *gid_to_name(int gid)
{
	struct group *gr;

	gr = getgrgid(gid);
	if (gr) {
		return gr->gr_name;
	} else {
		sprintf(gidbuf, "%d", gid);
		return gidbuf;
	}
}	
#endif

char access_bits[11];
char *get_access_bits(int mode)
{
	switch (mode & S_IFMT) {
#ifndef WIN32
	case S_IFLNK: access_bits[0] = 'l'; break;
#endif
	case S_IFREG: access_bits[0] = '-'; break;
	case S_IFDIR: access_bits[0] = 'd'; break;
	case S_IFCHR: access_bits[0] = 'c'; break;
	case S_IFBLK: access_bits[0] = 'b'; break;
	default: access_bits[0] = '*';
	}
	access_bits[1] = "-r"[!!(mode & S_IRUSR)];
	access_bits[2] = "-w"[!!(mode & S_IWUSR)];
	access_bits[3] = "-xSs"[    !!(mode & S_IXUSR)
#ifndef WIN32
				+ 2*!!(mode & S_ISUID)
#endif
			       ];
	access_bits[4] = "-r"[!!(mode & S_IRGRP)];
	access_bits[5] = "-w"[!!(mode & S_IWGRP)];
	access_bits[6] = "-xSs"[    !!(mode & S_IXGRP)
#ifndef WIN32
				+ 2*!!(mode & S_ISGID)
#endif
			       ];
	access_bits[7] = "-r"[!!(mode & S_IROTH)];
	access_bits[8] = "-w"[!!(mode & S_IWOTH)];
	access_bits[9] = "-xTt"[    !!(mode & S_IXOTH)
#ifndef WIN32
				+ 2*!!(mode & S_ISVTX)
#endif
			       ];
	return access_bits;
}

#define BUFSIZE 1024*1024
static char buf[BUFSIZE];



( run in 1.299 second using v1.01-cache-2.11-cpan-39bf76dae61 )