それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

UNIX Cプログラミング

アスキー出版局の古典名著(?) UNIX Cプログラミング を始めた。とりあえずLinuxGCC環境を用意している。4章は罠がいっぱいみたい。巻末にAppendix(注釈付の余録がある)

それでもLinuxでこのコードが動かなかった。

includeするファイルを間違ってると思うんだけど。如何せんC言語の基本文法と構造までしからないので、どうすることも出来ない。SystemVなんてわかんねーよ。

listfiles1.c

/**
* Non-BSD systems only.
*/
/**
* 何か問題があって動かない。たぶんPOSIXとの互換性
*/

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <dirent.h>
#define direct dirent

/**
* DIRSIZが無いと言われたからDefineした。たぶんこの辺ですでにおかしい。
*/
#define DIRSIZ 14
main(){
 int n;
 FILE *fp;
 struct direct dir;

 if ((fp = fopen(".","r")) == NULL){
         perror("current directory");
         exit(1);
 }

 /*
  * Read directory entries. Since we're reading
  * entries one at a time, we use the fread routine,
  * which buffers them internally. Don't use the
  * low-level read to do things this way, since
  * reading very small quantities of data (16 byyes)
  * at a time is very inefficient.
  *
  * */

 while(( n = fread(&dir,sizeof(dir),1, fp))>0) {
                 /**
                  * Skip removed files.
                  */
                 if (dir.d_ino==0){
                     continue;
                 }
                 /**
                  * Make sure we print no more than DIRSIZ
                  * characters.
                  * */
                 printf("%.*s\n", DIRSIZ, dir.d_name);
 }
 fclose(fp);
 fclose(fp);
 exit(0);

}

デバッグ技法を身につけないと時間がかかって仕方がない。