0% found this document useful (0 votes)
7 views4 pages

LR 1

The document contains two C programs. The first program reads a text file, converts its content to uppercase, and writes it to another file, while the second program waits for user input with a timeout and echoes the input to standard output. Both programs include error checking for system calls.

Uploaded by

Lfy Dun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

LR 1

The document contains two C programs. The first program reads a text file, converts its content to uppercase, and writes it to another file, while the second program waits for user input with a timeout and echoes the input to standard output. Both programs include error checking for system calls.

Uploaded by

Lfy Dun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <sys/types.

h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

#define COUNT 512

void str_upper(char *buf, int val)


{
for(int i = 0; i < val; i++)
{
if(buf[i] >= 97 && buf[i] <= 122)
buf[i] -= 32;
}
}

void err_check(int *errno_ptr)


{
if(*errno_ptr != 0)
{
printf("ERROR: %s\n", strerror(*errno_ptr));
*errno_ptr = 0;
exit(1);
}
}

int main (int argc, char *argv[])


{
char *file1;
char *file2;

if (argc != 3)
{
printf("Usage: ./lab1_1 file1.txt file2.txt\n");
return 1;
}

file1 = argv[1];
file2 = argv[2];
char buf[COUNT];
int val, total;
total = 0;

int f_to_read = open(file1, O_RDONLY | O_CLOEXEC);


err_check(&errno);
printf("The file is open for reading.\n");

int f_to_write = open(file2, O_CREAT | O_WRONLY | O_TRUNC |


O_CLOEXEC, S_IRWXU);
err_check(&errno);
printf("The file is open for writing.\n");
printf("Rewriting...\n");
do
{
val = read(f_to_read, buf, COUNT);
total += val;
err_check(&errno);

str_upper(buf,val);

write(f_to_write, buf, val);


err_check(&errno);
}
while(val == COUNT);
printf("Done. %i bytes sent.\n", total);

return 0;
}
#include <sys/select.h>
#include <sys/time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>

#define COUNT 1024

void err_check(int *errno_ptr)


{
if(*errno_ptr != 0)
{
printf("ERROR: %s\n", strerror(*errno_ptr));
*errno_ptr = 0;
exit(1);
}
}

int main(int argc, char *argv[])


{

if (argc != 2)
{
printf("Usage: ./lab1_2 <identifier>\n");
return 1;
}

char *identifier = argv[1];


struct timeval timeout;
fd_set readfds;

timeout.tv_sec = 5;
timeout.tv_usec = 0;

FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);

while (1)
{
int ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL,
&timeout);
err_check(&errno);

if (ret == 0)
{
fprintf(stderr, "Timeout occurred (%s)\n", identifier);
timeout.tv_sec = 5;
timeout.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &timeout);
} else if (FD_ISSET(STDIN_FILENO, &readfds))
{
char buff[COUNT];
ssize_t val = read(STDIN_FILENO, buff, COUNT);
err_check(&errno);

write(STDOUT_FILENO, buff, val);


}
}

return 0;
}

You might also like