ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

モスを中心とする謎の白鴎集団コミュの雑談板

  • mixiチェック
  • このエントリーをはてなブックマークに追加
まぁ一応必要かなと思って作ってみた。


なんか他の板に書く内容じゃないけど用があったりとか、単になんか書きたいから書いてみたりとか、そんな気分になったら書いてみたらいーんじゃなーい?

コメント(33)

早速だが、副管理人やりたい人いる?
>>ちゅん
シラネwwwww

なんかコミュが荒れたときで、俺がいないときになんかスレ消したりすんじゃね?www
>>ちゅん
強制イベントだwww
副管理人依頼しておいたwwwwwwwwww
素敵なかんじの>>5に変えてみた(`・ω・´)
>>ちゅん
おまいはそんな暇なのか?wwwwwwwwwwwwww
暇だったらみんな見ろ↓
http://www.kokaku-s.com/
なんかこのスレ俺の趣味板になりそうな悪寒www
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<time.h>
#include<stdio.h>

char *modes[] = {
"---","--x","-w-","-wx",
"r--","r-x","rw-","rwx"
};

void list(char *dirname);
void printout(char* dir, char* name);

int main(int argc, char **argv){
struct lstat sbuf;
int a=F=l=0;
int i,k;

if(argc < 3 | argv[1][0] != '-' | (k = strlen(argv[2])) <= 4){
fprintf(stderr, "Usage: %s \n", *argv);
exit(1);
}
else{
for(i=0; i<k; i++){
if(argv[1][i+1]=='a')
a = 1;
else if(argv[1][i+1]=='F')
F = 1;
else if(argv[1][i+1]=='l')
l = 1;
else
fprintf(stderr, "Usage: %s \n", *argv);
}

while(--argc){
if(lstat(*(++argv), &sbuf) < 0){
perror(*argv);
continue;
}

if((sbuf.st_mode & S_IFMT) == S_IFDIR)
list(*argv, a, F, l);
else
printout(".", *argv, a, F, l);
]
return 0;
}

void list(char *dirname, int a, int F, int l){
DIR *dp;
struct dirent *dir;

if((dp = opendir(dirname)) == NULL){
fprintf(stderr, "cannot open directory.\n");
exit(1);
}

while((dir = readdir(dp)) != NULL){
if(dir->d_ino == 0)
continue;

printout(dirname, dir->d_name, a, F, l);
}

closedir(dp);
}

void printout(char* dir, char* name, int a, int F, int l){
int i, j;
cahr perms[10];
struct lstat sbuf;
cahr newname[1024];

sprintf(newname, "%s/%s", dir, name);

lstat(newname, &sbuf);

printf("%5d ", sbuf.st_blocks);

switch(sbuf.st_mode & S_IFMT){
case S_IFREG:
putchar('-');
break;
case S_IFDIR:
putchar('d');
break;
case S_IFCHR:
putchar('c');
break;
case S_IFBLK:
putchar('b');
break;
default:
putchar('?');
break;
}

*perms = NULL;

for(i=2; i>=0; i--){
j = (sbuf.st_mode >> (i*3)) & 07;

strcat(perms, modes[j]);
}

if((sbuf.st_mode & S_IFUID) != 0)
perms[2] = 's';
if((sbuf.st_mode & S_IFGID) != 0)
perms[5] = 's';
if((sbuf.st_mode & S_IFVTX) != 0)
perms[8] = 't';

printf("%s%3d %5/%-5d ", perms, sbuf.st_nlink, sbuf.st_uid, sbuf.st_gid);

printf("%7d %.12s ", sbuf.st_size, ctime(&sbuf.st_mtime) + 4);

printf("%s\n" name);
}
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>

int main(int argc, char **argv){
FILE *fp;
int i, n;

if((fp = fopen(argv[1], "r")) == NULL){
perror(argv[1]);
exit(1);
}

lseek(fp, 0, SEEK_END);

for(i=0; n < 0; i++)
n = lseek(fp, -1L, SEEK_CUR);

close(fp);

printf("ファイル名:%s[%d]\n", argv[1], i);

return 0;
}
#include<stdio.h>
#include<unistd.h>

int main(int argc, char** argv){

if(argc != 2)
fprintf(stderr, "Usage: %s\n", *argv);

unlink(argv[1]);

return 0;
}
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<signal.h>
#include<unistd.h>
#include<strings.h>

#define ERR -1

void usage(void);
char *command_name;
int nwait;

void sigpwait(void);

int main(int argc, char *argv[]){
int pid;
int mypid;
char buf[32];

if((command_name = rindex(argv[0], '/')) != NULL)
command_name++;
else
command_name = argv[0];

if(argc != 1)
usage();

if(signal(SIGCHLD, (void*)sigpwait) == SIG_ERR){
perror("signal");
exit(1);
}

nwait = 0;

if((pid = fork()) == 0){
mypid = getpid();
printf("Child process : execute a new program (childA). pid = %d\n", mypid);
if(execl("childA", "childA", '\0') == ERR){
perror("execl");
exit(1);
}
}
else if(pid == ERR){
perror("fork");
exit(1);
}

while(1){
scanf("%s", buf);
if((pid = fork()) == 0){
mypid = getpid();
printf("Child process : execute a new program (%s). pid = %d\n", buf, mypid);
if(execl("buf", "buf", '\0') == ERR){
perror("execl");
exit(1);
}
}
else if(pid == ERR){
perror("fork");
exit(1);
}
}

while(nwait < 3);

return 0;
}

void usage(void){
fprintf(stderr, "Usage: %s\n", command_name);
exit(1);
}

void sigpwait(void){
int status;
int cpid;

if((cpid = wait(&status)) == ERR){
perror("wait");
exit(1);
}

nwait++;

printf("Parent process : child process has just finished.\n");
printf("pid = %d; status = %04x\n", cpid, status);

if(signal(SIGCHLD, (void*)sigpwait) == SIG_ERR){
perror("signal");
exit(1);
}
}


http://www.linux.or.jp/JM/
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<signal.h>
#include<unistd.h>
#include<strings.h>

#define ERR -1

void usage(void);
char *command_name;
int nwait;

void sigpwait(void);

int main(int argc, char *argv[]){
int pid;
int mypid;
char buf[32];
int i;

if((command_name = rindex(argv[0], '/')) != NULL)
command_name++;
else
command_name = argv[0];

if(argc != 1)
usage();

if(signal(SIGCHLD, (void*)sigpwait) == SIG_ERR){
perror("signal");
exit(1);
}

nwait = 0;

while(1){
printf("Process Name > ");
if((scanf("%s", buf)) == 1){
mypid = getpid();
printf("Child process : execute a new program (%s). pid = %d\n", buf, mypid);
if((pid = fork()) == 0){
if(execl(buf, buf, '\0') == ERR){
perror("execl");
exit(1);
}
}
else if(pid == ERR){
perror("fork");
exit(1);
}
}
}

while(nwait < 3);

return 0;
}

void usage(void){
fprintf(stderr, "Usage: %s\n", command_name);
exit(1);
}

void sigpwait(void){
int status;
int cpid;

if((cpid = wait(&status)) == ERR){
perror("wait");
exit(1);
}

nwait++;

printf("Parent process : child process has just finished.\n");
printf("pid = %d; status = %04x\n", cpid, status);

if(signal(SIGCHLD, (void*)sigpwait) == SIG_ERR){
perror("signal");
exit(1);
}
}

kadai3-03.c
#include<stdio.h>
#include<signal.h>
#include<unistd.h>

void sigmsg1(void);
void sigmsg2(void);
void usage(void);
char *command_name;

int main(int argc, char *argv[]){
int i;
int time;

if((command_name = rindex(argv[1], '/')) != NULL)
command_name++;
else
command_name = argv[1];

if(argc != 2){
usage();
}

time = atoi(command_name);

signal(SIGINT, SIG_IGN);

signal(SIGTSTP, SIG_IGN);

signal(SIGALRM, (void*)sigmsg1);

signal(SIGQUIT, (void*)sigmsg2);

alarm(time);

if(pause() == -1){
i = alarm(time);
printf("\nRemained time:%d\n", i);
printf("Time over\n");
}
return 0;
}

void sigmsg1(void){
if(signal(SIGINT, (void*)sigmsg1) == SIG_ERR){
perror("signal");
exit(1);
}
}

void sigmsg2(void){
printf("User quit\nRemained time:%d\n", alarm(0));
exit(1);
}

void usage(void){
fprintf(stderr, "Usage: %s\n", command_name);
exit(1);
}

mysleep.c
課題1
ソースファイル:mysleep.c
実行ファイル:mysleep

signal(SIGINT, SIG_IGN);
この行でSIGINT(キーボードからの割り込み)を無視する。

signal(SIGTSTP, SIG_IGN);
この行でSIGTSTP(端末から入力された一旦停止)を無視する。

signal(SIGALRM, (void*)sigmsg1);
この行でSIGALRM(アラームコール)を無視する。
 *アラームコールはシステムコールalarmからのタイマーシグナル

signal(SIGQUIT, (void*)sigmsg2);
この行でSIGQUIT(キーボードによる中止)を無視する。

alarm(time);
コマンド引数で指定された秒数(time)後にシグナルを送る。




課題2
ソースファイル:kadai3-03.c
実行ファイル:kadai3-03

parent_sig.cからの変更点は主に
while(1){
 printf("Process Name > ");
 if((scanf("%s", buf)) == 1){
  mypid = getpid();
  printf("Child process : execute a new program (%s). pid = %d\n", buf, mypid);
  if((pid = fork()) == 0){
   if(execl(buf, buf, '\0') == ERR){
    perror("execl");
    exit(1);
   }
  }
  else if(pid == ERR){
   perror("fork");
   exit(1);
  }
 }
}
の部分である。

fork関数とexec関数により子プロセスのダミーを作る部分を
while関数で繰り返している。
scanf関数でダミーを作りたい子プロセスを入力し、子プロ
セスを作る。
childA、childB、childC以外のプロセスもダミーを作れる仕様
になっている。
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>

#define ERR -1
#define MAX_ARGS 10
#define SIZE_COMLINE 128
#define CONTINUE 1
#define EXITSHELL 0
#define BUFSIZE 128

void usage(void);
int getcl(char *line, char* argmv[]);
int exec_one_command(char linebuf[]);
int redirect(char *line, int count);

char *command_name;

int main(int argc, char *argv[]){
char linebuf[SIZE_COMLINE];
int hist;
int i;
int in_s, out_s, pipe_s;

if((command_name = (char*)rindex(argv[0], '/')) != NULL)
command_name++;
else
command_name = argv[0];

if(argc != 1){
usage();
}

for(hist = 1; hist < 5 ; hist++){
printf("mysh[%d] ", hist);
fgets(linebuf, SIZE_COMLINE, stdin);

/*pipe,redirect*/
in_s = out_s = pipe_s = 0;
for(i=0; linebuf[i] != NULL; i++){
if(linebuf[i] == '<')
in_s++;
else if(linebuf[i] == '>')
out_s++;
else if(linebuf[i] == '|')
pipe_s++;
}
if(in_s > 1 || out_s > 1){
printf("Error\n");
continue;
}

if(pipe_s != 0){
continue;
}
else if(in_s == 1 || out_s == 1){
redirect(linebuf, (in_s+out_s));
}
/*end*/
else if(exec_one_command(linebuf) == EXITSHELL){
break;
}
}

return 0;
}

int getcl(char *line, char *argmv[]){
int argnum = 0;
int len;

len = strlen(line);
if(line[len-1] == '\n')
line[--len] = '\0';

do{
while(isspace(*line))
line++;

if(*line == '\0'){
argmv[argnum] = NULL;
return argnum;
}

argmv[argnum++] = line;

if(argnum == MAX_ARGS)
break;

while((*line != '\0') && !isspace(*line))
line++;

if(*line == '\0'){
argmv[argnum] = NULL;
return argnum;
}

*line = '\0';
line++;
}
while(1);

fprintf(stderr, "too many args.\n");
return 0;
}

int exec_one_command(char linebufp[]){
int argnum;
int pid;
int status;
char *argmv[MAX_ARGS];

argnum = getcl(linebufp, argmv);

if(argnum == 0)
return CONTINUE;

if(strncmp(argmv[0], "exit", 4) == 0)
return EXITSHELL;

if((pid = fork()) == 0){
if(execvp(argmv[0], argmv) == ERR){
perror("execvp");
exit(1);
}
}
else if(pid >= 1)
wait(&status);
else{
perror("fork");
exit(1);
}

return CONTINUE;
}

int redirect(char *line, int count){
char command[BUFSIZE];
char infile[BUFSIZE];
char outfile[BUFSIZE];
int i, n, j;

for(i=0; line[i] != '<' && line[i] != '>'; i++)
command[i] = line[i];
command[i] = '\n';

if(count == 2){
for(n=0; n<2; n++){
if(line[i] == '<'){
i = i+1;
for(j=0; line[i] != NULL && line[i] != '>'; i++)
infile[j] = line[i];
infile[j] = '\n';
}
else if(line[i] == '>'){
i = i+1;
for(j=0; line[i] != NULL && line[i] != '<'; i++)
outfile[j] = line[i];
outfile[j] = '\n';
}
}
}
else{
if(line[i] == '<'){
i = i+1;
for(j=0; line[i] != NULL; i++){
infile[j] = line[i];
j++;
}
infile[j] = '\n';
exec_red(command, infile, NULL);
return 0;
}
else if(line[i] == '>'){
i = i+1;
for(j=0; line[i] != NULL; i++){
outfile[j] = line[i];
j++;
}
outfile[j] = '\n';
exec_red(command, NULL, outfile);
return 0;
}
}

exec_red(command, infile, outfile);
return 0;
}

int exec_red(char *argv1, char *argv2, char *argv3){
char *argmv2[MAX_ARGS];
char *argmv3[MAX_ARGS];
int fdin;
int fdout;
int pid;

if(argv2 != NULL){
getcl(argv2, argmv2);
if((fdin = open(argmv2[0], O_RDONLY)) == ERR){
perror("open");
return 0;
}
close(0);
if(dup(fdin) == ERR){
perror("dup");
return 0;
}
close(fdin);
}

if(argv3 != NULL){
getcl(argv3, argmv3);
printf("%s\n", argmv3[0]);
fdout = open(argmv3[0], O_CREAT | O_WRONLY | O_TRUNC, 0660);
if(fdout == ERR){
perror("open");
return 0;
}
if((pid = fork()) == 0){
close(1);
printf("%s\n", argmv3[0]);
if(dup(fdout) == ERR){
perror("dup");
return 0;
}
close(fdout);
}
}


printf("%s\n", argmv3[0]);

exec_one_command(argv1);

return 0;
}

void usage(void){
fprintf(stderr, "Usage: %s env-name...\n", command_name);
exit(1);
}
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>

#define ERR -1
#define MAX_ARGS 10
#define SIZE_COMLINE 128
#define CONTINUE 1
#define EXITSHELL 0
#define BUFSIZE 128

void usage(void);
int getcl(char *line, char* argmv[]);
int exec_one_command(char linebuf[]);
int redirect(char *line, int count);

char *command_name;

int main(int argc, char *argv[]){
char linebuf[SIZE_COMLINE];
int hist;
int i;
int in_s, out_s, pipe_s;

if((command_name = (char*)rindex(argv[0], '/')) != NULL)
command_name++;
else
command_name = argv[0];

if(argc != 1){
usage();
}

for(hist = 1; hist < 5 ; hist++){
printf("mysh[%d] ", hist);
fgets(linebuf, SIZE_COMLINE, stdin);

/*pipe,redirect*/
in_s = out_s = pipe_s = 0;
for(i=0; linebuf[i] != NULL; i++){
if(linebuf[i] == '<')
in_s++;
else if(linebuf[i] == '>')
out_s++;
else if(linebuf[i] == '|')
pipe_s++;
}
if(in_s > 1 || out_s > 1){
printf("Error\n");
continue;
}

if(pipe_s != 0){
pipe_set(linebuf, pipe_s);
}
else if(in_s == 1 || out_s == 1){
redirect(linebuf, (in_s+out_s));
}
/*end*/
else if(exec_one_command(linebuf) == EXITSHELL){
break;
}
}

return 0;
}

int getcl(char *line, char *argmv[]){
int argnum = 0;
int len;

len = strlen(line);
if(line[len-1] == '\n')
line[--len] = '\0';

do{
while(isspace(*line))
line++;

if(*line == '\0'){
argmv[argnum] = NULL;
return argnum;
}

argmv[argnum++] = line;

if(argnum == MAX_ARGS)
break;

while((*line != '\0') && !isspace(*line))
line++;

if(*line == '\0'){
argmv[argnum] = NULL;
return argnum;
}

*line = '\0';
line++;
}
while(1);

fprintf(stderr, "too many args.\n");
return 0;
}

int exec_one_command(char linebufp[]){
int argnum;
int pid;
int status;
char *argmv[MAX_ARGS];

argnum = getcl(linebufp, argmv);

if(argnum == 0)
return CONTINUE;

if(strncmp(argmv[0], "exit", 4) == 0)
return EXITSHELL;

if((pid = fork()) == 0){
if(execvp(argmv[0], argmv) == ERR){
perror("execvp");
exit(1);
}
}
else if(pid >= 1)
wait(&status);
else{
perror("fork");
exit(1);
}

return CONTINUE;
}

int redirect(char *line, int count){
char command[BUFSIZE];
char infile[BUFSIZE];
char outfile[BUFSIZE];
int i, n, j;
int add_s = 0;

for(i=0; line[i] != '<' && line[i] != '>'; i++)
command[i] = line[i];
command[i] = '\n';

if(count == 2){
for(n=0; n<2; n++){
if(line[i] == '<'){
i = i+1;
for(j=0; line[i] != NULL && line[i] != '>'; i++)
infile[j] = line[i];
infile[j] = '\n';
}
else if(line[i] == '>'){
i = i+1;
if(line[i] == '>'){
add_s = 1;
i++;
}
for(j=0; line[i] != NULL && line[i] != '<'; i++)
outfile[j] = line[i];
outfile[j] = '\n';
}
}
}
else{
if(line[i] == '<'){
i = i+1;
for(j=0; line[i] != NULL; i++){
infile[j] = line[i];
j++;
}
infile[j] = '\n';
exec_red(command, infile, NULL);
return 0;
}
else if(line[i] == '>'){
i = i+1;
if(line[i] == '>'){
add_s = 1;
i++;
}
for(j=0; line[i] != NULL; i++){
outfile[j] = line[i];
j++;
}
outfile[j] = '\n';
exec_red(command, NULL, outfile);
return 0;
}
}

exec_red(command, infile, outfile, add_s);
return 0;
}

int exec_red(char *argv1, char *argv2, char *argv3, int add_s){
char *argmv1[MAX_ARGS];
char *argmv2[MAX_ARGS];
char *argmv3[MAX_ARGS];
int fdin;
int fdout;
int pid;

getcl(argv1, argmv1);

if((pid = fork()) == 0){
if(argv2 != NULL){
getcl(argv2, argmv2);
if((fdin = open(argmv2[0], O_RDONLY)) == ERR){
perror("open");
return 0;
}
close(0);
if(dup(fdin) == ERR){
perror("dup");
return 0;
}
close(fdin);
}

if(argv3 != NULL){
getcl(argv3, argmv3);
printf("%s\n", argmv3[0]);
if(add_s = 0)
fdout = open(argmv3[0], O_CREAT | O_WRONLY | O_TRUNC, 0660);
else if(add_s = 1)
fdout = open(argmv3[0], O_CREAT | O_WRONLY | O_TRUNC | O_APPEND, 0660);
if(fdout == ERR){
perror("open");
return 0;
}
close(1);
printf("%s\n", argmv3[0]);
if(dup(fdout) == ERR){
perror("dup");
return 0;
}
close(fdout);
}
if(execvp(argmv1[0], argmv1) == ERR){
perror("child: execvp");
return 0;
}
}


printf("%s\n", argmv3[0]);

return 0;
}

int pipe_set(char *line, int count){

for(i=0; line[i] != '|'; i++)
command[0][i] = line[i];

for(n=0; n < count+1; n++){
i++;
for(j=0; line[i] != '|' && line[i] != NULL; i++){
command[n+1][j] = line[i];
j++;
}
}

exe_pipe(command, count);

return 0;
}

int exe_pipe(char **argv, int count){
char buf[MAX_ARGS][BUFSIZE];
int i;
int pid;
int fd[MAX_ARGS][2];

for(i=0; i < (count+1); i++){
getcl(argv[i], buf);
argv[i] = buf[i];
}

for(i=0; i < (count+1); i++){
if((pid = fork()) == 0){
if(i != 0){
close(0);
dup(fd[i][0]);
}
close(1);
dup(fd[i][1]);
close(fd[i][0]);
close(fd[i][1]);
if(execvp(argv[i], argv+i) == ERR){
perror("child: execvp");
return 0;
}
}
}

return 0;
}

int exe_pipe_and_red(){
char buf[MAX_ARGS][BUFSIZE];
int i;
int pid;
int fd[MAX_ARGS][2];

for(i=0; i < (count+1); i++){
getcl(argv[i], buf);
argv[i] = buf[i];
}

for(i=0; i < (count+1); i++){
if((pid = fork()) == 0){
if(i = 0 && in_s = 1){
if((fdin = open(argmv2[0], O_RDONLY)) == ERR){
perror("open");
return 0;
}
close(0);
if(dup(fdin) == ERR){
perror("dup");
return 0;
}
close(fdin);
}
if(i = count && out_s = 1){
if(add_s = 0)
fdout = open(argmv3[0], O_CREAT | O_WRONLY | O_TRUNC, 0660);
else if(add_s = 1)
fdout = open(argmv3[0], O_CREAT | O_WRONLY | O_TRUNC | O_APPEND, 0660);
if(fdout == ERR){
perror("open");
return 0;
}
close(1);
if(dup(fdout) == ERR){
perror("dup");
return 0;
}
close(fdout);
}
if(i != 0){
close(0);
dup(fd[i][0]);
}
if(i != count){
close(1);
dup(fd[i][1]);
}
close(fd[i][0]);
close(fd[i][1]);
if(execvp(argv[i], argv+i) == ERR){
perror("child: execvp");
return 0;
}
}
}

return 0;
}

void usage(void){
fprintf(stderr, "Usage: %s env-name...\n", command_name);
exit(1);
}

ログインすると、残り12件のコメントが見れるよ

mixiユーザー
ログインしてコメントしよう!

モスを中心とする謎の白鴎集団 更新情報

モスを中心とする謎の白鴎集団のメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。