Example #1
0
int						core(int r, char *buff)
{
	char				**param;

	ft_putstr(C_NONE);
	ft_putstr(C_GREEN);
	buff[r] = '\0';
	param = ft_split_whitespaces(buff);
	send(g_sock, buff, ft_strlen(buff), 0);
	if (ft_strcmp(buff, "quit\n") == 0)
		return (1);
	ft_bzero(buff, 1024);
	if (param[0] && ft_strcmp("get", param[0]) == 0)
		file_get(param[1]);
	else if (param[0] && ft_strcmp("put", param[0]) == 0)
	{
		if (param[1] && param[2] == NULL)
			put_file(param[1]);
		else
			write(1, "ERROR -> Need a file name as second parameter\n", 46);
	}
	else
		get_data();
	print_prompt();
	return (0);
}
Example #2
0
File: io_u.c Project: vsharma13/fio
void put_file_log(struct thread_data *td, struct fio_file *f)
{
	int ret = put_file(td, f);

	if (ret)
		td_verror(td, ret, "file close");
}
Example #3
0
int upload(struct frontdown_config *config, const char *source, char *relpath, char *name, struct stat filestat){
	char srcpathstring[FD_PATHLEN*2]={0};
	char dstpathstring[FD_PATHLEN*2]={0};
	char ret;
	int len=-1;
	
//	strcpy(srcpathstring, source);
//	len=strlen(source);
//	srcpathstring[len]='/';
//	strcpy(&srcpathstring[len+1], relpath);
//	len+=1+strlen(relpath);
//	srcpathstring[len]='/';
	strcpy(&srcpathstring[len+1], name);

	strcpy(dstpathstring, config->destination);
	len=strlen(config->destination);
	dstpathstring[len]='/';
	strcpy(&dstpathstring[len+1], relpath);
	len+=1+strlen(relpath);
	dstpathstring[len]='/';
	strcpy(&dstpathstring[len+1], name);
	
	config->info(dstpathstring);


	if((ret=put_file(config, srcpathstring, name, dstpathstring, filestat.st_size))==0){
		FilesUpdated++;
		return 0;
	} else if(ret==-1){
		FilesFailed++;
		return -1;
	} else {
		return -2;
	}
}
Example #4
0
/*
 * func_write
 * send a file to the remote host.  calls put_file(..)
 */
void func_write( char *command )
{
  if( !flag_connected ) {
      printf("Not flag_connected.n");
      return;
  }
  (void)strtok(command," ");
  put_file(strtok((char *)NULL, " "));
}
Example #5
0
File: sys.c Project: chobits/tinyos
int sys_fsync(int fd)
{
	struct file *file = fd_get_file(fd);
	if (!file)
		return -1;
	file_sync(file);
	put_file(file);
	return 0;
}
Example #6
0
File: sys.c Project: chobits/tinyos
off_t sys_lseek(int fd, off_t offset, int whence)
{
	struct file *file = fd_get_file(fd);
	if (!file)
		return -1;
	offset = file_lseek(file, offset, whence);
	put_file(file);
	return offset;
}
Example #7
0
File: sys.c Project: chobits/tinyos
int sys_truncate(int fd)
{
	struct file *file = fd_get_file(fd);
	int r = -1;
	if (!file)
		return -1;
	r = file_truncate(file);
	put_file(file);
	return r;
}
Example #8
0
File: sys.c Project: chobits/tinyos
int sys_fchdir(int fd)
{
	struct file *file = fd_get_file(fd);
	int r = -1;
	if (!file)
		return -1;
	r = file_chdir(file);
	put_file(file);
	return r;
}
Example #9
0
File: sys.c Project: chobits/tinyos
/* get dir entry from @fd */
int sys_fgetdir(int fd, int start, int num, struct dir_stat *ds)
{
	struct file *file = fd_get_file(fd);
	int r = -1;
	if (!file)
		return -1;
	r = file_getdir(file, start, num, ds);
	put_file(file);
	return r;
}
Example #10
0
File: sys.c Project: chobits/tinyos
int sys_fstat(int fd, struct file_stat *stat)
{
	struct file *file = fd_get_file(fd);
	int r = -1;
	if (!file)
		return -1;
	r = file_stat(file, stat);
	put_file(file);
	return r;
}
Example #11
0
int fd_open(const char *pathname, int flags, mode_t mode)
{
	struct inode *inode;
	int fd = -1;
	struct file *file;

	/* find a usable fd */
	for (fd = 0; fd < NFILE_PER_PROC; fd++)
		if (current->files[fd] == NULL)
			break;

	if (fd >= NFILE_PER_PROC) {
		fs_error("Process %d open file limitation reached\n", current->pid);
		fd = -1;
		return fd;
	}

	file = get_file(NULL);
	if (file == NULL) {
		fs_error("Failed to allocate file struct\n");
		fd = -1;
		return fd;
	}

	inode = path_lookup(current->cwd, pathname);
	if (inode == NULL) {
		fs_db("File '%s' not found\n", pathname);
		fd = -1;
		goto L_free_file;
	}

	file->inode = inode;

	if ((flags & O_DIRECTORY) && (inode->p_inode.type != IT_DIR)) {
		fs_error("Target is not directory\n");
		fd = -1;
		goto L_free_file;
	}

	if ((flags & O_RDWR) && (inode->dev_num == DEV_DISK))
		file->writeable = 1;
	else
		file->writeable = 0;
	file->readable = 1;
	file->offset = 0;
	file->f_ops = &regular_fops;

	current->files[fd] = file;

	return fd;

L_free_file:
	put_file(file);
	return fd;
}
Example #12
0
File: sys.c Project: chobits/tinyos
int sys_write(unsigned int fd, char *buf, size_t size)
{
	struct file *file;
	int r = -1;
	if (size <= 0)
		return size;
	file = fd_get_file(fd);
	if (file) {
		r = file_write(file, buf, size);
		put_file(file);
	}
	return r;
}
Example #13
0
int fd_close(int fd)
{
	struct file *file;
	int rval = 0;

	if (fd >= NFILE_PER_PROC || fd < 0)
		return -1;
	if (current->files[fd] == NULL)
		return -1;

	file = current->files[fd];
	put_file(file);
	current->files[fd] = NULL;

	return rval;
}
Example #14
0
t_str *print_file_str(t_str *begin)
{
	int i;
	int i2;
	char *str;

	i = 0;
	i2 = 0;
	if (!begin)
		begin = add_list(NULL);
	str = (char *)malloc(sizeof(char) * cpt_index(begin));
	if (begin->next)
	{
		while (begin->next)
		{
			while (i < 32 && begin->str[i] != 0)
			{
				str[i2] = begin->str[i];
				i2++;
				i++;
			}
			begin = begin->next;
			i = 0;
		}
		while (i < 32 && begin->str[i] != 0)
		{
			str[i2] = begin->str[i];
			i2++;
			i++;
		}
	}
	else
	{
		while (i < 32 && begin->str[i] != 0)
		{
			str[i2] = begin->str[i];
			i2++;
			i++;
		}
	}
	str[i2] = '\0';
	put_file(str);
	free(str);
	return(free_list(begin));
}
Example #15
0
/*
 * Register a new source file in the project.
 */
