allocate a file descriptor */
| 59 | allocate a file descriptor |
| 60 | */ |
| 61 | static int new_file_descriptor(const char *pathname) |
| 62 | { |
| 63 | int i; |
| 64 | FAT_FILE *stream; |
| 65 | FIL *fh; |
| 66 | |
| 67 | for (i=0; i<MAX_FILES; ++i) { |
| 68 | if (isatty_(i)) { |
| 69 | continue; |
| 70 | } |
| 71 | if (file_table[i] == NULL) { |
| 72 | stream = (FAT_FILE *) calloc(sizeof(FAT_FILE),1); |
| 73 | if (stream == NULL) { |
| 74 | errno = ENOMEM; |
| 75 | return -1; |
| 76 | } |
| 77 | fh = (FIL *) calloc(sizeof(FIL),1); |
| 78 | if (fh == NULL) { |
| 79 | free(stream); |
| 80 | errno = ENOMEM; |
| 81 | return -1; |
| 82 | } |
| 83 | char *fname = (char *)malloc(strlen(pathname)+1); |
| 84 | if (fname == NULL) { |
| 85 | free(fh); |
| 86 | free(stream); |
| 87 | errno = ENOMEM; |
| 88 | return -1; |
| 89 | } |
| 90 | strcpy(fname, pathname); |
| 91 | stream->name = fname; |
| 92 | |
| 93 | file_table[i] = stream; |
| 94 | stream->fh = fh; |
| 95 | return i; |
| 96 | } |
| 97 | } |
| 98 | errno = ENFILE; |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | static FAT_FILE *fileno_to_stream(int fileno) |
| 103 | { |