home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / XAP / XFILEMAN / XFILEMAN.TAR / xfilemanager / devices.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-22  |  8.5 KB  |  328 lines

  1. /*
  2.  * Copyright 1993 by Ove Kalkan, Cremlingen, Germany
  3.  *
  4.  * Permission to use, copy, modify, distribute and sell this software and it's
  5.  * documentation for any purpose is hereby granted without fee, rpovided that
  6.  * the above copyright notice and this permission appear in supporting
  7.  * documentation, and that the name of Ove Kalkan not to be used in
  8.  * advertising or publicity pertaining to distributiopn of the software without
  9.  * specific, written prior permission. Ove Kalkan makes no representations
  10.  * about the suitability of this software for any purpose. It is provided
  11.  * as is without express or implied warranty.
  12.  *
  13.  * OVE KALKAN DISPLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  14.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABLILITY AND FITNESS, IN NO
  15.  * EVENT SHALL OVE KALKAN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  16.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  17.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  18.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  19.  * PERFORMANCE OF THIS SOFTWARE.
  20.  *
  21.  * $Header: filename,v 1.0 yyyy/mm/dd hh:mm:ss loginname Exp $
  22.  */
  23.  
  24. #include "global.h"
  25.  
  26. #ifdef linux
  27. #include <sys/vfs.h>
  28. #endif
  29.  
  30. /*
  31.  * Global variables
  32.  */
  33.  
  34. #define    DEV_TYPE_MSDOS    1
  35. #define    DEV_TYPE_EXT    2
  36. #define    DEV_TYPE_EXT2    4
  37. #define    DEV_TYPE_MINIX    8
  38. #define    DEV_TYPE_ISO    16
  39. #define    DEV_TYPE_XIAFS    32
  40.  
  41. #define    DEV_FLAG_RO        1
  42.  
  43. typedef struct {
  44.     char    *name;
  45.     char    *dev_file;
  46.     char    *mnt_file;
  47.     u_int    types;
  48.     u_int    flags;
  49.     u_int    mounted;
  50.     u_long    free;
  51.     u_long    total;
  52.     Pixmap    pmap;
  53.     char    *pmap_file;
  54. } Dev_Glyph;
  55.  
  56. static    unsigned char    dev_list_count;        /* Anzahl der mountbaren devices */
  57. static    Dev_Glyph    **dev_list;    /* feld mit den mountbaren devices */
  58.  
  59. Widget    CreateDeviceBox (Widget bar, Widget ref);
  60. void    ReadDeviceFile (void);
  61. void    LoadDeviceIcons (Widget toplevel);
  62.  
  63. static    char    *fs_names[] = {"MS-Dos Fs","Extended Fs",
  64.                 "Extended2 Filesystem","Minix Fs",
  65.                 "ISO9660 Fs","Xia Fs","Unknown"};
  66. static    char    *fs_mtab[] = {"msdos","ext","ext2","minix","iso9660","xia"};
  67.  
  68.  
  69. /*********************************************************
  70.  * name:    CreateDeviceBox
  71.  * description:    Erzeugt eine Box mit allen gemounteten und
  72.  *        mountbaren Devices -> Configurationsfile
  73.  * input:    Widget bar    - Widget auf dem das Box-Widget
  74.  *                  erzeugt wird.
  75.  * output:    none
  76.  * author:    Ove Kalkan
  77.  * date:    7.8.1993
  78.  *********************************************************/
  79. Widget    CreateDeviceBox(Widget bar, Widget ref)
  80. {
  81.     Widget    box = NULL;
  82.     int    i;
  83.     Widget    but;
  84.     FILE    *fp;
  85.  
  86.     dev_list_count = 0;
  87.     dev_list = NULL;
  88.  
  89.  
  90.     ReadDeviceFile();
  91.  
  92.     if (!dev_list_count)
  93.         return;
  94.  
  95.     /*
  96.      * Feststellen ob Device schon gemountet ist
  97.      */
  98. #ifdef linux
  99.     /*
  100.      * Gemountete devices stehen in /etc/mtab
  101.      */
  102.     if ((fp = fopen("/etc/mtab","r"))) {
  103.         char    s[100];
  104.  
  105.         while (fgets(s,99,fp)) {
  106.             char    *ss;
  107.  
  108.             ss = strchr(s,' ');
  109.             *ss = '\0';    /* Das erste Wort ist das Device_file */
  110.             for (i = 0; i < dev_list_count; i++)
  111.                 if (!strcmp(dev_list[i]->dev_file,s)) {
  112.                     int    j = 0;
  113.                     struct statfs    buf;
  114.                     char    tmp[100];
  115.  
  116.                     dev_list[i]->mounted = 6;
  117.                     ss++;
  118.                     sscanf(ss,"%s",&tmp);
  119.                     if (statfs(tmp,&buf) == 0) {
  120.                         dev_list[i]->total = buf.f_blocks*buf.f_bsize/1024;
  121.                         dev_list[i]->free = buf.f_bavail*buf.f_bsize/1024;
  122.                     }
  123.                     ss = strchr(ss,' ');
  124.                     ss + 1;
  125.                     sscanf(ss,"%s",&s);
  126.                     while (j < 6) {
  127.                         if (!strcmp(fs_mtab[j],s))
  128.                             dev_list[i]->mounted = j + 1;
  129.                         j++;
  130.                     }
  131.                 }
  132.         }
  133.         fclose(fp);
  134.     }
  135. #endif
  136.  
  137.     box = XtVaCreateManagedWidget("device_area",simpleWidgetClass,bar,
  138.                     XtNwidth, (defaults.multi_window ? 400 : 300),
  139.                     XtNheight, DIR_Y_STEP*dev_list_count,
  140.                     XtNborderWidth,0,
  141.                     XtNfromVert,ref,
  142.                     XtNleft,XawChainLeft,
  143.                     XtNright,XawChainRight,
  144.                     XtNtop,XawChainBottom,
  145.                     XtNbottom,XawChainBottom,
  146.                     NULL);
  147.     XtOverrideTranslations(box,XtParseTranslationTable(
  148.                     "<Expose>:    refresh-devices()"));
  149.     return(box);
  150. }
  151.  
  152.  
  153. /*********************************************************
  154.  * name:    ReadDeviceFile
  155.  * description:    Einlesen des Files in dem die mountbaren
  156.  *        devices fuer die Deviceliste stehen
  157.  * input:    none
  158.  * output:    none
  159.  * author:    Ove Kalkan
  160.  * date:    7.8.1993
  161.  *********************************************************/
  162. void    ReadDeviceFile(void)
  163. {
  164.     FILE    *fp;
  165.     char    s[1024];
  166.  
  167.     /* Versuchen, das File zu oeffnen */
  168.     if (!(fp = fopen(DEVICE_LIST,"r")))
  169.         return;
  170.  
  171.     /* File einlesen */
  172.     s[1023] = '\0';
  173.     while (fgets(s,1023,fp)) {
  174.         char    *sss,*ss;
  175.         char    *dev_file,*mnt_file,*name,*pmap_file;
  176.         int    type,flag;
  177.  
  178.         ss = s;
  179.         /*
  180.          * Devicefile lesen
  181.          */
  182.         mnt_file = NULL;
  183.         dev_file = NULL;
  184.         pmap_file = NULL;
  185.         name = NULL;
  186.         if ((sss = strchr(ss,':')) != NULL) {
  187.             *sss = '\0';
  188.             if (!(dev_file = (char *) malloc(strlen(ss) + 1)))
  189.                 FATAL_ERROR("ReadDeviceFile: malloc failed");
  190.             sprintf(dev_file,"%s\0",ss);
  191.             ss = sss + 1;    /* Zeiger weiterschieben */
  192.  
  193.             /*
  194.              * Mount-file holen
  195.              */
  196.             if (strlen(dev_file) && (sss = strchr(ss,':')) != NULL) {
  197.                 *sss = '\0';
  198.                 if (!(mnt_file = (char *) malloc(strlen(ss) + 1)))
  199.                     FATAL_ERROR("ReadDeviceFile: malloc failed");
  200.                 sprintf(mnt_file,"%s\0",ss);
  201.                 ss = sss + 1;    /* Zeiger weiterschieben */
  202.  
  203.                 /*
  204.                  * Type und Flag lesen
  205.                  */
  206.                 sscanf(ss,"%d %d",&type,&flag);
  207.                 ss = strchr(ss,':') + 1;
  208.  
  209.                 /*
  210.                  * pmap-file holen
  211.                  */
  212.                 if (strlen(mnt_file) && (sss = strchr(ss,':')) != NULL) {
  213.                     *sss = '\0';
  214.                     if (strlen(ss)) {
  215.                         if (!(pmap_file = (char *) malloc(strlen(ss) + 1)))
  216.                             FATAL_ERROR("ReadDeviceFile: malloc failed");
  217.                         sprintf(pmap_file,"%s\0",ss);
  218.                     }
  219.                     ss = sss + 1;    /* Zeiger weiterschieben */
  220.  
  221.                     /*
  222.                      * Label holen
  223.                      */
  224.                     if (!strlen(ss))
  225.                         name = dev_file;
  226.                     else {
  227.                         if (!(name = (char *) malloc(strlen(ss) + 2)))
  228.                             FATAL_ERROR("ReadDeviceFile: malloc failed");
  229.                         sprintf(name,"%s\0",ss);
  230.                         name[strlen(name)-1] = '\0';    /* Newline loeschen */
  231.                     }
  232.  
  233.                     /*
  234.                      * Deviceliste erweitern
  235.                      */
  236.                     if (!dev_list)
  237.                         if (!(dev_list = (Dev_Glyph **) malloc(sizeof(Dev_Glyph *))))
  238.                             FATAL_ERROR("ReadDeviceFile: malloc failed");
  239.                     else
  240.                         if (!(dev_list = (Dev_Glyph **) realloc((void *) dev_list,
  241.                                             sizeof(Dev_Glyph *) * (dev_list_count + 1))))
  242.                             FATAL_ERROR("ReadDeviceFile: malloc failed");
  243.                     if (!(dev_list[dev_list_count] = (Dev_Glyph *) malloc(sizeof(Dev_Glyph))))
  244.                         FATAL_ERROR("ReadDeviceFile: amlloc failed.");
  245.                     dev_list[dev_list_count]->dev_file = dev_file;
  246.                     dev_list[dev_list_count]->mnt_file = mnt_file;
  247.                     dev_list[dev_list_count]->pmap_file = pmap_file;
  248.                     dev_list[dev_list_count]->name = name;
  249.                     dev_list[dev_list_count]->types = type;
  250.                     dev_list[dev_list_count]->flags = flag;
  251.                     dev_list[dev_list_count]->mounted = 0;
  252.                     dev_list[dev_list_count]->free = 0;
  253.                     dev_list[dev_list_count]->total = 0;
  254.                     dev_list[dev_list_count]->pmap = (Pixmap) NULL;
  255.                     dev_list_count++;
  256.                 }
  257.                 else {
  258.                     if (mnt_file)    free(mnt_file);
  259.                     if (dev_file)    free(dev_file);
  260.                 }
  261.             }
  262.             else
  263.                 if (dev_file)    free(dev_file);
  264.                 
  265.         }
  266.     }
  267.  
  268.     /* File wieder schliessen */
  269.     fclose(fp);
  270. }
  271.  
  272. /*********************************************************
  273.  * name:    LoadDeviceIcons
  274.  * description:    laedt alle Icons fuer die Devices
  275.  * input:    Widget toplevel
  276.  * output:    none
  277.  * author:    Ove Kalkan
  278.  * date:    7.8.1993
  279.  *********************************************************/
  280. void    LoadDeviceIcons (Widget toplevel)
  281. {
  282.     int    i;
  283.  
  284.     for (i = 0; i < dev_list_count; i++)
  285.         if (dev_list[i]->pmap_file) {
  286.             int    w,h;
  287.  
  288.             dev_list[i]->pmap = ReadPixmapFromFile(dev_list[i]->pmap_file,&w,&h);
  289.         }
  290. }
  291.  
  292.  
  293. /*********************************************************
  294.  * name:    RefreshDevices
  295.  * description:    Grafiche Erneuerung des Device-liste
  296.  * input:    Das Uebliche fuer ActionProcs
  297.  * output:    none
  298.  * author:    Ove Kalkan
  299.  * date:    7.8.1993
  300.  *********************************************************/
  301. XtActionProc    RefreshDevices (Widget w, XExposeEvent *e, String *s, Cardinal *c)
  302. {
  303.     int    i;
  304.  
  305.     for (i = 0; i < dev_list_count; i++) {
  306.         if (!dev_list[i]->mounted) {
  307.             char    s[200];
  308.  
  309.             sprintf(s,"%s (unmounted)",
  310.                     dev_list[i]->name);
  311.             XDrawString(XtDisplay(w),XtWindow(w),line_gc,DIR_X_STEP + 20,
  312.                     i*DIR_Y_STEP + 14,s,strlen(s));
  313.         }
  314.         else {
  315.             char    s[200];
  316.  
  317.             sprintf(s,"%s (%s  F/T: %d/%d)",
  318.                     dev_list[i]->name,fs_names[dev_list[i]->mounted - 1],
  319.                     dev_list[i]->free,dev_list[i]->total);
  320.             XDrawString(XtDisplay(w),XtWindow(w),line_gc,DIR_X_STEP + 20,
  321.                     i*DIR_Y_STEP + 14,s,strlen(s));
  322.         }
  323.         if (dev_list[i]->pmap)
  324.             XCopyArea(XtDisplay(w),dev_list[i]->pmap,XtWindow(w),line_gc,
  325.                   0,0,16,16,DIR_X_STEP,i*DIR_Y_STEP+2);
  326.     }
  327. }
  328.