~ubuntu-branches/ubuntu/quantal/tilp2/quantal

« back to all changes in this revision

Viewing changes to man/dos2unix.c

  • Committer: Package Import Robot
  • Author(s): Logan Rosen
  • Date: 2012-06-21 16:50:58 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20120621165058-3f4um803djw50byx
Tags: 1.16-0ubuntu1
* New upstream release (LP: #1010222).
* Fix build structure for Lintian.
* Bump Standards-Version to 3.9.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <stdio.h>
2
 
#include <stdlib.h>
3
 
#include <string.h>
4
 
#include <dirent.h>
5
 
#include <sys/types.h>
6
 
#include <sys/stat.h>
7
 
 
8
 
/* Remove the '\r' characters specifics to DOS */
9
 
int convert(char *filename)
10
 
{
11
 
  FILE *in;
12
 
  FILE *tmp;
13
 
  FILE *out;
14
 
  int c;
15
 
 
16
 
  /* Open the input file and process it into a temp file */
17
 
  printf("Processing file <%s>... ", filename);  
18
 
  in=fopen(filename, "rt");
19
 
  if(in == NULL)
20
 
    {
21
 
      printf("Unable to open this file: <%s>.\n", filename);
22
 
      return -1;
23
 
    }
24
 
  tmp=fopen("dos2unix.tmp", "wt");
25
 
  if(tmp == NULL)
26
 
    {
27
 
      printf("Unable to open temporary file.\n");
28
 
      return -1;
29
 
    }
30
 
 
31
 
  while(!feof(in))
32
 
    {
33
 
      c=fgetc(in);
34
 
      if(feof(in)) break;
35
 
      if(c == 13) continue;
36
 
      fputc(c, tmp);
37
 
    }
38
 
 
39
 
  fclose(in);
40
 
  fclose(tmp);
41
 
 
42
 
  /* Copy the temp file into the input file */
43
 
  tmp=fopen("dos2unix.tmp", "rt");
44
 
  if(tmp == NULL)
45
 
    {
46
 
      printf("Unable to open this file: <%s>.\n", filename);
47
 
      return -1;
48
 
    }
49
 
  out=fopen(filename, "wt");
50
 
  if(out == NULL)
51
 
    {
52
 
      printf("Unable to open temporary file.\n");
53
 
      return -1;
54
 
    }
55
 
 
56
 
  while(!feof(tmp))
57
 
    {
58
 
      c=fgetc(tmp);
59
 
      if(feof(tmp)) break;
60
 
      fputc(c, out);
61
 
    }
62
 
 
63
 
  fclose(tmp);
64
 
  fclose(out);
65
 
  printf("Done.\n");
66
 
  unlink("dos2unix.tmp");
67
 
 
68
 
  return 0;
69
 
}
70
 
 
71
 
/* Return the filename or its extension if it has one */
72
 
char *file_extension(char *filename)
73
 
{
74
 
  int i;
75
 
  char *p;
76
 
  
77
 
  for(i=strlen(filename); i > 0; i--)
78
 
    {
79
 
      if(filename[i] == '.') break;
80
 
    }
81
 
  p=filename+i;
82
 
 
83
 
return p;
84
 
}
85
 
 
86
 
/* Used by scandir to select files to sort */
87
 
int select_file(const struct dirent *d)
88
 
{
89
 
  const char *files[]={ "Makefile", "Makefile.in", ".c", ".h", ".txt" , NULL};
90
 
  int i=0;
91
 
  
92
 
  while(files[i] != NULL)
93
 
    {
94
 
      if(!strcmp(file_extension((char *)d->d_name), files[i])) break;
95
 
      i++;
96
 
    }
97
 
  //printf("<%i: %s>\n", i, files[i]);
98
 
  if(files[i] == NULL)
99
 
    return 0;
100
 
  else
101
 
    return 1;
102
 
  
103
 
  return 0;
104
 
}
105
 
 
106
 
int main(int argc, char *argv[], char **arge)
107
 
{
108
 
  char filename[256];
109
 
  struct dirent **namelist;
110
 
  int n;
111
 
 
112
 
 
113
 
  if(argc > 1)
114
 
    {
115
 
      strcpy(filename, argv[1]);
116
 
      if(convert(filename))
117
 
        {
118
 
          printf("Error !!!\n");
119
 
        }
120
 
    }
121
 
  else
122
 
    {
123
 
      n = scandir(".", &namelist, select_file, alphasort);
124
 
      if (n < 0)
125
 
        perror("scandir");
126
 
      else
127
 
        while(n--) 
128
 
          {
129
 
            //printf("<%s>\n", namelist[n]->d_name);
130
 
            convert(namelist[n]->d_name);
131
 
            chmod(namelist[n]->d_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
132
 
          }
133
 
    }
134
 
  
135
 
  return 0;
136
 
}