Ejemplo n.º 1
0
Archivo: 9write.c Proyecto: nuxlli/npfs
int
main(int argc, char **argv)
{
	int i, n, off;
	int c, port, dotu;
	char *addr, *s;
	char *path;
	Npuser *user;
	Npcfsys *fs;
	Npcfid *fid;
	char buf[512];

	port = 564;
//	npc_chatty = 1;

#ifdef _WIN32
	init();
	user = np_default_users->uname2user(np_default_users, "nobody");
#else
	user = np_default_users->uid2user(np_default_users, geteuid());
	if (!user) {
		fprintf(stderr, "cannot retrieve user %d\n", geteuid());
		exit(1);
	}
#endif

	dotu = 1;
	while ((c = getopt(argc, argv, "dp:u:U")) != -1) {
		switch (c) {
		case 'd':
			npc_chatty = 1;
			break;

		case 'p':
			port = strtol(optarg, &s, 10);
			if (*s != '\0')
				usage();
			break;

		case 'u':
			user = np_default_users->uname2user(np_default_users, optarg);
			break;

		case 'U':
			dotu = 0;
			break;

		default:
			usage();
		}
	}

	

	if (argc - optind < 2)
		usage();

	addr = argv[optind];
	path = argv[optind+1];

	fs = npc_netmount(npc_netaddr(addr, port), dotu, user, port, NULL, NULL);

	fid = npc_open(fs, path, Owrite);
	if (!fid) {
		fid = npc_create(fs, path, 0666, Owrite);
		if (!fid) {
			fprintf(stderr, "error creating\n");
			exit(1);
		}
	}

	off = 0;
	while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) {
		i = npc_write(fid, (u8*) buf, n, off);
		if (i != n) {
			fprintf(stderr, "error writing\n");
			exit(1);
		}

		off += n;
	}
			
	npc_close(fid);
	npc_umount(fs);

	exit(0);
}
Ejemplo n.º 2
0
int
main(int argc, char **argv)
{
    int sfd, i, n, off;
    int c, port;
    char *addr, *s;
    char *uname, *path;
    Npuser *user;
    Npcfsys *fs;
    Npcfid *fid;
    struct sockaddr_in saddr;
    struct hostent *hostinfo;
    char buf[512];

    port = 564;
//	npc_chatty = 1;

    user = np_uid2user(getuid());
    if (!user) {
        fprintf(stderr, "cannot retrieve user %d\n", getuid());
        exit(1);
    }

    uname = user->uname;
    while ((c = getopt(argc, argv, "dp:")) != -1) {
        switch (c) {
        case 'd':
            npc_chatty = 1;
            break;

        case 'p':
            port = strtol(optarg, &s, 10);
            if (*s != '\0')
                usage();
            break;

        case 'u':
            uname = optarg;
            break;

        default:
            usage();
        }
    }



    if (argc - optind < 2)
        usage();

    addr = argv[optind];
    path = argv[optind+1];

    sfd = socket(PF_INET, SOCK_STREAM, 0);
    if (sfd < 0) {
        perror("socket");
        exit(1);
    }

    hostinfo = gethostbyname (addr);
    if (!hostinfo) {
        perror("gethostbyname");
        exit(1);
    }

    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(port);
    saddr.sin_addr = *(struct in_addr *) hostinfo->h_addr;

    if (connect(sfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) {
        perror("connect");
        exit(1);
    }

    fs = npc_mount(sfd, NULL, "lucho");

    fid = npc_open(fs, path, Owrite);
    if (!fid) {
        fid = npc_create(fs, path, 0666, Owrite);
        if (!fid) {
            fprintf(stderr, "error creating\n");
            exit(1);
        }
    }

    off = 0;
    while ((n = read(0, buf, sizeof(buf))) > 0) {
        i = npc_write(fid, (u8*) buf, n, off);
        if (i != n) {
            fprintf(stderr, "error writing\n");
            exit(1);
        }

        off += n;
    }

    npc_close(fid);
    npc_umount(fs);

    exit(0);
}