home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / d / d-linux.zip / dm-dist / delplay.c < prev    next >
C/C++ Source or Header  |  1991-03-01  |  1KB  |  81 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "structs.h"
  4.  
  5.  
  6. #define TOLOWER(c)  (((c)>='A'  && (c) <= 'Z') ? ((c)+('a'-'A')) : (c))
  7.  
  8.  
  9. int str_cmp(char *str1, char *str2)
  10. {
  11.     for (; *str1 || *str2; str1++, str2++)
  12.         if (TOLOWER(*str1) != TOLOWER(*str2))
  13.             return(1);
  14.  
  15.     return(0);
  16. }
  17.  
  18. void del(char *filename, int name)
  19. {
  20.     char confirm[80];
  21.     FILE *fl;
  22.     struct char_file_u player;
  23.     int pos, num;
  24.     long end;
  25.  
  26.     if (!(fl = fopen(filename, "r+")))
  27.     {
  28.         perror("list");
  29.         exit();
  30.     }
  31.  
  32.     puts("Searching for player:");
  33.  
  34.     for (num = 1, pos = 0;; pos++, num++)
  35.     {
  36.         fread(&player, sizeof(player), 1, fl);
  37.         if (feof(fl))
  38.         {
  39.             fprintf(stderr, "delplay: could not locate %d.\n", name);
  40.             exit();
  41.         }
  42.  
  43.         if (num == name) {
  44.             printf("Confirm deletion of [%s] by typeing Yes: ", player.name);
  45.             scanf("%s", confirm);
  46.             if (str_cmp("Yes", confirm)) {
  47.                 printf("Aborted delete.\n");
  48.                 exit();
  49.             } else {
  50.                 break;
  51.             }
  52.         }
  53.  
  54.     }
  55.  
  56.     /* read the last player */
  57.     fseek(fl, -sizeof(player), 2);
  58.     fread(&player, sizeof(player), 1, fl);
  59.     fseek(fl, pos*sizeof(player), 0);
  60.     fwrite(&player, sizeof(player), 1, fl);
  61.     fseek(fl, 0, 2);
  62.     end = ftell(fl);
  63.     fclose(fl);
  64.  
  65.     if (truncate(filename, end-sizeof(player)))
  66.         perror("truncate");
  67. }
  68.  
  69.     
  70. main(int argc, char **argv)
  71. {
  72.     if (argc != 3)
  73.         puts("Usage: delplay <DikuMUD player filename> <Player Number>");
  74.     else {
  75.         if (atoi(argv[2]) < 1)
  76.             puts("Illegal player number, must be >= 1");
  77.         else
  78.             del(argv[1], atoi(argv[2]));
  79.     }
  80. }
  81.