|
|
1.1 ! root 1: /* ! 2: * build0.c ! 3: * 07/11/92 ! 4: * Routines common to build and install. ! 5: */ ! 6: ! 7: #include <stdio.h> ! 8: #include <sys/stat.h> ! 9: #include "build0.h" ! 10: ! 11: /* Globals. */ ! 12: int abortmsg; /* print abort message */ ! 13: char *argv0; /* for error messages */ ! 14: char buf[NBUF]; /* input buffer */ ! 15: char cmd[NBUF]; /* command buffer */ ! 16: int dflag; /* debug */ ! 17: char *usagemsg; /* usage message */ ! 18: int vflag; /* verbose */ ! 19: ! 20: /* ! 21: * Clear the IBM AT console screen. ! 22: * Prompt for <Enter> if the flag is true or if dflag. ! 23: */ ! 24: void ! 25: cls(flag) register int flag; ! 26: { ! 27: if (flag || dflag) ! 28: get_line("\Hit <Enter> to continue..."); ! 29: putchar(0x1B); /* ESC */ ! 30: putchar('['); ! 31: putchar('2'); ! 32: putchar('J'); ! 33: fflush(stdout); ! 34: } ! 35: ! 36: /* ! 37: * Return 1 if file exists, 0 if not. ! 38: */ ! 39: int ! 40: exists(file) register char *file; ! 41: { ! 42: register int fd; ! 43: ! 44: if ((fd = open(file, 0)) < 0) ! 45: return 0; ! 46: close(fd); ! 47: return 1; ! 48: } ! 49: ! 50: /* ! 51: * Print a fatal error message. ! 52: * Print "Installation aborted..." message if invoked from build or install. ! 53: */ ! 54: void ! 55: fatal(s) char *s; ! 56: { ! 57: fprintf(stderr, "%s: %r\n", argv0, &s); ! 58: if (abortmsg) ! 59: fprintf(stderr, "Installation aborted before completion.\n"); ! 60: exit(1); ! 61: } ! 62: ! 63: /* ! 64: * Get an integer value in the given range. ! 65: */ ! 66: /* VARARGS */ ! 67: int ! 68: get_int(min, max, prompt) int min, max; char *prompt; ! 69: { ! 70: register char *s; ! 71: register int i; ! 72: ! 73: s = get_line("%r", &prompt); ! 74: for (;;) { ! 75: if (*s >= '0' && *s <= '9') { ! 76: i = atoi(s); ! 77: if (i >= min && i <= max) ! 78: return i; ! 79: } ! 80: get_line("Enter a value between %d and %d:", min, max); ! 81: } ! 82: } ! 83: ! 84: /* ! 85: * Print the args and get a line from the user to buf[]. ! 86: * Strip the trailing newline and return a pointer to the first non-space. ! 87: */ ! 88: /* VARARGS */ ! 89: char * ! 90: get_line(args) char *args; ! 91: { ! 92: register char *s; ! 93: ! 94: printf("%r ", &args); ! 95: fflush(stdout); ! 96: fgets(buf, sizeof buf, stdin); ! 97: buf[strlen(buf) - 1] = '\0'; ! 98: for (s = buf; ; ++s) ! 99: if (*s != ' ' && *s != '\t') ! 100: return s; ! 101: } ! 102: ! 103: /* ! 104: * Return: ! 105: * 1 dir exists and is a directory, ! 106: * 0 dir does not exist, ! 107: * or -1 dir exists but is not a directory. ! 108: */ ! 109: int ! 110: is_dir(dir) register char *dir; ! 111: { ! 112: struct stat s; ! 113: ! 114: if (stat(dir, &s) == -1) ! 115: return 0; /* does not exist */ ! 116: return (s.st_mode & S_IFDIR) ? 1 : -1; /* directory or nondirectory */ ! 117: } ! 118: ! 119: /* ! 120: * Print a nonfatal error message. ! 121: * Make sure the user sees it by waiting until he hits <Enter>. ! 122: */ ! 123: void ! 124: nonfatal(s) char *s; ! 125: { ! 126: fprintf(stderr, "%s: %r\n", argv0, &s); ! 127: get_line("\Hit <Enter> to continue..."); ! 128: } ! 129: ! 130: /* ! 131: * Execute the given command and return its exit status. ! 132: * The flag tells what to do if the command returns an error status: ! 133: * S_IGNORE ignore it ! 134: * S_NONFATAL report it ! 135: * S_FATAL report it and die ! 136: */ ! 137: int ! 138: sys(command, flag) char *command; int flag; ! 139: { ! 140: register int status; ! 141: ! 142: if (dflag || vflag) ! 143: printf("%s\n", command); ! 144: if (dflag) ! 145: return 0; ! 146: if ((status = system(command)) != 0) { ! 147: if (flag == S_NONFATAL) ! 148: nonfatal("command \"%s\" failed", command); ! 149: else if (flag == S_FATAL) ! 150: fatal("command \"%s\" failed", command); ! 151: } ! 152: sync(); ! 153: return status; ! 154: } ! 155: ! 156: /* ! 157: * Print a usage message and die. ! 158: */ ! 159: void ! 160: usage() ! 161: { ! 162: fprintf(stderr, usagemsg); ! 163: exit(1); ! 164: } ! 165: ! 166: /* ! 167: * Get the answer to a yes/no question. ! 168: * Return 1 for yes, 0 for no. ! 169: */ ! 170: /* VARARGS */ ! 171: int ! 172: yes_no(args) char *args; ! 173: { ! 174: register char *s; ! 175: ! 176: for (;;) { ! 177: printf("%r", &args); ! 178: s = get_line(" [y or n]?"); ! 179: if (*s == 'y' || *s == 'Y') ! 180: return 1; ! 181: else if (*s == 'n' || *s == 'N') ! 182: return 0; ! 183: } ! 184: } ! 185: ! 186: /* end of build0.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.