XINU
Functions
sprintf.c File Reference
#include <stdarg.h>
Include dependency graph for sprintf.c:

Go to the source code of this file.

Functions

void _fdoprnt (char *, va_list, int(*func)(int, int), int)
 
int sprintf (char *str, char *fmt,...)
 
static int sprntf (int, int)
 

Function Documentation

◆ _fdoprnt()

void _fdoprnt ( char *  ,
va_list  ,
int(*)(int, int)  func,
int   
)

Definition at line 20 of file fdoprnt.c.

References _prtl10(), _prtl16(), _prtl2(), _prtl8(), _prtX16(), MAXSTR, NULL, and va_arg.

Referenced by sprintf().

26 {
27  int c;
28  int i;
29  int f; /* The format character (comes after %) */
30  char *str; /* Running pointer in string */
31  char string[20]; /* The string str points to this output */
32 
33  /* from number conversion */
34  int length; /* Length of string "str" */
35  char fill; /* Fill character (' ' or '0') */
36  int leftjust; /* 0 = right-justified, else left-just */
37  int fmax, fmin; /* Field specifications % MIN . MAX s */
38  int leading; /* No. of leading/trailing fill chars */
39  char sign; /* Set to '-' for negative decimals */
40  char digit1; /* Offset to add to first numeric digit */
41  long larg;
42 
43  for (;;)
44  {
45  /* Echo characters until '%' or end of fmt string */
46  while ((c = *fmt++) != '%')
47  {
48  if (c == '\0')
49  {
50  return;
51  }
52  (*func) (farg, c);
53  }
54  /* Echo "...%%..." as '%' */
55  if (*fmt == '%')
56  {
57  (*func) (farg, *fmt++);
58  continue;
59  }
60  /* Check for "%-..." == Left-justified output */
61  if ((leftjust = ((*fmt == '-')) ? 1 : 0))
62  {
63  fmt++;
64  }
65  /* Allow for zero-filled numeric outputs ("%0...") */
66  fill = (*fmt == '0') ? *fmt++ : ' ';
67  /* Allow for minimum field width specifier for %d,u,x,o,c,s */
68  /* Also allow %* for variable width (%0* as well) */
69  fmin = 0;
70  if (*fmt == '*')
71  {
72  fmin = va_arg(ap, int);
73 
74  ++fmt;
75  }
76  else
77  {
78  while ('0' <= *fmt && *fmt <= '9')
79  {
80  fmin = fmin * 10 + *fmt++ - '0';
81  }
82  }
83  /* Allow for maximum string width for %s */
84  fmax = 0;
85  if (*fmt == '.')
86  {
87  if (*(++fmt) == '*')
88  {
89  fmax = va_arg(ap, int);
90  ++fmt;
91  }
92  else
93  {
94  while ('0' <= *fmt && *fmt <= '9')
95  {
96  fmax = fmax * 10 + *fmt++ - '0';
97  }
98  }
99  }
100 
101  str = string;
102  if ((f = *fmt++) == '\0')
103  {
104  (*func) (farg, '%');
105  return;
106  }
107  sign = '\0'; /* sign == '-' for negative decimal */
108 
109  switch (f)
110  {
111  case 'c':
112  string[0] = va_arg(ap, int);
113  string[1] = '\0';
114  fmax = 0;
115  fill = ' ';
116  break;
117 
118  case 's':
119  str = va_arg(ap, char *);
120 
121  if (NULL == str)
122  {
123  str = "(null)";
124  }
125  fill = ' ';
126  break;
127 
128  case 'd':
129  larg = va_arg(ap, long);
130 
131  if (larg < 0)
132  {
133  sign = '-';
134  larg = -larg;
135  }
136  _prtl10(larg, str);
137  break;
138 
139  case 'u':
140  digit1 = '\0';
141  /* "negative" longs in unsigned format */
142  /* can't be computed with long division */
143  /* convert *args to "positive", digit1 */
144  /* = how much to add back afterwards */
145  larg = va_arg(ap, long);
146 
147  while (larg < 0)
148  {
149  larg -= 1000000000L;
150  ++digit1;
151  }
152  _prtl10(larg, str);
153  str[0] += digit1;
154  fmax = 0;
155  break;
156 
157  case 'o':
158  larg = va_arg(ap, long);
159 
160  _prtl8(larg, str);
161  fmax = 0;
162  break;
163 
164  case 'X':
165  larg = va_arg(ap, long);
166 
167  _prtX16(larg, str);
168  fmax = 0;
169  break;
170 
171  case 'x':
172  larg = va_arg(ap, long);
173 
174  _prtl16(larg, str);
175  fmax = 0;
176  break;
177 
178  case 'b':
179  larg = va_arg(ap, long);
180 
181  _prtl2(larg, str);
182  fmax = 0;
183  break;
184 
185  default:
186  (*func) (farg, f);
187  break;
188  }
189  for (length = 0; str[length] != '\0'; length++)
190  {;
191  }
192  if (fmin > MAXSTR || fmin < 0)
193  {
194  fmin = 0;
195  }
196  if (fmax > MAXSTR || fmax < 0)
197  {
198  fmax = 0;
199  }
200  leading = 0;
201  if (fmax != 0 || fmin != 0)
202  {
203  if (fmax != 0)
204  {
205  if (length > fmax)
206  {
207  length = fmax;
208  }
209  }
210  if (fmin != 0)
211  {
212  leading = fmin - length;
213  }
214  if (sign == '-')
215  {
216  --leading;
217  }
218  }
219  if (sign == '-' && fill == '0')
220  {
221  (*func) (farg, sign);
222  }
223  if (leftjust == 0)
224  {
225  for (i = 0; i < leading; i++)
226  {
227  (*func) (farg, fill);
228  }
229  }
230  if (sign == '-' && fill == ' ')
231  {
232  (*func) (farg, sign);
233  }
234  for (i = 0; i < length; i++)
235  {
236  (*func) (farg, str[i]);
237  }
238  if (leftjust != 0)
239  {
240  for (i = 0; i < leading; i++)
241  (*func) (farg, fill);
242  }
243  }
244 
245 }
static void _prtl2(long num, char *str)
Definition: fdoprnt.c:352
static void _prtl10(long num, char *str)
Definition: fdoprnt.c:251
static void _prtX16(long num, char *str)
Definition: fdoprnt.c:327
static void _prtl16(long num, char *str)
Definition: fdoprnt.c:302
#define NULL
Definition: fdoprnt.c:6
#define va_arg(va, type)
可変長引数の値を返し、次の引数へ進む。 「va_start()とva_end()の間」もしくは「va_copy()とva_end()の間...
Definition: stdarg.h:34
static void _prtl8(long num, char *str)
Definition: fdoprnt.c:276
#define MAXSTR
Definition: fdoprnt.c:5
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sprintf()

