home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / nvdc87 / doslabel / volume.c < prev    next >
Text File  |  1987-07-02  |  4KB  |  115 lines

  1. /* VOLUME.C: Reads and writes volume label */
  2.  
  3. /* INCLUDES */
  4. #include <stdio.h>
  5. #include <dos.h>
  6. #include <string.h>
  7.  
  8. /* CONSTANTS */
  9. #define PROMPT "\nLabel is limited to 11 characters:"
  10. #define BEEP  7
  11. #ifndef TRUE
  12. #define TRUE  1
  13. #define FALSE 0
  14. #endif
  15.  
  16. /* LOCAL FUNCTION PROTOTYPES */
  17. char  gotlabel (void);
  18. void  change (void);
  19. void  addlabel (void);
  20. void  pad (char name[]);
  21.  
  22. /* GLOBALS */
  23. union REGS  reg;                                   /* register set */
  24. struct {                   /* simulated xfcb loaded by DOS fcn 11h */
  25.   char       skip1[8];                       /* space to old label */
  26.   char       oldlabel[11];                            /* old label */
  27.   char       skip2[5];                       /* space to new label */
  28.   char       newlabel[11];                            /* new label */
  29.   char       skip3[29];            /* to make struct 64 bytes long */
  30. } dta;
  31. struct xfcb fcbx = {{0xFF},    /* initialize xfcb for label search */
  32.                     {"\0\0\0\0\0"},              /* reserved zeros */
  33.                     {FA_LABEL},             /* set label attribute */
  34.                    };
  35. /* -------------------------- */
  36.  
  37. main ()
  38. {
  39. char   exists;
  40.  
  41. /* INITIALIZE */
  42.   setdta (MK_FP (_DS,(unsigned) &dta));    /* dta = scratch buffer */
  43.   puts ("\nGet volume label from which drive?");
  44.   fcbx.xfcb_fcb.fcb_drive = toupper (getch ()) - '@'; /* get drive */
  45.   strcpy (fcbx.xfcb_fcb.fcb_name, "???????????");  /* set wildcard */
  46.  
  47. /* PROCESS */
  48.   if (exists = gotlabel ())
  49.     printf ("\nLabel of drive %c is %.11s",
  50.               fcbx.xfcb_fcb.fcb_drive + '@', dta.oldlabel);
  51.   else
  52.     printf ("\nDrive %c has no label",
  53.               fcbx.xfcb_fcb.fcb_drive + '@');
  54.   puts ("\n\nDo you want to write a new label? (y/n)");
  55.   if (toupper (getch ()) == 'Y')
  56.     if (exists)
  57.       change ();
  58.     else
  59.       addlabel ();
  60. } /* ------------------------ */
  61.  
  62. char  gotlabel (void)                      /* read label from disk */
  63.                               /* returns TRUE if found, else FALSE */
  64. {
  65.   reg.x.dx = (unsigned) &fcbx;                 /* point DX to fcbx */
  66.   reg.h.ah = 0x11;                    /* DOS fcn: search for first */
  67.   intdos (®, ®);                                 /* call DOS */
  68.   if (!reg.h.al)                         /* AL = 0 when successful */
  69.     return (TRUE);
  70.   else                         /* non-zero AL means no label found */
  71.     return (FALSE);
  72. } /* ------------------------ */
  73.  
  74. void  change (void)                           /* change disk label */
  75. {                                /* same DOS call as renaming file */
  76.   puts (PROMPT);
  77.   scanf ("%11s", dta.newlabel);       /* ignore more than 11 chars */
  78.   pad (dta.newlabel);                  /* pad with trailing spaces */
  79.   reg.x.dx = (unsigned) &dta;               /* point to dta buffer */
  80.   reg.h.ah = 0x17;                         /* DOS fcn: rename file */
  81.   intdos (®, ®);                                 /* call DOS */
  82.   if (!reg.h.al)                         /* AL = 0 when successful */
  83.     printf ("\n\nLabel successfully changed to %.11s",
  84.            dta.newlabel);
  85.   else {
  86.     puts ("\n\nUnsuccessful! Disk may be damaged");
  87.     putchar (BEEP);
  88.   }
  89. } /* ------------------------ */
  90.  
  91. void  addlabel (void)                   /* label an unlabeled disk */
  92. {                            /* same DOS call as creating new file */
  93.   puts (PROMPT);
  94.   scanf ("%11s", fcbx.xfcb_fcb.fcb_name);          /* get 11 chars */
  95.   pad (fcbx.xfcb_fcb.fcb_name);        /* pad with trailing spaces */
  96.   reg.x.dx = (unsigned) &fcbx;        /* have to use xfcb for this */
  97.   reg.h.ah = 0x16;                         /* DOS fcn: create file */
  98.   intdos (®, ®);                                 /* call DOS */
  99.   if (!reg.h.al)                         /* AL = 0 when successful */
  100.     printf ("\n\nDisk is now labeled %.11s", fcbx.xfcb_fcb.fcb_name);
  101.   else {
  102.     puts ("\n\nUnsuccessful! Root directory may be full");
  103.     putchar (BEEP);
  104.   }
  105. } /* ------------------------ */
  106.  
  107. void  pad (char name[])       /* pad filename with trailing spaces */
  108. {                   /* DOS chokes on C null terminator in filename */
  109. int   p;
  110.  
  111.   if (strlen (name) < 11)
  112.     for (p = strlen (name); p < 11; p++)
  113.       name[p] = ' ';
  114. } /* ------------------------ */
  115.