string.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* $Header$ */
  2. /* STRING MANIPULATION AND PRINT ROUTINES */
  3. #include "string.h"
  4. #include "nopp.h"
  5. #include "str_params.h"
  6. #include "arith.h"
  7. #include "system.h"
  8. doprnt(fd, fmt, argp)
  9. char *fmt;
  10. int argp[];
  11. {
  12. char buf[SSIZE];
  13. sys_write(fd, buf, format(buf, fmt, (char *)argp));
  14. }
  15. /*VARARGS1*/
  16. printf(fmt, args)
  17. char *fmt;
  18. char args;
  19. {
  20. char buf[SSIZE];
  21. sys_write(1, buf, format(buf, fmt, &args));
  22. }
  23. /*VARARGS1*/
  24. fprintf(fd, fmt, args)
  25. char *fmt;
  26. char args;
  27. {
  28. char buf[SSIZE];
  29. sys_write(fd, buf, format(buf, fmt, &args));
  30. }
  31. /*VARARGS1*/
  32. char *
  33. sprintf(buf, fmt, args)
  34. char *buf, *fmt;
  35. char args;
  36. {
  37. buf[format(buf, fmt, &args)] = '\0';
  38. return buf;
  39. }
  40. int
  41. format(buf, fmt, argp)
  42. char *buf, *fmt;
  43. char *argp;
  44. {
  45. register char *pf = fmt, *pa = argp;
  46. register char *pb = buf;
  47. while (*pf) {
  48. if (*pf == '%') {
  49. register width, base, pad, npad;
  50. char *arg;
  51. char cbuf[2];
  52. char *badformat = "<bad format>";
  53. /* get padder */
  54. if (*++pf == '0') {
  55. pad = '0';
  56. ++pf;
  57. }
  58. else
  59. pad = ' ';
  60. /* get width */
  61. width = 0;
  62. while (*pf >= '0' && *pf <= '9')
  63. width = 10 * width + *pf++ - '0';
  64. /* get text and move pa */
  65. if (*pf == 's') {
  66. arg = *(char **)pa;
  67. pa += sizeof(char *);
  68. }
  69. else
  70. if (*pf == 'c') {
  71. cbuf[0] = * (char *) pa;
  72. cbuf[1] = '\0';
  73. pa += sizeof(int);
  74. arg = &cbuf[0];
  75. }
  76. else
  77. if (*pf == 'l') {
  78. /* alignment ??? */
  79. if (base = integral(*++pf)) {
  80. arg = int_str(*(long *)pa, base);
  81. pa += sizeof(long);
  82. }
  83. else {
  84. pf--;
  85. arg = badformat;
  86. }
  87. }
  88. else
  89. if (base = integral(*pf)) {
  90. arg = int_str((long)*(int *)pa, base);
  91. pa += sizeof(int);
  92. }
  93. else
  94. if (*pf == '%')
  95. arg = "%";
  96. else
  97. arg = badformat;
  98. npad = width - strlen(arg);
  99. while (npad-- > 0)
  100. *pb++ = pad;
  101. while (*pb++ = *arg++);
  102. pb--;
  103. pf++;
  104. }
  105. else
  106. *pb++ = *pf++;
  107. }
  108. return pb - buf;
  109. }
  110. integral(c)
  111. {
  112. switch (c) {
  113. case 'b':
  114. return -2;
  115. case 'd':
  116. return 10;
  117. case 'o':
  118. return -8;
  119. case 'u':
  120. return -10;
  121. case 'x':
  122. return -16;
  123. }
  124. return 0;
  125. }
  126. /* Integer to String translator
  127. */
  128. char *
  129. int_str(val, base)
  130. register long val;
  131. register base;
  132. {
  133. /* int_str() is a very simple integer to string converter.
  134. base < 0 : unsigned.
  135. base must be an element of [-16,-2] V [2,16].
  136. */
  137. static char numbuf[MAXWIDTH];
  138. static char vec[] = "0123456789ABCDEF";
  139. register char *p = &numbuf[MAXWIDTH];
  140. int sign = (base > 0);
  141. *--p = '\0'; /* null-terminate string */
  142. if (val) {
  143. if (base > 0) {
  144. if (val < (arith)0) {
  145. if ((val = -val) < (arith)0)
  146. goto overflow;
  147. }
  148. else
  149. sign = 0;
  150. }
  151. else
  152. if (base < 0) { /* unsigned */
  153. base = -base;
  154. if (val < (arith)0) {
  155. register mod, i;
  156. overflow:
  157. /* this takes a rainy Sunday afternoon to explain */
  158. /* ??? */
  159. mod = 0;
  160. for (i = 0; i < 8 * sizeof val; i++) {
  161. mod <<= 1;
  162. if (val < 0)
  163. mod++;
  164. val <<= 1;
  165. if (mod >= base) {
  166. mod -= base;
  167. val++;
  168. }
  169. }
  170. *--p = vec[mod];
  171. }
  172. }
  173. do {
  174. *--p = vec[(int) (val % base)];
  175. val /= base;
  176. } while (val != (arith)0);
  177. if (sign)
  178. *--p = '-'; /* don't forget it !! */
  179. }
  180. else
  181. *--p = '0'; /* just a simple 0 */
  182. return p;
  183. }
  184. /* return negative, zero or positive value if
  185. resp. s < t, s == t or s > t
  186. */
  187. int
  188. strcmp(s, t)
  189. register char *s, *t;
  190. {
  191. while (*s == *t++)
  192. if (*s++ == '\0')
  193. return 0;
  194. return *s - *--t;
  195. }
  196. /* return length of s
  197. */
  198. int
  199. strlen(s)
  200. char *s;
  201. {
  202. register char *b = s;
  203. while (*b++)
  204. ;
  205. return b - s - 1;
  206. }
  207. #ifndef NOPP
  208. /* append t to s
  209. */
  210. char *
  211. strcat(s, t)
  212. register char *s, *t;
  213. {
  214. register char *b = s;
  215. while (*s++)
  216. ;
  217. s--;
  218. while (*s++ = *t++)
  219. ;
  220. return b;
  221. }
  222. /* Copy t into s
  223. */
  224. char *
  225. strcpy(s, t)
  226. register char *s, *t;
  227. {
  228. register char *b = s;
  229. while (*s++ = *t++)
  230. ;
  231. return b;
  232. }
  233. char *
  234. rindex(str, chr)
  235. register char *str, chr;
  236. {
  237. register char *retptr = 0;
  238. while (*str)
  239. if (*str++ == chr)
  240. retptr = &str[-1];
  241. return retptr;
  242. }
  243. #endif NOPP