Git init
[external/mawk.git] / msdos / examples / add_cr.awk
1
2 # add_cr.awk
3 # converts from Unix (LF only) text files to
4 # DOS (CRLF) text files
5 #
6 # works on both unix and dos 
7 # if used on unix change COPY and DEL in BEGIN section
8 #
9 # mawk -f add_cr.awk  [files]
10
11 # with no files reads stdin writes stdout
12 # otherwise the original is overwritten
13 #
14 # If a file of the form `@file', then arguments are read from
15 # `file', one per line
16
17 #
18 # To add cr's to the whole distribution
19 #
20 # mawk -f doslist.awk packing.lis | mawk "{print $2}" > list
21 # mawk -f add_cr.awk  @list
22 #
23
24
25 # read arguments for @file into ARGV[]
26 function reset_argv(T, i, j, flag, file) #all args local
27 {
28   for( i = 1 ; i < ARGC ; i++ ) 
29   {
30     T[i] = ARGV[i]
31     if ( T[i] ~ /^@/ ) flag = 1
32   }
33
34   if ( ! flag )  return
35
36   # need to read from a @file into ARGV
37   j = 1
38   for( i = 1 ; i < ARGC ; i++ )
39   {
40     if ( T[i] !~ /^@/ ) ARGV[j++] = T[i]
41     else
42     {
43       T[i] = substr(T[i],2)
44       # read arguments from T[i]
45       while ( (getline file < T[i]) > 0 ) ARGV[j++] = file
46     }
47   }
48   ARGC = j
49 }
50    
51   
52 BEGIN {
53   COPY = "copy"    # unix: "cp"
54   DEL = "del"      # unix: "rm"
55
56   tmpfile = ENVIRON["MAWKTMPDIR"] "MAWK.TMP"
57
58   reset_argv()
59 }
60
61
62 FILENAME == "-" {
63    # just write to stdout
64    printf "%s\r\n" , $0
65    next
66 }
67
68 FILENAME != filename {
69    
70    if ( filename )
71    {
72      close(tmpfile)
73      syscmd = sprintf( "%s %s %s", COPY, tmpfile, filename )
74      system(syscmd)
75    }
76
77    filename = FILENAME
78 }
79
80 { printf "%s\r\n" , $0 > tmpfile }
81
82
83 END {
84   if ( filename )  
85   {
86     close(tmpfile)
87     syscmd = sprintf( "%s %s %s", COPY, tmpfile, filename )
88     system(syscmd)
89     system(DEL " " tmpfile)
90   }
91 }
92
93