Esempio n. 1
0
int main()
{
        ftp_init();

        struct ftp_connect client;
        memcpy(client.hostname, "192.168.0.107", sizeof(client.hostname));
        client.control_port = 21;
        memcpy(client.username, "odin", sizeof(client.username));
        memcpy(client.password, "odin", sizeof(client.password));
        client.mode = 0;

        struct file_description file;
        memcpy(file.remote_dir, "/test/X00/192.168.0.188/20121128/09", sizeof(file.remote_dir));
        memcpy(file.file_name, "a.txt", sizeof(file.file_name));
        int size = 1024 * 1024;
        char *ptr = (char *)malloc(size * sizeof(char));
        file.file_buffer = ptr;
        file.file_size = size;

        if (ftp_open(&client) == FTP_OK) {
                ftp_upload(&client, &file);
        }

        ftp_close(&client);
        free(ptr);
        ptr = NULL;

        ftp_release();

        return 0;
}
Esempio n. 2
0
void libmftp_tls_test(char *host, unsigned int port, char *user, char *pw, char *workingdirectory)
{
	ftp_connection *c = ftp_open(host, port, ftp_security_always);
	if (!c) {
		printf("No TLS connection!\n");
		return;
	}

	if (ftp_auth(c, user, pw, ftp_btrue) != FTP_OK) {
		printf("Could not authenticate. Error: %i\n", c->error);
	}

	if (ftp_item_exists(c, "httpdocs", NULL)) {
		printf("exists!°!11e3degvrbnm\n");
	}

	ftp_close(c);
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
    int fd = 0;
    char *currentfile, old_dir[PATH_MAX];
    playlist *pl = NULL;
    struct id3_file *id3struct = NULL;
    struct id3_tag *id3tag = NULL;
    int retval;

    buffer playbuf;
    
    struct mad_decoder decoder;
    pthread_t keyb_thread;

    key_t sem_key;
    key_t mem_key;
    key_t frames_key;

    union semun sem_ops;
    int shm_id;
    int frames_id;
    mad_decoder_position = 0;
    output_buffer_position = 0;
    
    old_dir[0] = '\0';

    playbuf.pl = pl = new_playlist();

    if (!pl)
    {
        fprintf(stderr, "malloc failed at startup!\n");
        exit(1);
    }

    loop_remaining = 1;

    options.volume = MAD_F_ONE;

    status = MPG321_PLAYING;
    
    /* Get the command line options */
    parse_options(argc, argv, pl);

    if(options.opt & MPG321_PRINT_FFT)
	    if(!(options.opt & MPG321_REMOTE_PLAY))
	    {
		    /* printf("FFT analysis can only be used in Remote mode play.\n\n"); */
		    usage(argv[0]);			
		    exit(0);
	    }


    /* If there were no files and no playlist specified just print the usage */
    if (!playlist_file && optind == argc)
    {
        usage(argv[0]);
        exit(0);
    }

    if (playlist_file)
        load_playlist(pl, playlist_file);

    if(options.opt & MPG321_RECURSIVE_DIR)
	    add_cmdline_files_recursive_dir(pl, argv);
    else
	    add_cmdline_files(pl, argv);

    if (shuffle_play)
        shuffle_files(pl);

    if(options.opt & MPG321_ENABLE_BUFFER)
    {
	    /* Initialize semaphore and shared memeory */
	    if(access(argv[0],X_OK) == 0)
		    sem_key = ftok(argv[0],0);
	    else
		    sem_key = ftok(MPG321_PATH,0);
	    if(sem_key == -1)
	    {
		    perror("Cannot obtain resources for semaphores");
		    exit(EXIT_FAILURE);
	    }
	    semarray = semget(sem_key,3,IPC_CREAT | IPC_EXCL | S_IRWXU);
	    if(semarray == -1)
	    {
		    perror("Cannot initialize semaphores");
		    exit(EXIT_FAILURE);
	    }
	    sem_ops.val = buffer_size-1;
	    if(semctl(semarray,0,SETVAL,sem_ops) == -1)
	    {
		    perror("Error while initializing mad_decoder semaphore");
		    if(semctl(semarray,0,IPC_RMID) == -1)
			    perror("Error while destroying semaphores");
		    goto out;
		    //exit(EXIT_FAILURE);
	    }
	    sem_ops.val = 0;
	    if(semctl(semarray,1,SETVAL,sem_ops) == -1)
	    {
		    perror("Error while initializing mad_decoder semaphore");
		    if(semctl(semarray,0,IPC_RMID) == -1)
			    perror("Error while destroying semaphores");
		    goto out;
		    //exit(EXIT_FAILURE);
	    }
	    sem_ops.val = 0;
	    if(semctl(semarray,2,SETVAL,sem_ops) == -1)
	    {
		    perror("Error while initializing mad_decoder semaphore");
		    if(semctl(semarray,0,IPC_RMID) == -1)
			    perror("Error while destroying semaphores");
		    goto out;
		    //exit(EXIT_FAILURE);
	    }

	    /* Shared Memory */
	    mem_key = ftok(argv[0],1);
	    shm_id = shmget(mem_key,buffer_size * sizeof(output_frame), IPC_CREAT | S_IREAD | S_IWRITE);
	    if(shm_id == -1)
	    {
		    perror("Cannot initialize shared buffer");
		    goto out;
		    //exit(EXIT_FAILURE);
	    }
	    Output_Queue = shmat(shm_id,NULL,0);
	    if(*(int *)Output_Queue == -1)
	    {
		    perror("Error while attaching shared buffer to mad_decoder");
		    if(shmctl(shm_id,IPC_RMID,NULL))
			    perror("Cannot destroy shared buffer");
		    goto out;
		    //exit(EXIT_FAILURE);
	    }
	    static int n;
	    for(n=0;n<buffer_size;n++)
	    {
		    memset((Output_Queue+n)->data,'\0',4608);
		    memset((Output_Queue+n)->time,'\0',80);
		    (Output_Queue+n)->length = 0;
		    (Output_Queue+n)->seconds = 0;
		    (Output_Queue+n)->num_frames = 0;
	    }
	    
	    frames_key = ftok(argv[0],2);
	    frames_id = shmget(frames_key,buffer_size * sizeof(decoded_frames), IPC_CREAT | S_IREAD | S_IWRITE);
	    if(frames_id == -1)
	    {
		    perror("Cannot initialize shared frames counter");
		    goto out;
		    //exit(EXIT_FAILURE);
	    }
	    Decoded_Frames = shmat(frames_id,NULL,0);
	    if(*(int *)Decoded_Frames == -1)
	    {
		    perror("Error while attaching shared frames counter to mad_decoder");
		    if(shmctl(frames_id,IPC_RMID,NULL))
			    perror("Cannot destroy shared frames counter");
		    goto out;
		    //exit(EXIT_FAILURE);
	    }
	    	
	    Decoded_Frames->is_http = 0;
	    Decoded_Frames->is_file = 0;

    }
    else {
	    ao_initialize();
	    check_default_play_device();
    }
    
    if (!(options.opt & MPG321_REMOTE_PLAY))
    {
        handle_signals(-1); /* initialize signal handler */
        remote_input_buf[0] = '\0';
    }
    
    if (!(options.opt & MPG321_QUIET_PLAY)) 
        mpg123_boilerplate();
    
    if (options.opt & MPG321_REMOTE_PLAY)
    {
        printf ("@R MPG123\n");
	if(options.opt & MPG321_ENABLE_BUFFER)
	{
#ifdef HAVE_ALSA
		init_alsa_volume_control("default"); /* For the moment use "default", it works on most of the systems. Tested in Debian,Fedora,Ubuntu,RedHat,CentOS,Gentoo */
		if(options.volume != MAD_F_ONE)
			mpg321_alsa_set_volume((long)options.volume*volume_max/100);
#endif
	}
    }

    /* Fork here. */
    if(options.opt & MPG321_ENABLE_BUFFER)
    {
	    output_pid = fork();
	    if(output_pid == -1)
	    {
		    perror("Error while forking output process");
		    goto out; /* Release shared memeory and semaphores */
		//    exit(EXIT_FAILURE);
	    }

	    if(output_pid == 0)
	    {
		    frame_buffer_p();
		    exit(EXIT_SUCCESS);
	    }
	    signal(SIGUSR1,handle_signals);
	    if(!(options.opt & MPG321_REMOTE_PLAY))
	    {
#ifdef HAVE_ALSA
		    init_alsa_volume_control("default");
		    if(options.volume != MAD_F_ONE)
			    mpg321_alsa_set_volume((long)options.volume*volume_max/100);
#endif
	    }
    }

    if( (options.volume != MAD_F_ONE) && !(options.opt & MPG321_ENABLE_BUFFER))
    {
	    options.volume = mad_f_tofixed((long)options.volume/100.0);
    }
    else{
	    options.volume = MAD_F_ONE; /* When using the buffer options.volume when decoding each frame should be equal to MAD_F_ONE */
//	    options.volume = mad_f_tofixed((long)100.0/100.0);
    }

    if (!(options.opt & MPG321_REMOTE_PLAY))
    {
	     if(options.opt & MPG321_ENABLE_BASIC)
	     {
	 	     /* Now create and detach the basic controls thread */
		     sem_init(&main_lock,0,0);
	 	     pthread_create(&keyb_thread,NULL,read_keyb,NULL);
		     pthread_detach(keyb_thread);
	     }
     }
    if(set_xterm)
    {
	    tty_control();
	    get_term_title(title);
    }else
    {
     	
	    if (!(options.opt & MPG321_REMOTE_PLAY))
	    {
	    	    if (tcgetattr(0, &terminal_settings) < 0)
	    		    perror("tcgetattr()");
	    	    memcpy(&old_terminal_settings, &terminal_settings, sizeof(struct termios));
		    /* Early thread start */
		    sem_post(&main_lock);
	    }
    }
    /* Play the mpeg files or zip it! */
    while((currentfile = get_next_file(pl, &playbuf)))
    {
        //printf("Current File: %s\n",currentfile);
	   if (quit_now) 
            break;
        
        signal(SIGINT, SIG_DFL);
        
        playbuf.buf = NULL;
        playbuf.fd = -1;
        playbuf.length = 0;
        playbuf.done = 0;
        playbuf.num_frames = 0;
        current_frame = 0;
        playbuf.max_frames = -1;
        strncpy(playbuf.filename,currentfile, PATH_MAX);
        playbuf.filename[PATH_MAX-1] = '\0';
        
        if (status == MPG321_PLAYING || status == MPG321_STOPPED) 
            file_change = 1;

        mad_timer_reset(&playbuf.duration);
        
        mad_timer_reset(&current_time);

	id3struct = NULL;

        if (!(options.opt & MPG321_QUIET_PLAY) && file_change)
        {
           /* id3struct = id3_file_open (currentfile, ID3_FILE_MODE_READONLY);*/

            if (id3struct == NULL)
		    get_id3_info(currentfile, &id3struct, &id3tag);
	    if(id3tag)
		    show_id3(id3tag);
	}

	scrobbler_time = -1;
	if(options.opt & MPG321_USE_SCROBBLER)
	{
		if(id3struct == NULL)
			get_id3_info(currentfile,&id3struct,&id3tag);
                
	    if (id3tag)
	    {
		    char emptystring[31], emptyyear[5] = "    ";
		    int i;

		    if(parse_id3(scrobbler_args, id3tag))
		    {
			    memset(emptystring, ' ', 30);
			    emptystring[30] = '\0';
	    		    if((options.opt & MPG321_VERBOSE_PLAY) && (options.opt & MPG321_USE_SCROBBLER))
			    {
				    fprintf(stderr, "\nPreparing for the AudioScrobbler:\n");
				    for(i = 0; i < 6; i++)
				    {
					    if(scrobbler_args[i] == NULL)
						    scrobbler_args[i] =
							    ( i == 3 ? emptyyear: emptystring);
					    fprintf(stderr, "- %s\n", scrobbler_args[i]);
				    }
			    }
		    }
	    }
	}
      

        if (options.opt & MPG321_REMOTE_PLAY && file_change)
        {
		if(id3struct == NULL)
			get_id3_info(currentfile, &id3struct, &id3tag);
		if(id3tag)
                {
                    if (!show_id3(id3tag))
                    {
                        /* This shouldn't be necessary, but it appears that
                           libid3tag doesn't necessarily know if there are no
                           id3 tags on a given mp3 */
                        char * basec = strdup(currentfile);
                        char * basen = basename(basec);
                        
                        char * dot = strrchr(basen, '.');
                        
                        if (dot)
                            *dot = '\0';
                        
                        printf("@I %s\n", basen);

                        free(basec);
                    }
                }
            else
            {
                char * basec = strdup(currentfile);
                char * basen = basename(basec);
                
                char * dot = strrchr(basen, '.');
                
                if (dot)
                    *dot = '\0';
                
                printf("@I %s\n", basen);

                free(basec);
            }
        }

	if(id3struct != NULL)
		id3_file_close(id3struct);

        /* Create the MPEG stream */
        /* Check if source is on the network */
        if((fd = raw_open(currentfile)) != 0 || (fd = http_open(currentfile)) != 0
            || (fd = ftp_open(currentfile)) != 0)
        {
            playbuf.fd = fd;
            playbuf.buf = malloc(BUF_SIZE);
            playbuf.length = BUF_SIZE;
	    if(options.opt & MPG321_ENABLE_BUFFER)
	    {
		    Decoded_Frames->is_http = 1;
		    Decoded_Frames->is_file = 0;
	    }
	    calc_http_length(&playbuf);

            mad_decoder_init(&decoder, &playbuf, read_from_fd, read_header, /*filter*/0,
                            output, handle_error, /* message */ 0);
        }

        /* Check if we are to use stdin for input */
        else if(strcmp(currentfile, "-") == 0)
        {
            playbuf.fd = fileno(stdin);
            playbuf.buf = malloc(BUF_SIZE);
            playbuf.length = BUF_SIZE;

            mad_decoder_init(&decoder, &playbuf, read_from_fd, read_header, /*filter*/0,
                            output, handle_error, /* message */ 0);
        }
            
        /* currentfile is a local file (presumably.) mmap() it */
        else
        {
            struct stat stat;
            
            if((fd = open(currentfile, O_RDONLY)) == -1)
            {
	    
                mpg321_error(currentfile);
		/* Restore TTY from keyboard reader thread */
	        if(options.opt & MPG321_ENABLE_BASIC)
			if (tcsetattr(0, TCSANOW, &old_terminal_settings) < 0)
				perror("tcsetattr ICANON");
		if(set_xterm)
	    	{
			set_tty_restore();
	    		osc_print(0,0,title);
	    		if (ctty)
	    			fclose(ctty);
	    	}
		if( options.opt & MPG321_REMOTE_PLAY)
			if(remote_restart)
			{	
				clear_remote_file(pl); /* If restart is enabled, restart remote shell when file doesn't exist*/
				continue;
			}
		if(options.opt & MPG321_ENABLE_BUFFER)
			goto out;
		else
			exit(1);
                /* mpg123 stops immediately if it can't open a file */
		/* If sth goes wrong break!!!*/
                break;
            }

	    if(options.opt & MPG321_ENABLE_BUFFER)
	    {
		    Decoded_Frames->is_http = 0;
		    Decoded_Frames->is_file = 1;
	    }
            
            if(fstat(fd, &stat) == -1)
            {
                mpg321_error(currentfile);
		close(fd);
                continue;
            }
            
            if (!S_ISREG(stat.st_mode))
            {
		    if(S_ISFIFO(stat.st_mode))
		    {
			    fallback = 1;
			    goto fall_back_to_read_from_fd;
		    }

		close(fd);    
                continue;
            }
            
            retval = calc_length(currentfile, &playbuf); //FIXME Check also if it is an mp3 file. If not break and go to the next file possible
	    if(retval < 0)
	    {
		    if(options.opt & MPG321_REMOTE_PLAY)
		    {
			    fprintf(stderr,"@E Corrupted file: %s\n",currentfile);
			    close(fd);
			    if(remote_restart)
			    {
				    clear_remote_file(pl); /* If restart is enabled, restart remote shell when file is corrupted */
				    continue;
			    }
			    break;
		    }
		    mpg321_error(currentfile);
		    close(fd);
//		    break; //FIXME Break and stop OR continue the playlist ????
		    continue;
	    }

	    if((options.opt & MPG321_VERBOSE_PLAY) && (options.opt & MPG321_USE_SCROBBLER))
		    fprintf(stderr, "Track duration: %ld seconds\n",playbuf.duration.seconds);

	    if(options.opt & MPG321_USE_SCROBBLER)
		    scrobbler_set_time(playbuf.duration.seconds);

            if ((options.maxframes != -1) && (options.maxframes <= playbuf.num_frames))
            { 
                playbuf.max_frames = options.maxframes;
            }
            
            playbuf.frames = malloc((playbuf.num_frames + 1) * sizeof(void*));
            playbuf.times = malloc((playbuf.num_frames + 1) * sizeof(mad_timer_t));
#ifdef __uClinux__
	    if((playbuf.buf = mmap(0, playbuf.length, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
#else       
 	    if((playbuf.buf = mmap(0, playbuf.length, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
#endif		    
            {
                mpg321_error(currentfile);
                continue;
            }
            
            playbuf.frames[0] = playbuf.buf;
		    
	    mad_decoder_init(&decoder, &playbuf, read_from_mmap, read_header, /*filter*/0,
            
	    		    output, handle_error, /* message */ 0);
fall_back_to_read_from_fd:	//FIXME. Reported that on some embedded systems with low memory, less than 16MB doesn't work properly.
	    if(fallback)
	    {
	    
		    playbuf.fd = fd;
		    playbuf.buf = malloc(BUF_SIZE);
		    playbuf.length = BUF_SIZE;
		    mad_decoder_init(&decoder, &playbuf, read_from_fd, read_header, /*filter*/0,
				    output, handle_error, /* message */ 0);
		    fallback = 1;
	    }
        }

        if(!(options.opt & MPG321_QUIET_PLAY))/*zip it!!!*/
        {
            /* Because dirname might modify the argument */
            char * dirc = strdup(currentfile);
            char * basec = strdup(currentfile);

            char * basen = basename(basec);
            char * dirn = dirname(dirc);
            
            /* make sure that the file has a pathname; otherwise don't print out
               a Directory: listing */
            if(strchr(currentfile, '/') && strncmp(old_dir, dirn, PATH_MAX) != 0)
            {
                /* Print information about the file */
                fprintf(stderr, "\n");
                fprintf(stderr,"Directory: %s\n", dirn);
                
                strncpy(old_dir, dirn, PATH_MAX);
                old_dir[PATH_MAX-1] = '\0';
            }
            
            /* print a newline between different songs only, not after
               Directory: listing */
            else
            {
                fprintf(stderr, "\n");
            }

            fprintf(stderr,"Playing MPEG stream from %s ...\n", basen);
            
	    /* Printing xterm title */
	    if(set_xterm)
	    {
		    osc_print(0,0,basen);
	    }
	    
            free(dirc);
            free(basec);
        }    

        signal(SIGINT, handle_signals);
	signal(SIGCHLD, handle_sigchld);
        /*Give control back so that we can implement SIG's*/
	if(set_xterm)
	{
		set_tty_restore();
		if (tcgetattr(0, &terminal_settings) < 0)
			perror("tcgetattr()");
		memcpy(&old_terminal_settings, &terminal_settings, sizeof(struct termios));
		/* disable canonical mode processing in the line discipline driver */
		terminal_settings.c_lflag &= ~(ICANON | ECHO);
		/* apply our new settings */
		if (tcsetattr(0, TCSANOW, &terminal_settings) < 0)
			perror("tcsetattr ICANON");
		if(options.opt & MPG321_ENABLE_BASIC)
		{
			/* Late thread start */
			sem_post(&main_lock);
		}
	}
        /* Every time the user gets us to rewind, we exit decoding,
           reinitialize it, and re-start it */
	    
	if(options.opt & MPG321_ENABLE_BUFFER)
    	{
		Decoded_Frames->total_decoded_frames = 0;
		Decoded_Frames->done = 0;
	}
      
        while (1)
        {
            decoder.options |= MAD_OPTION_IGNORECRC;
            mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
	    if(options.opt & MPG321_ENABLE_BUFFER)
	    {
		    static struct sembuf start_sops = {2,-1,0};
		    semop(semarray,&start_sops,1);
		    mad_decoder_position = 0;
		    output_buffer_position = 0;
		    union semun sem_ops;
		    sem_ops.val = 0;
		    semctl(semarray,2,SETVAL,sem_ops);
		    Decoded_Frames->total_decoded_frames = 0;
		    Decoded_Frames->done = 0;
		    Decoded_Frames->is_http = 0;
		    Decoded_Frames->is_file = 0;
	    }
            /* if we're rewinding on an mmap()ed stream */
            if(status == MPG321_REWINDING && playbuf.fd == -1) 
            {
                mad_decoder_init(&decoder, &playbuf, read_from_mmap, read_header, /*filter*/0,
                    output, /*error*/0, /* message */ 0);
            }    
            else
                break;
        } 

        if (!(options.opt & MPG321_QUIET_PLAY))
        {
            char time_formatted[11];
            mad_timer_string(current_time, time_formatted, "%.1u:%.2u", MAD_UNITS_MINUTES,
                       MAD_UNITS_SECONDS, 0);
	    fprintf(stderr,"                                                                            \r");
	    fprintf(stderr, "\n[%s] Decoding of %s finished.\n",time_formatted, basename(currentfile)); /* Report total decoded seconds. Maybe for the frame buffer report only total played time?? */
        }
        
        if (options.opt & MPG321_REMOTE_PLAY && status == MPG321_STOPPED)
        {
            clear_remote_file(pl);
        }

        mad_decoder_finish(&decoder);

        if (playbuf.frames)
             free(playbuf.frames);

        if (playbuf.times)
            free(playbuf.times);
            
        if (playbuf.fd == -1)
        {
            munmap(playbuf.buf, playbuf.length);
            close(fd);
        }

        else
        {
            free(playbuf.buf);
            if (playbuf.fd != fileno(stdin)) 
                close(playbuf.fd);
        }
    }

    if(!(options.opt & MPG321_ENABLE_BUFFER))
    {
  	     if(playdevice)
		     ao_close(playdevice);
	     ao_shutdown();
     }

     if (!(options.opt & MPG321_REMOTE_PLAY))
     {
	     if(options.opt & MPG321_ENABLE_BASIC)
	     {
	 	     pflag = 1;
		     /* Restore TTY from keyboard reader thread */
	 	     if (tcsetattr(0, TCSANOW, &old_terminal_settings) < 0)
		 	     perror("tcsetattr ICANON");
	     }
     }
    /*Restoring TTY*/
    if(set_xterm)
    {
	    set_tty_restore();
	    osc_print(0,0,title);
	    if (ctty)
		    fclose(ctty);
    }

out:
    if(options.opt & MPG321_ENABLE_BUFFER)
    {
	    if(kill(output_pid,SIGUSR1) == -1)
		    perror("Error while stopping output process");
	    static int wstatus;
	    wait(&wstatus);
	    if(wstatus == -1)
		    perror("Error while waiting for output process to exit");
	    if(semctl(semarray,0,IPC_RMID) == -1)
		    perror("Error while destroying semaphores");
	    if(shmdt(Output_Queue) == -1)
		    perror("Error while detaching shared buffer");
	    if(shmctl(shm_id,IPC_RMID,NULL))
		    perror("Cannot destroy shared buffer");
	    if(shmctl(frames_id,IPC_RMID,NULL))
		    perror("Cannot destroy shared buffer");
    }
    return(0);
}
Esempio n. 4
0
void libmftp_main_test(char *host, unsigned int port, char *user, char *pw, char *workingdirectory)
{
	int success = 0;
	ftp_file *f = NULL, *g = NULL;
	ftp_content_listing *cl = NULL, *cl2 = NULL;
	ftp_date d;
	char *buf = NULL;
	ftp_connection *c = ftp_open(host, port, tls == 0 ? ftp_security_none : ftp_security_always);

	/*c->_current_features->use_mlsd = ftp_bfalse;*/

	if (!c) {
		printf("Could not connect. Error: %i\n",ftp_error);
		if (ftp_error == FTP_ESECURITY) {
			printf("This server does not support a TLS connection. FTP_ESECURITY\n");
		}
		goto end;
	}

	if (ftp_auth(c, user, pw, ftp_btrue) != FTP_OK) {
		printf("Could not authenticate. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_change_cur_directory(c, workingdirectory) != FTP_OK) {
		printf("Could not cwd. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_reload_cur_directory(c) != FTP_OK) {
		printf("Could not reload wd. Error: %i\n", c->error);
		goto end;
	}

	if (strcmp(workingdirectory, c->cur_directory) != 0) {
		printf("Warning: Working dir (%s) does not match ftp dir (%s)! Continue? (y/n)\n",workingdirectory,c->cur_directory);
		if (getchar() != 'y') {
			goto end;
		}
	}

	char *test = "This is a test string that will be written to the server.";
	size_t test_len = strlen(test);

	//TEST UPLOAD 1

	f = ftp_fopen(c, "testfile.test", FTP_WRITE, 0);
	if (!f) {
		printf("Could not fopen to write. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_fwrites(test, f) != test_len) {
		printf("Could not write test string. Error: %i\n", *(f->error));
		goto end;
	}

	ftp_fclose(f);
	f = NULL;

	//TEST CONTENT LISTING

	int entry_count;
	cl = ftp_contents_of_directory(c, &entry_count);

	if (!cl) {
		printf("Could not get content listing. Error: %i\n", c->error);
		goto end;
	}

	if (entry_count != 1 || cl->next) {
		printf("More than 1 entry in content listing.\n");
		goto end;
	}

	if (!ftp_item_exists_in_content_listing(cl, "testfile.test", &cl2)) {
		printf("Could not find previously generated file in content listing.\n");
		goto end;
	}

	if (!cl2) {
		printf("Did not get a pointer to the content listing entry.\n");
		goto end;
	}

	if (!cl2->facts.given.modify) {
		printf("Warning: Remote host does not provide modification date information! Continue? (y/n)\n");
		if (getchar() != 'y') {
			goto end;
		}
	} else {
		time_t cur = time(NULL);
		struct tm *curtm = localtime(&cur);
		if (curtm->tm_year + 1900 != cl2->facts.modify.year) {
			printf("Warning: Modification year of remote file (%i) does not equal local year (%i). Continue? (y/n)\n",cl2->facts.modify.year,curtm->tm_year + 1900);
			if (getchar() != 'y') {
				goto end;
			}
		}
	}

	//TEST SIZE

	size_t srv_size;
	if (ftp_size(c, "testfile.test", &srv_size) != FTP_OK) {
		printf("Could not get file size. Error: %i\n", c->error);
		goto end;
	}

	if (srv_size != test_len) {
		printf("Remote file size (%lu) differs from local file size (%lu).\n",srv_size,test_len);
		goto end;
	}

	//TEST READ

	f = ftp_fopen(c, "testfile.test", FTP_READ, 0);
	if (!f) {
		printf("Could not fopen to read. Error: %i\n", c->error);
		goto end;
	}

	buf = malloc(srv_size + 1);

	if (ftp_fread(buf, 1, srv_size, f) != srv_size) {
		printf("Could not read file. Error: %i\n", *(f->error));
		goto end;
	}

	ftp_fclose(f);
	f = NULL;

	*(buf+srv_size) = '\0';
	if (strcmp(test, buf) != 0) {
		printf("Remote file content differs from local file content.\n");
		goto end;
	}

	//TEST UPLOAD 2 (SIMULTANEOUS)

	f = ftp_fopen(c, "testfile1.txt", FTP_WRITE, 0);
	if (!f) {
		printf("Could not fopen to write 2 (1). Error: %i\n", c->error);
		goto end;
	}
	g = ftp_fopen(c, "testfile2.txt", FTP_WRITE, 0);
	if (!g) {
		printf("Could not fopen to write 2 (2). Error: %i\n", c->error);
		goto end;
	}
	if (ftp_fwrites(test, f) != test_len) {
		printf("Could not write test string 2 (1). Error: %i\n", *(f->error));
		goto end;
	}
	if (ftp_fwrites(test, g) != test_len) {
		printf("Could not write test string 2 (2). Error: %i\n", *(g->error));
		goto end;
	}
	ftp_fclose(f);
	ftp_fclose(g);
	f = g = NULL;

	//TEST SIZE 2

	size_t srv_size2;
	if (ftp_size(c, "testfile1.txt", &srv_size) != FTP_OK) {
		printf("Could not get file size 2 (1). Error: %i\n", c->error);
		goto end;
	}
	if (ftp_size(c, "testfile2.txt", &srv_size2) != FTP_OK) {
		printf("Could not get file size 2 (2). Error: %i\n", c->error);
		goto end;
	}
	if (srv_size != test_len || srv_size2 != test_len) {
		printf("Remote file size 2 differs from local file size.\n");
		goto end;
	}

	//TEST FOLDERS

	if (ftp_create_folder(c, "testfolder") != FTP_OK) {
		printf("Could not create folder. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_move(c, "testfile1.txt", "testfolder/testfile1.txt")) {
		printf("Could not move file. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_change_cur_directory(c, "testfolder") != FTP_OK) {
		printf("Could not cwd 2. Error: %i\n", c->error);
		goto end;
	}

	if (!ftp_item_exists(c, "testfile1.txt", NULL)) {
		printf("File does not exist where it was moved to.");
		goto end;
	}

	//TEST DELETE

	if (ftp_delete(c, "testfile1.txt", ftp_bfalse) != FTP_OK) {
		printf("Could not delete file. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_item_exists(c, "testfile1.txt", NULL)) {
		printf("Deleted file still exists.");
		goto end;
	}

	if (ftp_change_cur_directory(c, workingdirectory) != FTP_OK) {
		printf("Could not cwd 3. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_delete(c, "testfolder", ftp_btrue) != FTP_OK) {
		printf("Could not delete folder. Error: %i\n", c->error);
		goto end;
	}

	if (ftp_item_exists(c, "testfolder", NULL)) {
		printf("Deleted folder still exists.");
		goto end;
	}

	if (ftp_delete(c, "testfile.test", ftp_bfalse) != FTP_OK || ftp_delete(c, "testfile2.txt", ftp_bfalse) != FTP_OK) {
		printf("Could not clean up. Error: %i\n", c->error);
		goto end;
	}

	success = 1;
end:
	if (cl) ftp_free(cl);
	if (f) ftp_fclose(f);
	if (g) ftp_fclose(g);
	if (c) ftp_close(c);
	if (buf) free(buf);
	if (!success) {
		printf("Test was NOT successful. :-(\n");
	} else {
		printf("Test was successful! :-)\n");
	}
}
Esempio n. 5
0
int main(int argc, char *argv[])
{
    int use_audio, use_video;
    int fullscreen;
    int scalesize;
    int scale_width, scale_height;
    int loop_play;
    int i, pause;
    int volume;
    Uint32 seek;
    float skip;
    int bilinear_filtering;
    SDL_Surface *screen = NULL;
    SMPEG *mpeg;
    SMPEG_Info info;
    char *basefile;
    const char *title = NULL;
    SDL_version sdlver;
    SMPEG_version smpegver;
    int fd;
    char buf[32];
    int status;

    /* Get the command line options */
    use_audio = 1;
    use_video = 1;
    fullscreen = 0;
    scalesize = 1;
    scale_width = 0;
    scale_height = 0;
    loop_play = 0;
    volume = 100;
    seek = 0;
    skip = 0;
    bilinear_filtering = 0;
    fd = 0;
    for ( i=1; argv[i] && (argv[i][0] == '-') && (argv[i][1] != 0); ++i ) {
        if ( (strcmp(argv[i], "--noaudio") == 0) ||
                (strcmp(argv[i], "--nosound") == 0) ) {
            use_audio = 0;
        } else if ( strcmp(argv[i], "--novideo") == 0 ) {
            use_video = 0;
        } else if ( strcmp(argv[i], "--fullscreen") == 0 ) {
            fullscreen = 1;
        } else if ((strcmp(argv[i], "--double") == 0)||(strcmp(argv[i], "-2") == 0)) {
            scalesize = 2;
        } else if ((strcmp(argv[i], "--loop") == 0) || (strcmp(argv[i], "-l") == 0)) {
            loop_play = 1;
        } else if ( strcmp(argv[i], "--bilinear") == 0 ) {
            bilinear_filtering = 1;
        } else if ((strcmp(argv[i], "--seek") == 0)||(strcmp(argv[i], "-S") == 0)) {
            ++i;
            if ( argv[i] ) {
                seek = atol(argv[i]);
            }
        } else if ((strcmp(argv[i], "--skip") == 0)||(strcmp(argv[i], "-k") == 0)) {
            ++i;
            if ( argv[i] ) {
                skip = (float)atof(argv[i]);
            }
        } else if ((strcmp(argv[i], "--volume") == 0)||(strcmp(argv[i], "-v") == 0)) {
            ++i;
            if (i >= argc)
            {
                fprintf(stderr, "Please specify volume when using --volume or -v\n");
                return(1);
            }
            if ( argv[i] ) {
                volume = atoi(argv[i]);
            }
            if ( ( volume < 0 ) || ( volume > 100 ) ) {
                fprintf(stderr, "Volume must be between 0 and 100\n");
                volume = 100;
            }
        } else if ((strcmp(argv[i], "--title") == 0)||(strcmp(argv[i], "-t") == 0)) {
            ++i;
            if (i >= argc)
            {
                fprintf(stderr, "Please specify title when using --title or -t\n");
                return(1);
            }
            if ( argv[i] ) {
                title = argv[i];
            }
        } else if ((strcmp(argv[i], "--version") == 0) ||
                   (strcmp(argv[i], "-V") == 0)) {
            sdlver = *SDL_Linked_Version();
            SMPEG_VERSION(&smpegver);
            printf("SDL version: %d.%d.%d\n"
                   "SMPEG version: %d.%d.%d\n",
                   sdlver.major, sdlver.minor, sdlver.patch,
                   smpegver.major, smpegver.minor, smpegver.patch);
            return(0);
        } else if ((strcmp(argv[i], "--scale") == 0)||(strcmp(argv[i], "-s") == 0)) {
            ++i;
            if ( argv[i] ) {
                sscanf(argv[i], "%dx%d", &scale_width, &scale_height);
            }
        } else if ((strcmp(argv[i], "--help") == 0) || (strcmp(argv[i], "-h") == 0)) {
            usage(argv[0]);
            return(0);
        } else {
            fprintf(stderr, "Warning: Unknown option: %s\n", argv[i]);
        }
    }
    /* If there were no arguments just print the usage */
    if (argc == 1) {
        usage(argv[0]);
        return(0);
    }

#if defined(linux) || defined(__FreeBSD__) /* Plaympeg doesn't need a mouse */
    putenv("SDL_NOMOUSE=1");
#endif

    /* Play the mpeg files! */
    status = 0;
    for ( ; argv[i]; ++i ) {
        /* Initialize SDL */
        if ( use_video ) {
            if ((SDL_Init(SDL_INIT_VIDEO) < 0) || !SDL_VideoDriverName(buf, 1)) {
                fprintf(stderr, "Warning: Couldn't init SDL video: %s\n",
                        SDL_GetError());
                fprintf(stderr, "Will ignore video stream\n");
                use_video = 0;
            }
        }

        if ( use_audio ) {
            if ((SDL_Init(SDL_INIT_AUDIO) < 0) || !SDL_AudioDriverName(buf, 1)) {
                fprintf(stderr, "Warning: Couldn't init SDL audio: %s\n",
                        SDL_GetError());
                fprintf(stderr, "Will ignore audio stream\n");
                use_audio = 0;
            }
        }

        /* Allow Ctrl-C when there's no video output */
        signal(SIGINT, next_movie);

        /* Create the MPEG stream */
#ifdef NET_SUPPORT
#ifdef RAW_SUPPORT
        /* Check if source is an IP address and port*/
        if((fd = raw_open(argv[i])) != 0)
            mpeg = SMPEG_new_descr(fd, &info, use_audio);
        else
#endif
#ifdef HTTP_SUPPORT
            /* Check if source is an http URL */
            if((fd = http_open(argv[i])) != 0)
                mpeg = SMPEG_new_descr(fd, &info, use_audio);
            else
#endif
#ifdef FTP_SUPPORT
                /* Check if source is an http URL */
                if((fd = ftp_open(argv[i])) != 0)
                    mpeg = SMPEG_new_descr(fd, &info, use_audio);
                else
#endif
#endif
#ifdef VCD_SUPPORT
                    /* Check if source is a CDROM device */
                    if((fd = vcd_open(argv[i])) != 0)
                        mpeg = SMPEG_new_descr(fd, &info, use_audio);
                    else
#endif
                    {
                        if(strcmp(argv[i], "-") == 0) /* Use stdin for input */
                            mpeg = SMPEG_new_descr(0, &info, use_audio);
                        else
                            mpeg = SMPEG_new(argv[i], &info, use_audio);
                    }

        if ( SMPEG_error(mpeg) ) {
            fprintf(stderr, "%s: %s\n", argv[i], SMPEG_error(mpeg));
            SMPEG_delete(mpeg);
            status = -1;
            continue;
        }
        SMPEG_enableaudio(mpeg, use_audio);
        SMPEG_enablevideo(mpeg, use_video);
        SMPEG_setvolume(mpeg, volume);

        /* Enable software bilinear filtering, if desired */
        if ( bilinear_filtering ) {
            SMPEG_Filter *filter;

            filter = SMPEGfilter_bilinear();
            filter = SMPEG_filter( mpeg, filter );
            filter->destroy(filter);
        }

        /* Print information about the video */
        basefile = strrchr(argv[i], '/');
        if ( basefile ) {
            ++basefile;
        } else {
            basefile = argv[i];
        }
        if ( info.has_audio && info.has_video ) {
            printf("%s: MPEG system stream (audio/video)\n", basefile);
        } else if ( info.has_audio ) {
            printf("%s: MPEG audio stream\n", basefile);
        } else if ( info.has_video ) {
            printf("%s: MPEG video stream\n", basefile);
        }
        if ( info.has_video ) {
            printf("\tVideo %dx%d resolution\n", info.width, info.height);
        }
        if ( info.has_audio ) {
            printf("\tAudio %s\n", info.audio_string);
        }
        if ( info.total_size ) {
            printf("\tSize: %d\n", info.total_size);
        }
        if ( info.total_time ) {
            printf("\tTotal time: %f\n", info.total_time);
        }

        /* Set up video display if needed */
        if ( info.has_video && use_video ) {
            const SDL_VideoInfo *video_info;
            Uint32 video_flags;
            int video_bpp;
            int width, height;

            /* Get the "native" video mode */
            video_info = SDL_GetVideoInfo();
            switch (video_info->vfmt->BitsPerPixel) {
            case 16:
            case 24:
            case 32:
                video_bpp = video_info->vfmt->BitsPerPixel;
                break;
            default:
                video_bpp = 16;
                break;
            }
            if ( scale_width ) {
                width = scale_width;
            } else {
                width = info.width;
            }
            width *= scalesize;
            if ( scale_height ) {
                height = scale_height;
            } else {
                height = info.height;
            }
            height *= scalesize;
            video_flags = SDL_SWSURFACE;
            if ( fullscreen ) {
                video_flags = SDL_FULLSCREEN|SDL_DOUBLEBUF|SDL_HWSURFACE;
            }
            video_flags |= SDL_ASYNCBLIT;
            video_flags |= SDL_RESIZABLE;
            screen = SDL_SetVideoMode(width, height, video_bpp, video_flags);
            if ( screen == NULL ) {
                fprintf(stderr, "Unable to set %dx%d video mode: %s\n",
                        width, height, SDL_GetError());
                continue;
            }
            if (title != NULL) {
                SDL_WM_SetCaption(title, title);
            } else {
                SDL_WM_SetCaption(argv[i], "plaympeg");
            }
            if ( screen->flags & SDL_FULLSCREEN ) {
                SDL_ShowCursor(0);
            }
            SMPEG_setdisplay(mpeg, screen, NULL, update);
            SMPEG_scaleXY(mpeg, screen->w, screen->h);
        } else {
            SDL_QuitSubSystem(SDL_INIT_VIDEO);
            use_video = 0;
        }

        /* Set any special playback parameters */
        if ( loop_play ) {
            SMPEG_loop(mpeg, 1);
        }

        /* Seek starting position */
        if(seek) SMPEG_seek(mpeg, seek);

        /* Skip seconds to starting position */
        if(skip) SMPEG_skip(mpeg, skip);

        /* Play it, and wait for playback to complete */
        SMPEG_play(mpeg);
        done = 0;
        pause = 0;
        while ( ! done && ( pause || (SMPEG_status(mpeg) == SMPEG_PLAYING) ) ) {
            SDL_Event event;

            while ( use_video && SDL_PollEvent(&event) ) {
                switch (event.type) {
                case SDL_VIDEORESIZE: {
                    SDL_Surface *old_screen = screen;
                    SMPEG_pause(mpeg);
                    screen = SDL_SetVideoMode(event.resize.w, event.resize.h, screen->format->BitsPerPixel, screen->flags);
                    if ( old_screen != screen ) {
                        SMPEG_setdisplay(mpeg, screen, NULL, update);
                    }
                    SMPEG_scaleXY(mpeg, screen->w, screen->h);
                    SMPEG_pause(mpeg);
                }
                break;
                case SDL_KEYDOWN:
                    if ( (event.key.keysym.sym == SDLK_ESCAPE) || (event.key.keysym.sym == SDLK_q) ) {
                        // Quit
                        done = 1;
                    } else if ( event.key.keysym.sym == SDLK_RETURN ) {
                        // toggle fullscreen
                        if ( event.key.keysym.mod & KMOD_ALT ) {
                            SDL_WM_ToggleFullScreen(screen);
                            fullscreen = (screen->flags & SDL_FULLSCREEN);
                            SDL_ShowCursor(!fullscreen);
                        }
                    } else if ( event.key.keysym.sym == SDLK_UP ) {
                        // Volume up
                        if ( volume < 100 ) {
                            if ( event.key.keysym.mod & KMOD_SHIFT ) {   // 10+
                                volume += 10;
                            } else if ( event.key.keysym.mod & KMOD_CTRL ) { // 100+
                                volume = 100;
                            } else {                                     // 1+
                                volume++;
                            }
                            if ( volume > 100 )
                                volume = 100;
                            SMPEG_setvolume(mpeg, volume);
                        }
                    } else if ( event.key.keysym.sym == SDLK_DOWN ) {
                        // Volume down
                        if ( volume > 0 ) {
                            if ( event.key.keysym.mod & KMOD_SHIFT ) {
                                volume -= 10;
                            } else if ( event.key.keysym.mod & KMOD_CTRL ) {
                                volume = 0;
                            } else {
                                volume--;
                            }
                            if ( volume < 0 )
                                volume = 0;
                            SMPEG_setvolume(mpeg, volume);
                        }
                    } else if ( event.key.keysym.sym == SDLK_PAGEUP ) {
                        // Full volume
                        volume = 100;
                        SMPEG_setvolume(mpeg, volume);
                    } else if ( event.key.keysym.sym == SDLK_PAGEDOWN ) {
                        // Volume off
                        volume = 0;
                        SMPEG_setvolume(mpeg, volume);
                    } else if ( event.key.keysym.sym == SDLK_SPACE ) {
                        // Toggle play / pause
                        if ( SMPEG_status(mpeg) == SMPEG_PLAYING ) {
                            SMPEG_pause(mpeg);
                            pause = 1;
                        } else {
                            SMPEG_play(mpeg);
                            pause = 0;
                        }
                    } else if ( event.key.keysym.sym == SDLK_RIGHT ) {
                        // Forward
                        if ( event.key.keysym.mod & KMOD_SHIFT ) {
                            SMPEG_skip(mpeg, 100);
                        } else if ( event.key.keysym.mod & KMOD_CTRL ) {
                            SMPEG_skip(mpeg, 50);
                        } else {
                            SMPEG_skip(mpeg, 5);
                        }
                    } else if ( event.key.keysym.sym == SDLK_LEFT ) {
                        // Reverse
                        if ( event.key.keysym.mod & KMOD_SHIFT ) {

                        } else if ( event.key.keysym.mod & KMOD_CTRL ) {

                        } else {

                        }
                    } else if ( event.key.keysym.sym == SDLK_KP_MINUS ) {
                        // Scale minus
                        if ( scalesize > 1 ) {
                            scalesize--;
                        }
                    } else if ( event.key.keysym.sym == SDLK_KP_PLUS ) {
                        // Scale plus
                        scalesize++;
                    } else if ( event.key.keysym.sym == SDLK_f ) {
                        // Toggle filtering on/off
                        if ( bilinear_filtering ) {
                            SMPEG_Filter *filter = SMPEGfilter_null();
                            filter = SMPEG_filter( mpeg, filter );
                            filter->destroy(filter);
                            bilinear_filtering = 0;
                        } else {
                            SMPEG_Filter *filter = SMPEGfilter_bilinear();
                            filter = SMPEG_filter( mpeg, filter );
                            filter->destroy(filter);
                            bilinear_filtering = 1;
                        }
                    }
                    break;
                case SDL_QUIT:
                    done = 1;
                    break;
                default:
                    break;
                }
            }
            SDL_Delay(1000/2);
        }
        SMPEG_delete(mpeg);
    }
    SDL_Quit();

#if defined(RAW_SUPPORT) || defined(HTTP_SUPPORT) || defined(FTP_SUPPORT) || \
    defined(VCD_SUPPORT)
    if(fd) close(fd);
#endif

    return(status);
}
Esempio n. 6
0
File: mpg321.c Progetto: e3c/mpg321
int main(int argc, char *argv[])
{
    int fd = 0;
    char *currentfile, old_dir[PATH_MAX];
    playlist *pl = NULL;
    struct id3_file *id3struct = NULL;
    struct id3_tag *id3tag = NULL;

    buffer playbuf;
    
    struct mad_decoder decoder;

    old_dir[0] = '\0';

    playbuf.pl = pl = new_playlist();

    if (!pl)
    {
        fprintf(stderr, "malloc failed at startup!\n");
        exit(1);
    }

    options.volume = MAD_F_ONE;

    status = MPG321_PLAYING;
    
    /* Get the command line options */
    parse_options(argc, argv, pl);

    /* If there were no files and no playlist specified just print the usage */
    if (!playlist_file && optind == argc)
    {
        usage(argv[0]);
        exit(0);
    }

    if (playlist_file)
        load_playlist(pl, playlist_file);
    
    add_cmdline_files(pl, argv);

    if (shuffle_play)
        shuffle_files(pl);

    ao_initialize();

    check_default_play_device();
    
    if (!(options.opt & MPG321_REMOTE_PLAY))
    {
        handle_signals(-1); /* initialize signal handler */
     
        remote_input_buf[0] = '\0';
    }
    
    if (!(options.opt & MPG321_QUIET_PLAY)) 
        mpg123_boilerplate();
    
    if (options.opt & MPG321_REMOTE_PLAY)
    {
        printf ("@R MPG123\n");
    }
    
    /* Play the mpeg files or zip it! */
    while((currentfile = get_next_file(pl, &playbuf)))
    {
        if (quit_now) 
            break;
        
        signal(SIGINT, SIG_DFL);
        
        playbuf.buf = NULL;
        playbuf.fd = -1;
        playbuf.length = 0;
        playbuf.done = 0;
        playbuf.num_frames = 0;
        playbuf.max_frames = -1;
        strncpy(playbuf.filename,currentfile, PATH_MAX);
        playbuf.filename[PATH_MAX-1] = '\0';
        
        if (status == MPG321_PLAYING) 
            file_change = 1;

        mad_timer_reset(&playbuf.duration);
        
        mad_timer_reset(&current_time);

        if (!(options.opt & MPG321_QUIET_PLAY) && file_change)
        {
            id3struct = id3_file_open (currentfile, ID3_FILE_MODE_READONLY);

            if (id3struct)
            {
                id3tag = id3_file_tag (id3struct);
            
                if (id3tag)
                {
                    show_id3 (id3tag);
                }

                id3_file_close (id3struct);
            }
        }

        if (options.opt & MPG321_REMOTE_PLAY && file_change)
        {
            id3struct = id3_file_open (currentfile, ID3_FILE_MODE_READONLY);

            if (id3struct)
            {
                id3tag = id3_file_tag (id3struct);
            
                if (id3tag)
                {
                    if (!show_id3(id3tag))
                    {
                        /* This shouldn't be necessary, but it appears that
                           libid3tag doesn't necessarily know if there are no
                           id3 tags on a given mp3 */
                        char * basec = strdup(currentfile);
                        char * basen = basename(basec);
                        
                        char * dot = strrchr(basen, '.');
                        
                        if (dot)
                            *dot = '\0';
                        
                        printf("@I %s\n", basen);

                        free(basec);
                    }
                }
                
                else
                {
                    fprintf(stderr, "Allocation error");
                    exit(1);
                }

                id3_file_close (id3struct);
            }
            
            else
            {
                char * basec = strdup(currentfile);
                char * basen = basename(basec);
                
                char * dot = strrchr(basen, '.');
                
                if (dot)
                    *dot = '\0';
                
                printf("@I %s\n", basen);

                free(basec);
            }
        }

        /* Create the MPEG stream */
        /* Check if source is on the network */
        if((fd = raw_open(currentfile)) != 0 || (fd = http_open(currentfile)) != 0
            || (fd = ftp_open(currentfile)) != 0)
        {
            playbuf.fd = fd;
            playbuf.buf = malloc(BUF_SIZE);
            playbuf.length = BUF_SIZE;
            
            mad_decoder_init(&decoder, &playbuf, read_from_fd, read_header, /*filter*/0,
                            output, /*error*/0, /* message */ 0);
        }

        /* Check if we are to use stdin for input */
        else if(strcmp(currentfile, "-") == 0)
        {
            playbuf.fd = fileno(stdin);
            playbuf.buf = malloc(BUF_SIZE);
            playbuf.length = BUF_SIZE;

            mad_decoder_init(&decoder, &playbuf, read_from_fd, read_header, /*filter*/0,
                            output, /*error*/0, /* message */ 0);
        }
            
        /* currentfile is a local file (presumably.) mmap() it */
        else
        {
            struct stat stat;
            
            if((fd = open(currentfile, O_RDONLY)) == -1)
            {
                mpg321_error(currentfile);

                /* mpg123 stops immediately if it can't open a file */
                break;
            }
            
            if(fstat(fd, &stat) == -1)
            {
                close(fd);
                mpg321_error(currentfile);
                continue;
            }
            
            if (!S_ISREG(stat.st_mode))
            {
                close(fd);
                continue;
            }
            
            calc_length(currentfile, &playbuf);

            if ((options.maxframes != -1) && (options.maxframes <= playbuf.num_frames))
            { 
                playbuf.max_frames = options.maxframes;
            }
            
            playbuf.frames = malloc((playbuf.num_frames + 1) * sizeof(void*));
            playbuf.times = malloc((playbuf.num_frames + 1) * sizeof(mad_timer_t));
    
            if((playbuf.buf = mmap(0, playbuf.length, PROT_READ, MAP_SHARED, fd, 0))
                                == MAP_FAILED)
            {
                close(fd);
                mpg321_error(currentfile);
                continue;
            }
            
            close(fd);
            playbuf.frames[0] = playbuf.buf;
            
            mad_decoder_init(&decoder, &playbuf, read_from_mmap, read_header, /*filter*/0,
                            output, /*error*/0, /* message */ 0);
        }

        if(!(options.opt & MPG321_QUIET_PLAY))/*zip it!!!*/
        {
            /* Because dirname might modify the argument */
            char * dirc = strdup(currentfile);
            char * basec = strdup(currentfile);

            char * basen = basename(basec);
            char * dirn = dirname(dirc);
            
            /* make sure that the file has a pathname; otherwise don't print out
               a Directory: listing */
            if(strchr(currentfile, '/') && strncmp(old_dir, dirn, PATH_MAX) != 0)
            {
                /* Print information about the file */
                fprintf(stderr, "\n");
                fprintf(stderr,"Directory: %s/\n", dirn);
                
                strncpy(old_dir, dirn, PATH_MAX);
                old_dir[PATH_MAX-1] = '\0';
            }
            
            /* print a newline between different songs only, not after
               Directory: listing */
            else
            {
                fprintf(stderr, "\n");
            }

            fprintf(stderr,"Playing MPEG stream from %s ...\n", basen);
    
            free(dirc);
            free(basec);
        }    

        signal(SIGINT, handle_signals);

        /* Every time the user gets us to rewind, we exit decoding,
           reinitialize it, and re-start it */
        while (1)
        {
            mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
            
            /* if we're rewinding on an mmap()ed stream */
            if(status == MPG321_REWINDING && playbuf.fd == -1) 
            {
                mad_decoder_init(&decoder, &playbuf, read_from_mmap, read_header, /*filter*/0,
                    output, /*error*/0, /* message */ 0);
            }    
            else
                break;
        } 

        if (!(options.opt & MPG321_QUIET_PLAY))
        {
            char time_formatted[11];
            mad_timer_string(current_time, time_formatted, "%.1u:%.2u", MAD_UNITS_MINUTES,
                       MAD_UNITS_SECONDS, 0);
            fprintf(stderr, "\n[%s] Decoding of %s finished.\n",time_formatted, basename(currentfile));
        }
        
        if (options.opt & MPG321_REMOTE_PLAY && status == MPG321_STOPPED)
        {
            clear_remote_file(pl);
        }

        mad_decoder_finish(&decoder);

        if (quit_now)
            break;

        if (playbuf.frames)
             free(playbuf.frames);

        if (playbuf.times)
            free(playbuf.times);
            
        if (playbuf.fd == -1)
        {
            munmap(playbuf.buf, playbuf.length);
        }

        else
        {
            free(playbuf.buf);
            if (playbuf.fd != fileno(stdin)) 
                close(playbuf.fd);
        }
    }

    if(playdevice)
        ao_close(playdevice);

    ao_shutdown();

#if defined(RAW_SUPPORT) || defined(HTTP_SUPPORT) || defined(FTP_SUPPORT) 
    if(fd) close(fd);
#endif

    return(0);
}