2014年8月12日火曜日

開発環境

初めてのPerl 第6版 (Randal L. Schwartz (著)、brian d foy (著)、Tom Phoenix (著)、近藤 嘉雪 (翻訳)、オライリージャパン)の16章(プロセス管理)の16.9(練習問題)1.をC言語で考えてみる。

その他参考書籍

16.9(練習問題)1.

コード(BBEdit, Emacs)

sample1.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main(int argc, char *argv[])
{
  if (argc < 2) {
    fprintf(stderr, "Usage: sample1 <directory>\n");
    exit (1);
  }
  char *dir = argv[1];
  
  if (execlp("ls", "ls", "-l", dir, NULL) == -1) {
    fprintf(stderr, "%s\n", strerror(errno));
    exit (1);
  }

  return (0);
}

Makefile

CC=cc
CFLAGS=-g -Wall
SRC=sample1.c
OBJ=sample1.o

all: sample1

sample1: $(OBJ)
 $(CC) $(CFLAGS) $(OBJ) -o sample1

sample1.o: sample1.c
 $(CC) $(CFLAGS) -c sample1.c -o sample1.o

clear:
 rm -rf sample1 $(OBJ)

入出力結果(Terminal)

$ make
cc -g -Wall -c sample1.c -o sample1.o
cc -g -Wall sample1.o -o sample1
$ ./sample1
Usage: sample1 <directory>
$ ./sample1 /
total 16437
drwxrwxr-x+ 61 root  admin     2074 Aug 11 23:29 Applications
drwxr-xr-x+ 66 root  wheel     2244 May 13 19:51 Library
drwxr-xr-x@  2 root  wheel       68 Aug 25  2013 Network
drwxr-xr-x+  4 root  wheel      136 Apr 22 02:37 System
drwxr-xr-x   6 root  admin      204 Apr 26 05:24 Users
drwxrwxrwt@  8 root  admin      272 Aug 12 17:58 Volumes
drwxr-xr-x@ 39 root  wheel     1326 Jul  1 08:51 bin
drwxrwxr-t@  2 root  admin       68 Aug 25  2013 cores
dr-xr-xr-x   3 root  wheel     4548 Aug 11 03:25 dev
lrwxr-xr-x@  1 root  wheel       11 Apr 22 02:18 etc -> private/etc
dr-xr-xr-x   2 root  wheel        1 Aug 11 03:25 home
-rwxr-xr-x@  1 root  wheel  8394000 Jun  4 13:27 mach_kernel
dr-xr-xr-x   2 root  wheel        1 Aug 11 03:25 net
drwxr-xr-x   5 root  wheel      170 Jul 25 13:33 opt
drwxr-xr-x@  6 root  wheel      204 Apr 22 02:49 private
drwxr-xr-x@ 62 root  wheel     2108 Jul  1 08:51 sbin
lrwxr-xr-x@  1 root  wheel       11 Apr 22 02:19 tmp -> private/tmp
drwxr-xr-x@ 10 root  wheel      340 May 27 09:14 usr
lrwxr-xr-x@  1 root  wheel       11 Apr 22 02:19 var -> private/var
$ ./sample1 /abcde
ls: /abcde: No such file or directory
$

0 コメント:

コメントを投稿