일반
2012.02.06 08:12

디버깅용 string 프로그램 소스

조회 수 15778 댓글 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
번호 분류 제목 날짜 조회 수 추천 수
101 Pi Arduino Pro Mini PIN 정보 file 2018.03.18 7469 0
100 Python 줄 바꿈 없이 출력하는 방법 2019.03.30 7419 0
99 LINUX Ubuntu TFTP 서버 2019.10.30 7400 0
98 일반 [HTML] 복사해서 사용가능한 5개의 실용적인 List Style 예제 2015.11.03 7399 0
97 Android 안드로이드 소스에서 shell 명령어 실행하기 2014.12.30 7399 0
96 Pi 아두이노 (Arduino) 소개 및 유형별 종류, 제품별 특징 2016.11.15 7392 0
95 Pi 무선랜 wifi 설정 2015.10.01 7244 0
94 Pi SD Memory 카드 SPI 3.3V/5.0V 인터페이스 모듈 Atmega2560 제어 예제 2016.12.27 7232 0
93 LINUX 우분투(Ubuntu) 설치된 패키지 목록 확인하기 2016.03.17 7184 0
92 Android 안드로이드 웨어 디자인 (Android Wear Design) [Korean] 2015.02.25 7104 0
91 Pi SSD1306 OLED Displays with Raspberry Pi 2017.05.10 7056 0
90 C# Best way to implement keyboard shortcuts in a Windows Forms application 2015.11.06 7044 0
89 MFC [Collection] ArrayList 예제 2017.01.25 7017 0
88 일반 BT와 BLE 2017.05.16 7008 0
87 LINUX 리눅스의 기본 명령어들 2015.01.20 6981 0
목록
Board Pagination ‹ Prev 1 ... 23 24 25 26 27 28 29 30 31 32 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


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

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

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5