일반
2012.02.06 08:12

디버깅용 string 프로그램 소스

조회 수 15690 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
Practice of Programming 에서 발췌.

바이너리 파일에서 6자 이상의 문자열을 모두 출력해주는 프로그램.

 /* Copyright (C) 1999 Lucent Technologies */
/* Excerpted from 'The Practice of Programming' */
/* by Brian W. Kernighan and Rob Pike */

#include <stdio.h>
#include <ctype.h>
#include "eprintf.h"

void strings(char *, FILE *);

enum { MINLEN = 6 };

/* strings main: find printable strings in files */
int main(int argc, char *argv[])
{
    int i;
    FILE *fin;

    setprogname("strings");
    if (argc == 1)
        eprintf("usage: strings filenames");
    else {
        for (i = 1; i < argc; i++) {
            if ((fin = fopen(argv[i], "rb")) == NULL)
                weprintf("can't open %s:", argv[i]);
            else {
                strings(argv[i], fin);
                fclose(fin);
            }
        }
    }
    return 0;
}

/* strings: extract printable strings from stream */
void strings(char *name, FILE *fin)
{
    int c, i;
    char buf[BUFSIZ];

    do {    /* once for each string */
        for (i = 0; (c = getc(fin)) != EOF; ) {
            if (!isprint(c))
                break;
            buf[i++] = c;
            if (i >= BUFSIZ)
                break;
        }
        if (i >= MINLEN) /* print if long enough */
            printf("%s:%.*s\n", name, i, buf);
    } while (c != EOF);
}



 

/* eprintf.h: 에러 wrapper 함수 제공 */

extern void eprintf(char *fmt, ...);
extern void weprintf(char *fmt, ...);
extern char *estrdup(char *s);
extern void *emalloc(size_t n);
extern void setprogname(char *str);
extern char *progname(void);



 

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include "eprintf.h"|

/* 
 eprintf: 에러 메시지를 출력하고 프로그램을 종료한다. 
*/
void eprintf(char *fmt, ...)
{
 va_list args;

 fflush(stdout);
 if(progname() != NULL)
  fprintf(stderr, "%s: ", progname());

 va_start(args, fmt);
 vfprintf(stderr, fmt, args);
 va_end(args);

 if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':')
  fprintf(stderr, "%s", strerror(errno));
 fprintf(stderr, "\n");
 exit(2); /*프로그램이 실패하는 경우의 관습적인 값*/
}

void weprintf(char *fmt, ...)
{
 va_list args;

 fflush(stdout);
 if(progname() != NULL)
  fwprintf(stderr, "%s: ", progname());

 va_start(args, fmt);
 vfwprintf(stderr, fmt, args);
 va_end(args);

 if (fmt[0] != '\0' && fmt[wcslen(fmt)-1] == ':')
  fwprintf(stderr, "%s", strerror(errno));
 fwprintf(stderr, "\n");
 exit(2); /*프로그램이 실패하는 경우의 관습적인 값*/
}


/* 
 estrdup: 문자열을 복사하고 에러 발생시 종료
*/
char *estrdup(char *s)
{
 char *t;

 t = (char*) malloc(strlen(s)+1);
 if (t == NULL)
  eprintf("estrdup(\"%.20s\") failed:", s);
 strcpy(t, s);
 return t;
}

/* 
 emalloc: 메모리를 할당하고, 에러 발생시 종료
*/
void *emalloc(size_t n)
{
 void *p;

 p = malloc(n);
 if (p == NULL)
  eprintf("malloc of %u bytes failed:", n);
 return p;
}

static char *name = NULL; // 메시지 출력을 위한 이름
/* 
 setprognmae: 프로그램 이름 지정
*/
void setprogname(char *str)
{
 name = estrdup(str);
}

/* 
 progname: 저장된 프로그램 이름 리턴
*/
char *progname(void)
{
 return name;
}


 


Dreamy의 코드 스크랩

내가 모으고 내가 보는

List of Articles
번호 분류 제목 날짜 조회 수 추천 수
281 일반 Visul Studio 2013 유용한 단축키 2014.03.01 26565 0
280 일반 cmd.exe 쉘 환경변수 추출 및 처리 2014.03.20 11831 0
279 C# INI 파일 대신에 XML 로 설정값 저장/유지 하기 2014.04.03 12592 0
278 C# Ini 파일 작성과 사용하기 2014.04.03 16921 0
277 LINUX mc(Midnight Commander) 사용법 2 2014.04.07 18031 0
276 LINUX Midnight Commander 간단 조작법 2014.04.07 11313 0
275 일반 정규식 요약 프린트 file 2014.04.21 10914 0
274 Python paramiko로 SSH 접속하기 2014.04.22 21760 0
273 Python distutils 개요 2014.04.24 9681 0
272 Python wxPython Tutorial 링크 2014.04.24 10806 0
271 Python Lambda 함수, 축약 함수 2014.04.25 12687 0
270 Python pylab - Plotting with Matplotlib 2014.04.28 9963 0
269 PHP move_uploaded_file() 2014.04.29 10613 0
268 Python map() 함수 2014.04.30 10154 0
267 Python python 내장 함수 2014.04.30 16259 0
목록
Board Pagination ‹ Prev 1 ... 11 12 13 14 15 16 17 18 19 20 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5