home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************************
- * strings.c: Miscellaneous string functions.
- *
- * Part of...
- *
- * FREE: Display free space on your disk volumes.
- * Author: Daniel Jay Barrett, barrett@cs.jhu.edu.
- *
- * This program is Freely Distributable. Make all the copies you want
- * and give them away. Use this code in any way you like.
- ***************************************************************************/
-
- #include "free.h"
-
- /* Compare two strings, ignoring the difference between upper/lower case.
- * If the strings are the same, return 0; else, return 1. */
-
- int StrCaseCmp(char *s1, char *s2)
- {
- while (*s1 && *s2)
- {
- if (toupper(*s1) != toupper(*s2))
- return(1); /* Different. */
- s1++, s2++;
- }
- return(0); /* Same. */
- }
-
-
- /* This is a safe method for concatenating string "src" onto the end of
- * string "dest". If dest does not have enough room to fit "src", an
- * error routine is called, and FALSE is returned. Otherwise, the
- * concatenation takes place, and TRUE is returned.
- *
- * The flag "ok" is static. When one error sets "ok" to FALSE, we never
- * need to run this function anymore. */
-
- int Concat(char dest[], char src[])
- {
- static int ok = TRUE;
- int destLen;
-
- if (ok)
- {
- destLen = strlen(dest);
- if (destLen + strlen(src) < memSize) /* memSize global. */
- strcat(dest, src);
- else
- {
- ErrorMsg(ERROR_TOO_SMALL);
- if (destLen > 0)
- dest[destLen-1] = '\n';
- else
- dest[0] = '\0';
- ok = FALSE;
- }
- }
- }
-
-
- /* Create an array of size memSize and return a pointer to it. We also
- * copy the empty string into the array, in preparation for later
- * strcat() calls. */
-
- char *MakeOutArray()
- {
- char *arr = NULL;
-
- /* Note that memSize is global, and set by GetOptions. */
-
- if ((memSize <= 0) || !(arr = (char *)malloc(memSize)))
- ExitCleanly((char *)NULL, ERROR_CANT_MALLOC, RETURN_FAIL);
- else
- {
- strcpy(arr, "");
- return(arr);
- }
- }
-
-
- /* We turn "format[]" into a format string suitable for printf().
- * We create this format string because we want proper indenting of
- * all our output. The key value to watch is the %d value, which is
- * calculated using the length of the volume name and the default spacing.
- * We set "d_or_s" to be 'd' or 's', depending on whether we want a
- * decimal field or a string field to be last in the format string.
- * We pass "cr" as TRUE if we want a carriage return (really a newline)
- * at the end of the format string. */
-
- void MakeFormatString(char *volume, char format[], char d_or_s, int cr)
- {
- char metaFormat[FORMAT_LENGTH];
-
- if (d_or_s == 'd')
- strcpy(metaFormat, "%%s%%s%%s%%%dld%s");
- else if (d_or_s == 's')
- strcpy(metaFormat, "%%s%%s%%s%%%ds%s");
- else
- ErrorMsg(ERROR_IMPOSSIBLE);
-
- /* volumeNameLen is global and set in InitializeGlobals() or
- * GetOptions(). */
-
- sprintf(format, metaFormat,
- DEFAULT_SPACING + volumeNameLen - strlen(volume),
- cr ? "\n" : "");
- }
-
-
-