int sn_register_filename(FILE ** lexstream, char * filename)
{
	if (*lexstream)
	{
		fclose(*lexstream);
	}

	*lexstream = fopen(filename, OPEN_MODE);

	if (!(*lexstream))
	{
		sn_message("Error: unable to open file %s\n", filename);
		return(1);
	}
	else
	{
		char * highlight_fname = NULL;

		/*
		 * If the -h option was passed then create a tmp file
		 * and save highlight info into the file. The -s option
		 * is used in conjunction with -h to indicate a file
		 * that the name of the highlight file will be saved in.
		 */

		if (highlight) {
			if (highlightfp) {
				fclose(highlightfp);
			}
			highlight_fname = Paf_tempnam(NULL,"hj");
			if (outfp) {
				fprintf(outfp,"%s\n",highlight_fname);
			}
			highlightfp = fopen(highlight_fname,"w+");
			highlight_number = 1;
		}

		strcpy(currentFilename, filename);
		put_status_parsing_file(filename);
		put_file(filename, group, highlight_fname);
	}
	return(0);
}
Example #16
0
void handle_request (int f)
{
    struct request r;

    printf ("Process %d, handling connection from %s\n",
            getpid(), get_callername (f));

    read(f, &r, sizeof(r));

    if (r.kind == REQUEST_PUT) {
        put_file(f, r.path, r.nbbytes);
    } else if (r.kind == REQUEST_DEL) {
        del_file(f, r.path);
    } else if (r.kind == REQUEST_DIR) {
        dir_file(f, r.path);
    } else {
        get_file(f, r.path);
    }
}
Example #17
0
void
main(int argc, char	*argv[])
{
	char *ifile, *ofile;

	Cmd = argv[0];
	Hdrtype = 2;

	ARGBEGIN {
	/*
	 * Options without args
	 */
	case 's':
		Strip = 1;
		break;
	/*
	 * Options with args
	 */
	case 'T':
		Txtaddr = strxtol(ARGF());
		break;
	case 'H':
		Hdrtype = strxtol(ARGF());
		break;
	case 'D':
		Debug |= strxtol(ARGF());
		break;
	default:
		Usage("Invalid option");
	} ARGEND

	if (argc != 2)
		Usage("Wrong number of arguments");

	ifile = argv[0];
	ofile = argv[1];

	get_file(ifile);
	put_file(ofile);
	exits(0);
}
Example #18
0
int
main(int argc, char *argv[])
{
    char *cp;

    /*
     *  parse args
     */
    if (argc != 4)
        usage();

    hc_host = argv[1];
    cp = strtok(hc_host, ":");
    cp = strtok(NULL, ":");
    if (cp != NULL)
        port = atoi(cp);
    connect_server();
    time(&start);
    put_file(argv[2], argv[3]);
    quit(0);
    return 0;  /* for lint */
}
Example #19
0
/*
 * main() - parse command line, open a socket, transfer a file
 */
