home *** CD-ROM | disk | FTP | other *** search
- /* This source file is part of the LynxLib miscellaneous library by
- Robert Fischer, and is Copyright 1990 by Robert Fischer. It costs no
- money, and you may not make money off of it, but you may redistribute
- it. It comes with ABSOLUTELY NO WARRANTY. See the file LYNXLIB.DOC
- for more details.
- To contact the author:
- Robert Fischer \\80 Killdeer Rd \\Hamden, CT 06517 USA
- (203) 288-9599 fischer-robert@cs.yale.edu */
-
- /* volume.c */
- /* Change volume name */
-
- #include <stat.h>
- #include <osbind.h>
- #include <errno.h>
-
- /* Gemdos date-word format:
- * ------------------------------
- * | year (7) | mo (4) | day (5) |
- * ------------------------------
- * 15........9 8......5 4.......0
- */
-
- /* Misc. constants */
- #define PATH_LENGTH 65
- #define TOS1_4 0x0104 /* Version number of TOS 1.4 */
- #define AUG_08_88 ((8<<9) | (8<<5) | 8) /* Release date of developer's
- RAM TOS 1.4 */
-
- /* Directory mode bit combinations */
- #define FILE_MODE 0x0
- #define S_IJALL 0x37
-
- /* User-defined error */
- #define NAME_EXISTS (-257L)
-
- /* Flags */
- #define SET_ATTRIB 1
-
- /* Function headers */
- extern void whichtos();
-
- /*--------------------------------------------------------------------
- * d_search
- * Searches directory for name matching pattern and mode
- * Copies matching named string if successful
- * Returns AE_OK if successful; AEFILNF otherwise
- *--------------------------------------------------------------------*/
- int d_search(pattern, mode, label)
- char *pattern;
- int mode;
- char *label;
- {
- DMABUFFER dma;
- register DMABUFFER *olddmap;
- long retval;
-
- #if 0
- Cconws("d_search called with:\r\n Pattern = ");
- Cconws(pattern);
- Cconws("\r\n Label = ");
- Cconws(label);
- #endif
- /* Search directory */
- olddmap = (DMABUFFER *) Fgetdta();
- Fsetdta(&dma);
- if ((retval = Fsfirst(pattern, mode)) == AE_OK) { /* found */
- strcpy(label, dma.d_fname);
- }
- else { /* not found */
- *label = '\0';
- }
- Fsetdta(olddmap);
- #if 0
- Cconws("\r\n\r\n Label = ");
- Cconws(label);
- Cconws("\r\nd_search returning ");
- Cconws((retval == AE_OK ? "Success\r\n" : "Failure\r\n"));
- #endif
- return retval;
- }
- /*--------------------------------------------------------------------
- * get_vname
- * Gets volume name
- * Returns pointer label string
- *--------------------------------------------------------------------*/
- int get_vname(drvnum, label)
- int drvnum;
- char *label;
- {
- char path[PATH_LENGTH];
-
- /* Prepare search string */
- strcpy(path, "a:\\*.*");
- path[0] = (char) drvnum + 'a';
-
- /* Search directory */
- return d_search(path, S_IJVOL, label);
- }
- /*--------------------------------------------------------------------
- * set_vname
- * Replaces old volume name(s) with new one
- * Deletes volume name if newname is empty string (old TOS only)
- * Returns TOS error code in case of trouble
- *--------------------------------------------------------------------*/
- int set_vname(drvnum, newname)
- int drvnum;
- char *newname;
- {
- char path[PATH_LENGTH]; /* path buffer */
- char dummy[PATH_LENGTH];
- unsigned tosver; /* TOS version number (2 bytes) */
- unsigned tosdate; /* TOS release date in Gemdos
- date-word format */
- register char *leafptr; /* pointer into path buffer */
- register int fd; /* file descriptor */
- register long err; /* error value */
-
- /* Set up path buffer with <drive letter>:\ */
- strcpy(path, "a:\\");
- path[0] += drvnum;
- leafptr = &path[3];
-
- whichtos(&tosver, &tosdate);
- if (tosver < TOS1_4 ||
- (tosver == TOS1_4 && tosdate == AUG_08_88)) {
-
- /* --------------------------------------------------------------- */
- /* Create new volume label -- TOS prior to official release of 1.4 */
- /* --------------------------------------------------------------- */
- /* Make sure new name does not already exist as a file or directory */
- if (*newname != '\0') {
- strcpy(leafptr, newname);
- if (d_search(path, S_IJALL, dummy) == AE_OK) return NAME_EXISTS;
- }
-
- /* Delete any old volume labels */
- while (get_vname(drvnum, leafptr), *leafptr != '\0') {
- if ((fd = err = Fcreate(path, FILE_MODE)) < 0) return err;
- if ((err = Fclose(fd)) != AE_OK) return err;
- if ((err = Fdelete(path)) != AE_OK) return err;
- }
-
- /* Create new volume label */
- if (*newname != '\0') {
- strcpy(leafptr, newname);
- if ((fd = err = Fcreate(path, FILE_MODE)) < 0) return err;
- if ((err = Fclose(fd)) != AE_OK) return err;
- Fattrib(path, SET_ATTRIB, S_IJVOL);
- }
- return AE_OK;
- }
-
- else {
- /* ---------------------------------------------------- */
- /* Create new volume label -- TOS version 1.4 and after */
- /* ---------------------------------------------------- */
- if (*newname != '\0') strcpy(leafptr, newname);
- else strcpy(leafptr, "nolabel"); /* Can't delete, so set to default */
- if ((fd = err = Fcreate(path, S_IJVOL)) < 0) return err;
- err = Fclose(fd);
- return err;
- }
- }
- /* ------------------------------------------------------------- */
- #if 0
- main()
- {
- char label[20];
- force_dchange(0);
- get_vname(0, label + 1);
- printf("%s\n",label + 1);
- Cnecin();
- force_dchange(0);
- get_vname(0, label + 1);
- printf("%s\n",label + 1);
- }
- #endif
-