Message-Id: <3.0.1.32.19971222163207.007e6620@yacker.xiotech.com> Date: Mon, 22 Dec 1997 16:32:07 -0600 To: djgpp-workers AT delorie DOT com From: Randy Maas Subject: mntent patch - c file Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=====================_882851527==_" Precedence: bulk --=====================_882851527==_ Content-Type: text/plain; charset="us-ascii" Here's a "not so small" patch to mntent.c It makes mntent open and scan a file, just like unixy mntents do. But it does it *last* after all of the DOS mounts have been examined. Since there is no standard place for the mount file, and /etc may (or may not) be present I also modified how the file name is chosen. The macro MNT_MNTTAB now refers to a routine that checks for an environment variable (MNTTAB) that would have the preferred file path. If it can't find the environment variable, it uses the DEFAULT_MNT_MNTTAB macro ("/etc/mnttab"). For consistency I propose that the following line be added to the djgpp.env file: +MNTTAB=%DJDIR%/etc/mnttab And a etc/ directory added to the standard tree. That said, the question of *why* I'd want this. Partly because it makes djgpp a little more unixy. Mostly because I'd rather use the mntent family of functions in an add-on library for mount'ing and automounting a series of loadable fsext modules. Randy randym AT acm DOT org --=====================_882851527==_ Content-Type: text/plain; charset="us-ascii" Content-Disposition: attachment; filename="mntent.dif" *** src/libc/compat/mntent/mntent.c~1 Mon Aug 12 23:14:46 1996 --- src/libc/compat/mntent/mntent.c Mon Nov 24 17:12:10 1997 *************** *** 5,8 **** --- 5,9 ---- * * Copyright (c) 1995-96 Eli Zaretskii + * Modified 1997 Randall Maas * * This software may be used freely so long as this copyright notice is *************** *** 27,30 **** --- 28,45 ---- * and Ralf Brown's Interrupt List. * + * Notes + * The entries stored in the psuedo /etc/mnttab (/etc/mtab, or whatever) will + * be examined after every other MS-DOS entry has been examined. + * The file name for the psuedo /etc/mnttab defaults to "/etc/mnttab", but + * is overridden by the environment variable "MNTTAB". I recommend adding + * +MNTTAB=%DJDIR%/etc/mnttab + * to the djgpp section of djgpp.env + * + * If the passed file does not exist (or fopen fails for some other reason) + * the working FILE* is set 1 (the same value as before rcm tinkered with + * it) + * addmntent always appends to the end of the file, but returns to where it + * was before. + * endmntent doesn't really seem to do anything. Should it? */ #include *************** *** 43,46 **** --- 58,62 ---- #include #include + #include /* Macro to convert a segment and an offset to a "far offset" suitable *************** *** 70,73 **** --- 86,90 ---- static unsigned char mnt_fsname[128]; static char dev_opts[] = "r ,dev= "; + static unsigned char mnt_opts[128]; /* for scanning from a file */ static char NAME_dblsp[] = "dblsp"; *************** *** 82,85 **** --- 99,109 ---- static char NAME_join[] = "join"; static char NAME_unknown[] = "???"; + static char NAME_other[128]; /* for scanning from a file */ + + static char* _mnttab_filename = NULL; + /* A helper array */ + static char* NAMEs[] = {NAME_dblsp, NAME_stac, NAME_jam, NAME_ram, NAME_cdrom, + NAME_net, NAME_fd, NAME_hd, NAME_subst, NAME_join, NAME_unknown}; + #define Num_NAMEs (sizeof(NAMEs)/sizeof(NAMEs[0])) int _is_remote_drive(int); *************** *** 445,448 **** --- 469,479 ---- /* Exported library functions. */ + char* + __mnt_mnttab(void) + { + /* This is a little helper routine to find the mount table */ + if (!_mnttab_filename) _mnttab_filename = getenv("MNTTAB"); + return _mnttab_filename ? _mnttab_filename : DEFAULT_MNT_MNTTAB; + } FILE * *************** *** 453,456 **** --- 484,488 ---- /* Need TRUE DOS version. */ unsigned short true_dos_version = _get_dos_version(1); + FILE* filep = NULL; dos_mem_base = _go32_info_block.selector_for_linear_memory; *************** *** 497,501 **** } ! return (FILE *) 1; } --- 529,535 ---- } ! /* Try to open the mount table... return 1 if we can't */ ! filep = fopen(filename, type); ! return filep ? filep : (FILE *) 1; } *************** *** 503,509 **** getmntent(FILE *filep) { if (drive_number == -1) return NULL; ! if (filep != (FILE *)1) { errno = EBADF; /* fake errno for invalid handle */ --- 537,545 ---- getmntent(FILE *filep) { + /* Modified 1997/11/17 (rcm) to be able to scan a line from a real file + *after* all attempts have been made to looks at the internal DOS mounts*/ if (drive_number == -1) return NULL; ! if (filep == NULL) { errno = EBADF; /* fake errno for invalid handle */ *************** *** 785,795 **** /* Go try next drive, if any left. */ } ! return NULL; } int addmntent(FILE *filep, const struct mntent *mnt) { return 1; } --- 821,909 ---- /* Go try next drive, if any left. */ } ! ! if (filep && filep != (FILE*) 1) ! { ! /* We point to a real file. */ ! char *tmp, *buff=alloca(1024), **bp=&buff; ! if (feof(filep)) return NULL; ! ! /* Try to scan it, use strseps instead of fscanf due to fields being ! optional (and whitespace confusing scanf). */ ! if (!fgets(buff, 1023, filep)) return NULL; ! mnt_fsname[0]=0; ! tmp= strsep(bp, " "); ! if (!bp || !tmp) return NULL; ! strncat(mnt_fsname, tmp, 127); ! ! mnt_dir[0]=0; ! tmp= strsep(bp, " "); ! if (!bp || !tmp) return NULL; ! strncat(mnt_dir, tmp, 127); ! ! NAME_other[0]=0; ! tmp = strsep(bp, " "); ! if (!bp || !tmp) return NULL; ! strncat(NAME_other, tmp, 127); ! ! mnt_opts[0]=0; ! tmp = strsep(bp, " "); ! if (!bp || !tmp) return NULL; ! strncat(mnt_opts, tmp, 127); ! ! tmp=strsep(bp, " "); ! if (!bp || !tmp) return NULL; ! mntent.mnt_passno=atoi(tmp); ! ! tmp=strsep(bp, " "); ! if (!bp || !tmp) return NULL; ! mntent.mnt_time=atol(tmp); ! ! /* Fill in the structure */ ! mntent.mnt_fsname = mnt_fsname; ! mntent.mnt_dir = mnt_dir; ! mntent.mnt_type = NAME_other; ! mntent.mnt_opts = mnt_opts; ! ! return &mntent; ! } ! return NULL; } + /* Print nothing out if the string is null */ + #define safe_str(A) ((A)?(A):"") + int addmntent(FILE *filep, const struct mntent *mnt) { + /* Modified 1997/11/17 (rcm) to add stuff to the mount table + returns 0 on success, 1 on failure + */ + if (filep && filep != (FILE*) 1) + { + /* Possibly add stuff to mount table. But check that that the type is + not one of the magic types scanned for above. */ + long here; + unsigned I; + + for (I = 0; I < Num_NAMEs; I++) + if (strcmp(NAMEs[I], mnt->mnt_type) == 0) return 1; + + /* Add the entry to the end of the mount table, but preserve current + possition */ + here = ftell(filep); + fflush(filep); + fseek(filep, 0, SEEK_END); + fprintf(filep, "%s %s %s %s %d %ld\n", safe_str(mnt->mnt_fsname), + safe_str(mnt->mnt_dir), + safe_str(mnt->mnt_type), + safe_str(mnt->mnt_opts), + mnt->mnt_passno, mnt->mnt_time); + + /* return us back to where we were */ + fflush(filep); + fseek(filep, here, SEEK_SET); + return 0; + } return 1; } *************** *** 804,811 **** endmntent(FILE *filep) { if (filep != (FILE *)1) { errno = EBADF; /* fake errno for invalid handle */ ! return NULL; } drive_number = 0; --- 918,932 ---- endmntent(FILE *filep) { + /* Modified 1997/11/17 (rcm) to return 1 if filep points to a real file */ if (filep != (FILE *)1) { + if (filep) + { + /* points to a real file */ + fclose(filep); + return 1; + } errno = EBADF; /* fake errno for invalid handle */ ! return 0; } drive_number = 0; --=====================_882851527==_ Content-Type: text/plain; charset="us-ascii" --=====================_882851527==_--