Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accepting a wildcard to open a file

Tags:

c

file

wildcard

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 '%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?

like image 383
Ryan Avatar asked Dec 21 '22 07:12

Ryan


2 Answers

*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.

like image 83
cyco130 Avatar answered Dec 29 '22 11:12

cyco130


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

like image 35
IronMensan Avatar answered Dec 29 '22 12:12

IronMensan