|
|
1.1 ! root 1: #include <ctype.h> ! 2: #include <stdio.h> ! 3: #include "mail.h" ! 4: #include "string.h" ! 5: #include "aux.h" ! 6: ! 7: /* imports */ ! 8: extern char **getflds(); ! 9: extern char *getenv(); ! 10: ! 11: /* predeclared */ ! 12: string *getdbfiles(); ! 13: int translate(); ! 14: int lookup(); ! 15: int compare(); ! 16: ! 17: /* loop through the names to be translated */ ! 18: main(ac, av) ! 19: int ac; ! 20: char *av[]; ! 21: { ! 22: string *alias; /* the alias for the name */ ! 23: char *thissys; /* name of this system */ ! 24: string *files; /* list of files to search */ ! 25: int i; ! 26: ! 27: /* get environmental info */ ! 28: thissys = sysname_read(); ! 29: files = getdbfiles(); ! 30: alias = s_new(); ! 31: ! 32: /* loop through the names to be translated (from standard input) */ ! 33: for(i=1; i<ac; i++) { ! 34: (void)translate(av[i], thissys, files, alias); ! 35: if (*s_to_c(alias) == '\0') ! 36: printf("local!%s\n", av[i]); ! 37: else ! 38: printf("%s\n", s_to_c(alias)); ! 39: fflush(stdout); ! 40: } ! 41: return 0; ! 42: } ! 43: ! 44: /* get the list of dbfiles to search */ ! 45: string * ! 46: getdbfiles() ! 47: { ! 48: FILE *fp; ! 49: string *files = s_new(); ! 50: ! 51: /* system wide aliases */ ! 52: if (chdir(UPASROOT) < 0) { ! 53: perror("translate(chdir):"); ! 54: return files; ! 55: } ! 56: if ((fp = fopen(SYSALIAS, "r")) != NULL) { ! 57: while (s_gettoken(fp, files) != NULL) ! 58: s_append(files, " "); ! 59: fclose(fp); ! 60: } ! 61: ! 62: return files; ! 63: } ! 64: ! 65: /* loop through the translation files */ ! 66: int ! 67: translate(name, thissys, files, alias) ! 68: char *name; /* name to translate */ ! 69: char *thissys; /* name of this system */ ! 70: string *files; ! 71: string *alias; /* where to put the alias */ ! 72: { ! 73: string *file = s_new(); ! 74: string *fullname; ! 75: char *home; ! 76: ! 77: /* ! 78: printf("translate(%s, %s, %s, %s)\n", name, thissys, ! 79: s_to_c(files), s_to_c(alias)); ! 80: */ ! 81: /* create the full name to avoid loops (system!name) */ ! 82: fullname = s_copy(thissys); ! 83: s_append(fullname, "!"); ! 84: s_append(fullname, name); ! 85: ! 86: /* look at user's local names */ ! 87: home = getenv("HOME"); ! 88: if (home != NULL) { ! 89: s_append(file, home); ! 90: s_append(file, USERALIAS); ! 91: if (lookup(name, fullname, file, alias, thissys)==0) { ! 92: s_free(fullname); ! 93: s_free(file); ! 94: return 0; ! 95: } ! 96: } ! 97: ! 98: /* look at system-wide names */ ! 99: s_restart(files); ! 100: while (s_parse(files, s_restart(file)) != NULL) { ! 101: if (lookup(name, fullname, file, alias, thissys)==0) { ! 102: s_free(fullname); ! 103: s_free(file); ! 104: return 0; ! 105: } ! 106: } ! 107: ! 108: /* first look for mailbox */ ! 109: s_restart(file); ! 110: abspath(name, MAILROOT, file); ! 111: if (access(s_to_c(file), 0) == 0) { ! 112: s_append(alias, "local"); ! 113: s_append(alias, "!"); ! 114: s_append(alias, name); ! 115: s_free(file); ! 116: return 0; ! 117: } ! 118: ! 119: return -1; ! 120: } ! 121: ! 122: /* Loop through the entries in a translation file looking for a match. ! 123: * Return 0 if found, -1 otherwise. ! 124: */ ! 125: int ! 126: lookup(name, fullname, file, alias, thissys) ! 127: char *name; ! 128: string *fullname; ! 129: string *file; ! 130: string *alias; /* returned string */ ! 131: char *thissys; ! 132: { ! 133: FILE *fp; ! 134: string *line = s_new(); ! 135: string *token = s_new(); ! 136: int rv = -1; ! 137: char *cp; ! 138: ! 139: /* ! 140: printf("lookup(%s, %s, %s, %s)\n", name, s_to_c(fullname), ! 141: s_to_c(file), s_to_c(alias)); ! 142: */ ! 143: s_reset(alias); ! 144: if ((fp = fopen(s_to_c(file), "r")) == NULL) ! 145: return -1; ! 146: ! 147: /* look for a match */ ! 148: while (s_getline(fp, s_restart(line))!=NULL) { ! 149: if (s_parse(s_restart(line), s_restart(token))==NULL) ! 150: continue; ! 151: if (compare(token, name)!=0) ! 152: continue; ! 153: /* match found, get the alias */ ! 154: while(s_parse(line, s_restart(token))!=NULL) { ! 155: /* avoid definition loops */ ! 156: if (compare(token, name)==0 || ! 157: compare(token, s_to_c(fullname))==0){ ! 158: s_append(alias, "local"); ! 159: s_append(alias, "!"); ! 160: if(cp = strrchr(s_to_c(token), '!')) ! 161: cp++; ! 162: else ! 163: cp = s_to_c(token); ! 164: s_append(alias, cp); ! 165: } else { ! 166: s_append(alias, s_to_c(token)); ! 167: } ! 168: s_append(alias, " "); ! 169: } ! 170: rv = 0; ! 171: break; ! 172: } ! 173: s_free(line); ! 174: s_free(token); ! 175: return rv; ! 176: } ! 177: ! 178: /* compare two strings (case insensitive) */ ! 179: #define lower(c) (isupper(c)?c-('A'-'a'):c) ! 180: int ! 181: compare(s1, p2) ! 182: string *s1; ! 183: register char *p2; ! 184: { ! 185: register char *p1 = s_to_c(s1); ! 186: register int rv; ! 187: ! 188: while(1) { ! 189: rv = lower(*p1) - lower(*p2); ! 190: if (rv) ! 191: return rv; ! 192: if (*p1 == '\0') ! 193: return 0; ! 194: p1++; ! 195: p2++; ! 196: } ! 197: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.