This repository was archived by the owner on Jan 14, 2026. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -229,6 +229,14 @@ int printk(const char *fmt, ...) {
229229 // widthとパディングの簡易サポート
230230 int width = 0 ;
231231 int pad_zero = 0 ;
232+ int left_align = 0 ; // 左詰めフラグ
233+
234+ // '-' フラグをチェック(左詰め)
235+ if (fmt [i ] == '-' ) {
236+ left_align = 1 ;
237+ i ++ ;
238+ }
239+
232240 if (fmt [i ] == '0' ) {
233241 pad_zero = 1 ;
234242 i ++ ;
@@ -310,8 +318,35 @@ int printk(const char *fmt, ...) {
310318 buffer [j ++ ] = numbuf [k ];
311319 } else if (spec == 's' ) {
312320 const char * s = va_arg (args , const char * );
313- while (* s && j < (int )sizeof (buffer ) - 1 )
314- buffer [j ++ ] = * s ++ ;
321+ int len = 0 ;
322+ const char * p = s ;
323+ // 文字列の長さを計算
324+ while (* p ) {
325+ len ++ ;
326+ p ++ ;
327+ }
328+ // 左詰めの場合: 文字列を先に出力してからパディング
329+ if (left_align ) {
330+ while (* s && j < (int )sizeof (buffer ) - 1 )
331+ buffer [j ++ ] = * s ++ ;
332+ // 右側にパディング
333+ if (width > len ) {
334+ int pad = width - len ;
335+ while (pad -- > 0 &&
336+ j < (int )sizeof (buffer ) - 1 )
337+ buffer [j ++ ] = ' ' ;
338+ }
339+ } else {
340+ // 右詰め(デフォルト): パディングしてから文字列
341+ if (width > len ) {
342+ int pad = width - len ;
343+ while (pad -- > 0 &&
344+ j < (int )sizeof (buffer ) - 1 )
345+ buffer [j ++ ] = ' ' ;
346+ }
347+ while (* s && j < (int )sizeof (buffer ) - 1 )
348+ buffer [j ++ ] = * s ++ ;
349+ }
315350 } else if (spec == 'c' ) {
316351 char c = (char )va_arg (args , int );
317352 buffer [j ++ ] = c ;
You can’t perform that action at this time.
0 commit comments