LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 829|回复: 2

一个dos文本与unix文本格式互换的程序

[复制链接]
发表于 2002-11-28 00:55:32 | 显示全部楼层 |阅读模式
我正在学习linux系统下编程,用的是linux programming in 24 hours这本书.
书中第四章有个例程,是一个dos文本与unix文本格式互换的程序,主要就是去掉
或加上控制字符,该程序我在redhat8.0上编译通过,但程序似乎并未工作,请各位
前辈指教,最好能写出排错过程,谢了.下面是源程序:
/* dos_cvrt.c */

static const char rcsid[] =
"$Id: dos_cvrt.c,v 1.7 1998/11/27 01:31:39 student1 Exp $";

/* REVISION HISTORY:
* $Log: dos_cvrt.c,v $
* Revision 1.7 1998/11/27 01:31:39 student1
* Now handles multiple input files, plus
* put basename code into its own Basename() 
* function.
*
* Revision 1.6 1998/11/27 01:01:28 student1
* Separated conversions into unix2dos() and
* dos2unix() functions.
*
* Revision 1.5 1998/11/23 05:32:21 student1
* Completed utility & tested ok.
*
* Revision 1.4 1998/11/23 05:04:23 student1
* Coded basename test
*
* Revision 1.3 1998/11/21 22:49:39 student1
* Demonstration of RCS substitutions
*/
#include <stdio.h>
#include <stdlib.h>
#include
#include <string.h>

/*
* Convert file named by pathname to DOS
* text format, on standard output:
*/
int
unix2dos(const char *pathname) {
int ch; /* Current input character */
FILE *in = 0; /* Input file */

if ( !(in = fopen(pathname,"r")) ) {
fprintf(stderr,"Cannot open input file.\n");
return 2;
}

while ( (ch = fgetc(in)) != EOF ) {
if ( ch == '\n' )
putchar('\r');
putchar(ch);
}

fclose(in);
return 0;
}

/*
* Convert file named by pathname, to
* UNIX text file format on standard output:
*/
int
dos2unix(const char *pathname) {
int ch; /* Current input character */
int cr_flag; /* True when CR prev. encountered */
FILE *in = 0; /* Input file */

if ( !(in = fopen(pathname,"r")) ) {
fprintf(stderr,"Cannot open input file.\n");
return 2;
}

cr_flag = 0; /* No CR encountered yet */
while ( (ch = fgetc(in)) != EOF ) {
if ( cr_flag && ch != '\n' ) {
/* This CR did not preceed LF */
putchar('\r');
}
if ( !(cr_flag = ch == '\r') )
putchar(ch);
}

fclose(in);
return 0;
}

/*
* Return the pointer to the basename component
* of a pathname:
*/
char *
Basename(const char *path) {
char *base_name = 0;

if ( ( base_name = strrchr(path,'/') ) != 0 )
++base_name; /* Skip over '/' */
else
base_name = (char *) path; /* No dir. */
return base_name;
}

int
main(int argc,char **argv) {
char *base_name = 0; /* Basename of command */
int rc = 0; /* Command return code */
int (*conv)(const char *pathname); /* Conv. Func. Ptr */
int x;

/*
* Determine the basename of the command name. This
* is necessary since this command could be invoked
* with a pathname. For example, argv[0] could be
* "/usr/local/bin/unix_cvrt" if it was installed
* in the system that way.
*/
base_name = Basename(argv[0]);

/*
* Check for missing input file:
*/
if ( argc < 2 ) {
fprintf(stderr,"Missing input file(s).\n");
return 1;
}

/*
* Now that we know the basename of the command
* name used, we can determine which function we
* must carry out here.
*/
if ( !strcmp(base_name,"unix_cvrt") )
conv = unix2dos;
else
conv = dos2unix;

/*
* Perform a text conversion
*/
for ( x=1; x<argc; ++x )
if ( (rc = conv(argv[x])) != 0 )
break; /* An error occured */

return rc;
}
发表于 2002-11-28 17:41:20 | 显示全部楼层
你是怎么执行程序的?我仔细看了你贴出来的源程序,发现它的用法有点特别。这个程序有两个功能,一个是dos文本转换到unix文本,另一个则相反。由于在编程时采用了一个技巧:使用了函数指针,这个程序的功能将随着可执行文件的名称而改变。当执行文件名为unix_cvrt,则执行unix2dos,如果为dos_cvrt,则执行dos2unix。如果你将这个程序编译后命名为unix_cvrt,则它就是转换unix文本到dos文本的程序,再把它更名为dos_cvrt,则它变成了转换dos文本到unix的程序。也就是说一个同样的程序只要换个名字就有不同的功能。
程序的用法为unix_cvrt xxx.txt 或dos_cvrt xxx.txt。
发表于 2003-4-6 16:25:02 | 显示全部楼层
建议你看看flip的源代码,flip就是拿来干这个的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表