]> git.sur5r.net Git - cc65/blobdiff - samples/diodemo.c
Remove trailings spaces from CBM-related asm files
[cc65] / samples / diodemo.c
index d767a1a2a8072f2d62d509dc5dc4cc9e05f1106c..3e52f2fa920c8b67c69a17ceac128acf174252e4 100644 (file)
@@ -36,6 +36,7 @@
 #include <conio.h>
 #include <ctype.h>
 #include <errno.h>
+#include <cc65.h>
 #include <dio.h>
 
 
@@ -54,27 +55,29 @@ static void ClearLine (void)
 }
 
 
-static driveid_t AskForDrive (const char* Name)
+static unsigned char AskForDrive (const char* Name)
 /* Ask for a drive id and return it */
 {
-    driveid_t Drive = 0;
-    char      Char;
+    unsigned char Drive = 0;
+    char          Char;
 
-    cprintf ("\r\n%s Drive ID ?", Name);
+    cprintf ("\r\n%s Drive ID ? ", Name);
 
+    cursor (1);
     do {
         Char = cgetc ();
         if (isdigit (Char)) {
             cputc (Char);
-            Drive = (driveid_t) (Drive * 10 + Char - '0');
+            Drive = Drive * 10 + Char - '0';
         }
     } while (Char != CH_ENTER);
+    cursor (0);
 
     return Drive;
 }
 
 
-static void AskForDisk (const char* Name, driveid_t Drive)
+static void AskForDisk (const char* Name, unsigned char Drive)
 /* Ask the user to insert a specific disk */
 {
     ClearLine ();
@@ -84,51 +87,75 @@ static void AskForDisk (const char* Name, driveid_t Drive)
 }
 
 
-static char* AllocBuffer (sectsize_t SectSize, sectnum_t SectCount, sectnum_t* ChunkCount)
+static char* AllocBuffer (unsigned int SectSize, unsigned int SectCount, unsigned int* ChunkCount)
 /* Allocate a copy buffer on the heap and return a pointer to it */
 {
-    void*         Buffer = NULL;
+    char*         Buffer = NULL;
     unsigned long BufferSize;
     unsigned int  Chunks = 1;
 
     /* Increase number of chunks resp. decrease size */
     /* of one chunk until buffer allocation succeeds */
     do {
-        *ChunkCount = (sectnum_t) ((SectCount + Chunks - 1) / Chunks);
+        *ChunkCount = (unsigned int) ((SectCount + Chunks - 1) / Chunks);
         BufferSize = *ChunkCount * (unsigned long) SectSize;
         if (BufferSize < UINT_MAX) {
             Buffer = malloc ((size_t) BufferSize);
         }
     } while (Buffer == NULL && ++Chunks <= MAX_CHUNKS);
 
-    return (char*) Buffer;
+    return Buffer;
 }
 
 
-int main (void)
+int main (int argc, const char* argv[])
 {
-    driveid_t  SourceId;
-    driveid_t  TargetId;
-    dhandle_t  Source = NULL;
-    dhandle_t  Target = NULL;
-    sectsize_t SectSize;
-    sectnum_t  SectCount;
-    char*      Buffer;
-    sectnum_t  Sector;
-    sectnum_t  ChunkCount;
-    sectnum_t  ChunkOffset = 0;
+    unsigned char SourceId;
+    unsigned char TargetId;
+    dhandle_t     Source = NULL;
+    dhandle_t     Target = NULL;
+    unsigned int  SectSize;
+    unsigned int  SectCount;
+    char*         Buffer;
+    unsigned int  Sector;
+    unsigned int  ChunkCount;
+    unsigned int  ChunkOffset = 0;
 
     clrscr ();
     screensize (&ScreenX, &ScreenY);
 
+    /* Allow user to read exit messages */
+    if (doesclrscrafterexit ()) {
+        atexit ((void (*)) cgetc);
+    }
+
     cputs ("Floppy Disk Copy\r\n");
     chline (16);
     cputs ("\r\n");
 
     /* Get source and target drive id (which may very well be identical) */
-    SourceId = AskForDrive ("Source");
-    TargetId = AskForDrive ("Target");
-    cputs ("\r\n\n");
+    switch (argc) {
+      case 1:
+        SourceId = AskForDrive ("Source");
+        TargetId = AskForDrive ("Target");
+        cputs ("\r\n");
+        break;
+
+      case 2:
+        SourceId = TargetId = atoi (argv[1]);
+        break;
+
+      case 3:
+        SourceId = atoi (argv[1]);
+        TargetId = atoi (argv[2]);
+        break;
+
+      default:
+        cprintf ("\r\nToo many arguments\r\n");
+        return EXIT_FAILURE;
+    }
+
+    cputs ("\r\n");
 
     do {
         /* Check for single drive copy or inital iteration */
@@ -136,8 +163,10 @@ int main (void)
             AskForDisk ("Source", SourceId);
         }
 
-        /* Open drive on initial iteration */
+        /* Check for initial iteration */
         if (Source == NULL) {
+
+            /* Open source drive */
             Source = dio_open (SourceId);
             if (Source == NULL) {
                 cprintf ("\r\n\nError %d on opening Drive %d\r\n", (int) _oserror, SourceId);
@@ -147,7 +176,7 @@ int main (void)
             SectSize  = dio_query_sectsize (Source);
             SectCount = dio_query_sectcount (Source);
 
-            /* */
+            /* Allocate buffer */
             Buffer = AllocBuffer (SectSize, SectCount, &ChunkCount);
             if (Buffer == NULL) {
                 cputs ("\r\n\nError on allocating Buffer\r\n");
@@ -163,7 +192,7 @@ int main (void)
 
             /* Read one sector */
             if (dio_read (Source, Sector, Buffer + (Sector - ChunkOffset) * SectSize) != 0) {
-                cprintf ("\r\n\nError %d on reading Drive %d\r\n", (int) _oserror, SourceId);
+                cprintf ("\r\n\nError %d on reading from Drive %d\r\n", (int) _oserror, SourceId);
                 return EXIT_FAILURE;
             }
         }
@@ -173,7 +202,7 @@ int main (void)
             AskForDisk ("Target", TargetId);
         }
 
-        /* Open drive on initial iteration */
+        /* Open target drive on initial iteration */
         if (Target == NULL) {
             Target = dio_open (TargetId);
             if (Target == NULL) {
@@ -197,7 +226,7 @@ int main (void)
 
             /* Write one sector */
             if (dio_write (Target, Sector, Buffer + (Sector - ChunkOffset) * SectSize) != 0) {
-                cprintf ("\r\n\nError %d on opening Drive %d\r\n", (int) _oserror, TargetId);
+                cprintf ("\r\n\nError %d on writing to Drive %d\r\n", (int) _oserror, TargetId);
                 return EXIT_FAILURE;
             }
         }