Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

program of §8.6 in K&R won't work

Name: Anonymous 2007-04-21 19:19 ID:KrXJSEPw

stderr always yells me a 4096 error, wtf?

Name: Anonymous 2007-04-21 20:35 ID:kRDpJUxN

NO EXCEPTIONS

Name: Anonymous 2007-04-21 20:46 ID:4FaKBXvk

try
{
    1;
}
except( ... )
{
}

Name: Anonymous 2007-04-21 20:58 ID:kRDpJUxN

5. Any new thread in /prog/ will be replied to. No exceptions.

Name: Anonymous 2007-04-22 9:15 ID:rYl3orTf

up

Name: Anonymous 2007-04-22 9:22 ID:gmsJVGhq

>>1
Post your code
here's a way to list a directory

int main(int argc, char **argv) {
   
    char PATH[200];
    int i, n;
   
    if(argc != 2) {
        fprintf(stderr, "usage: %s [dir]\n", argv[0]);
        return 1;
    }
   
    n = scandir(argv[1], &namelist, 0, alphasort);
   
    if(n < 0) {
        fprintf(stderr, "Invalid directory %s\n", argv[1]);
        return 2;
    }
   
    for(i = 0; i < n; i++) {
        sprintf(PATH, "%s/%s", argv[1], namelist[i]->d_name);
        stat(PATH, &filestatus);
        puts(namelist[i]->d_name);
        free(namelist[i]);
    }
    free(namelist);
}

Name: Anonymous 2007-04-22 9:31 ID:9ZBOpYhs

>>char PATH[200];

LOL

Name: Anonymous 2007-04-22 10:23 ID:gmsJVGhq

LOL

char PATH[200]

Name: Anonymous 2007-04-22 10:27 ID:2+/KiPk2

5. Any forced meme in /prog/ will be unfunny. No exceptions.

Name: Anonymous 2007-04-22 11:49 ID:rYl3orTf

>>6
ok
ls.h
#define MAXNAME 25

typedef struct
{
  long ino;
  char name[MAXNAME+1];
} Ent_dir;

typedef struct
{
  int fd;
  Ent_dir e;
} dir;

dir *open_dir(char *namedir);
Ent_dir *read_dir(dir *fdr);
void close_dir(dir *fdr);

ls.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "ls.h"
#include <stdlib.h>
#define S_IFMT  0170000
#define S_IFDIR 0040000

void sizef(char *);

int
main(int argc, char **argv)
{
  char current = '.';

  if (argc == 1)
    sizef(&current);
  else
    while (--argc > 0)
      sizef(*++argv);
  return 0;
}

void scan_dir(char *, void (*fcn) (char *));

void
sizef(char *name)
{
  struct stat sttamp;

  if (stat(name, &sttamp) == -1)
  {
    fprintf(stderr, "sizef : no access to   %s\n", name);
    return;
  }
  if ((sttamp.st_mode & S_IFMT) == S_IFDIR)
    scan_dir(name, sizef);
  printf("%8ld %s\n", sttamp.st_size, name);
}

#define MAXNAMES 1024
void close_dir(dir *);

void
scan_dir(char *dir, void (*fcn) (char *))
{
  char name[MAXNAMES];
  Ent_dir *pr;
  dir *fdr;

  if ((fdr = ouvrir_dir(dir)) == NULL)
  {
    fprintf(stderr, "scan_dir : can't open %s\n", dir);
    return;
  }
  while ((pr = read_dir(fdr)) != NULL)
  {
    if (strcmp(pr->name, ".") == 0 || strcmp(pr->name, "..") == 0)
      continue;
    if (strlen(dir) + strlen(pr->name) + 2 > sizeof(name))
      fprintf(stderr, "scan_dir : name %s%s too long", dir, pr->name);
    else
    {
      sprintf(name, "%s%s", dir, pr->name);
      (*fcn) (name);
    }
    close_dir(fdr);
  }
}

#include <sys/dir.h>
dir *
ouvrir_dir(char *namedir)
{
  int fd;
  struct stat sttamp;
  dir *pr;

  if ((fd = open(namedir, O_RDONLY, 0) == -1 || fstat(fd, &sttamp) == -1
       || (sttamp.st_mode & S_IFMT) != S_IFDIR)
      || (pr = (dir *) malloc(sizeof(dir))) == NULL)
    return NULL;
  pr->fd = fd;
  return pr;
}

void
close_dir(dir * pr)
{
  if (pr)
  {
    close(pr->fd);
    free(pr);
  }
}

#ifndef DIRSIZ
#  define DIRSIZ 14
#endif

Ent_dir *
read_dir(dir * pr)
{
  struct direct tamp_dir;
  static Ent_dir r;

  while (read(pr->fd, (char *) &tamp_dir, sizeof(tamp_dir)) ==
         sizeof(tamp_dir))
  {
    if (tamp_dir.d_ino == 0)
      continue;
    r.ino = tamp_dir.d_ino;
    strncpy(r.ino, tamp_dir.d_name, DIRSIZ);
    r.name[DIRSIZ] = '\0';
    return &r;
  }
  return NULL;
}

I had to modify some stuff or original code won't compile.
Thank you for your solution, but scandir is precisely the thing I 'm trying to do.

Name: Anonymous 2007-04-22 23:16 ID:auu6PBsZ

>>1
The process of walking a directory varies from OSes, genius.

Name: Anonymous 2007-04-23 0:37 ID:rnPzhvJB

>>11


import os
for root, dirs, files in os.walk('.'):
    for f in files:
        print os.path.join(root, f)

Name: Anonymous 2007-04-23 1:33 ID:yeicaXRF

>>6
Dangerous code is dangerous.

Name: Anonymous 2007-04-23 5:02 ID:rkw3Zt/4

>>11
This was designed for a *nix os and oh, I'm under a *nix os. Genius.

Name: Anonymous 2007-04-23 5:05 ID:w7vpP+DB

Beware that, AFAIK, K&R is pre-ISO C and pre-POSIX.

Name: Anonymous 2007-04-23 5:19 ID:rkw3Zt/4

>>15
Yes, and because of that, I had to modify stuff in the original code, I know that the problem is somewhere inside scan_dir but didn't find the solution yet.

Name: Anonymous 2007-04-23 5:49 ID:Pk8NBHlT

turing

Name: Anonymous 2007-04-23 13:57 ID:rnPzhvJB

Name: Anonymous 2007-04-24 4:08 ID:4TbPze83

bump

Name: Anonymous 2009-01-14 14:47

ENTERPRISE QUALITY

Name: Anonymous 2009-03-06 6:02


long time lurker first   time poster here.

Name: Anonymous 2010-11-14 16:50

Name: Anonymous 2010-12-09 15:04

Name: Sgt.Kabu뵡kiman剼 2012-05-29 0:31

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List