[plug] A question for perl hackers

Arie Hol arie99 at ozemail.com.au
Wed Aug 21 23:25:55 WST 2002


At 11:00 PM 21-08-2002 +0800, you wrote:
>I am trying to grab the filesize in bytes from a listing like:
>
>   310272 Aug 21 22:18 inbox
>  5193576 Jul  7 09:45 lyndon
>        0 Aug 12 09:58 mailing lists
>    32720 Jun 10 20:10 mchoice
>    46513 Aug 16 16:18 netvigator
>        0 Aug 21 20:04 outbox
> 10069208 Aug 21 20:04 sent-mail
>  2377093 Aug 21 19:33 trash
>

Why not read the i-node data for a given file, and read the attribute that
stores the file size, you could even use this method inside a foreach
statement - although I cannot remember how to do it, I know it can be done.

At uni last semester we had to write a small c programme to show the access
time, last modification time and status change times for each specified file
in a directory, so I guess it can be modified to return the filesize too.

I hope this helps.

//#####################################################################
// Author  :  Arie Hol                                                #
// Date    :  23 May, 2002                                            #     
// Semester 1, 2002                                                   #
//                                                                    #
//                                                                    #
// Use C/C++ to design and write a program which takes its command    #
// line parameters which are names of files and directories and       #
// displays the current time, the access time, last modification time #
// and status change times for each specified file or directory. The  #
// following is a sample output of such a program: ls is first used   #
// to show what files are available in the current directory.         #
//                                                                    #
//#####################################################################

#include <sys/stat.h>
#include <stdio.h>	
#include <time.h>

char * showtime(time_t do_time);

main(int argc, char *argv[])
    {
    int i, keypress;	
    char * filename;
    struct stat filedata;  
    time_t ntime;
    struct tm *nowtime = localtime( &ntime );


    for (i=1;i<argc;i++)	
        {
	// Check if file exists and load structure - if error then exit
    	if ( -1 ==  stat (argv[i], &filedata))
    		{
    		printf("\n\n Error opening file :  %s \n\n\n", argv[i]);
	    	exit(0);
            }
        printf("\n %s \n", showtime(ntime));
        printf("\nFOR FILE : %s \n\n", argv[i]);
        printf("Access time is %s \n", showtime(filedata.st_atime));
        printf("Last modification time = %s\n",showtime(filedata.st_mtime));
        printf("Last status change time = %s\n",showtime(filedata.st_ctime));
        printf("\t\t\n\n\n Press <enter> to continue ");
        keypress=getchar();
        }
    }

char * showtime(time_t do_time)
    {
    struct tm *filetime;
    static char timestr[30];

    filetime=localtime(&do_time);

    strftime(timestr, sizeof timestr, "%h %e %H:%M\n", filetime);

    return(timestr);
    }

//--------------------------------end of file------------------------------//
Regards Arie

>--------------------------------------------<

For the concert of life, nobody has a program.

>--------------------------------------------<



More information about the plug mailing list