일반
2012.02.06 08:12

디버깅용 string 프로그램 소스

조회 수 15618 댓글 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
번호 분류 제목 날짜 조회 수 추천 수
146 PHP GD 라이브러리 간단 2014.07.09 9530 0
145 Pi 라즈베리파이 PI CAMERA 설치하기 2018.04.24 9513 0
144 일반 사용하지 않는 COM Port 삭제하기 2015.03.18 9466 0
143 Android adb 를 이용한 터치 이벤트 보내기 2014.07.30 9431 0
142 Pi ATTINY85 PIN정보, Data sheet 2 file 2016.12.10 9412 0
141 일반 Html에서 사용하는 contentType의 종류 2015.10.27 9402 0
140 Pi 라즈베리파이를 크롬캐스트 처럼 사용하는 방법 - RaspiCast 2017.10.17 9324 0
139 LINUX 리눅스 명령행에서 메일 보내기(send mail from linux command line) 2019.11.25 9253 0
138 Pi NodeMCU PIN 정보 file 2017.01.09 9222 0
137 C# Download Files from Web [C#] 2014.09.11 9194 0
136 LINUX 명령어 뒤의 옵션 자동완성 기능, complete 명령어 2015.01.06 9078 0
135 LINUX epoch time을 실제 시간으로 변환해주는 excel 함수 2015.03.18 9077 0
134 Android ion memory 사용량 확인하기 2014.12.03 9012 0
133 Python json 데이터 핸들링 2017.03.09 8961 0
132 C++ istringstream 을 이용한 string type 변환 & 토크나이징 2017.06.07 8947 0
목록
Board Pagination ‹ Prev 1 ... 20 21 22 23 24 25 26 27 28 29 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


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

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

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5