[f40a309] | 1 | /*
|
---|
| 2 | =============================================================================
|
---|
| 3 | filname.c -- file name and extension extractors
|
---|
| 4 | Version 3 -- 1987-07-09 -- D.N. Lynx Crowe
|
---|
| 5 | =============================================================================
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #define TESTER 0 /* define non-zero for test routine */
|
---|
| 9 |
|
---|
| 10 | #include <stddefs.h>
|
---|
| 11 |
|
---|
| 12 | #define MAX_NAME 8 /* maximum length of a file name */
|
---|
| 13 | #define MAX_EXT 3 /* maximum length of an extension */
|
---|
| 14 |
|
---|
| 15 | /*
|
---|
| 16 | =============================================================================
|
---|
| 17 | FilName(s, p) -- Return a pointer to a string containing the file name
|
---|
| 18 | found in the string at 's'. Copy the file name into string 'p'.
|
---|
| 19 | =============================================================================
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | char *
|
---|
| 23 | FilName(s, p)
|
---|
| 24 | register char *s, *p;
|
---|
| 25 | {
|
---|
| 26 | register char *tp;
|
---|
| 27 | register int i;
|
---|
| 28 |
|
---|
| 29 | tp = p;
|
---|
| 30 |
|
---|
| 31 | for (i = 0; i < MAX_NAME; i++) { /* scan the name */
|
---|
| 32 |
|
---|
| 33 | if (*s) { /* ... until we hit a '\0' */
|
---|
| 34 |
|
---|
| 35 | if (*s EQ '.') /* ... or a '.' */
|
---|
| 36 | break;
|
---|
| 37 |
|
---|
| 38 | *p++ = *s++; /* ... copying as we go */
|
---|
| 39 |
|
---|
| 40 | } else { /* stop at '\0' */
|
---|
| 41 |
|
---|
| 42 | *p = '\0'; /* terminate the string */
|
---|
| 43 | return(tp); /* return a pointer to it */
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | *p = '\0'; /* copied MAX_NAME bytes - that's all folks */
|
---|
| 48 | return(tp);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | /* |
---|
| 52 |
|
---|
| 53 | */
|
---|
| 54 |
|
---|
| 55 | /*
|
---|
| 56 | =============================================================================
|
---|
| 57 | FilExt(s, p) -- Returns a pointer to a copy of the extension of
|
---|
| 58 | the file name in 's'. The file name is copied into string 'p' and
|
---|
| 59 | a pointer to 'p' is returned.
|
---|
| 60 | =============================================================================
|
---|
| 61 | */
|
---|
| 62 |
|
---|
| 63 | char *
|
---|
| 64 | FilExt(s, p)
|
---|
| 65 | register char *s, *p;
|
---|
| 66 | {
|
---|
| 67 | register char c, *tp;
|
---|
| 68 | register int i;
|
---|
| 69 |
|
---|
| 70 | tp = p;
|
---|
| 71 |
|
---|
| 72 | while (c = *s) { /* scan the string */
|
---|
| 73 |
|
---|
| 74 | if (c EQ '.') { /* ... until we hit the dot */
|
---|
| 75 |
|
---|
| 76 | ++s; /* point past the dot */
|
---|
| 77 |
|
---|
| 78 | for (i = 0; i < MAX_EXT; i++) { /* scan the extension ... */
|
---|
| 79 |
|
---|
| 80 | if (*s) { /* ... until we hit a '\0' */
|
---|
| 81 |
|
---|
| 82 | *p++ = *s++; /* ... copying as we go */
|
---|
| 83 |
|
---|
| 84 | } else { /* stop at '\0' */
|
---|
| 85 |
|
---|
| 86 | *p = '\0'; /* terminate the string */
|
---|
| 87 | return(tp); /* return a pointer to it */
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | *p = '\0'; /* copied MAX_EXT bytes - that's all folks */
|
---|
| 92 | return(tp); /* return a pointer to the result */
|
---|
| 93 |
|
---|
| 94 | } else {
|
---|
| 95 |
|
---|
| 96 | ++s; /* advance the pointer */
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | *p = '\0'; /* terminate the string */
|
---|
| 101 | return(tp); /* return a pointer to the result */
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /* |
---|
| 105 |
|
---|
| 106 | */
|
---|
| 107 |
|
---|
| 108 | #if TESTER
|
---|
| 109 |
|
---|
| 110 | char *fn[] = { /* test cases */
|
---|
| 111 |
|
---|
| 112 | "FILE.NAM",
|
---|
| 113 | "FILE",
|
---|
| 114 | "FILE.",
|
---|
| 115 | ".NAM",
|
---|
| 116 | ".",
|
---|
| 117 | "",
|
---|
| 118 | "fartoolonganame.longextension",
|
---|
| 119 | "fartoolonganame",
|
---|
| 120 | "fartoolonganame.",
|
---|
| 121 | ".longextension"
|
---|
| 122 | };
|
---|
| 123 |
|
---|
| 124 | #define NCASES ((sizeof fn) / (sizeof (char *)))
|
---|
| 125 |
|
---|
| 126 | char temp1[MAX_NAME+1], temp2[MAX_EXT+1];
|
---|
| 127 |
|
---|
| 128 | fnt(s) /* test both functions and print the result */
|
---|
| 129 | char *s;
|
---|
| 130 | {
|
---|
| 131 | printf("[%s] gave [%s] [%s]\r\n",
|
---|
| 132 | s, FilName(s, temp1), FilExt(s, temp2));
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | main()
|
---|
| 136 | {
|
---|
| 137 | int i;
|
---|
| 138 |
|
---|
| 139 | /* hand the functions each of the test cases */
|
---|
| 140 |
|
---|
| 141 | for (i = 0; i < NCASES; i++)
|
---|
| 142 | fnt(fn[i]);
|
---|
| 143 |
|
---|
| 144 | exit(0);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | #endif
|
---|