CS318 - Pintos
Pintos source browser for JHU CS318 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 
14 int
15 main (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 }
buf
static char buf[BUF_SIZE]
Definition: child-syn-read.c:16
write
int write(int fd, const void *buffer, unsigned size)
Definition: syscall.c:121
toupper
static int toupper(int c)
lib/ctype.h
Definition: ctype.h:26
ctype.h
printf
int printf(const char *format,...)
Writes formatted output to the console.
Definition: stdio.c:79
open
int open(const char *file)
Definition: syscall.c:103
tell
unsigned tell(int fd)
Definition: syscall.c:133
EXIT_SUCCESS
#define EXIT_SUCCESS
Typical return values from main() and arguments to exit().
Definition: syscall.h:19
seek
void seek(int fd, unsigned position)
Definition: syscall.c:127
main
int main(int argc, char *argv[])
lineup.c
Definition: lineup.c:15
close
void close(int fd)
Definition: syscall.c:139
exit
void exit(int status)
Definition: syscall.c:72
read
int read(int fd, void *buffer, unsigned size)
Definition: syscall.c:115