home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / util / wb / FileSize21.lha / FileSize / FileSize.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-27  |  3.5 KB  |  187 lines

  1. /* Simple appicon that displays the size of a file */
  2.  
  3. #include <stdio.h>
  4. #include <proto/wb.h>
  5.  
  6. extern near long __stack=16384;
  7.  
  8. extern char *id(char *buf,char *fname);
  9.  
  10. int filesize(char *name);
  11. char *build_name(char *buf,char *name, BPTR dir);
  12. void msg(char *msg);
  13. BOOL quit(void);
  14. char *getversion(char *buf, char *name);
  15.     
  16. struct IntuiText msg_text={
  17.     1,0,JAM1,16,9,NULL,NULL,NULL };
  18.  
  19. struct IntuiText ok_text={
  20.     1,0,JAM1,16,9,NULL,NULL,NULL };
  21.  
  22. struct IntuiText die_text={
  23.     1,0,JAM1,16,9,NULL,NULL,NULL };
  24.  
  25. void main(void) {
  26.     struct MsgPort *mp;
  27.     struct DiskObject *dob;
  28.     struct AppIcon *ap;
  29.     struct AppMessage *am;
  30.  
  31.     dob=GetDiskObject("filesize_appicon");
  32.     
  33.     if(!dob) {
  34.         dob=GetDefDiskObject(WBDISK);
  35.         if(!dob) {
  36.             printf("Serious probs with icons!\n");
  37.             return;
  38.         }
  39.     }
  40.     
  41.     
  42.     if(!(mp=CreateMsgPort())) {
  43.         printf("Unable to open a msg port!\n");
  44.         FreeDiskObject(dob);
  45.         return;
  46.     }
  47.  
  48.     ap=AddAppIcon(0x1,0,"FileSize",mp,0,dob,TAG_END);
  49.     
  50.     if(!ap) {
  51.         printf("App Icon failed!\n");
  52.         DeleteMsgPort(mp);
  53.         FreeDiskObject(dob);
  54.         return;
  55.     } 
  56.  
  57.     while(1) {
  58.         if(Wait((ULONG)(SIGBREAKF_CTRL_C|(1<<mp->mp_SigBit)))==SIGBREAKF_CTRL_C) 
  59.             goto end;
  60.         else {
  61.             char buf[1000];
  62.             char buf2[1000];
  63.             char buf3[500];
  64.             
  65.             while(am=(struct AppMessage *)GetMsg(mp)) {
  66.                 char str[2000];
  67.                 if(am->am_NumArgs==0) {
  68.                     /* Double clicked */
  69.                     if(quit()) { ReplyMsg((struct Message *)am); goto end; }
  70.                 } else {
  71.  
  72.                     build_name(buf,am->am_ArgList->wa_Name,am->am_ArgList->wa_Lock);
  73.                     sprintf(str,"FileSize v2.1 © 1997 by Ben Matthew\n===================================\n\nFile: %s\nSize: %d bytes\n\nType: %s\nVersion: %s",buf,filesize(buf),id(buf2,buf),getversion(buf3,buf));
  74.                     msg(str);
  75.                 }
  76.                 ReplyMsg((struct Message *)am);
  77.             }
  78.         }    
  79.     }
  80.  
  81.     end:
  82.     while(am=(struct AppMessage *)GetMsg(mp))
  83.         ReplyMsg((struct Message *)am);
  84.     DeleteMsgPort(mp);
  85.  
  86.     FreeDiskObject(dob);    
  87.     RemoveAppIcon(ap);
  88.     
  89. }
  90.  
  91. int filesize(char *name) {
  92.     FILE *fp=0;
  93.     int size= -1;
  94.  
  95.     if(fp=fopen(name,"r")) {
  96.         fseek(fp,0,SEEK_END);
  97.         size=ftell(fp);
  98.         fclose(fp);
  99.     }
  100.  
  101.     return(size);
  102. }
  103.  
  104. char *build_name(char *buf,char *name, BPTR dir) {
  105.  
  106.     char path[1024];
  107.  
  108.     strcpy(path,"");
  109.  
  110.     NameFromLock(dir,path,1023);
  111.     
  112.     strcpy(buf,path);
  113.     if(strcmp(buf,"")) {
  114.         if(buf[strlen(buf)-1]!=':') {
  115.             strcat(buf,"/");
  116.         }
  117.     }
  118.     
  119.     strcat(buf,name);
  120.         
  121.     return(buf);
  122.  
  123. }
  124.  
  125. void msg(char *msg) {
  126.  
  127.     struct EasyStruct easypeasy;
  128.  
  129.     easypeasy.es_StructSize=sizeof(struct EasyStruct);
  130.     easypeasy.es_Flags=0;
  131.     easypeasy.es_Title="FileSize v2.1 © 1997 by Ben Matthew";
  132.     easypeasy.es_TextFormat=msg;
  133.     easypeasy.es_GadgetFormat="Continue";
  134.  
  135.     EasyRequest(NULL,&easypeasy,NULL,TAG_DONE);
  136.  
  137. }
  138.  
  139. BOOL quit(void) {
  140.  
  141.     struct EasyStruct easypeasy;
  142.  
  143.     easypeasy.es_StructSize=sizeof(struct EasyStruct);
  144.     easypeasy.es_Flags=0;
  145.     easypeasy.es_Title="FileSize v2.1 © 1997 by Ben Matthew";
  146.     easypeasy.es_TextFormat="FileSize © 1997 v2.1 by Ben Matthew\n===================================\n\nReally Quit?\n";
  147.     easypeasy.es_GadgetFormat="Yep|Nahh Maybe Not";
  148.  
  149.     return(EasyRequest(NULL,&easypeasy,NULL,TAG_DONE));
  150.     
  151. }
  152.  
  153. char *getversion(char *buf, char *name) {
  154.     char command[500];
  155.     FILE *fp;
  156.     UBYTE c=1;
  157.     char dud;
  158.     
  159.     sprintf(command,"version %s >ram:filesize_tmp",name);
  160.     system(command);
  161.     
  162.     strcpy(buf,"");
  163.     
  164.     if(fp=fopen("ram:filesize_tmp","r")) {
  165.         while(c!=EOF) {
  166.             char ch[5];
  167.             c=fgetc(fp);
  168.             sprintf(ch,"%c",c);
  169.             strcat(buf,ch);
  170.         }
  171.         buf[strlen(buf)-1]=0;
  172.         fclose(fp);
  173.         DeleteFile("ram:filesize_tmp");
  174.     }
  175.     
  176.     if(strlen(buf)>10) {
  177.         dud=buf[5];
  178.         buf[5]=0;
  179.         if(!strcmp(buf,"Could")) 
  180.             strcpy(buf,"Not available\n");
  181.         else
  182.             buf[5]=dud;
  183.     }
  184.     
  185.  
  186.     return(buf);
  187. }