int sprintf ( char *  str,
char *  fmt,
  ... 
)

Definition at line 12 of file sprintf.c.

References _fdoprnt(), sprntf(), va_end, and va_start.

Referenced by ascdate(), startup(), and xsh_netinfo().

17 {
18  va_list ap;
19  char *s;
20 
21  s = str;
22  va_start(ap, fmt);
23  _fdoprnt(fmt, ap, sprntf, (int)&s);
24  va_end(ap);
25  *s++ = '\0';
26 
27  return ((int)str);
28 }
void _fdoprnt(char *, va_list, int(*func)(int, int), int)
Definition: fdoprnt.c:20
#define va_start(last, va)
va_list型を初期化し、可変長引数の使用を開始する。
Definition: stdarg.h:25
#define va_end(va)
可変長引数の処理を終了する。
Definition: stdarg.h:42
__builtin_va_list va_list
可変個の実引数を扱うための情報を保持するための型(__builtin_va_listはGCCに定義された型) ...
Definition: stdarg.h:7
static int sprntf(int, int)
Definition: sprintf.c:34
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sprntf()

static int sprntf ( int  acpp,
int  ac 
)
static

Definition at line 34 of file sprintf.c.

Referenced by sprintf().

38 {
39  char **cpp = (char **)acpp;
40  char c = (char)ac;
41 
42  return (*(*cpp)++ = c);
43 }
Here is the caller graph for this function: