fputc: C program that writes characters out of nowhere.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
fputc: C program that writes characters out of nowhere.
Hi:
I have the source of a C program (that listed bellow) which was intended to be compiled and executed under MS-DOS. Under MS-DOS version 5.00 it compiles without errors and runs as expected. It converts the Unix style end-of-line sequence 0x0a to MS-DOS style end-of-line sequence 0x0d 0x0a (linefeed to carriage return + linefeed).
There is an odd thing about this program. Nowhere in it is the carriage return written to the output file. There are two files: an input file, the Unix style file, and the output file, the DOS style file. Characters are read by the fgetc statement and written by fputc. The only way I see to understand this program is that, under DOS,
fputc(ch, outfile)
writes 0x0d 0x0a when ch == 0x0a. And this would match the program's logic. If you can unveil the mystery please let me know.
About compiling it and see if it works in linux I say the program cannot do magic.
Code:
#include <stdio.h>
#include <sys.h>
#include <stdlib.h>
/*
* Unix To DOS text file conversion utility.
*
* Converts the Unix style end-of-line sequence 0AH
* to MS-DOS style end-of-line sequence 0DH 0AH
*/
#define TMPNAME "$U2D$TMP.$$$"
/*
* int fixfile(char * filename)
*
* "Fix" the text file specified by "filename".
* Returns 0 on error, non zero otherwise.
*/
int
fixfile(char * filename)
{
FILE * infile;
FILE * outfile;
char ch;
unsigned long in, out;
printf("u2d: %s: ", filename);
fflush(stdout);
infile = fopen(filename, "r"); /* try to open input file */
if (!infile) {
printf("not found\n");
return 0;
}
outfile = fopen(TMPNAME, "w"); /* try to open scratch file */
if (!outfile) {
fclose(infile);
printf("unable to open temp file %s\n", TMPNAME);
return 1;
}
in = out = 0;
while ((ch = fgetc(infile)) != EOF) { /* while not end of file */
if (fputc(ch, outfile) == EOF) {
fclose(infile);
fclose(outfile);
printf("error writing to temp file %s\n", TMPNAME);
return 1;
}
++in;
++out;
if (ch == '\n')
++out;
}
fclose(infile);
fclose(outfile);
if (remove(filename) != 0) {
printf("unable to delete %s\n", filename);
return 1;
}
if (rename(TMPNAME, filename) != 0) {
printf("unable to rename %s to %s\n", TMPNAME, filename);
return 1;
}
printf("converted: %lu bytes read, %lu bytes written\n", in, out);
return 0;
}
main(int argc, char ** argv)
{
int i;
if (argc == 1) {
argv = _getargs(0, "u2d");
argc = _argc_;
}
if (argc == 1) {
fprintf(stderr, "u2d: usage: u2d file1 file2 ...\n");
fprintf(stderr, "u2d: wildcards, e.g. *.c are expanded\n");
exit(1);
}
for (i = 1; i != argc; i++) {
if (fixfile(argv[i])) {
fprintf(stderr, "u2d: aborted!\n");
exit(1);
}
}
}
I attach the same program in case it's more legible.
I guessed the same thing. But then, one wouldn't have full control. There would be some character operations on streams that would be impossible to acomplish, save through the use of low-level i/o routines. Thanks for having taken the trouble to read the program.
So, when the program makes i/o with fputc somewhere in the way a call to the DOS routines happens and these are who make the translation! Very grateful for your useful post.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.