grnmarker筆記

/usr/sbin/dadriveinfo 1 2>/dev/null| /bin/grep -F "Serial Number" | awk '{print $3}'


Drive:                                   Drive1 (/dev/xdb)
Type:                                    SATA
Model:                                   Hitachi HDS5C3020ALA632                 
Serial Number:                           ML0220F31ABGJN
Firmware Revision:                       ML6OA580
Size [KB] (1 KB = 1024 Bytes):           1953514584 KB
Size [GB] (1 GB = 1024^3 Bytes):         1863 GB
Size [GB] (1 GB = 10^9 Bytes):           2000 GB
Read Total  [MB] (1 MB = 1024^2 Bytes):  3496 MB
Write Total [MB] (1 MB = 1024^2 Bytes):  1392 MB
Read block requests:                     26079
Write block requests:                    18727
Last access:                             0 s ago
Failure reason:                          Not_Available
Disk cache:                              disabled


int main(void)
{
    char cmd[64] = {0};
    FILE *fp = NULL;
    char file[64] = {0};
    char string[512] = {0};
    int result = 0;
    int i = 0;
    char *p = NULL;

    sprintf(file, "mytmp");
    //sprintf(cmd, "cat /proc/cpuinfo > mytmp");
    //result = system(cmd);
    //printf("%s", cmd);
    fp = fopen(file, "r");
    if(fp == NULL){
        printf("%s is not exist", file); 
        return 0;
    }   
    while(fgets(cmd, sizeof(cmd), fp) != NULL)
    {   
        for(i = 0; i < GRN_KEYS_CNT; i++)
        {   
            if(strncmp(cmd, grn_keys[i], strlen(grn_keys[i]))){
             continue;
            }   
            printf("%d\t**i=%d\tcmd=%s\t**grn_keys[%d] = %s\t\n", __LINE__, i, cmd, i, grn_keys[i]);
    
            if((p = strchr(cmd, '\n')))
                *p = 0;

            printf("%d\tp = %s\tcmd=%s\n", __LINE__, p, cmd);
            p = cmd + strlen(grn_keys[i]);
            while(*p == ' ' || *p == '\t'){
                p++;  
            printf("%d\tp = %s\tcmd=%s\n", __LINE__, p, cmd);
            }   
            printf("%d\tp = %s\n", __LINE__, p); 
        }   

        //printf("%d\tp = %s\tq=%s\n", __LINE__, p, q);
    
    }   
    

    return 0;  
}



int static do_system_ret_buf(char *command, char *ret_buf, int buf_size)
{
    char    *tmps;
    int     pipefd[2];
    pid_t   pid, childpid;
    int     status, orig_stdin, rc;

    if ((ret_buf == NULL) || (buf_size == 0))   /* caller should call system directly */
        return system(command);

    orig_stdin = dup(0);
    if (orig_stdin < 0) {
        return 1;
    }

    /* Flush all open output streams (stdout, in particular).  Without the flush,
     * garbage can end up in ret_buf.
     */
    if (fflush(NULL) != 0) {
        close(orig_stdin);
        return 1;
    }

    if(pipe(pipefd)) {
        close(orig_stdin);
        return 1;
    }

    switch(pid=fork()){
    case -1:
        close(orig_stdin);
        return 1;
    case 0 : /* child's path */
        /* pipefd[1] is for writing to the pipe. We want the output
         * that used to go to the standard output (file descriptor 1)
         * to be written to the pipe. The following command does this,
         * creating a new file descripter 1 (the lowest available)
         * that writes where pipefd[1] goes.
         */
        if (dup2(pipefd[1], 1) < 0)    /* points pipefd[1] at stdout */
            exit(1);
        close (pipefd[0]);          /* child isn't going to read from the pipe */
        rc = system(command);
        if (rc == 0)
            exit(0);  // system call success
        exit(1);
    default: /* parent's path */
        if (dup2(pipefd[0], 0) < 0) {  /* Set fd 0 (stdin) to read from the pipe */
            dup2(orig_stdin, 0);
            close(orig_stdin);
            return 1;
        }
        close (pipefd[1]);          /* no need to write to the pipe */
        close(pipefd[0]);           /* close the read pipe also */

        tmps = ret_buf;
        do {
            *tmps = fgetc(stdin);
        } while ((*tmps++ != EOF) && (--buf_size > 0));
        tmps--;
        *tmps = 0;

        dup2(orig_stdin, 0);            /* restore original stdin */
        close(orig_stdin);              /* close backup stdin */

        childpid = waitpid(0, &status, 0);

        if (WEXITSTATUS(status))
        	;
        else
            return 0;  /* child exited normally and system call ok */

        return 1;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章