1: /*	ccreat.c	1.10	85/03/24	*/
   2: 
   3: #include <stdio.h>
   4: #include <ctype.h>
   5: #include "cpmio.h"
   6: #include "cpmfio.h"
   7: 
   8: /*
   9:  * Create cp/m file with the given file name and extension, return
  10:  * file pointer. A null pointer is returned if the file could not
  11:  * be created or if the file already exists.
  12:  */
  13: 
  14: C_FILE *
  15: c_creat(name, ext, flag)
  16:     char *name, *ext;
  17: {
  18: 
  19:     register int    i, index;
  20:     register C_FILE *fptr;
  21:     char *malloc(), alloc(), *fixname();
  22: 
  23:     if (searchdir(name, ext) != -1) {
  24:         fprintf(stderr, "file already exists: %s\n",
  25:             fixname(name, ext));
  26:         return(NULL);
  27:     }
  28:     if (checkname(name) || checkname(ext)) {
  29:         fprintf(stderr, "illegal character in file name: %s\n",
  30:             fixname(name, ext));
  31:         return(NULL);
  32:     }
  33:     if ((index = creext(-1)) < NULL) {
  34:         fprintf(stderr, "c_creat: no directory space\n");
  35:         return (NULL);
  36:     }
  37: #ifdef DEBUG
  38:     printf("directory index: %d\n", index);
  39: #endif
  40: 
  41:     /* find free slot for file descriptor */
  42:     for ((i = 0, fptr=c_iob); i < C_NFILE; i++,fptr++) {
  43:         if (!(fptr->c_flag))
  44:             break;
  45:     }
  46:     if (i == C_NFILE) {
  47:         fprintf(stderr,"c_creat: too many open files\n");
  48:         return (NULL);
  49:     }
  50: 
  51:     /*
  52: 	 * Free file descriptor slot found, initialize and allocate buffer
  53: 	 * memory
  54: 	 */
  55:     if ((fptr->c_buf=malloc(blksiz)) == NULL) {
  56:         fprintf(stderr, "c_creat: no memory!\n");
  57:         return (NULL);
  58:     }
  59:     fptr->c_dirp = dirbuf+index;
  60:     if ((fptr->c_dirp->pointers[0] = alloc()) == '\0') {
  61:         fprintf(stderr, "c_creat: disk full\n");
  62:         return (NULL);
  63:     }
  64:     fptr->c_dirp->status = '\0';
  65:     fptr->c_dirp->extno  = '\0';
  66:     fptr->c_dirp->notused[0]  = '\0';
  67:     fptr->c_dirp->notused[1]  = '\0';
  68:     strncpy(fptr->c_dirp->name, name, 8);
  69:     strncpy(fptr->c_dirp->ext, ext, 3);
  70:     fptr->c_dirp->blkcnt = '\0';
  71:     fptr->c_blk = 0;
  72:     fptr->c_base = fptr->c_buf;
  73:     fptr->c_flag = WRITE | flag;
  74:     fptr->c_ext = index;
  75:     fptr->c_seccnt = 0;
  76:     fptr->c_cnt = blksiz;
  77:     fptr->c_extno = 0;
  78:     return (fptr);
  79: }
  80: 
  81: /*
  82:  * checkname: check for illegal characters in file names,
  83:  * cp/m allows only alphanumeric characters in filenames,
  84:  * no underscores, or other special characters....
  85:  */
  86: 
  87: static
  88: checkname(s)
  89:     char *s;
  90: {
  91: 
  92:     while (*s) {
  93:         if (!isalpha(*s) && !isdigit(*s) && (*s != ' '))
  94:             return(1);
  95:         s++;
  96:     }
  97:     return(0);
  98: }
  99: 
 100: static char nmbuf[15];
 101: 
 102: char *
 103: fixname(name, ext)
 104:     char *name, *ext;
 105: {
 106: 
 107:     char *p, *index();
 108: 
 109:     nmbuf[8] = '\0';
 110:     nmbuf[12] = '\0';
 111:     strncpy(nmbuf, name, 8);
 112:     if ((p = index(nmbuf, ' ')) == NULL)
 113:         p = nmbuf+8;
 114:     if (*ext != ' ') {
 115:         *p++ = '.';
 116:         strncpy(p, ext, 3);
 117:         if (p = index(p, ' '))
 118:             *p = '\0';
 119:     } else
 120:         *p = '\0';
 121:     return(nmbuf);
 122: }

Defined functions

c_creat defined in line 14; used 2 times
checkname defined in line 87; used 2 times
  • in line 28(2)
fixname defined in line 102; used 15 times

Defined variables

nmbuf defined in line 100; used 6 times
Last modified: 1985-03-25
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 649
Valid CSS Valid XHTML 1.0 Strict