开发者

Accepting a wildcard to open a file

开发者 https://www.devze.com 2023-04-11 05:10 出处:网络
OK this is embarrassing and as much as I hate to, I have no other option.I don\'t know C but I was presented with a problem that I need to solve and although I\'ve done some researching the time it wo

OK this is embarrassing and as much as I hate to, I have no other option. I don't know C but I was presented with a problem that I need to solve and although I've done some researching the time it would take me to modify the program myself is just too much so I have to swallow my pride (and I'm guessing some rep pts) to ask for help.

This is a simple program to convert a unix file to dos, the only problem is that I need it to accept wildcards (eg.. c:/>unix2dos *.txt or file*.txt ) Nothing fancy.

Here is the code that I have now..

    // UNIX2DOS - a Win32 utility to convert single text files from Unix to MS-DOS format.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utime.h>

#ifndef TRUE
#   define TRUE  (1)
#   define FALSE (0)
#endif

#define R_CNTRL   "rb"
#define W_CNTRL   "wb"

struct stat s_buf;

int u2dos (path)
char *path;
{
    FILE *in, *out;
    int ch,
        prev_ch= 0, 
        rval = FALSE;
    char temppath [16];
    struct _utimbuf ut_buf;
    strcpy (temppath, "./clntmp");
    strcat (temppath, "XXXXXX");
    mktemp (temppath);
    if ((in=fopen (path, R_CNTRL)) == (FILE *) 0)
        return TRUE;
    if ((out=fopen (temppath, W_CNTRL)) == (FILE *) 0)
    {
        fclose (in);
        return TRUE;
    }

    #define LF        0x0A
    #define CR        0x0D

    while ((ch = getc (in)) != EOF)
    {
        if (    ( ch == LF)
             && ( prev_ch != CR)
             && ( putc( CR, out) == EOF)
             || ( putc( ch, out) == EOF)
           )
        {
            rval = TRUE;
            break;
        }
        prev_ch= ch ;
    }

    if (fclose (in) == EOF)
    {
        rval = TRUE;
    }
    if (fclose (out) == EOF)
    {
        rval = TRUE;
    }
    ut_buf.actime = s_buf.st_atime;
    ut_buf.modtime = s_buf.st_mtime;
    if (_utime (temppath, &ut_buf) == -1)
        rval = TRUE;
    if (unlink (path) == -1)
        rval = TRUE;
    if (rval)
    {
        unlink (temppath);
        return TRUE;
    }
    if (rename (temppath,path) == -1)
    {
        fprintf (stderr, "Unix2Dos: Problems renaming '%s' to '%开发者_C百科s'.\n", temppath, path);
        fprintf (stderr, "          However, file '%s' remains.\n", temppath);
        exit (1);
    }
    unlink (temppath);
    return FALSE;
}

void main (argc, argv)
int argc;
char **argv;
{
    char *path;
    while (--argc>0)
    {
        if (stat (path=*++argv, &s_buf) != -1)
        {
            printf ("Unix2Dos: Processing file %s ...\n", path);
            if (u2dos (path))
            {
                fprintf (stderr, "Unix2Dos: Problems processing file %s.\n", path);
                exit (1);
            }
        }
        else
        {
            fprintf (stderr, "Unix2Dos: Can't stat '%s'.\n", path);
            exit (1);
        }
    }
}

I cant believe I have digressed to one of the "Send me da codez" people I have grown to despise but right now it seems like this is my best option.

I'm going to go burry my head in the sand now. Thanks for your time.

EDIT

Although implied, I thought I should make the question obvious. Can you please provide some assistance in modifying this program to accept wildcard variables in a windows environment?


*nix shells, (e.g. bash), automatically expand wildcard arguments. Windows shell does not. But Microsoft Visual C runtime library does have an option to do it automatically on your program's startup. How to do it is explained here. Basically you need to link against setargv.obj.


Since this will run on Windows, you can use FindFirstFile, FindNextFile and FindClose. There is an example here: http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx


You'd have to reinvent a whole bunch of wheels to use wildcards in the file names, and even more to do it portably. Try using cygwin's bash and unix2dos.


Why not take the program as is, and let some (.)bat(ch) file magic do the expansion of wildcards?

On how to do this please see here: http://support.microsoft.com/kb/68268/en-us

Although you'd be starting a new process for each file to be converted, which is quiet expensive, it'll keep you from hacking around using a language you do not seem to like ... ;-)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号