-/*
- * Get name, room number, school phone, and home phone.
- */
-char *
-getfingerinfo(pwd)
- struct passwd *pwd;
-{
- char in_str[BUFSIZ];
- struct default_values *defaults, *get_defaults();
- static char answer[4*BUFSIZ];
-
- answer[0] = '\0';
- defaults = get_defaults(pwd->pw_gecos);
- printf("Default values are printed inside of '[]'.\n");
- printf("To accept the default, type <return>.\n");
- printf("To have a blank entry, type the word 'none'.\n");
- /*
- * Get name.
- */
- do {
- printf("\nName [%s]: ", defaults->name);
- (void) fgets(in_str, BUFSIZ, stdin);
- if (special_case(in_str, defaults->name))
- break;
- } while (illegal_input(in_str));
- (void) strcpy(answer, in_str);
- /*
- * Get room number.
- */
- do {
- printf("Room number (Exs: 597E or 197C) [%s]: ",
- defaults->office_num);
- (void) fgets(in_str, BUFSIZ, stdin);
- if (special_case(in_str, defaults->office_num))
- break;
- } while (illegal_input(in_str) || illegal_building(in_str));
- (void) strcat(strcat(answer, ","), in_str);
- /*
- * Get office phone number.
- * Remove hyphens.
- */
- do {
- printf("Office Phone (Ex: 6426000) [%s]: ",
- defaults->office_phone);
- (void) fgets(in_str, BUFSIZ, stdin);
- if (special_case(in_str, defaults->office_phone))
- break;
- remove_hyphens(in_str);
- } while (illegal_input(in_str) || not_all_digits(in_str));
- (void) strcat(strcat(answer, ","), in_str);
- /*
- * Get home phone number.
- * Remove hyphens if present.
- */
- do {
- printf("Home Phone (Ex: 9875432) [%s]: ", defaults->home_phone);
- (void) fgets(in_str, BUFSIZ, stdin);
- if (special_case(in_str, defaults->home_phone))
- break;
- remove_hyphens(in_str);
- } while (illegal_input(in_str) || not_all_digits(in_str));
- (void) strcat(strcat(answer, ","), in_str);
- if (strcmp(answer, pwd->pw_gecos) == 0) {
- printf("Finger information unchanged.\n");
- exit(1);
- }
- return (answer);
-}
-
-/*
- * Prints an error message if a ':' or a newline is found in the string.
- * A message is also printed if the input string is too long.
- * The password file uses :'s as seperators, and are not allowed in the "gcos"
- * field. Newlines serve as delimiters between users in the password file,
- * and so, those too, are checked for. (I don't think that it is possible to
- * type them in, but better safe than sorry)
- *
- * Returns '1' if a colon or newline is found or the input line is too long.
- */
-illegal_input(input_str)
- char *input_str;
-{
- char *ptr;
- int error_flag = 0;
- int length = strlen(input_str);
-
- if (index(input_str, ':')) {
- printf("':' is not allowed.\n");
- error_flag = 1;
- }
- if (input_str[length-1] != '\n') {
- /* the newline and the '\0' eat up two characters */
- printf("Maximum number of characters allowed is %d\n",
- BUFSIZ-2);
- /* flush the rest of the input line */
- while (getchar() != '\n')
- /* void */;
- error_flag = 1;
- }
- /*
- * Delete newline by shortening string by 1.
- */
- input_str[length-1] = '\0';
- /*
- * Don't allow control characters, etc in input string.
- */
- for (ptr=input_str; *ptr != '\0'; ptr++) {
- if ((int) *ptr < 040) {
- printf("Control characters are not allowed.\n");
- error_flag = 1;
- break;
- }
- }
- return (error_flag);
-}
-
-/*
- * Removes '-'s from the input string.
- */
-remove_hyphens(str)
- char *str;
-{
- char *hyphen;
-
- while ((hyphen = index(str, '-')) != NULL)
- (void) strcpy(hyphen, hyphen+1);
-}
-
-/*
- * Checks to see if 'str' contains only digits (0-9). If not, then
- * an error message is printed and '1' is returned.
- */
-not_all_digits(str)
- char *str;
-{
- char *ptr;
-
- for (ptr = str; *ptr != '\0'; ++ptr)
- if (!isdigit(*ptr)) {
- printf("Phone numbers can only contain digits.\n");
- return (1);
- }
- return (0);
-}
-
-/*
- * Deal with Berkeley buildings. Abbreviating Cory to C and Evans to E.
- * Correction changes "str".
- *
- * Returns 1 if incorrect room format.
- *
- * Note: this function assumes that the newline has been removed from str.
- */
-illegal_building(str)
- register char *str;
-{
- int length = strlen(str);
- register char *ptr;
-
- /*
- * If the string is [Ee]vans or [Cc]ory or ends in
- * [ \t0-9][Ee]vans or [ \t0-9M][Cc]ory, then contract the name
- * into 'E' or 'C', as the case may be, and delete leading blanks.
- */
- if (length >= 5 && strcmp(ptr = str + length - 4, "vans") == 0 &&
- (*--ptr == 'e' || *ptr == 'E') &&
- (--ptr < str || isspace(*ptr) || isdigit(*ptr))) {
- for (; ptr > str && isspace(*ptr); ptr--)
- ;
- ptr++;
- *ptr++ = 'E';
- *ptr = '\0';
- } else
- if (length >= 4 && strcmp(ptr = str + length - 3, "ory") == 0 &&
- (*--ptr == 'c' || *ptr == 'C') &&
- (--ptr < str || *ptr == 'M' || isspace(*ptr) || isdigit(*ptr))) {
- for (; ptr > str && isspace(*ptr); ptr--)
- ;
- ptr++;
- *ptr++ = 'C';
- *ptr = '\0';
- }
- return (0);
-}
-
-/*
- * get_defaults picks apart "str" and returns a structure points.
- * "str" contains up to 4 fields separated by commas.
- * Any field that is missing is set to blank.
- */
-struct default_values *
-get_defaults(str)
- char *str;
-{
- struct default_values *answer;
-
- answer = (struct default_values *)
- malloc((unsigned)sizeof(struct default_values));
- if (answer == (struct default_values *) NULL) {
- fprintf(stderr,
- "\nUnable to allocate storage in get_defaults!\n");
- exit(1);
- }
- /*
- * Values if no corresponding string in "str".
- */
- answer->name = str;
- answer->office_num = "";
- answer->office_phone = "";
- answer->home_phone = "";
- str = index(answer->name, ',');
- if (str == 0)
- return (answer);
- *str = '\0';
- answer->office_num = str + 1;
- str = index(answer->office_num, ',');
- if (str == 0)
- return (answer);
- *str = '\0';
- answer->office_phone = str + 1;
- str = index(answer->office_phone, ',');
- if (str == 0)
- return (answer);
- *str = '\0';
- answer->home_phone = str + 1;
- return (answer);
-}
-
-/*
- * special_case returns true when either the default is accepted
- * (str = '\n'), or when 'none' is typed. 'none' is accepted in
- * either upper or lower case (or any combination). 'str' is modified
- * in these two cases.
- */
-special_case(str,default_str)
- char *str, *default_str;
-{
- static char word[] = "none\n";
- char *ptr, *wordptr;
-
- /*
- * If the default is accepted, then change the old string do the
- * default string.
- */
- if (*str == '\n') {
- (void) strcpy(str, default_str);
- return (1);
- }
- /*
- * Check to see if str is 'none'. (It is questionable if case
- * insensitivity is worth the hair).
- */
- wordptr = word-1;
- for (ptr = str; *ptr != '\0'; ++ptr) {
- ++wordptr;
- if (*wordptr == '\0') /* then words are different sizes */
- return (0);
- if (*ptr == *wordptr)
- continue;
- if (isupper(*ptr) && (tolower(*ptr) == *wordptr))
- continue;
- /*
- * At this point we have a mismatch, so we return
- */
- return (0);
- }
- /*
- * Make sure that words are the same length.
- */
- if (*(wordptr+1) != '\0')
- return (0);
- /*
- * Change 'str' to be the null string
- */
- *str = '\0';
- return (1);