문자열 문제점..

papa3721의 이미지

이놈 하느라 잠도 못자고.. 고수님들 도와 주세요.. ㅠ.ㅠ

첫번째 나온 결과 값는 ID 만드는 함수에서 뽑하낸 것이고.
3번째 문자열 합친후 Q가 생기는데 왜 이러는지.. 도저히 알길이 없네요. ㅠ.ㅠ

제가 작성한 방법에 문제가 있나요?
많은 조언 부탁 드립니다.

==== 결과 ====
[20071014232341]

[mailbox/papa1/mail_list]
[mailbox/papa1/20071014232341Q]
[20071014232341 admin 2007-10-3 23:23:41 Welcome
]

=== 소스 코드 ==

#define BUFSIZE 1024
#define LINESIZE 100
#define LISTENQ 20
#define MODE 0777
#define MEMBERS "USERS"
#define T_MEMBERS ".USERS"
#define MAXSTRBUF 100
 
#define D_MAILBOX "mailbox"
#define F_MAIL_LIST "mail_list"
 
char serverName[50];
 
void trim(char s[])
{
    int i,len;
    len = strlen(s);
    for(i=0; i<len; i++){
        if( (s[i] == ' ')  || (s[i] == '\t') ||
            (s[i] == '\r') || (s[i] == '\n'))
            break;
    }
    s[i]='\0';
}
 
char * getNowTime()
{
 
	time_t now;
	struct tm * theTime;
	char *t = (char *)malloc(sizeof(char) * MAXSTRBUF);
 
	time(&now);
	theTime=gmtime(&now);
 
	sprintf(t,"%d-%d-%d %d:%d:%d", theTime->tm_year+1900,
						   	 theTime->tm_mon+1,
						   	 theTime->tm_mday,
						   	 theTime->tm_hour,
						   	 theTime->tm_min,
						   	 theTime->tm_sec);
 
	t=(char *)realloc(t,strlen(t));
 
	return t;
}
 
char *getNewMailId()
{
 
	time_t now;
	struct tm * theTime;
	char *id= (char *)malloc(sizeof(char)*MAXSTRBUF);
	memset(id,0,sizeof(char)*MAXSTRBUF);
	time(&now);
	theTime=gmtime(&now);
	theTime->tm_mon++;
	int mon  = (theTime->tm_mon  < 10) ? theTime->tm_mon+10  : theTime->tm_mon;
	int mday = (theTime->tm_mday < 10) ? theTime->tm_mday+11 : theTime->tm_mday;
	int hour = (theTime->tm_hour < 10) ? theTime->tm_hour+10 : theTime->tm_hour;
	int min  = (theTime->tm_min  < 10) ? theTime->tm_min+10  : theTime->tm_min;
	int sec  = (theTime->tm_sec  < 10) ? theTime->tm_sec+10  : theTime->tm_sec;
 
	sprintf(id,"%4d%2d%2d%2d%2d%2d", theTime->tm_year+1900, mon, mday, hour, min, sec);
 
	id = (char *)realloc(id, strlen(id));
 
	printf("[%s]\n\n",id);
	return id;
}
 
 
// r=-1  not valid 
// r=0   valid
int IsValidId(char *user, char *id)
{
 
	FILE *fp;
 
	char id_buf[MAXSTRBUF];
	int len=strlen(id);
	int r=0;
 
	char *mail_list_path_file = (char *)malloc(sizeof(char)*MAXSTRBUF);	
	sprintf(mail_list_path_file, "%s/%s/%s", D_MAILBOX, user,F_MAIL_LIST);	
 
	if( (fp = fopen(mail_list_path_file,"r")) == NULL) 
		error_at_line(1, errno, __FILE__,__LINE__,"Error: file open %s",mail_list_path_file);
 
	while( (fgets(id_buf, MAXSTRBUF, fp)) != NULL){
		if( (strncmp(id_buf, id, len)) == 0)
			r=-1; break;
	}
	fclose(fp);
 
 
 
	return (r);
}
 
void sendMail(char *from, char *to, char *subject, char *body)
{
	FILE * fp_mail_list; 	// a file pointer for the mail_list file
	FILE * fp_mail_body; 	// a file pointer for the mail body file
 
	char *time; 			// time pointer
	char *id; 				// id pointer
	int n;
	time = getNowTime(); 	// get current time
 
	id = getNewMailId(); 	// get a new mail id	
 
	while(1){ 
		if((n=IsValidId(to, id)) < 0 )		
			id = getNewMailId(); 	// get a new mail id	
		else
			break;
 
	}
 
 
	char *mail_list_path_file = (char *)malloc(sizeof(char)*MAXSTRBUF);
	char *mail_list_body_file = (char *)malloc(sizeof(char)*MAXSTRBUF);
	char *mail_list_content   = (char *)malloc(sizeof(char)*MAXSTRBUF);
 
	memset(mail_list_path_file, 0,sizeof(char)*MAXSTRBUF);
	memset(mail_list_body_file, 0,sizeof(char)*MAXSTRBUF);
	memset(mail_list_content, 0,sizeof(char)*MAXSTRBUF);
 
	// to set the mail list path file
	sprintf(mail_list_path_file, "%s/%s/%s", D_MAILBOX, to, F_MAIL_LIST);	
	sprintf(mail_list_body_file, "%s/%s/%s", D_MAILBOX, to, id);			
	sprintf(mail_list_content,   "%s\t%s\t%s\t%s\n", id, from, time, subject);
 
	mail_list_path_file = (char *)realloc(mail_list_path_file, strlen(mail_list_path_file));
	mail_list_body_file = (char *)realloc(mail_list_body_file, strlen(mail_list_body_file));
	mail_list_content   = (char *)realloc(mail_list_content,   strlen(mail_list_content));
 
 
	printf("[%s]\n[%s]\n[%s]\n",mail_list_path_file,mail_list_body_file,mail_list_content);
 
 
	if( (fp_mail_list=fopen(mail_list_path_file, "a+")) == NULL)
		error_at_line(1, errno, __FILE__,__LINE__,"Error: file open %s",mail_list_path_file);
 
	fputs(mail_list_content, fp_mail_list);
	fclose(fp_mail_list);
 
	if( (fp_mail_body=fopen(mail_list_body_file, "w")) == NULL)
		error_at_line(1, errno, __FILE__,__LINE__,"Error: file open %s",mail_list_path_file);
 
	fputs(body, fp_mail_body);
	fclose(fp_mail_body);
 
	free(time);
	free(id);
	free(mail_list_path_file);
	free(mail_list_body_file);
	free(mail_list_content);		
 
}
 
int main()
{
	//int r;
	char *subject="Welcome";
	char *body="This is a simple email system\n"
			   "This beta test version is currently running on the linux sytem\n"
			   "If you have any questions Please, feel free ask me.\n\n"
			   "Thank you very much for choosing this system\n"
			   " by admin\n";
 
	sendMail("admin", "papa1", subject, body);
	return 0;
}
espereto의 이미지

일반적인 NULL terminated 문자열 버퍼의 크기는 문자열의 최대 길이보다 1바이트이상 더 커야 합니다.
왜냐하면, 문자열의 마지막을 알리는 NULL이 꼭 포함되어야 하기 때문입니다.

malloc, realloc 하는 부분을 다시 체크 해 보세요.

Hyun의 이미지

탭에 대한 공백수는 8이죠...
4로하면 다른쪽으로 소스를 옮겼을 때, 프린트할때 위와같이 깨짐이 생깁니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.