/ circle3.1 / src / alias.c
alias.c
  1  /* ***********************************************************************
  2  *  File: alias.c				A utility to CircleMUD	 *
  3  * Usage: writing/reading player's aliases.				 *
  4  *									 *
  5  * Code done by Jeremy Hess and Chad Thompson				 *
  6  * Modifed by George Greer for inclusion into CircleMUD bpl15.		 *
  7  *									 *
  8  * Copyright (C) 1993, 94 by the Trustees of the Johns Hopkins University *
  9  * CircleMUD is based on DikuMUD, Copyright (C) 1990, 1991.		 *
 10  *********************************************************************** */
 11  
 12  #include "conf.h"
 13  #include "sysdep.h"
 14  #include "structs.h"
 15  #include "utils.h"
 16  #include "interpreter.h"
 17  #include "db.h"
 18  
 19  void write_aliases(struct char_data *ch);
 20  void read_aliases(struct char_data *ch);
 21  void delete_aliases(const char *charname);
 22  
 23  void write_aliases(struct char_data *ch)
 24  {
 25    FILE *file;
 26    char fn[MAX_STRING_LENGTH];
 27    struct alias_data *temp;
 28  
 29    get_filename(fn, sizeof(fn), ALIAS_FILE, GET_NAME(ch));
 30    remove(fn);
 31  
 32    if (GET_ALIASES(ch) == NULL)
 33      return;
 34  
 35    if ((file = fopen(fn, "w")) == NULL) {
 36      log("SYSERR: Couldn't save aliases for %s in '%s'.", GET_NAME(ch), fn);
 37      perror("SYSERR: write_aliases");
 38      return;
 39    }
 40  
 41    for (temp = GET_ALIASES(ch); temp; temp = temp->next) {
 42      int aliaslen = strlen(temp->alias);
 43      int repllen = strlen(temp->replacement) - 1;
 44  
 45      fprintf(file, "%d\n%s\n"	/* Alias */
 46  		  "%d\n%s\n"	/* Replacement */
 47  		  "%d\n",	/* Type */
 48  		aliaslen, temp->alias,
 49  		repllen, temp->replacement + 1,
 50  		temp->type);
 51    }
 52    
 53    fclose(file);
 54  }
 55  
 56  void read_aliases(struct char_data *ch)
 57  {   
 58    FILE *file;
 59    char xbuf[MAX_STRING_LENGTH];
 60    struct alias_data *t2, *prev = NULL;
 61    int length;
 62  
 63    get_filename(xbuf, sizeof(xbuf), ALIAS_FILE, GET_NAME(ch));
 64  
 65    if ((file = fopen(xbuf, "r")) == NULL) {
 66      if (errno != ENOENT) {
 67        log("SYSERR: Couldn't open alias file '%s' for %s.", xbuf, GET_NAME(ch));
 68        perror("SYSERR: read_aliases");
 69      }
 70      return;
 71    }
 72   
 73    CREATE(GET_ALIASES(ch), struct alias_data, 1);
 74    t2 = GET_ALIASES(ch); 
 75  
 76    for (;;) {
 77      /* Read the aliased command. */
 78      if (fscanf(file, "%d\n", &length) != 1)
 79        goto read_alias_error;
 80  
 81      fgets(xbuf, length + 1, file);
 82      t2->alias = strdup(xbuf);
 83  
 84      /* Build the replacement. */
 85      if (fscanf(file, "%d\n", &length) != 1)
 86         goto read_alias_error;
 87  
 88      *xbuf = ' ';		/* Doesn't need terminated, fgets() will. */
 89      fgets(xbuf + 1, length + 1, file);
 90      t2->replacement = strdup(xbuf); 
 91  
 92      /* Figure out the alias type. */
 93      if (fscanf(file, "%d\n", &length) != 1)
 94        goto read_alias_error;
 95  
 96      t2->type = length; 
 97  
 98      if (feof(file))
 99        break;
100  
101      CREATE(t2->next, struct alias_data, 1);
102      prev = t2;
103      t2 = t2->next;
104    }; 
105    
106    fclose(file);
107    return;
108  
109  read_alias_error:
110    if (t2->alias)
111      free(t2->alias);
112    free(t2);
113    if (prev)
114      prev->next = NULL;
115    fclose(file);
116  } 
117  
118  void delete_aliases(const char *charname)
119  {
120    char filename[PATH_MAX];
121  
122    if (!get_filename(filename, sizeof(filename), ALIAS_FILE, charname))
123      return;
124  
125    if (remove(filename) < 0 && errno != ENOENT)
126      log("SYSERR: deleting alias file %s: %s", filename, strerror(errno));
127  }
128