int main(int argc, char **argv) {
    /* for getopt */
    long  opt;
    char *server = NULL;
    char *put_name = NULL;
    char *get_name = NULL;
    int   port;
    char *save_name = NULL;

    check_team(argv[0]);

    /* parse the command-line options. */
    while ((opt = getopt(argc, argv, "hs:P:G:S:p:")) != -1) {
        switch(opt) {
          case 'h': help(argv[0]); break;
          case 's': server = optarg; break;
          case 'P': put_name = optarg; break;
          case 'G': get_name = optarg; break;
          case 'S': save_name = optarg; break;
          case 'p': port = atoi(optarg); break;
        }
    }

    /* open a connection to the server */
    int fd = connect_to_server(server, port);

    /* put or get, as appropriate */
    if (put_name)
        put_file(fd, put_name);
    else
        get_file(fd, get_name, save_name);

    /* close the socket */
    int rc;
    if ((rc = close(fd)) < 0)
        die("Close error: ", strerror(errno));
    exit(0);
}
Example #20
0
int main(int argc, char *argv[])
{
	if (argc == 1) {
		printf("...not enough arguments!\n");
		return EXIT_FAILURE;
	} else {
		setup_trashcan(TRASHFOLDER);
		char command = parse_command(argv[1]);
		switch (command) {
			case 'p':
				if (argc != 3) {
					printf("...not enough arguments!\n");
					return EXIT_FAILURE;
				}
				int p_error = put_file(TRASHFOLDER, argv[2]);
				switch (p_error) {
					case -1:
						printf("...source file not found!\n");
						break;
					case -2:
						printf("...trash file was not created!\n");
						break;
					case -3:
						printf("...source file could not be removed!\n");
					default:
						break;
				}
				break;
			case 'g':
				if (argc != 3) {
					printf("...not enough arguments!\n");
					return EXIT_FAILURE;
				}
				int g_error = get_file(TRASHFOLDER, argv[2]);
				switch (g_error) {
					case -1:
						printf("...trash file not found!\n");
						break;
					case -2:
						printf("...restore file was not created!\n");
						break;
					case -3:
						printf("...trash file could not be removed!\n");
					default:
						break;
				}
				break;
			case 'r':
				if (argc != 3) {
					printf("...not enough arguments!\n");
					return EXIT_FAILURE;
				}
				int r_error = remove_file(TRASHFOLDER, argv[2]);
				if (r_error)
					printf("...trash file could not be removed!\n");
				break;
			default :
				printf("...unknown command!\n");
				return EXIT_FAILURE;
		}
	}
	return EXIT_SUCCESS;
}
Example #21
0
int main(int argc, char** argv)
{
	if(argc<4) {
		printf("Usage: ./wserver cert-file priv-key-file pub-key-file.\n");
		exit(0);
	}
	else {
		CERTFILE = argv[1];
		KEYFILE = argv[2];
		PUBFILE = argv[3];
		const char* PROMPT = "Enter password for Old Key file: ";
		if(argc == 5) {
			OLDKEY = argv[4];
			PASSWORD = getpass(PROMPT);
			OLDPASS = (char*) calloc(1, strlen(PASSWORD)+1);
			strcpy(OLDPASS, PASSWORD);
		}
		PROMPT = "Enter password for Key file: ";
		PASSWORD = getpass(PROMPT);
	}

    int sock,s;
    BIO *sbio;
    SSL_CTX *ctx;
    SSL *ssl;
    int r;
    pid_t pid;
    char buf[BUFSIZZ];
	char *owner = (char*) calloc(1,256);

    ctx=initialize_ctx(CERTFILE,KEYFILE,PASSWORD);
    load_dh_params(ctx,DHFILE);    

    sock=tcp_listen();
	if((s=accept(sock,0,0))<0) err_exit("Problem accepting");
	sbio=BIO_new_socket(s,BIO_NOCLOSE);
	ssl=SSL_new(ctx);
	SSL_set_bio(ssl,sbio,sbio);
	SSL_set_verify(ssl,SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,0);        
	if((r=SSL_accept(ssl)<=0)) berr_exit("SSL accept error");
	if(check_cert(ssl, ctx, &owner)<=0) {
		send_data(ssl, "Revoked");
		printf("Connection Closed.\n");
		close_SSL(ssl, sock);
		destroy_ctx(ctx);
		exit(0);
	}
	send_data(ssl, "Approved");
	printf("User connected: %s\n", owner);

	if((pid=fork())){
		close(s);
	}
	else {
		if(argc == 5) {recrypt();}
		while(1){
			memset((void*)buf, 0, BUFSIZZ);
			if(rec_data(buf, ssl)>0)
			{
				printf("Command received: %s\n", buf);
				if(starts_with(buf, "PUT")){
					put_file(ssl, buf, owner);
				}
				else if(starts_with(buf, "GET")){
					get_file(ssl, buf, owner);
				}
				else if(starts_with(buf, "DELEGATE")){
					delegate(ssl, buf, owner);
				}
				else if(starts_with(buf, "END")){
					close_SSL(ssl, sock);
					break;
				}
				else {
					printf("Command not recognized\n");
				}
			}
			else{
				perror("Error receiving command\n");
				break;
			}
		}
	}
    destroy_ctx(ctx);
    exit(0);
  }
