File: rm_once.c

package info (click to toggle)
mcpp 2.5-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,544 kB
  • ctags: 28,000
  • sloc: ansic: 34,825; sh: 3,812; makefile: 120; cpp: 84; exp: 18
file content (42 lines) | stat: -rw-r--r-- 1,187 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
 * rm_once.c    2002/08, 2004/11    kmatsui
 *  to remove '#pragma __once' and '#pragma once' line from the top of
 *  header files
 */

#include    "stdio.h"
#include    "stdlib.h"
#include    "string.h"

main( int argc, char ** argv)
{
    char    buf[ BUFSIZ];
    char    *__once = "\n#pragma __once\n\n";
    char    *once = "\n#pragma once\n\n";
    char    *tmp = "tmp.once";
    char    *fname;
    FILE    *fp_in, *fp_out;
    int     i;
    char    *cp;

    while (*++argv) {
        if ((fp_in = fopen( *argv, "r")) != NULL) {
            for (cp = buf, i = 0; i < 3; i++, cp += strlen( cp))
                fgets( cp, BUFSIZ, fp_in);
            if (strcmp( buf, once) != 0 || strcmp( buf, __once) != 0) {
                fprintf( stderr, "Skipped %s\n", *argv);
                fclose( fp_in);
                continue;
            }
            fprintf( stderr, "Converted %s\n", *argv);
            fp_out = fopen( tmp, "w");
            while (fgets( buf, BUFSIZ, fp_in) != NULL)
                fputs( buf, fp_out);
            fclose( fp_in);
            fclose( fp_out);
            remove( *argv);
            rename( tmp, *argv);
        }
    }
    return 0;
}