simple XML

phpでxmlを扱うにも、いろいろな方法があります。文字列のxmlを読み込んで解析し、xmlの内容を出力します。

<?php
// set XML
$xml_str = <<<XML
<?xml version='1.0'?>
<items>
    <item id="101">
        <name>石鹸</name>
        <price>510</price>
    </item>
    <item id="102">
        <name>ブラシ</name>
        <price>330</price>
    </item>
</items>
XML;

// analyze XML
$xml = simplexml_load_string($xml_str);
// display each item info
foreach ($xml->item as $it){
    $attr = $it->attributes();
    echo "(id:".$attr["id"].")";
    echo $it->name." - ".$it->price." 円\n";
}

brk

ヒープ領域に新たにメモリを割り当て、そのメモリ上に文字列をコピーして、その文字列を標準出力に出力する例です。

#include 

#include 
#include 

int main(){
    char *s;
    
    if ((s = sbrk(7))== (void *)-1){
        perror("sbrk");
        return 1;
    }
    strcpy(s, "hello\n");
    write(1, s, 6);
    
    if (brk(s) < 0){
        perror("brk");
        return 1;
    }
    return 0;
}

lseekプログラム

#include < sys/types.h >
#include < unistd.h >

#include < sys/stat.h >
#include < fcntl.h >
#include < stdio.h >

int main()
{
    int fd;
    off_t offset;
    ssize_t n;
    char buf[4096];
    
    if((fd = open("file.txt", O_RDONLY)) < 0){
        perror("open");
        return 1;
    }
    
    if ((offset = lseek(fd, 10, SEEK_SET)) < 0){
        perror("lseek");
        return 1;
    }
    
    if ((n = read(fd, buf, sizeof buf)) < 0){
        perror("read");
        return 1;
    }
    write(1, buf, n);
    
    return 0;
}

setpgid

引数pid、引数pgidの両方にゼロを指定

#include < unistd.h >

#include < stdio.h >

int main()
{
    if (setpgid(0, 0) < 0){
        perror("setpgid");
        return 1;
    }
    return 0;
}

uid, euidの設定

#include < sys/types.h >
#include < unistd.h >

#include < stdio.h >

static void
printuid()
{
    printf("uid = %d euid = %d\n",
           (int)getuid(),(int)geteuid());
}

int main() {
    uid_t uid, euid;
    
    uid = getuid();
    euid=geteuid();
    
    printuid();
    
    if(setuid(uid) < 0){
        perror("setuid");
        return 1;
    }
    printuid();
    
    if(setuid(euid) < 0){
        perror("setuid");
        return 1;
    }
    printuid();
    
    return 0;
}

getpid/getppid

自分または親のプロセスID

#include < sys/types.h >
#include < unistd.h >

#include 

int
main()
{
    pid_t pid, ppid;
    
    pid = getpid();
    ppid = getppid();
    
    printf("pid = %d, ppid = %d\n", (int)pid, (int)ppid);
    return 0;
}

読出しサンプル

#include < stdio.h >
#include < math.h >
#include < fcnt1.h >
#include < math.h >
#include < sys/types.h >
#include < unistd.h >
#include < sys/mman.h >
typedef struct {
    char str[512];
    long lval;
    double dval;
}SSS;
#define NUMBER (10000)
void main()
{
    int fd;
    long psize,size;
    SSS *ptr;
    long i;
    if((fd=open("MapFile",O_RDWR))== -1){
        perror("open");
        exit(-1);
    }
#ifdef BSD
    psize=getpagesize();
#else
    psize=sysconf(_SC_PAGE_SIZE);
#endif
    size=(NUMBER*sizeof(SSS)/psize+1)*psize;
    /* map */
    ptr=(SSS *)mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    /* test */
    while(1){
        for(i=0;i