1: /* 2: * Focal, 1981. 3: * Operating system dependent code. 4: * Most has something to do with the syntax of 5: * a file name or the format of the directory of the 6: * disc; most of this is in the library command. 7: */ 8: #include "focal.h" 9: 10: #ifdef __STDC__ 11: #define unlink remove 12: #endif 13: 14: #ifdef DIR_SUPPORT 15: # ifdef DOS 16: # include 17: # else 18: # include 19: # include 20: # include 21: # include 22: # ifdef DIRENT 23: # include 24: # define direct dirent 25: # else 26: # include 27: # endif 28: struct stat sb; 29: struct direct db; 30: # endif 31: #endif 32: 33: /* 34: * Process the library command. 35: * This command (sadly) requires a rather 36: * detailed knowledge of the file system of 37: * the operating system. 38: */ 39: void library() 40: { 41: register char *p; 42: register int c; 43: register int d; 44: register char *sctp; 45: register struct line *lp1; 46: register struct line *lp2; 47: register FILE *fp; 48: 49: c = getnb(); 50: if (c!='c' && c!='s' && c!='l' && c!='d') 51: diag("Bad library command"); 52: while (isalpha(*ctp)) 53: ++ctp; 54: while ((d = *ctp)==' ' || d=='\t') 55: ++ctp; 56: if (c!='l' && d==0) 57: diag("Missing file name"); 58: p = ctp; 59: while (*ctp != 0) 60: ++ctp; 61: switch (c) { 62: 63: case 'c': 64: if ((fp=fopen(p, "r")) == NULL) 65: diag("Cannot open"); 66: lp1 = linev; 67: while (lp1 != NULL) { 68: lp2 = lp1->l_fp; 69: free ((char *) lp1); 70: lp1 = lp2; 71: } 72: linev = NULL; 73: sctp = ctp; 74: while (getline(abuf, fp) != 0) { 75: ctp = abuf; 76: if ((c=getnb()) != 0) { 77: if (isdigit(c) == 0) 78: diag("Direct line in call"); 79: inject(c); 80: } 81: } 82: fclose(fp); 83: ctp = sctp; 84: break; 85: 86: case 'd': 87: #ifdef vax 88: if (delete(p) < 0) 89: #else 90: if (unlink(p) < 0) 91: #endif 92: diag("Cannot delete"); 93: break; 94: 95: case 'l': 96: #ifndef DIR_SUPPORT 97: diag("Library list not implemented"); 98: #else 99: { 100: #ifdef DOS 101: int attr = _A_RDONLY | _A_ARCH; 102: int rc; 103: static struct find_t de; 104: static char fname[FILENAME_MAX]; 105: char *s; 106: 107: if (d == 0) 108: strcpy(fname, "*.*"); 109: else { 110: strcpy(fname, p); 111: s = fname + strlen(fname) - 1; 112: if (*s == '\\') 113: strcat(fname, "*.*"); 114: else if (*s == '.') 115: strcat(fname, "\\*.*"); 116: } 117: rc = _dos_findfirst(fname, attr, &de); 118: while (!rc) { 119: if ((de.attrib & _A_SUBDIR) == 0 120: && strcmp(de.name, ".") != 0 121: && strcmp(de.name, "..") != 0) 122: printf("%-13s\n", de.name); 123: rc = _dos_findnext(&de); 124: } 125: #else 126: #ifdef DIRENT 127: register struct dirent *de; 128: register DIR *dp; 129: 130: if (d == 0) 131: p = "."; 132: if ((dp = opendir(p)) == NULL) { 133: perror("focal"); 134: diag("Bad directory"); 135: } 136: while ((de = readdir(dp)) != NULL) { 137: if (de->d_ino == 0 138: || strcmp(de->d_name, ".") == 0 139: || strcmp(de->d_name, "..") == 0) 140: continue; 141: printf("%.*s\n", de->d_reclen, de->d_name); 142: } 143: closedir(dp); 144: #else 145: register int fd = 0; 146: 147: if (d == 0) 148: p = "."; 149: if (stat(p, &sb) < 0 150: || (sb.st_mode&S_IFMT) != S_IFDIR 151: || (fd = open(p, 0)) < 0) 152: diag("Bad directory"); 153: while (read(fd, &db, sizeof(db)) == sizeof(db)) { 154: if (db.d_ino == 0 155: || strncmp(db.d_name, ".", DIRSIZ) == 0 156: || strncmp(db.d_name, "..", DIRSIZ) == 0) 157: continue; 158: printf("%.*s\n", DIRSIZ, db.d_name); 159: } 160: close(fd); 161: #endif 162: #endif 163: } 164: #endif 165: break; 166: 167: case 's': 168: if ((fp=fopen(p, "w")) == NULL) 169: diag("Cannot create"); 170: save(NULL, fp); 171: fclose(fp); 172: break; 173: } 174: } 175: 176: /* 177: * Set up to catch the user's 178: * ^C interrupt. 179: */ 180: void catchcc() 181: { 182: signal(SIGINT, (void (*) ARG((int)))onintr); 183: } 184: 185: /* 186: * This routine is called by 187: * the ^C signal handler. All it does 188: * is set a flag, which is looked at 189: * by the dispatcher. 190: */ 191: void onintr() 192: { 193: ++intflag; 194: }