PKUOS - Pintos
Pintos source browser for PKU Operating System course
lineup.c
Go to the documentation of this file.
1/** lineup.c
2
3 Converts a file to uppercase in-place.
4
5 Incidentally, another way to do this while avoiding the seeks
6 would be to open the input file, then remove() it and reopen
7 it under another handle. Because of Unix deletion semantics
8 this works fine. */
9
10#include <ctype.h>
11#include <stdio.h>
12#include <syscall.h>
13
14int
15main (int argc, char *argv[])
16{
17 char buf[1024];
18 int handle;
19
20 if (argc != 2)
21 exit (1);
22
23 handle = open (argv[1]);
24 if (handle < 0)
25 exit (2);
26
27 for (;;)
28 {
29 int n, i;
30
31 n = read (handle, buf, sizeof buf);
32 if (n <= 0)
33 break;
34
35 for (i = 0; i < n; i++)
36 buf[i] = toupper ((unsigned char) buf[i]);
37
38 seek (handle, tell (handle) - n);
39 if (write (handle, buf, n) != n)
40 printf ("write failed\n");
41 }
42
43 close (handle);
44
45 return EXIT_SUCCESS;
46}
static char buf[BUF_SIZE]
static int toupper(int c)
lib/ctype.h
Definition: ctype.h:26
int printf(const char *format,...)
Writes formatted output to the console.
Definition: stdio.c:79
void exit(int status)
Definition: syscall.c:72
void close(int fd)
Definition: syscall.c:139
int open(const char *file)
Definition: syscall.c:103
int write(int fd, const void *buffer, unsigned size)
Definition: syscall.c:121
void seek(int fd, unsigned position)
Definition: syscall.c:127
unsigned tell(int fd)
Definition: syscall.c:133
int read(int fd, void *buffer, unsigned size)
Definition: syscall.c:115
#define EXIT_SUCCESS
Typical return values from main() and arguments to exit().
Definition: syscall.h:19
int main(int argc, char *argv[])
lineup.c
Definition: lineup.c:15