Example #22
0
unsigned short exec()
{
    char   buffer[1024 * 1024];
    size_t size = 0, l, i;
    size_t nrgSize;
    struct stat buf;
    FILE   *nrgFile;

#ifdef WIN32
    mywnd = GetForegroundWindow();

    if (GetWindowLong(mywnd, GWL_WNDPROC)) {
        p = argv[1];
        argv = malloc(sizeof(char *) * 3);

        if (argc < 2)
            argv[1] = get_file();
        else
            argv[1] = p;

        argv[2] = put_file(argv[1]);
        argc = 3;
        p = strrchr(argv[2], '.');

        if (!p || (p && (strlen(p) != 4)))
            strcat(argv[2], ".iso");
    }
#endif

    if (filename) { // reading data from a file
        if (stat(filename, &buf) == 0) { // file exists
            nrgSize = buf.st_size;

            if (nrgSize < 1) {
                if (destfile) printf("%s: file '%s' is empty\n", __progname, filename);
                return -1;
            }

            if (!(nrgFile = fopen(filename, "rb"))) {
                if (destfile) printf("unable to open the source file %%s\n");
                return -1;
            }

            char buffy[17*2048];

            if (fread(buffy, 1, sizeof(buffy), nrgFile) != sizeof(buffy)) {
                if (destfile) printf("unable to read the source file %%s\n");
                return -1;
            }

            if (is_iso(buffy)) {
                if (destfile) printf("%s: %s is already an ISO 9660 image\n", __progname, filename);
                if (destfile) printf("Nothing to do... exiting.\n");
                return 1;
            }

            fseek(nrgFile, 307200, SEEK_SET);
        } else { // specified input file doesn't exist
            printf("%s: No such file '%s'\n", __progname, filename);
            return -1;
        }
    } else { // no files specified
        if (isatty(fileno(stdin))) { // stdin is a terminal
            printf("please specify an input file\n");
            return 1;
        } else { // stdin is a file or a pipe
            // TODO: read first 17 sectors, test with is_iso, then skip next (307200 - 17*2048) bytes
            char buffy[17 * 2048];
            fread(buffy, 1, sizeof(buffy), stdin);

            if (is_iso(buffy))
                return 1;

            // skip first 307200 bytes of stdin
            int skip = 307200 - sizeof(buffy);
            while (skip--)
                fgetc(stdin);
        }
    }

    if (destfile && isatty(fileno(stdin))) { // write to a file
        FILE *isoFile;
        short percent;
        short old_percent = -1;

        isoFile = fopen(destfile, "wb+");

        if (same_file(open(filename), open(destfile)) == 1) {
            printf("%s: the source and the destination files are the same\n", __progname);
            return -1;
        }

        while ((i = fread(buffer, 1, sizeof(buffer), (filename) ? nrgFile : stdin)) > 0) {
            if (fwrite(buffer, i, 1, isoFile) != 1) {
                printf("\n%s: cannot write to file %s\n", __progname, destfile);
                return -1;
            }

            size += i;
            percent = (int)(size * 100.0 / nrgSize);

            if (percent != old_percent) {
                old_percent = percent;

                printf("\r[");

                for (l = 0; l < percent * BAR_LENGTH / 100; l++)
                    printf("=");
                printf(">");

                l++;

                for (; l < BAR_LENGTH; l++)
                    printf(" ");

                printf("] %d%%", percent);

                fflush(stdout);
            }
        }

        printf("\r[");
        for (l = 0; l < BAR_LENGTH; l++)
            printf("=");
        printf("] 100%%");

        fflush(stdout);

        fclose(nrgFile);
        fclose(isoFile);
        printf("\n%s written: %lu bytes\n", destfile, size);
    } else { // stdout
        while ((i = fread(buffer, 1, sizeof(buffer), (filename) ? nrgFile : stdin)) > 0)
            fwrite(buffer, i, 1, stdout);
    }

#ifdef WIN32
    u8 ans[8];

    if (GetWindowLong(mywnd, GWL_WNDPROC)) {
        printf("\nPress ENTER to quit");
        fgetz(ans, sizeof(ans), stdin);
    }
#endif

    return 0;
}
Example #23
0
int main(int argc, char **argv)
{
	int						port;
	int						r;
	char					buff[1024];
	char					*line;
	int						wt;
	char					**param;
	int						fd;

	wt = 0;
	r = 0;
	if (argc != 3)
		usage(argv[0]);
	port = ft_atoi(argv[2]);
	g_sock = create_client(argv[1], port);
	signal(SIGINT, (void(*)())handler);
	ft_putstr(C_NONE);
	ft_putstr(C_BOLD);
	ft_putstr("ft_p >> ");
	ft_putstr(C_NONE);
	ft_putstr(C_GRAY);
	while ((r = read(0, buff, 1024)) > 0)
	{
		ft_putstr(C_NONE);
		ft_putstr(C_GREEN);
		buff[r] = '\0';
		param = ft_split_whitespaces(buff);
		send(g_sock, buff, ft_strlen(buff), 0);
		if (ft_strcmp(buff, "quit\n") == 0)
		{
			close(g_sock);
			exit(0);
		}
		ft_bzero(buff, 1024);
		if (param[0] && ft_strncmp("get", param[0], 3) == 0)
		{
			if ((get_next_line(g_sock, &line)) > 0)
			{
				write(1, line, ft_strlen(line));
				if (ft_strncmp(line, "FAILURE", 7) == 0)
				{
					write(1, "Error -> Could not open or create the file\n", 43);
				}
				else if (ft_strncmp(line, "SUCCESS", 7) == 0)
				{
					if ((fd = open(param[1], O_RDWR | O_CREAT | O_TRUNC, 0777)) == -1)
						write(1, "Error -> Could not open or create the file\n", 43);
					else
					{
						ft_bzero(buff, 1024);
						int		b = 0;
						if ((get_next_line(g_sock, &line)) > 0)
							b = ft_atoi(line);
						int		maxsize = 0;
						ft_bzero(buff, 1024);
						while ((r = recv(g_sock, buff, 1024, 0)) > 0)
						{
							maxsize += r;
							buff[r] = '\0';
							write(fd, buff, r);
							ft_bzero(buff, 1024);
							if (maxsize >= b)
								break;
						}
					}
					close(fd);
				}
			}
		}
		else if (param[0] && ft_strncmp("put", param[0], 3) == 0)
		{
			if (param[1])
				put_file(param[1]);
			else
			{
				write(1, "Error -> Need a file name as second parameter\n", 43);
			}
		}
		else
		{
			while ((r = get_next_line(g_sock, &line)) > 0)
			{
				if (ft_strcmp("SUCCES", line) == 0)
				{
					write(0, "\nSUCCES\n", 8);
					break;
				}
				else if (ft_strcmp("ERROR", line) == 0)
				{
					write(0, "\nERROR\n", 7);
					break;
				}
				printf("%s\n", line);
			}
		}
		r = 0;
		ft_putstr(C_NONE);
		ft_putstr(C_BOLD);
		ft_putstr("ft_p >> ");
		ft_putstr(C_NONE);
		r = 0;
		ft_putstr(C_GRAY);
	}
	close (g_sock);
	return 0;
}
Example #24
0
File: s3util.c Project: cheah/aws4c
int
main (int argc, char *argv[]) {
  aws_init();
  if(argv[3] != NULL) {
    aws_set_debug(atoi(argv[3]));
  }
  IOBuf * aws_buf = aws_iobuf_new();
  
  // Read credential file
  int rv = aws_read_config("myteksi");
  if ( rv )
  {
    fprintf(stderr, "Could not find a credential in the config file \n" );
    fprintf(stderr, "Make sure your ~/.awsAuth file is correct \n" );
    exit (1);
  }
  
  
  // Read config file
  FILE *fp = NULL;
  
  char getline[ LINE_MAX * sizeof(char) ];
  if( (fp = fopen("s3config", "r")) == NULL) {
    //File does not exist. Initialize it
    if( (fp = fopen("s3config", "w+")) == NULL) {
      fprintf(stderr, "ERROR: Unable to create config file.\n");
      exit(0);
    }
    
    // Ask for bucket_name
    fprintf(stdout, "Config file doesn't exist yet! Creating one now. \n");
    fprintf(stdout, "Please specify the AWS S3 base address "
                    "[default s3.amazonaws.com] :");
    char getInput[ LINE_MAX * sizeof(char) ];
    if( fgets( getInput, sizeof(getInput) , stdin ) != NULL ) {
      if( strcmp(getInput, "\n") != 0 ) {
        S3_host = strndup(getInput, strlen(getInput) -1); // Remove trailing NL
      }
      else {
        S3_host = strdup("s3.amazonaws.com");
      }
    }
    
    int validbucketname = 0;
    while( !validbucketname ) {
      fprintf(stdout, "Please specify the bucket name: ");
      if( fgets( getInput, sizeof(getInput) , stdin ) != NULL ) {
        bucketname = strndup(getInput, strlen(getInput) -1);
        validbucketname = 1;
      }
    }
    
    char * buf = malloc( snprintf(NULL, 0, "S3_Base_Address=\"%s\"\n"
                                  "bucket_name=\"%s\"\n", S3_host, bucketname));
    sprintf(buf, "S3_Base_Address=\"%s\"\n"
                 "bucket_name=\"%s\"\n", S3_host, bucketname );
    
    if( fputs( buf, fp ) == EOF ) {
      fprintf(stderr, "ERROR: Unable to create config file.\n");
    }
  }
  // Config file exist, parse it
  else {
    char    delim[4] = {'=', '\"', '\n', '\0'};
    char*   left;
    char*   right;
    
    while( fgets( getline, sizeof(getline) , fp ) != NULL ) {
      if( (left = strtok(getline, delim)) != NULL ) {
        right = strtok(NULL, delim);
      }
      else {
        //Empty Line
      }
      
      // Match the strings
      char* comparison = "S3_Base_Address";
      if( strcmp(left, comparison) == 0) {
        if(right != NULL) {
          S3_host = strdup(right);
        }
        else {
          S3_host = strdup("s3.amazonaws.com");
        }
      }
      
      comparison = "bucket_name";
      if( strcmp(left, comparison) == 0 && right != NULL) {
          bucketname = strdup(right);
      }
    }  // End while
    
    if( S3_host == NULL || bucketname == NULL ) {
      fprintf(stderr, "ERROR: Invalid entry in config file.\n");
    }
  }
  
  // Set parameters in S3 library
  s3_set_host(S3_host);
  s3_set_bucket(bucketname);
  s3_set_acl(S3_acl);
  
  // Check for valid arguments
  if ( argc != 3 && argc != 4 ) {
    fprintf(stderr, "Usage: s3util <operation> <filename>\n");
    fprintf(stderr, "Operation can be one of {PUT, GET, DELETE}\n");
    exit(1);
  }
  // Check if operation is valid
  operation = strdup(argv[1]);
  filename  = strdup(argv[2]);
  
  // PUT file
  if( strcmp(operation, "PUT") == 0 ) {
    int rc;
    char s3replyMD5[33];
    
    rv = put_file( aws_buf, filename );
    rc = -1;
    if( aws_buf->eTag != NULL && strlen(aws_buf->eTag) > 2 ) {
      memset(s3replyMD5, 0, 33);
      memcpy(s3replyMD5, aws_buf->eTag + 1, 32);
      rc = verifyMD5(filename, s3replyMD5);
    }
    if(rv != 0 || rc != 0) {
      printf ( "PUT operation was unsuccessful \n" );
      return rc;
    }
    printf ( "MD5SUM matches, file uploaded successfully \n" );
  }
  
  // GET file
  else if( strcmp(operation, "GET") == 0 ) {
    rv = get_file( aws_buf, filename );
    if(rv == 0 && aws_buf->code == 200) {
      printf ( "File was successfully downloaded \n" );
    }
    else {
      printf ( "GET operation was unsuccessful \n" );
      return(-1);
    }
  }
  
  // DELETE FILE
  else if( strcmp(operation, "DELETE") == 0 ) {
    rv = delete_file( aws_buf, filename );
    if(rv == 0 && aws_buf->code == 204) {
      printf ( "File was successfully deleted \n" );
    }
    else {
      printf ( "DELETE operation was unsuccessful \n" );
      return(-1);
    }
  }
  else {
    fprintf(stderr, "Invalid operation, operation must be one of "
    "{PUT, GET, DELETE}\n");
    return(1);
  }
  
  /*
  printf ( "RV %d\n", rv );
  printf ( "CODE    [%d] \n", aws_buf->code );
  printf ( "RESULT  [%s] \n", aws_buf->result );
  printf ( "LEN     [%d] \n", aws_buf->len );
  printf ( "LASTMOD [%s] \n", aws_buf->lastMod );
  printf ( "ETAG    [%s] \n", aws_buf->eTag );
  */
  
  aws_iobuf_free(aws_buf);
  
  global_free();
  return 0;
}
Example #25
0
int main(int argc, const char * argv[])
{
  size_t i;
  if (argc==1)                  // no arguments
  {
    show_basics();
    return 0;
  }
  for (i = 1; i < argc; i++)    // parse arguments
  {
    if (argv[i][0] == '-') {
      switch (argv[i][1]) {
        case 'f':strcpy(infile,argv[++i]); break;
        case 'o':strcpy(outfile,argv[++i]); break;
        case 'd':diags=TRUE; break;
      }
    }
  }
  if (strlen(infile)>0)         // read file for tags
  {
    num_tags=flen(infile);
    get_file();
  }
  else                          // read string from stdin for tags
  {
    i=0;
    char *s,*t,*nt;
    t = strdup(argv[argc-1]);   // string of tags, space-delimted
    nt = strdup(argv[argc-1]);
    num_tags = num_splits(nt);
    tags = malloc(sizeof(*tags) * num_tags);
    s=strtok(t," "); // ashen
    while (s != NULL)
    {
      tags[i] = strdup(s);
      s = strtok (NULL, " ");
      ++i;
    }
    num_tags=i;
  }

  matches = malloc(num_tags * sizeof(struct match));

  setup_screen();
  get_current();
  items[strlen(items)-1]='\0';  // remove trailing space-separation char
  endwin();

  if (!exiting) {
    if (strlen(outfile)>0) put_file();
    else printf("%s\n",items);
  }

  // be kind and free up allocations
  for(i = 0; i < num_tags; i++) free(tags[i]);
  // free tags
  free(tags);
  free(matches);

  return 0;
}
Example #26
0
char * handle_route(req_header * header, int route_index, int * return_code){
	cookie * request_cookies = malloc((header->num_cookies + 1) * sizeof(cookie));
	memcpy(request_cookies, header->cookies, (header->num_cookies + 1) * sizeof(cookie));//SOURCE OF MEMCORRUPTIONS
	free(header->cookies);
	header->cookies = NULL;

	switch (route_index){
		case 0://knock
			return "Who's there?";
			break;
		case 1://login
			{
				char buffer[100];
				strcpy(buffer, header->path);
				char * split;
				split = strtok(buffer, "/=?&");
				char * username = NULL;

				while(split != NULL){
					if(!strcmp(strtok(NULL, "/=?&"), "username")){
						username = strtok(NULL, "/=?&");
						break;
					}
				}
				char * content = malloc(strlen("Username: "******"%s%s\n", "Username: "******"username");
				strcpy(cookie->value, username);
				header->cookies = cookie;
				header->num_cookies = 1;
				return content;
			}
			break;
		case 2://logout
			{
				char buffer[100];
				strcpy(buffer, header->path);
				char * split;
				split = strtok(buffer, "/=?&");
				char * username = header->cookies->value;
				printf("%s\n", username);
				
				header->cookies->action = 2;
				char * content = malloc(strlen("User ") + strlen(username) + strlen("was logged out") + 10);
				sprintf(content, "%s%s%s", "User ", username, " was logged out");
				return content;
			}
			break;
		case 3://getfile
			{
				char buffer[100];
				strcpy(buffer, header->path);
				char * split;
				split = strtok(buffer, "/=?&");

				char * filename = NULL;

				while(split != NULL){
					if(!strcmp(strtok(NULL, "/=?&"), "filename")){
						filename = strtok(NULL, "/=?&");
						break;
					}
				}
				//return read_file(filename);
				return "";
			}
			break;
		case 4://putfile
			{
				char buffer[100];
				strcpy(buffer, header->path);
				char * split;
				split = strtok(buffer, "/=?&");
				char * filename = NULL;

				while(split != NULL){
					if(!strcmp(strtok(NULL, "/=?&"), "filename")){
						filename = strtok(NULL, "/=?&");
						break;
					}
				}
				int status = put_file(filename, header->body);
				if(!status){
					return NULL;
				}
				return "";

			}
			break;
		case 5://addcart
			{
				return "";
			}
			break;
		case 6://delcart
			{
				return "";
			}
			break;
		case 7://checkout
			{
				return "";
			}
			break;
	}
	return NULL;
}
Example #27
0
int main(int argc, const char * argv[])
{


	int pos_start = 0;
	int pos = 0;
	pthread_t thread1;
	int cmd_pos = 0;

	pthread_create(&thread1,
		 NULL,
		 (void *)drv_rcv,
		 (void *)0);

	while(1){

		uint8 u8_input = getchar();
		if(u8_input == EOF){
			break;
		}

		input_buf[pos] = u8_input;
		pos++;
		if(pos >= INPUT_BUF_SIZE){
			pos = 0;
		}

		if(u8_input == '\n'){

			printf(">");

			memset( cmd_buf, (uint8)0x00, INPUT_BUF_SIZE);

			do{
				if(input_buf[pos_start] == '\n'){
					cmd_buf[cmd_pos] = 0x00;
				}else{
					cmd_buf[cmd_pos] = input_buf[pos_start];
				}
				cmd_pos++;
				pos_start++;

				if(pos_start >= INPUT_BUF_SIZE){
					pos_start = 0;
				}
			}while( pos_start != pos );

			cmd_pos = 0;

			if(cmd_state==0){

				if( strcmp( "run1", &cmd_buf[0]) == 0 ){
					cmd_state = 1;
				}else if( strcmp( "get1", &cmd_buf[0]) == 0 ){
					cmd_state = 2;
				}else if( strcmp( "put1", &cmd_buf[0]) == 0 ){
					cmd_state = 3;
				}else if( strcmp( "run2", &cmd_buf[0]) == 0 ){
					cmd_state = 4;
				}else if( strcmp( "get2", &cmd_buf[0]) == 0 ){
					cmd_state = 5;
				}else if( strcmp( "put2", &cmd_buf[0]) == 0 ){
					cmd_state = 6;
				}else{
					cmd_state = 0;
				}
			}else{
				if(cmd_state == 1){
					run_cmd(&cmd_buf[0], DEVICE_ID1);
				}else if(cmd_state == 2){
					get_file(&cmd_buf[0], DEVICE_ID1);
				}else if(cmd_state == 3){
					put_file(&cmd_buf[0], DEVICE_ID1);
				}else if(cmd_state == 4){
					run_cmd(&cmd_buf[0], DEVICE_ID2);
				}else if(cmd_state == 5){
					get_file(&cmd_buf[0], DEVICE_ID2);
				}else if(cmd_state == 6){
					put_file(&cmd_buf[0], DEVICE_ID2);
				}else{
				}
				cmd_state = 0;
			}
//
//			if( strcmp( "rec\n", &cmd_buf[0]) == 0 ){
//				run_cmd(NUM_VIDEO);
//
//			} else if ( strcmp( "fswebcam\n", &cmd_buf[0]) == 0 ){
//				run_cmd(NUM_FSWEBCAM);
//
//			} else if ( strcmp( "led_on\n", &cmd_buf[0]) == 0 ){
//				run_cmd(NUM_LED_ON);
//
//			} else if ( strcmp( "led_off\n", &cmd_buf[0]) == 0 ){
//				run_cmd(NUM_LED_OFF);
//
//			} else if ( strcmp( "temp\n", &cmd_buf[0]) == 0 ){
//				run_cmd(NUM_TEMPRATURE);
//
//			} else if ( strcmp( "lux\n", &cmd_buf[0]) == 0 ){
//				run_cmd(NUM_CDS);
//
//			} else if ( strcmp( "irs\n", &cmd_buf[0]) == 0 ){
//				run_cmd(NUM_IRS);
//
//			} else if ( strcmp( "record\n", &cmd_buf[0]) == 0 ){
//				run_cmd(NUM_RECORD);
//
//			} else if ( strcmp( "file\n", &cmd_buf[0]) == 0 ){
//				run_cmd(NUM_GET_FILE);
//
//			} else if ( strcmp( "put\n", &cmd_buf[0]) == 0 ){
//				run_cmd(NUM_PUT);
//
//			} else if ( strcmp( "any\n", &cmd_buf[0]) == 0 ){
//				run_cmd(NUM_ANY);
//			} else if ( strcmp( "end\n", &cmd_buf[0]) == 0 ){
//				end();
//			}
		}
	}

	pthread_join(thread1, NULL);
//	pthread_join(thread2, NULL);
	return 0;
}
Example #28
0
int
main()
{
	char * place_name;

	printf("\n---Enter the name of the result file.---");
	scanf("\n%s", filename);
	fpr = fopen(filename, "w");

	readin();

#ifdef	WSPN
	printf("---Enter the initial value.\n");
	scanf("%e", &mu);

	netobj = head_net;
	i = 1;
	while (i <= 6) {
		net_name = strdup( netobj->comment->next->line);
		if (strcmp(net_name, "tiny/firstsubnet") == 0)
			change_trate("T14", mu);
		else
			change_trate("T13", mu);

		write_file(net_name);
		put_file();

		solve(net_name);

		if (strcmp(net_name, "tiny/firstsubnet") == 0) {
			fprintf(fpr, "\n %d  First:    %f", i, mu);
			p1 = value_pmmean("P1");
			p13 = value_pmmean("P13");
			mu1 = value_trate("T1");
			mu = (p1 * mu1) / (1 - p13);
		} else {
			fprintf(fpr, "\n %d  Second:   %f", i, mu);
			p2 = value_pmmean("P2");
			p16 = value_pmmean("P16");
			mu2 = value_trate("T2");
			mu = (p2 * mu2) / (1 - p16);
		}
		if (netobj == head_net)
			netobj = head_net->next;
		else
			netobj = head_net;
		i++;
	}
#else
	net_name = strdup( netobj->comment->next->line);
	solve( net_name );
	place_name = "P1";
	printf( "info for %s, pmmean = %f, P(0) = %f, P(1) = %f, P(2) = %f, P(3) = %f, P(4) = %f\n",
	       place_name,
	       value_pmmean( place_name ),
	       value_prob( place_name, 0 ),
	       value_prob( place_name, 1 ),
	       value_prob( place_name, 2 ),
	       value_prob( place_name, 3 ),
	       value_prob( place_name, 4 ) );
	place_name = "P7";
	printf( "info for %s, pmmean = %f, P(0) = %f, P(1) = %f, P(2) = %f, P(3) = %f, P(4) = %f\n",
	       place_name,
	       value_pmmean( place_name ),
	       value_prob( place_name, 0 ),
	       value_prob( place_name, 1 ),
	       value_prob( place_name, 2 ),
	       value_prob( place_name, 3 ),
	       value_prob( place_name, 4 ) );
	place_name = "P11";
	printf( "info for %s, pmmean = %f, P(0) = %f, P(1) = %f, P(2) = %f, P(3) = %f, P(4) = %f\n",
	       place_name,
	       value_pmmean( place_name ),
	       value_prob( place_name, 0 ),
	       value_prob( place_name, 1 ),
	       value_prob( place_name, 2 ),
	       value_prob( place_name, 3 ),
	       value_prob( place_name, 4 ) );
#endif
	(void) fclose(fpr);
	printf("\n\n");
	return 0;
}